rustc: More helpful error message when using a struct type like a function

Closes #6702
This commit is contained in:
Tim Chevalier 2013-08-13 17:54:14 -07:00
parent 393a130b3d
commit fb9f97ebee
2 changed files with 48 additions and 12 deletions

View file

@ -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));
}
}
}
}
}

View file

@ -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 */ }`?
}