Skip to content

Commit

Permalink
Update E0050 to new error format
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Sep 28, 2016
1 parent bdad702 commit 61cbf41
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/map/mod.rs
Expand Up @@ -260,7 +260,7 @@ impl<'ast> Map<'ast> {
EntryVariant(p, _) |
EntryExpr(p, _) |
EntryStmt(p, _) |
EntryTy(p, _) |
EntryTy(p, _) |
EntryLocal(p, _) |
EntryPat(p, _) |
EntryBlock(p, _) |
Expand Down
64 changes: 60 additions & 4 deletions src/librustc_typeck/check/compare_method.rs
Expand Up @@ -138,14 +138,70 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
}

if impl_m.fty.sig.0.inputs.len() != trait_m.fty.sig.0.inputs.len() {
span_err!(tcx.sess, impl_m_span, E0050,
let trait_number_args = trait_m.fty.sig.0.inputs.len();
let impl_number_args = impl_m.fty.sig.0.inputs.len();
let trait_m_node_id = tcx.map.as_local_node_id(trait_m.def_id);
let trait_span = if let Some(trait_id) = trait_m_node_id {
match tcx.map.expect_trait_item(trait_id).node {
TraitItem_::MethodTraitItem(ref trait_m_sig, _) => {
if let Some(arg) = trait_m_sig.decl.inputs.get(
if trait_number_args > 0 {
trait_number_args - 1
} else {
0
}) {
Some(arg.pat.span)
} else {
trait_item_span
}
}
_ => bug!("{:?} is not a method", impl_m)
}
} else {
trait_item_span
};
let impl_m_node_id = tcx.map.as_local_node_id(impl_m.def_id).unwrap();
let impl_span = match tcx.map.expect_impl_item(impl_m_node_id).node {
ImplItemKind::Method(ref impl_m_sig, _) => {
if let Some(arg) = impl_m_sig.decl.inputs.get(
if impl_number_args > 0 {
impl_number_args - 1
} else {
0
}) {
arg.pat.span
} else {
impl_m_span
}
}
_ => bug!("{:?} is not a method", impl_m)
};
let mut err = struct_span_err!(tcx.sess, impl_span, E0050,
"method `{}` has {} parameter{} \
but the declaration in trait `{}` has {}",
trait_m.name,
impl_m.fty.sig.0.inputs.len(),
if impl_m.fty.sig.0.inputs.len() == 1 {""} else {"s"},
impl_number_args,
if impl_number_args == 1 {""} else {"s"},
tcx.item_path_str(trait_m.def_id),
trait_m.fty.sig.0.inputs.len());
trait_number_args);
if let Some(trait_span) = trait_span {
err.span_label(trait_span,
&format!("trait requires {}",
&if trait_number_args != 1 {
format!("{} parameters", trait_number_args)
} else {
format!("{} parameter", trait_number_args)
}));
}
err.span_label(impl_span,
&format!("expected {}, found {}",
&if trait_number_args != 1 {
format!("{} parameters", trait_number_args)
} else {
format!("{} parameter", trait_number_args)
},
impl_number_args));
err.emit();
return;
}

Expand Down
9 changes: 8 additions & 1 deletion src/test/compile-fail/E0050.rs
Expand Up @@ -9,13 +9,20 @@
// except according to those terms.

trait Foo {
fn foo(&self, x: u8) -> bool;
fn foo(&self, x: u8) -> bool; //~ NOTE trait requires 2 parameters
fn bar(&self, x: u8, y: u8, z: u8); //~ NOTE trait requires 4 parameters
fn less(&self); //~ NOTE trait requires 1 parameter
}

struct Bar;

impl Foo for Bar {
fn foo(&self) -> bool { true } //~ ERROR E0050
//~| NOTE expected 2 parameters, found 1
fn bar(&self) { } //~ ERROR E0050
//~| NOTE expected 4 parameters, found 1
fn less(&self, x: u8, y: u8, z: u8) { } //~ ERROR E0050
//~| NOTE expected 1 parameter, found 4
}

fn main() {
Expand Down

0 comments on commit 61cbf41

Please sign in to comment.