From fb9f97ebeebb095e997427b80199b2564b886010 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Tue, 13 Aug 2013 17:54:14 -0700 Subject: [PATCH] rustc: More helpful error message when using a struct type like a function Closes #6702 --- src/librustc/middle/resolve.rs | 41 ++++++++++++++++++++--------- src/test/compile-fail/issue-6702.rs | 19 +++++++++++++ 2 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 src/test/compile-fail/issue-6702.rs diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 9654bf3fc01f..cbc6cd1a3430 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 000000000000..168aa5f9d691 --- /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 or the MIT license +// , 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 */ }`? +}