Suggest fixes for use foo::self
This commit is contained in:
parent
672b272077
commit
84a44218ad
@ -435,9 +435,24 @@ fn build_reduced_graph_for_use_tree(
|
||||
} else {
|
||||
// Disallow `self`
|
||||
if source.ident.name == kw::SelfLower {
|
||||
let parent = module_path.last();
|
||||
|
||||
let span = match parent {
|
||||
// only `::self` from `use foo::self as bar`
|
||||
Some(seg) => seg.ident.span.shrink_to_hi().to(source.ident.span),
|
||||
None => source.ident.span,
|
||||
};
|
||||
let span_with_rename = match rename {
|
||||
// only `self as bar` from `use foo::self as bar`
|
||||
Some(rename) => source.ident.span.to(rename.span),
|
||||
None => source.ident.span,
|
||||
};
|
||||
self.r.report_error(
|
||||
use_tree.span,
|
||||
ResolutionError::SelfImportsOnlyAllowedWithin,
|
||||
span,
|
||||
ResolutionError::SelfImportsOnlyAllowedWithin {
|
||||
root: parent.is_none(),
|
||||
span_with_rename,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -301,13 +301,40 @@ impl<'a> Resolver<'a> {
|
||||
}
|
||||
err
|
||||
}
|
||||
ResolutionError::SelfImportsOnlyAllowedWithin => struct_span_err!(
|
||||
self.session,
|
||||
span,
|
||||
E0429,
|
||||
"{}",
|
||||
"`self` imports are only allowed within a { } list"
|
||||
),
|
||||
ResolutionError::SelfImportsOnlyAllowedWithin { root, span_with_rename } => {
|
||||
let mut err = struct_span_err!(
|
||||
self.session,
|
||||
span,
|
||||
E0429,
|
||||
"{}",
|
||||
"`self` imports are only allowed within a { } list"
|
||||
);
|
||||
|
||||
// None of the suggestions below would help with a case like `use self`.
|
||||
if !root {
|
||||
// use foo::bar::self -> foo::bar
|
||||
// use foo::bar::self as abc -> foo::bar as abc
|
||||
err.span_suggestion(
|
||||
span,
|
||||
"Remove `::self`..",
|
||||
"".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
|
||||
// use foo::bar::self -> foo::bar::{self}
|
||||
// use foo::bar::self as abc -> foo::bar::{self as abc}
|
||||
let braces = vec![
|
||||
(span_with_rename.shrink_to_lo(), "{".to_string()),
|
||||
(span_with_rename.shrink_to_hi(), "}".to_string()),
|
||||
];
|
||||
err.multipart_suggestion(
|
||||
"..or add braces around `self`",
|
||||
braces,
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
err
|
||||
}
|
||||
ResolutionError::SelfImportCanOnlyAppearOnceInTheList => {
|
||||
let mut err = struct_span_err!(
|
||||
self.session,
|
||||
|
@ -194,7 +194,7 @@ enum ResolutionError<'a> {
|
||||
/// Error E0426: use of undeclared label.
|
||||
UndeclaredLabel(&'a str, Option<Symbol>),
|
||||
/// Error E0429: `self` imports are only allowed within a `{ }` list.
|
||||
SelfImportsOnlyAllowedWithin,
|
||||
SelfImportsOnlyAllowedWithin { root: bool, span_with_rename: Span },
|
||||
/// Error E0430: `self` import can only appear once in the list.
|
||||
SelfImportCanOnlyAppearOnceInTheList,
|
||||
/// Error E0431: `self` import can only appear in an import list with a non-empty prefix.
|
||||
|
@ -1,8 +1,17 @@
|
||||
error[E0429]: `self` imports are only allowed within a { } list
|
||||
--> $DIR/E0429.rs:1:5
|
||||
--> $DIR/E0429.rs:1:13
|
||||
|
|
||||
LL | use std::fmt::self;
|
||||
| ^^^^^^^^^^^^^^
|
||||
| ^^^^^^
|
||||
|
|
||||
help: Remove `::self`..
|
||||
|
|
||||
LL | use std::fmt;
|
||||
| --
|
||||
help: ..or add braces around `self`
|
||||
|
|
||||
LL | use std::fmt::{self};
|
||||
| ^ ^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
@ -5,10 +5,19 @@ LL | use foo as self;
|
||||
| ^^^^ expected identifier, found keyword
|
||||
|
||||
error[E0429]: `self` imports are only allowed within a { } list
|
||||
--> $DIR/import-self.rs:12:5
|
||||
--> $DIR/import-self.rs:12:8
|
||||
|
|
||||
LL | use foo::self;
|
||||
| ^^^^^^^^^
|
||||
| ^^^^^^
|
||||
|
|
||||
help: Remove `::self`..
|
||||
|
|
||||
LL | use foo;
|
||||
| --
|
||||
help: ..or add braces around `self`
|
||||
|
|
||||
LL | use foo::{self};
|
||||
| ^ ^
|
||||
|
||||
error[E0255]: the name `foo` is defined multiple times
|
||||
--> $DIR/import-self.rs:6:11
|
||||
|
@ -2,7 +2,7 @@ error[E0429]: `self` imports are only allowed within a { } list
|
||||
--> $DIR/use-keyword.rs:6:13
|
||||
|
|
||||
LL | use self as A;
|
||||
| ^^^^^^^^^
|
||||
| ^^^^
|
||||
|
||||
error[E0432]: unresolved import `super`
|
||||
--> $DIR/use-keyword.rs:8:13
|
||||
|
@ -1,14 +1,32 @@
|
||||
error[E0429]: `self` imports are only allowed within a { } list
|
||||
--> $DIR/use-mod-4.rs:1:5
|
||||
--> $DIR/use-mod-4.rs:1:8
|
||||
|
|
||||
LL | use foo::self;
|
||||
| ^^^^^^^^^
|
||||
| ^^^^^^
|
||||
|
|
||||
help: Remove `::self`..
|
||||
|
|
||||
LL | use foo;
|
||||
| --
|
||||
help: ..or add braces around `self`
|
||||
|
|
||||
LL | use foo::{self};
|
||||
| ^ ^
|
||||
|
||||
error[E0429]: `self` imports are only allowed within a { } list
|
||||
--> $DIR/use-mod-4.rs:4:5
|
||||
--> $DIR/use-mod-4.rs:4:13
|
||||
|
|
||||
LL | use std::mem::self;
|
||||
| ^^^^^^^^^^^^^^
|
||||
| ^^^^^^
|
||||
|
|
||||
help: Remove `::self`..
|
||||
|
|
||||
LL | use std::mem;
|
||||
| --
|
||||
help: ..or add braces around `self`
|
||||
|
|
||||
LL | use std::mem::{self};
|
||||
| ^ ^
|
||||
|
||||
error[E0432]: unresolved import `foo`
|
||||
--> $DIR/use-mod-4.rs:1:5
|
||||
|
Loading…
Reference in New Issue
Block a user