Skip to content

Commit 8ca40fc

Browse files
committed
typeck: suggest (x.field)(...) to call struct fields even when x is a reference
Fixes: #33784
1 parent 476fe6e commit 8ca40fc

File tree

2 files changed

+83
-22
lines changed

2 files changed

+83
-22
lines changed

src/librustc_typeck/check/method/suggest.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -178,28 +178,43 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
178178
rcvr_ty,
179179
None);
180180

181-
// If the item has the name of a field, give a help note
182-
if let (&ty::TyStruct(def, substs), Some(expr)) = (&rcvr_ty.sty, rcvr_expr) {
183-
if let Some(field) = def.struct_variant().find_field_named(item_name) {
184-
let expr_string = match tcx.sess.codemap().span_to_snippet(expr.span) {
185-
Ok(expr_string) => expr_string,
186-
_ => "s".into() // Default to a generic placeholder for the
187-
// expression when we can't generate a string
188-
// snippet
189-
};
190-
191-
let field_ty = field.ty(tcx, substs);
192-
193-
if self.is_fn_ty(&field_ty, span) {
194-
err.span_note(span,
195-
&format!("use `({0}.{1})(...)` if you meant to call \
196-
the function stored in the `{1}` field",
197-
expr_string, item_name));
198-
} else {
199-
err.span_note(span, &format!("did you mean to write `{0}.{1}`?",
200-
expr_string, item_name));
201-
}
202-
}
181+
// If the method name is the name of a field with a function or closure type,
182+
// give a helping note that it has to be called as (x.f)(...).
183+
if let Some(expr) = rcvr_expr {
184+
self.autoderef(
185+
span,
186+
rcvr_ty,
187+
|| None,
188+
UnresolvedTypeAction::Ignore,
189+
LvaluePreference::NoPreference,
190+
|ty, _| {
191+
if let ty::TyStruct(def, substs) = ty.sty {
192+
if let Some(field) = def.struct_variant()
193+
.find_field_named(item_name) {
194+
let snippet = tcx.sess.codemap().span_to_snippet(expr.span);
195+
let expr_string = match snippet {
196+
Ok(expr_string) => expr_string,
197+
_ => "s".into() // Default to a generic placeholder for the
198+
// expression when we can't generate a
199+
// string snippet
200+
};
201+
202+
let field_ty = field.ty(tcx, substs);
203+
204+
if self.is_fn_ty(&field_ty, span) {
205+
err.note(&format!("use `({0}.{1})(...)` if you meant to \
206+
call the function stored in the `{1}` \
207+
field",
208+
expr_string, item_name));
209+
} else {
210+
err.note(&format!("did you mean to write `{0}.{1}`?",
211+
expr_string, item_name));
212+
}
213+
return Some(());
214+
}
215+
}
216+
None
217+
});
203218
}
204219

205220
if self.is_fn_ty(&rcvr_ty, span) {

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

Lines changed: 46 additions & 0 deletions
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)