Skip to content

Calibrate span for method call error messages #13713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
}
Some(method) => {
debug!("(privacy checking) checking impl method");
self.check_method(expr.span, method.origin, ident);
self.check_method(expr.span, method.origin, ident.node);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5083,7 +5083,7 @@ impl<'a> Resolver<'a> {
debug!("(recording candidate traits for expr) recording \
traits for {}",
expr.id);
let traits = self.search_for_traits_containing_method(ident.name);
let traits = self.search_for_traits_containing_method(ident.node.name);
self.trait_map.insert(expr.id, traits);
}
_ => {
Expand Down
15 changes: 7 additions & 8 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
}
_ => {
fcx.tcx().sess.span_bug(
sp,
callee_expr.span,
format!("method without bare fn type"));
}
}
Expand Down Expand Up @@ -1936,7 +1936,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
// Checks a method call.
fn check_method_call(fcx: &FnCtxt,
expr: &ast::Expr,
method_name: ast::Ident,
method_name: ast::SpannedIdent,
args: &[@ast::Expr],
tps: &[ast::P<ast::Ty>]) {
let rcvr = args[0];
Expand All @@ -1952,7 +1952,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,

let tps = tps.iter().map(|&ast_ty| fcx.to_ty(ast_ty)).collect::<Vec<_>>();
let fn_ty = match method::lookup(fcx, expr, rcvr,
method_name.name,
method_name.node.name,
expr_t, tps.as_slice(),
DontDerefArgs,
CheckTraitsAndInherentMethods,
Expand All @@ -1966,11 +1966,10 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
None => {
debug!("(checking method call) failing expr is {}", expr.id);

fcx.type_error_message(expr.span,
fcx.type_error_message(method_name.span,
|actual| {
format!("type `{}` does not implement any method in scope \
named `{}`",
actual, token::get_ident(method_name))
format!("type `{}` does not implement any method in scope named `{}`",
actual, token::get_ident(method_name.node))
},
expr_t,
None);
Expand All @@ -1982,7 +1981,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
};

// Call the generic checker.
let ret_ty = check_method_argument_types(fcx, expr.span,
let ret_ty = check_method_argument_types(fcx, method_name.span,
fn_ty, expr, args,
DontDerefArgs);

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ pub enum Expr_ {
ExprBox(@Expr, @Expr),
ExprVec(Vec<@Expr>),
ExprCall(@Expr, Vec<@Expr>),
ExprMethodCall(Ident, Vec<P<Ty>>, Vec<@Expr>),
ExprMethodCall(SpannedIdent, Vec<P<Ty>>, Vec<@Expr>),
ExprTup(Vec<@Expr>),
ExprBinary(BinOp, @Expr, @Expr),
ExprUnary(UnOp, @Expr),
Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use abi;
use ast::{P, Ident};
use ast;
use ast_util;
use codemap::{Span, respan, DUMMY_SP};
use codemap::{Span, respan, Spanned, DUMMY_SP};
use ext::base::ExtCtxt;
use ext::quote::rt::*;
use fold::Folder;
Expand Down Expand Up @@ -548,8 +548,9 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
expr: @ast::Expr,
ident: ast::Ident,
mut args: Vec<@ast::Expr> ) -> @ast::Expr {
let id = Spanned { node: ident, span: span };
args.unshift(expr);
self.expr(span, ast::ExprMethodCall(ident, Vec::new(), args))
self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
}
fn expr_block(&self, b: P<ast::Block>) -> @ast::Expr {
self.expr(b.span, ast::ExprBlock(b))
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ pub fn noop_fold_expr<T: Folder>(e: @Expr, folder: &mut T) -> @Expr {
}
ExprMethodCall(i, ref tps, ref args) => {
ExprMethodCall(
folder.fold_ident(i),
respan(i.span, folder.fold_ident(i.node)),
tps.iter().map(|&x| folder.fold_ty(x)).collect(),
args.iter().map(|&x| folder.fold_expr(x)).collect())
}
Expand Down
10 changes: 8 additions & 2 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,11 @@ impl<'a> Parser<'a> {
ExprCall(f, args)
}

fn mk_method_call(&mut self, ident: Ident, tps: Vec<P<Ty>> , args: Vec<@Expr> ) -> ast::Expr_ {
fn mk_method_call(&mut self,
ident: ast::SpannedIdent,
tps: Vec<P<Ty>>,
args: Vec<@Expr>)
-> ast::Expr_ {
ExprMethodCall(ident, tps, args)
}

Expand Down Expand Up @@ -1919,6 +1923,7 @@ impl<'a> Parser<'a> {
if self.eat(&token::DOT) {
match self.token {
token::IDENT(i, _) => {
let dot = self.last_span.hi;
hi = self.span.hi;
self.bump();
let (_, tys) = if self.eat(&token::MOD_SEP) {
Expand All @@ -1940,7 +1945,8 @@ impl<'a> Parser<'a> {
hi = self.last_span.hi;

es.unshift(e);
let nd = self.mk_method_call(i, tys, es);
let id = spanned(dot, hi, i);
let nd = self.mk_method_call(id, tys, es);
e = self.mk_expr(lo, hi, nd);
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ impl<'a> State<'a> {
let base_args = args.slice_from(1);
try!(self.print_expr(*args.get(0)));
try!(word(&mut self.s, "."));
try!(self.print_ident(ident));
try!(self.print_ident(ident.node));
if tys.len() > 0u {
try!(word(&mut self.s, "::<"));
try!(self.commasep(Inconsistent, tys.as_slice(),
Expand Down
30 changes: 30 additions & 0 deletions src/test/compile-fail/method-call-err-msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test that parameter cardinality or missing method error gets span exactly.

pub struct Foo;
impl Foo {
fn zero(self) -> Foo { self }
fn one(self, _: int) -> Foo { self }
fn two(self, _: int, _: int) -> Foo { self }
}

fn main() {
let x = Foo;
x.zero(0) //~ ERROR this function takes 0 parameters but 1 parameter was supplied
.one() //~ ERROR this function takes 1 parameter but 0 parameters were supplied
.two(0); //~ ERROR this function takes 2 parameters but 1 parameter was supplied

let y = Foo;
y.zero()
.take() //~ ERROR type `Foo` does not implement any method in scope named `take`
.one(0);
}