Skip to content

Commit 2343712

Browse files
Daniel J RollinsManishearth
Daniel J Rollins
authored andcommitted
Use last path segment for uncalled method note if span_to_segment fails
PR: #32053
1 parent fa0efa6 commit 2343712

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/librustc_typeck/check/method/suggest.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ use middle::subst::Substs;
2525
use middle::traits::{Obligation, SelectionContext};
2626
use util::nodemap::{FnvHashSet};
2727

28+
2829
use syntax::ast;
2930
use syntax::codemap::Span;
3031
use syntax::errors::DiagnosticBuilder;
3132
use rustc_front::print::pprust;
3233
use rustc_front::hir;
34+
use rustc_front::hir::Expr_;
3335

3436
use std::cell;
3537
use std::cmp::Ordering;
@@ -130,17 +132,28 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
130132
}
131133

132134
if is_fn_ty(&rcvr_ty, &fcx, span) {
133-
if let Some(expr) = rcvr_expr {
134-
if let Ok (expr_string) = cx.sess.codemap().span_to_snippet(expr.span) {
135+
macro_rules! report_function {
136+
($span:expr, $name:expr) => {
135137
err.fileline_note(
136-
expr.span,
138+
$span,
137139
&format!("{} is a function, perhaps you wish to call it",
138-
expr_string));
140+
$name));
141+
}
142+
}
143+
144+
if let Some(expr) = rcvr_expr {
145+
if let Ok (expr_string) = cx.sess.codemap().span_to_snippet(expr.span) {
146+
report_function!(expr.span, expr_string);
139147
err.span_suggestion(expr.span,
140148
"try calling the base function:",
141149
format!("{}()",
142150
expr_string));
143151
}
152+
else if let Expr_::ExprPath(_, path) = expr.node.clone() {
153+
if let Some(segment) = path.segments.last() {
154+
report_function!(expr.span, segment.identifier.name);
155+
}
156+
}
144157
}
145158
}
146159

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ fn func() -> ret {
2222
}
2323

2424
fn main() {
25-
obj::func.x();
25+
obj::func.x();
2626
//~^ ERROR no method named `x` found for type `fn() -> ret {obj::func}` in the current scope
2727
//~^^ NOTE obj::func is a function, perhaps you wish to call it
28-
func.x();
28+
func.x();
2929
//~^ ERROR no method named `x` found for type `fn() -> ret {func}` in the current scope
3030
//~^^ NOTE func is a function, perhaps you wish to call it
3131
}

0 commit comments

Comments
 (0)