Skip to content

Commit 5390414

Browse files
committed
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.
1 parent 3752b3d commit 5390414

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/librustc_resolve/error_reporting.rs

+8
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@ impl<'a> Resolver<'a> {
379379
Applicability::MaybeIncorrect
380380
);
381381
},
382+
ExprKind::Field(ref _expr, ident) => {
383+
err.span_suggestion(
384+
sm.start_point(parent.span).to(ident.span),
385+
"use `::` to access an associated item",
386+
format!("{}::{}", path_str, ident),
387+
Applicability::MaybeIncorrect
388+
);
389+
}
382390
_ => {
383391
err.span_label(
384392
span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
pub mod Mod {
2+
pub struct Foo {}
3+
impl Foo {
4+
pub const BAR: usize = 42;
5+
}
6+
}
7+
8+
fn foo(_: usize) {}
9+
10+
fn main() {
11+
foo(Mod::Foo.Bar);
12+
//~^ ERROR expected value, found
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0423]: expected value, found struct `Mod::Foo`
2+
--> $DIR/assoc-const-as-field.rs:11:9
3+
|
4+
LL | foo(Mod::Foo.Bar);
5+
| ^^^^^^^^----
6+
| |
7+
| help: use `::` to access an associated item: `Mod::Foo::Bar`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)