about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2019-03-17 21:18:06 -0700
committerEsteban Küber <esteban@kuber.com.ar>2019-03-24 19:21:54 -0700
commit539041437956abf05bd4ad0b6ecb253591df3085 (patch)
treee058a1d732c597f9fa64ecdc710c596f57e9cead
parent3752b3d3a56bf3eabb588b7d595cd1f8accc0286 (diff)
downloadrust-539041437956abf05bd4ad0b6ecb253591df3085.tar.gz
rust-539041437956abf05bd4ad0b6ecb253591df3085.zip
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.
-rw-r--r--src/librustc_resolve/error_reporting.rs8
-rw-r--r--src/test/ui/suggestions/assoc-const-as-field.rs13
-rw-r--r--src/test/ui/suggestions/assoc-const-as-field.stderr11
3 files changed, 32 insertions, 0 deletions
diff --git a/src/librustc_resolve/error_reporting.rs b/src/librustc_resolve/error_reporting.rs
index fc8452e49ad..0da10176d29 100644
--- a/src/librustc_resolve/error_reporting.rs
+++ b/src/librustc_resolve/error_reporting.rs
@@ -379,6 +379,14 @@ impl<'a> Resolver<'a> {
                                         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,
diff --git a/src/test/ui/suggestions/assoc-const-as-field.rs b/src/test/ui/suggestions/assoc-const-as-field.rs
new file mode 100644
index 00000000000..678b58936a8
--- /dev/null
+++ b/src/test/ui/suggestions/assoc-const-as-field.rs
@@ -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
+}
diff --git a/src/test/ui/suggestions/assoc-const-as-field.stderr b/src/test/ui/suggestions/assoc-const-as-field.stderr
new file mode 100644
index 00000000000..3f23ae57c3d
--- /dev/null
+++ b/src/test/ui/suggestions/assoc-const-as-field.stderr
@@ -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`.