Skip to content

Commit cb3ca98

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#36383 - GuillaumeGomez:e0049, r=jonathandturner
Update E0049 to new error format Fixes rust-lang#35210. Part of rust-lang#35233. r? @jonathandturner
2 parents d1acabe + 3558462 commit cb3ca98

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/librustc_typeck/check/compare_method.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
3838
impl_m_span: Span,
3939
impl_m_body_id: ast::NodeId,
4040
trait_m: &ty::Method<'tcx>,
41-
impl_trait_ref: &ty::TraitRef<'tcx>) {
41+
impl_trait_ref: &ty::TraitRef<'tcx>,
42+
trait_item_span: Option<Span>) {
4243
debug!("compare_impl_method(impl_trait_ref={:?})",
4344
impl_trait_ref);
4445

@@ -97,14 +98,42 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
9798
let num_impl_m_type_params = impl_m.generics.types.len();
9899
let num_trait_m_type_params = trait_m.generics.types.len();
99100
if num_impl_m_type_params != num_trait_m_type_params {
100-
span_err!(tcx.sess, impl_m_span, E0049,
101+
let impl_m_node_id = tcx.map.as_local_node_id(impl_m.def_id).unwrap();
102+
let span = match tcx.map.expect_impl_item(impl_m_node_id).node {
103+
ImplItemKind::Method(ref impl_m_sig, _) => {
104+
if impl_m_sig.generics.is_parameterized() {
105+
impl_m_sig.generics.span
106+
} else {
107+
impl_m_span
108+
}
109+
}
110+
_ => bug!("{:?} is not a method", impl_m)
111+
};
112+
113+
struct_span_err!(tcx.sess, span, E0049,
101114
"method `{}` has {} type parameter{} \
102115
but its trait declaration has {} type parameter{}",
103116
trait_m.name,
104117
num_impl_m_type_params,
105118
if num_impl_m_type_params == 1 {""} else {"s"},
106119
num_trait_m_type_params,
107-
if num_trait_m_type_params == 1 {""} else {"s"});
120+
if num_trait_m_type_params == 1 {""} else {"s"})
121+
.span_label(trait_item_span.unwrap(),
122+
&format!("expected {}",
123+
&if num_trait_m_type_params != 1 {
124+
format!("{} type parameters",
125+
num_trait_m_type_params)
126+
} else {
127+
format!("{} type parameter",
128+
num_trait_m_type_params)
129+
}))
130+
.span_label(span, &format!("found {}",
131+
&if num_impl_m_type_params != 1 {
132+
format!("{} type parameters", num_impl_m_type_params)
133+
} else {
134+
format!("1 type parameter")
135+
}))
136+
.emit();
108137
return;
109138
}
110139

src/librustc_typeck/check/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1015,13 +1015,15 @@ fn check_impl_items_against_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
10151015
_ => span_bug!(impl_item.span, "non-method impl-item for method")
10161016
};
10171017

1018+
let trait_span = tcx.map.span_if_local(ty_trait_item.def_id());
10181019
if let &ty::MethodTraitItem(ref trait_method) = ty_trait_item {
10191020
compare_impl_method(ccx,
10201021
&impl_method,
10211022
impl_item.span,
10221023
body.id,
10231024
&trait_method,
1024-
&impl_trait_ref);
1025+
&impl_trait_ref,
1026+
trait_span);
10251027
} else {
10261028
let mut err = struct_span_err!(tcx.sess, impl_item.span, E0324,
10271029
"item `{}` is an associated method, \

src/test/compile-fail/E0049.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
// except according to those terms.
1010

1111
trait Foo {
12-
fn foo<T: Default>(x: T) -> Self;
12+
fn foo<T: Default>(x: T) -> Self; //~ NOTE expected 1 type parameter
1313
}
1414

1515
struct Bar;
1616

1717
impl Foo for Bar {
1818
fn foo(x: bool) -> Self { Bar } //~ ERROR E0049
19+
//~| NOTE found 0 type parameters
1920
}
2021

2122
fn main() {

0 commit comments

Comments
 (0)