Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better diagnostic for use Self::.. #111652

Merged
merged 1 commit into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
clubby789 marked this conversation as resolved.
Show resolved Hide resolved
("`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