Skip to content

Commit

Permalink
Better diagnostic for use Self::..
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed May 16, 2023
1 parent b652d9a commit eaf47a3
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 eaf47a3

Please # to comment.