Skip to content

Commit

Permalink
Use last path segment for uncalled method note if span_to_segment fails
Browse files Browse the repository at this point in the history
PR: #32053
  • Loading branch information
Daniel J Rollins authored and Manishearth committed Mar 19, 2016
1 parent fa0efa6 commit 2343712
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
21 changes: 17 additions & 4 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,17 +132,28 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
}

if is_fn_ty(&rcvr_ty, &fcx, span) {
if let Some(expr) = rcvr_expr {
if let Ok (expr_string) = cx.sess.codemap().span_to_snippet(expr.span) {
macro_rules! report_function {
($span:expr, $name:expr) => {
err.fileline_note(
expr.span,
$span,
&format!("{} is a function, perhaps you wish to call it",
expr_string));
$name));
}
}

if let Some(expr) = rcvr_expr {
if let Ok (expr_string) = cx.sess.codemap().span_to_snippet(expr.span) {
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);
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-29124.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 2343712

Please sign in to comment.