Skip to content

Commit fb9f97e

Browse files
committed
rustc: More helpful error message when using a struct type like a function
Closes #6702
1 parent 393a130 commit fb9f97e

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/librustc/middle/resolve.rs

+29-12
Original file line numberDiff line numberDiff line change
@@ -4981,20 +4981,37 @@ impl Resolver {
49814981
wrong_name));
49824982
}
49834983
else {
4984-
// limit search to 5 to reduce the number
4985-
// of stupid suggestions
4986-
match self.find_best_match_for_name(wrong_name, 5) {
4987-
Some(m) => {
4984+
// Be helpful if the name refers to a struct
4985+
// (The pattern matching def_tys where the id is in self.structs
4986+
// matches on regular structs while excluding tuple- and
4987+
// enum-like structs, which wouldn't result in this error.)
4988+
match self.resolve_path(expr.id, path, TypeNS, false, visitor) {
4989+
Some(def_ty(struct_id))
4990+
if self.structs.contains(&struct_id) => {
49884991
self.session.span_err(expr.span,
4989-
fmt!("unresolved name `%s`. \
4990-
Did you mean `%s`?",
4991-
wrong_name, m));
4992-
}
4993-
None => {
4994-
self.session.span_err(expr.span,
4995-
fmt!("unresolved name `%s`.",
4996-
wrong_name));
4992+
fmt!("`%s` is a structure name, but this expression \
4993+
uses it like a function name", wrong_name));
4994+
4995+
self.session.span_note(expr.span, fmt!("Did you mean to write: \
4996+
`%s { /* fields */ }`?", wrong_name));
4997+
49974998
}
4999+
_ =>
5000+
// limit search to 5 to reduce the number
5001+
// of stupid suggestions
5002+
match self.find_best_match_for_name(wrong_name, 5) {
5003+
Some(m) => {
5004+
self.session.span_err(expr.span,
5005+
fmt!("unresolved name `%s`. \
5006+
Did you mean `%s`?",
5007+
wrong_name, m));
5008+
}
5009+
None => {
5010+
self.session.span_err(expr.span,
5011+
fmt!("unresolved name `%s`.",
5012+
wrong_name));
5013+
}
5014+
}
49985015
}
49995016
}
50005017
}

src/test/compile-fail/issue-6702.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Monster {
12+
damage: int
13+
}
14+
15+
16+
fn main() {
17+
let _m = Monster(); //~ ERROR `Monster` is a structure name, but
18+
//~^ NOTE Did you mean to write: `Monster { /* fields */ }`?
19+
}

0 commit comments

Comments
 (0)