Skip to content

Commit 13f3585

Browse files
authored
Rollup merge of #111652 - clubby789:self-import-improvement, r=compiler-errors
Better diagnostic for `use Self::..` Fixes #111627 cc `@petrochenkov,` you might have thoughts on a better way to handle this (#63720 (comment))
2 parents 1397827 + eaf47a3 commit 13f3585

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18321832
}
18331833
(msg, None)
18341834
} else if ident.name == kw::SelfUpper {
1835-
("`Self` is only available in impls, traits, and type definitions".to_string(), None)
1835+
// As mentioned above, `opt_ns` being `None` indicates a module path in import.
1836+
// We can use this to improve a confusing error for, e.g. `use Self::Variant` in an
1837+
// impl
1838+
if opt_ns.is_none() {
1839+
("`Self` cannot be used in imports".to_string(), None)
1840+
} else {
1841+
(
1842+
"`Self` is only available in impls, traits, and type definitions".to_string(),
1843+
None,
1844+
)
1845+
}
18361846
} else if ident.name.as_str().chars().next().map_or(false, |c| c.is_ascii_uppercase()) {
18371847
// Check whether the name refers to an item in the value namespace.
18381848
let binding = if let Some(ribs) = ribs {

tests/ui/use/use-self-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0432]: unresolved import `Self`
88
--> $DIR/use-self-type.rs:6:13
99
|
1010
LL | use Self::f;
11-
| ^^^^ `Self` is only available in impls, traits, and type definitions
11+
| ^^^^ `Self` cannot be used in imports
1212

1313
error: aborting due to 2 previous errors
1414

0 commit comments

Comments
 (0)