Use last path segment for uncalled method note if span_to_segment fails

PR: 
This commit is contained in:
Daniel J Rollins 2016-03-05 19:50:34 +00:00 committed by Manish Goregaokar
parent fa0efa69c5
commit 234371216e
2 changed files with 19 additions and 6 deletions
src
librustc_typeck/check/method
test/compile-fail

@ -25,11 +25,13 @@ use middle::subst::Substs;
use middle::traits::{Obligation, SelectionContext};
use util::nodemap::{FnvHashSet};
use syntax::ast;
use syntax::codemap::Span;
use syntax::errors::DiagnosticBuilder;
use rustc_front::print::pprust;
use rustc_front::hir;
use rustc_front::hir::Expr_;
use std::cell;
use std::cmp::Ordering;
@ -130,17 +132,28 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
}
if is_fn_ty(&rcvr_ty, &fcx, span) {
macro_rules! report_function {
($span:expr, $name:expr) => {
err.fileline_note(
$span,
&format!("{} is a function, perhaps you wish to call it",
$name));
}
}
if let Some(expr) = rcvr_expr {
if let Ok (expr_string) = cx.sess.codemap().span_to_snippet(expr.span) {
err.fileline_note(
expr.span,
&format!("{} is a function, perhaps you wish to call it",
expr_string));
report_function!(expr.span, expr_string);
err.span_suggestion(expr.span,
"try calling the base function:",
format!("{}()",
expr_string));
}
else if let Expr_::ExprPath(_, path) = expr.node.clone() {
if let Some(segment) = path.segments.last() {
report_function!(expr.span, segment.identifier.name);
}
}
}
}

@ -22,10 +22,10 @@ fn func() -> ret {
}
fn main() {
obj::func.x();
obj::func.x();
//~^ ERROR no method named `x` found for type `fn() -> ret {obj::func}` in the current scope
//~^^ NOTE obj::func is a function, perhaps you wish to call it
func.x();
func.x();
//~^ ERROR no method named `x` found for type `fn() -> ret {func}` in the current scope
//~^^ NOTE func is a function, perhaps you wish to call it
}