Skip to content

Commit b42a384

Browse files
authored
Auto merge of #35456 - birkenfeld:issue-33784, r=nikomatsakis
typeck: suggest (x.field)(...) to call struct fields even when x is a reference Fixes: #33784 Note: This is a reopen of #33785.
2 parents 8a4641b + 59af2ac commit b42a384

File tree

2 files changed

+74
-20
lines changed

2 files changed

+74
-20
lines changed

src/librustc_typeck/check/method/suggest.rs

+28-20
Original file line numberDiff line numberDiff line change
@@ -162,26 +162,34 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
162162
},
163163
rcvr_ty);
164164

165-
// If the item has the name of a field, give a help note
166-
if let (&ty::TyStruct(def, substs), Some(expr)) = (&rcvr_ty.sty, rcvr_expr) {
167-
if let Some(field) = def.struct_variant().find_field_named(item_name) {
168-
let expr_string = match tcx.sess.codemap().span_to_snippet(expr.span) {
169-
Ok(expr_string) => expr_string,
170-
_ => "s".into() // Default to a generic placeholder for the
171-
// expression when we can't generate a string
172-
// snippet
173-
};
174-
175-
let field_ty = field.ty(tcx, substs);
176-
177-
if self.is_fn_ty(&field_ty, span) {
178-
err.span_note(span,
179-
&format!("use `({0}.{1})(...)` if you meant to call \
180-
the function stored in the `{1}` field",
181-
expr_string, item_name));
182-
} else {
183-
err.span_note(span, &format!("did you mean to write `{0}.{1}`?",
184-
expr_string, item_name));
165+
// If the method name is the name of a field with a function or closure type,
166+
// give a helping note that it has to be called as (x.f)(...).
167+
if let Some(expr) = rcvr_expr {
168+
for (ty, _) in self.autoderef(span, rcvr_ty) {
169+
if let ty::TyStruct(def, substs) = ty.sty {
170+
if let Some(field) = def.struct_variant().find_field_named(item_name) {
171+
let snippet = tcx.sess.codemap().span_to_snippet(expr.span);
172+
let expr_string = match snippet {
173+
Ok(expr_string) => expr_string,
174+
_ => "s".into() // Default to a generic placeholder for the
175+
// expression when we can't generate a
176+
// string snippet
177+
};
178+
179+
let field_ty = field.ty(tcx, substs);
180+
181+
if self.is_fn_ty(&field_ty, span) {
182+
err.span_note(span, &format!(
183+
"use `({0}.{1})(...)` if you meant to call the function \
184+
stored in the `{1}` field",
185+
expr_string, item_name));
186+
} else {
187+
err.span_note(span, &format!(
188+
"did you mean to write `{0}.{1}`?",
189+
expr_string, item_name));
190+
}
191+
break;
192+
}
185193
}
186194
}
187195
}

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

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2016 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+
use std::ops::Deref;
12+
13+
struct Obj<F> where F: FnMut() -> u32 {
14+
fn_ptr: fn() -> (),
15+
closure: F,
16+
}
17+
18+
struct C {
19+
c_fn_ptr: fn() -> (),
20+
}
21+
22+
struct D(C);
23+
24+
impl Deref for D {
25+
type Target = C;
26+
fn deref(&self) -> &C {
27+
&self.0
28+
}
29+
}
30+
31+
32+
fn empty() {}
33+
34+
fn main() {
35+
let o = Obj { fn_ptr: empty, closure: || 42 };
36+
let p = &o;
37+
p.closure(); //~ ERROR no method named `closure` found
38+
//~^ NOTE use `(p.closure)(...)` if you meant to call the function stored in the `closure` field
39+
let q = &p;
40+
q.fn_ptr(); //~ ERROR no method named `fn_ptr` found
41+
//~^ NOTE use `(q.fn_ptr)(...)` if you meant to call the function stored in the `fn_ptr` field
42+
let r = D(C { c_fn_ptr: empty });
43+
let s = &r;
44+
s.c_fn_ptr(); //~ ERROR no method named `c_fn_ptr` found
45+
//~^ NOTE use `(s.c_fn_ptr)(...)` if you meant to call the function stored in the `c_fn_ptr`
46+
}

0 commit comments

Comments
 (0)