Skip to content

Commit

Permalink
Rollup merge of rust-lang#111652 - clubby789:self-import-improvement,…
Browse files Browse the repository at this point in the history
… r=compiler-errors

Better diagnostic for `use Self::..`

Fixes rust-lang#111627

cc `@petrochenkov,` you might have thoughts on a better way to handle this (rust-lang#63720 (comment))
  • Loading branch information
Dylan-DPC authored May 20, 2023
2 parents 1397827 + eaf47a3 commit 13f3585
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
(msg, None)
} else if ident.name == kw::SelfUpper {
("`Self` is only available in impls, traits, and type definitions".to_string(), None)
// As mentioned above, `opt_ns` being `None` indicates a module path in import.
// We can use this to improve a confusing error for, e.g. `use Self::Variant` in an
// impl
if opt_ns.is_none() {
("`Self` cannot be used in imports".to_string(), None)
} else {
(
"`Self` is only available in impls, traits, and type definitions".to_string(),
None,
)
}
} else if ident.name.as_str().chars().next().map_or(false, |c| c.is_ascii_uppercase()) {
// Check whether the name refers to an item in the value namespace.
let binding = if let Some(ribs) = ribs {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/use/use-self-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ error[E0432]: unresolved import `Self`
--> $DIR/use-self-type.rs:6:13
|
LL | use Self::f;
| ^^^^ `Self` is only available in impls, traits, and type definitions
| ^^^^ `Self` cannot be used in imports

error: aborting due to 2 previous errors

Expand Down

0 comments on commit 13f3585

Please sign in to comment.