Provide suggestion when using field access instead of path
When trying to access an associated constant as if it were a field of an instance, provide a suggestion for the correct syntax.
This commit is contained in:
parent
3752b3d3a5
commit
5390414379
@ -379,6 +379,14 @@ fn smart_resolve_context_dependent_help(
|
||||
Applicability::MaybeIncorrect
|
||||
);
|
||||
},
|
||||
ExprKind::Field(ref _expr, ident) => {
|
||||
err.span_suggestion(
|
||||
sm.start_point(parent.span).to(ident.span),
|
||||
"use `::` to access an associated item",
|
||||
format!("{}::{}", path_str, ident),
|
||||
Applicability::MaybeIncorrect
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
err.span_label(
|
||||
span,
|
||||
|
13
src/test/ui/suggestions/assoc-const-as-field.rs
Normal file
13
src/test/ui/suggestions/assoc-const-as-field.rs
Normal file
@ -0,0 +1,13 @@
|
||||
pub mod Mod {
|
||||
pub struct Foo {}
|
||||
impl Foo {
|
||||
pub const BAR: usize = 42;
|
||||
}
|
||||
}
|
||||
|
||||
fn foo(_: usize) {}
|
||||
|
||||
fn main() {
|
||||
foo(Mod::Foo.Bar);
|
||||
//~^ ERROR expected value, found
|
||||
}
|
11
src/test/ui/suggestions/assoc-const-as-field.stderr
Normal file
11
src/test/ui/suggestions/assoc-const-as-field.stderr
Normal file
@ -0,0 +1,11 @@
|
||||
error[E0423]: expected value, found struct `Mod::Foo`
|
||||
--> $DIR/assoc-const-as-field.rs:11:9
|
||||
|
|
||||
LL | foo(Mod::Foo.Bar);
|
||||
| ^^^^^^^^----
|
||||
| |
|
||||
| help: use `::` to access an associated item: `Mod::Foo::Bar`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0423`.
|
Loading…
Reference in New Issue
Block a user