@@ -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 }
0 commit comments