Skip to content

Commit 4e1172e

Browse files
committed
auto merge of #12937 : sinistersnare/rust/method-error-message, r=huonw
its a common (yet easily fixable) error to just forget parens at the end of getter-like methods without any arguments. The current error message for that case asks for an anonymous function, this patch adds a note asking for either an anonymous function, or for trailing parens. This is my first contribution! do i need to do anything else?
2 parents 9e89ffc + 8b6592e commit 4e1172e

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/librustc/middle/typeck/check/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2317,11 +2317,13 @@ fn check_expr_with_unifier(fcx: @FnCtxt,
23172317
fcx.type_error_message(
23182318
expr.span,
23192319
|actual| {
2320-
format!("attempted to take value of method `{}` on type `{}` \
2321-
(try writing an anonymous function)",
2320+
format!("attempted to take value of method `{}` on type `{}`",
23222321
token::get_name(field), actual)
23232322
},
23242323
expr_t, None);
2324+
2325+
tcx.sess.span_note(expr.span,
2326+
"maybe a missing `()` to call it? If not, try an anonymous function.");
23252327
}
23262328

23272329
None => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2014 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+
// Tests to make sure that parens are needed for method calls without arguments.
12+
// outputs text to make sure either an anonymous function is provided or
13+
// open-close '()' parens are given
14+
15+
16+
struct Point {
17+
x: int,
18+
y: int
19+
}
20+
impl Point {
21+
fn new() -> Point {
22+
Point{x:0, y:0}
23+
}
24+
fn get_x(&self) -> int {
25+
self.x
26+
}
27+
}
28+
29+
fn main() {
30+
let point: Point = Point::new();
31+
let px: int = point.get_x;//~ ERROR attempted to take value of method `get_x` on type `Point`
32+
//~^ NOTE maybe a missing `()` to call it? If not, try an anonymous function.
33+
}
34+

0 commit comments

Comments
 (0)