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:
Esteban Küber 2019-03-17 21:18:06 -07:00
parent 3752b3d3a5
commit 5390414379
3 changed files with 32 additions and 0 deletions

View File

@ -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,

View 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
}

View 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`.