about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2013-08-13 17:54:14 -0700
committerTim Chevalier <chevalier@alum.wellesley.edu>2013-08-21 17:17:50 -0700
commitfb9f97ebeebb095e997427b80199b2564b886010 (patch)
treec0aaaab8dace0e3da0f0287494f1c55d8f9cd63d
parent393a130b3da254e7ac02931d7c4a74d02af41b5c (diff)
downloadrust-fb9f97ebeebb095e997427b80199b2564b886010.tar.gz
rust-fb9f97ebeebb095e997427b80199b2564b886010.zip
rustc: More helpful error message when using a struct type like a function
Closes #6702
-rw-r--r--src/librustc/middle/resolve.rs41
-rw-r--r--src/test/compile-fail/issue-6702.rs19
2 files changed, 48 insertions, 12 deletions
diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs
index 9654bf3fc01..cbc6cd1a343 100644
--- a/src/librustc/middle/resolve.rs
+++ b/src/librustc/middle/resolve.rs
@@ -4981,20 +4981,37 @@ impl Resolver {
                                         wrong_name));
                         }
                         else {
-                            // limit search to 5 to reduce the number
-                            // of stupid suggestions
-                            match self.find_best_match_for_name(wrong_name, 5) {
-                                Some(m) => {
+                            // Be helpful if the name refers to a struct
+                            // (The pattern matching def_tys where the id is in self.structs
+                            // matches on regular structs while excluding tuple- and
+                            // enum-like structs, which wouldn't result in this error.)
+                            match self.resolve_path(expr.id, path, TypeNS, false, visitor) {
+                                Some(def_ty(struct_id))
+                                  if self.structs.contains(&struct_id) => {
                                     self.session.span_err(expr.span,
-                                            fmt!("unresolved name `%s`. \
-                                                Did you mean `%s`?",
-                                                wrong_name, m));
-                                }
-                                None => {
-                                    self.session.span_err(expr.span,
-                                            fmt!("unresolved name `%s`.",
-                                                wrong_name));
+                                            fmt!("`%s` is a structure name, but this expression \
+                                                uses it like a function name", wrong_name));
+
+                                    self.session.span_note(expr.span, fmt!("Did you mean to write: \
+                                                `%s { /* fields */ }`?", wrong_name));
+
                                 }
+                                _ =>
+                                   // limit search to 5 to reduce the number
+                                   // of stupid suggestions
+                                   match self.find_best_match_for_name(wrong_name, 5) {
+                                       Some(m) => {
+                                           self.session.span_err(expr.span,
+                                               fmt!("unresolved name `%s`. \
+                                                   Did you mean `%s`?",
+                                                   wrong_name, m));
+                                       }
+                                       None => {
+                                           self.session.span_err(expr.span,
+                                                fmt!("unresolved name `%s`.",
+                                                    wrong_name));
+                                       }
+                                   }
                             }
                         }
                     }
diff --git a/src/test/compile-fail/issue-6702.rs b/src/test/compile-fail/issue-6702.rs
new file mode 100644
index 00000000000..168aa5f9d69
--- /dev/null
+++ b/src/test/compile-fail/issue-6702.rs
@@ -0,0 +1,19 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct Monster {
+    damage: int
+}
+
+
+fn main() {
+    let _m = Monster(); //~ ERROR `Monster` is a structure name, but
+    //~^ NOTE Did you mean to write: `Monster { /* fields */ }`?
+}