From 6225e980bf0376986c10943216fb0b7779d29e58 Mon Sep 17 00:00:00 2001 From: Ellen Date: Thu, 5 May 2022 12:23:42 +0100 Subject: [PATCH 1/5] handle mismatched generic parameter kinds --- .../rustc_typeck/src/check/compare_method.rs | 215 +++++++++++++----- .../mismatched_ty_const_in_trait_impl.rs | 25 ++ .../mismatched_ty_const_in_trait_impl.stderr | 52 +++++ .../const_params_have_right_type.rs | 12 + .../const_params_have_right_type.stderr | 15 ++ 5 files changed, 264 insertions(+), 55 deletions(-) create mode 100644 src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs create mode 100644 src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr create mode 100644 src/test/ui/generic-associated-types/const_params_have_right_type.rs create mode 100644 src/test/ui/generic-associated-types/const_params_have_right_type.stderr diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index 6d78a863d54cf..c42280a8208e4 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -48,6 +48,10 @@ crate fn compare_impl_method<'tcx>( return; } + if let Err(_) = compare_generic_param_kinds(tcx, impl_m, trait_m, trait_item_span) { + return; + } + if let Err(_) = compare_number_of_method_arguments(tcx, impl_m, impl_m_span, trait_m, trait_item_span) { @@ -62,10 +66,6 @@ crate fn compare_impl_method<'tcx>( { return; } - - if let Err(_) = compare_const_param_types(tcx, impl_m, trait_m, trait_item_span) { - return; - } } fn compare_predicate_entailment<'tcx>( @@ -914,62 +914,165 @@ fn compare_synthetic_generics<'tcx>( if let Some(reported) = error_found { Err(reported) } else { Ok(()) } } -fn compare_const_param_types<'tcx>( +/// Checks that all parameters in the generics of a given assoc item in a trait impl have +/// the same kind as the respective generic parameter in the trait def. +/// +/// For example all 4 errors in the following code are emitted here: +/// ``` +/// trait Foo { +/// fn foo(); +/// type bar; +/// fn baz(); +/// type blah; +/// } +/// +/// impl Foo for () { +/// fn foo() {} +/// //~^ error +/// type bar {} +/// //~^ error +/// fn baz() {} +/// //~^ error +/// type blah = u32; +/// //~^ error +/// } +/// ``` +/// +/// This function does not handle lifetime parameters +fn compare_generic_param_kinds<'tcx>( tcx: TyCtxt<'tcx>, - impl_m: &ty::AssocItem, - trait_m: &ty::AssocItem, + impl_item: &ty::AssocItem, + trait_item: &ty::AssocItem, trait_item_span: Option, ) -> Result<(), ErrorGuaranteed> { - let const_params_of = |def_id| { - tcx.generics_of(def_id).params.iter().filter_map(|param| match param.kind { - GenericParamDefKind::Const { .. } => Some(param.def_id), - _ => None, + assert_eq!(impl_item.kind, trait_item.kind); + + let ty_const_params_of = |def_id| { + tcx.generics_of(def_id).params.iter().filter(|param| { + matches!( + param.kind, + GenericParamDefKind::Const { .. } | GenericParamDefKind::Type { .. } + ) }) }; - let const_params_impl = const_params_of(impl_m.def_id); - let const_params_trait = const_params_of(trait_m.def_id); - - for (const_param_impl, const_param_trait) in iter::zip(const_params_impl, const_params_trait) { - let impl_ty = tcx.type_of(const_param_impl); - let trait_ty = tcx.type_of(const_param_trait); - if impl_ty != trait_ty { - let (impl_span, impl_ident) = match tcx.hir().get_if_local(const_param_impl) { - Some(hir::Node::GenericParam(hir::GenericParam { span, name, .. })) => ( - span, - match name { - hir::ParamName::Plain(ident) => Some(ident), - _ => None, - }, - ), - other => bug!( - "expected GenericParam, found {:?}", - other.map_or_else(|| "nothing".to_string(), |n| format!("{:?}", n)) - ), - }; - let trait_span = match tcx.hir().get_if_local(const_param_trait) { - Some(hir::Node::GenericParam(hir::GenericParam { span, .. })) => Some(span), - _ => None, - }; - let mut err = struct_span_err!( - tcx.sess, - *impl_span, - E0053, - "method `{}` has an incompatible const parameter type for trait", - trait_m.name - ); - err.span_note( - trait_span.map_or_else(|| trait_item_span.unwrap_or(*impl_span), |span| *span), - &format!( - "the const parameter{} has type `{}`, but the declaration \ - in trait `{}` has type `{}`", - &impl_ident.map_or_else(|| "".to_string(), |ident| format!(" `{ident}`")), - impl_ty, - tcx.def_path_str(trait_m.def_id), - trait_ty - ), - ); - let reported = err.emit(); - return Err(reported); + + let get_param_span = |param: &ty::GenericParamDef| match tcx.hir().get_if_local(param.def_id) { + Some(hir::Node::GenericParam(hir::GenericParam { span, .. })) => Some(span), + _ => None, + }; + + let get_param_ident = |param: &ty::GenericParamDef| match tcx.hir().get_if_local(param.def_id) { + Some(hir::Node::GenericParam(hir::GenericParam { name, .. })) => match name { + hir::ParamName::Plain(ident) => Some(ident), + _ => None, + }, + other => bug!( + "expected GenericParam, found {:?}", + other.map_or_else(|| "nothing".to_string(), |n| format!("{:?}", n)) + ), + }; + + let ty_const_params_impl = ty_const_params_of(impl_item.def_id); + let ty_const_params_trait = ty_const_params_of(trait_item.def_id); + let assoc_item_str = assoc_item_kind_str(&impl_item); + + for (param_impl, param_trait) in iter::zip(ty_const_params_impl, ty_const_params_trait) { + use GenericParamDefKind::*; + match (¶m_impl.kind, ¶m_trait.kind) { + (Const { .. }, Const { .. }) => { + let impl_ty = tcx.type_of(param_impl.def_id); + let trait_ty = tcx.type_of(param_trait.def_id); + if impl_ty != trait_ty { + let param_impl_span = get_param_span(param_impl).unwrap(); + let param_impl_ident = get_param_ident(param_impl); + let param_trait_span = get_param_span(param_trait); + + let mut err = struct_span_err!( + tcx.sess, + *param_impl_span, + E0053, + "{} `{}` has an incompatible const parameter type for trait", + assoc_item_str, + trait_item.name, + ); + err.span_note( + param_trait_span.map_or_else( + || trait_item_span.unwrap_or(*param_impl_span), + |span| *span, + ), + &format!( + "the const parameter{} has type `{}`, but the declaration \ + in trait `{}` has type `{}`", + ¶m_impl_ident + .map_or_else(|| "".to_string(), |ident| format!(" `{ident}`")), + impl_ty, + tcx.def_path_str(trait_item.def_id), + trait_ty + ), + ); + let reported = err.emit(); + return Err(reported); + } + } + (Const { .. }, Type { .. }) => { + let impl_ty = tcx.type_of(param_impl.def_id); + let param_impl_span = get_param_span(param_impl).unwrap(); + let param_impl_ident = get_param_ident(param_impl); + let param_trait_span = get_param_span(param_trait); + + let mut err = struct_span_err!( + tcx.sess, + *param_impl_span, + E0053, + "{} `{}` has an incompatible generic parameter for trait", + assoc_item_str, + trait_item.name, + ); + err.span_note( + param_trait_span + .map_or_else(|| trait_item_span.unwrap_or(*param_impl_span), |span| *span), + &format!( + "the trait impl specifies{} a const parameter of type `{}`, but the declaration \ + in trait `{}` requires it is a type parameter", + ¶m_impl_ident + .map_or_else(|| "".to_string(), |ident| format!(" `{ident}` is")), + impl_ty, + tcx.def_path_str(trait_item.def_id), + ), + ); + let reported = err.emit(); + return Err(reported); + } + (Type { .. }, Const { .. }) => { + let trait_ty = tcx.type_of(param_trait.def_id); + let param_impl_span = get_param_span(param_impl).unwrap(); + let param_impl_ident = get_param_ident(param_impl); + let param_trait_span = get_param_span(param_trait); + + let mut err = struct_span_err!( + tcx.sess, + *param_impl_span, + E0053, + "{} `{}` has an incompatible generic parameter for trait", + assoc_item_str, + trait_item.name, + ); + err.span_note( + param_trait_span + .map_or_else(|| trait_item_span.unwrap_or(*param_impl_span), |span| *span), + &format!( + "the trait impl specifies{} a type parameter, but the declaration \ + in trait `{}` requires it is a const parameter of type `{}`", + ¶m_impl_ident + .map_or_else(|| "".to_string(), |ident| format!(" `{ident}` is")), + tcx.def_path_str(trait_item.def_id), + trait_ty, + ), + ); + let reported = err.emit(); + return Err(reported); + } + _ => (), } } @@ -1095,6 +1198,8 @@ crate fn compare_ty_impl<'tcx>( let _: Result<(), ErrorGuaranteed> = (|| { compare_number_of_generics(tcx, impl_ty, impl_ty_span, trait_ty, trait_item_span)?; + compare_generic_param_kinds(tcx, impl_ty, trait_ty, trait_item_span)?; + let sp = tcx.def_span(impl_ty.def_id); compare_type_predicate_entailment(tcx, impl_ty, sp, trait_ty, impl_trait_ref)?; diff --git a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs new file mode 100644 index 0000000000000..0c6669fa9b6d1 --- /dev/null +++ b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs @@ -0,0 +1,25 @@ +trait Trait { + fn foo() {} +} +impl Trait for () { + fn foo() {} + //~^ error: method `foo` has an incompatble generic parameter for trait +} + +trait Other { + fn bar() {} +} +impl Other for () { + fn bar() {} + //~^ error: method `bar` has an incompatible generic parameter for trait +} + +trait Uwu { + fn baz() {} +} +impl Uwu for () { + fn baz() {} + //~^ error: method `baz` has an incompatible generic parameter for trait +} + +fn main() {} diff --git a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr new file mode 100644 index 0000000000000..d400c2165904b --- /dev/null +++ b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr @@ -0,0 +1,52 @@ +error[E0049]: method `foo` has 0 type parameters but its trait declaration has 1 type parameter + --> $DIR/mismatched_ty_const_in_trait_impl.rs:5:12 + | +LL | fn foo() {} + | - expected 1 type parameter +... +LL | fn foo() {} + | ^^^^^^^^^^^^ found 0 type parameters + +error[E0049]: method `foo` has 1 const parameter but its trait declaration has 0 const parameters + --> $DIR/mismatched_ty_const_in_trait_impl.rs:5:12 + | +LL | fn foo() {} + | - expected 0 const parameters +... +LL | fn foo() {} + | ^^^^^^^^^^^^ found 1 const parameter + +error[E0049]: method `bar` has 1 type parameter but its trait declaration has 0 type parameters + --> $DIR/mismatched_ty_const_in_trait_impl.rs:13:12 + | +LL | fn bar() {} + | ----------- expected 0 type parameters +... +LL | fn bar() {} + | ^ found 1 type parameter + +error[E0049]: method `bar` has 0 const parameters but its trait declaration has 1 const parameter + --> $DIR/mismatched_ty_const_in_trait_impl.rs:13:12 + | +LL | fn bar() {} + | ----------- expected 1 const parameter +... +LL | fn bar() {} + | ^ found 0 const parameters + +error[E0053]: method `baz` has an incompatible const parameter type for trait + --> $DIR/mismatched_ty_const_in_trait_impl.rs:21:12 + | +LL | fn baz() {} + | ^^^^^^^^^^^^ + | +note: the const parameter `N` has type `i32`, but the declaration in trait `Uwu::baz` has type `u32` + --> $DIR/mismatched_ty_const_in_trait_impl.rs:18:12 + | +LL | fn baz() {} + | ^^^^^^^^^^^^ + +error: aborting due to 5 previous errors + +Some errors have detailed explanations: E0049, E0053. +For more information about an error, try `rustc --explain E0049`. diff --git a/src/test/ui/generic-associated-types/const_params_have_right_type.rs b/src/test/ui/generic-associated-types/const_params_have_right_type.rs new file mode 100644 index 0000000000000..675132587bfdd --- /dev/null +++ b/src/test/ui/generic-associated-types/const_params_have_right_type.rs @@ -0,0 +1,12 @@ +#![feature(generic_associated_types)] + +trait Trait { + type Foo; +} + +impl Trait for () { + type Foo = u32; + //~^ error: associated type `Foo` has an incompatible const parameter type +} + +fn main() {} diff --git a/src/test/ui/generic-associated-types/const_params_have_right_type.stderr b/src/test/ui/generic-associated-types/const_params_have_right_type.stderr new file mode 100644 index 0000000000000..62353180c6796 --- /dev/null +++ b/src/test/ui/generic-associated-types/const_params_have_right_type.stderr @@ -0,0 +1,15 @@ +error[E0053]: associated type `Foo` has an incompatible const parameter type for trait + --> $DIR/const_params_have_right_type.rs:8:14 + | +LL | type Foo = u32; + | ^^^^^^^^^^^^ + | +note: the const parameter `N` has type `u64`, but the declaration in trait `Trait::Foo` has type `u8` + --> $DIR/const_params_have_right_type.rs:4:14 + | +LL | type Foo; + | ^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0053`. From 4208c53ed695ddb822b6510e3faa0a46e591060e Mon Sep 17 00:00:00 2001 From: Ellen Date: Thu, 5 May 2022 12:41:43 +0100 Subject: [PATCH 2/5] exit out of `compare_number_of_generics` early --- .../rustc_typeck/src/check/compare_method.rs | 30 ++++++++++++++ .../mismatched_ty_const_in_trait_impl.rs | 4 +- .../mismatched_ty_const_in_trait_impl.stderr | 41 +++++++------------ .../const_params_have_right_type.rs | 2 +- .../const_params_have_right_type.stderr | 2 +- 5 files changed, 48 insertions(+), 31 deletions(-) diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index c42280a8208e4..c810be16bf846 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -579,6 +579,27 @@ fn compare_self_type<'tcx>( Ok(()) } +/// Checks that the number of generics on a given assoc item in a trait impl is the same +/// as the number of generics on the respective assoc item in the trait definition. +/// +/// For example this code emits the errors in the following code: +/// ``` +/// trait Trait { +/// fn foo(); +/// type Assoc; +/// } +/// +/// impl Trait for () { +/// fn foo() {} +/// //~^ error +/// type Assoc = u32; +/// //~^ error +/// } +/// ``` +/// +/// Notably this does not error on `foo` implemented as `foo` or +/// `foo` implemented as `foo`. This is handled in +/// [`compare_generic_param_kinds`]. This function also does not handle lifetime parameters fn compare_number_of_generics<'tcx>( tcx: TyCtxt<'tcx>, impl_: &ty::AssocItem, @@ -589,6 +610,15 @@ fn compare_number_of_generics<'tcx>( let trait_own_counts = tcx.generics_of(trait_.def_id).own_counts(); let impl_own_counts = tcx.generics_of(impl_.def_id).own_counts(); + // This avoids us erroring on `foo` implemented as `foo` as this is implemented + // in `compare_generic_param_kinds` which will give a nicer error message than something like: + // "expected 1 type parameter, found 0 type parameters" + if (trait_own_counts.types + trait_own_counts.consts) + == (impl_own_counts.types + impl_own_counts.consts) + { + return Ok(()); + } + let matchings = [ ("type", trait_own_counts.types, impl_own_counts.types), ("const", trait_own_counts.consts, impl_own_counts.consts), diff --git a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs index 0c6669fa9b6d1..fd57060d5e7c4 100644 --- a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs +++ b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs @@ -3,7 +3,7 @@ trait Trait { } impl Trait for () { fn foo() {} - //~^ error: method `foo` has an incompatble generic parameter for trait + //~^ error: method `foo` has an incompatible generic parameter for trait } trait Other { @@ -19,7 +19,7 @@ trait Uwu { } impl Uwu for () { fn baz() {} - //~^ error: method `baz` has an incompatible generic parameter for trait + //~^ error: method `baz` has an incompatible const parameter type for trait } fn main() {} diff --git a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr index d400c2165904b..cea8f4e50b067 100644 --- a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr +++ b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr @@ -1,38 +1,26 @@ -error[E0049]: method `foo` has 0 type parameters but its trait declaration has 1 type parameter +error[E0053]: method `foo` has an incompatible generic parameter for trait --> $DIR/mismatched_ty_const_in_trait_impl.rs:5:12 | -LL | fn foo() {} - | - expected 1 type parameter -... LL | fn foo() {} - | ^^^^^^^^^^^^ found 0 type parameters - -error[E0049]: method `foo` has 1 const parameter but its trait declaration has 0 const parameters - --> $DIR/mismatched_ty_const_in_trait_impl.rs:5:12 + | ^^^^^^^^^^^^ + | +note: the trait impl specifies `M` is a const parameter of type `u64`, but the declaration in trait `Trait::foo` requires it is a type parameter + --> $DIR/mismatched_ty_const_in_trait_impl.rs:2:12 | LL | fn foo() {} - | - expected 0 const parameters -... -LL | fn foo() {} - | ^^^^^^^^^^^^ found 1 const parameter + | ^ -error[E0049]: method `bar` has 1 type parameter but its trait declaration has 0 type parameters +error[E0053]: method `bar` has an incompatible generic parameter for trait --> $DIR/mismatched_ty_const_in_trait_impl.rs:13:12 | -LL | fn bar() {} - | ----------- expected 0 type parameters -... LL | fn bar() {} - | ^ found 1 type parameter - -error[E0049]: method `bar` has 0 const parameters but its trait declaration has 1 const parameter - --> $DIR/mismatched_ty_const_in_trait_impl.rs:13:12 + | ^ + | +note: the trait impl specifies `T` is a type parameter, but the declaration in trait `Other::bar` requires it is a const parameter of type `u8` + --> $DIR/mismatched_ty_const_in_trait_impl.rs:10:12 | LL | fn bar() {} - | ----------- expected 1 const parameter -... -LL | fn bar() {} - | ^ found 0 const parameters + | ^^^^^^^^^^^ error[E0053]: method `baz` has an incompatible const parameter type for trait --> $DIR/mismatched_ty_const_in_trait_impl.rs:21:12 @@ -46,7 +34,6 @@ note: the const parameter `N` has type `i32`, but the declaration in trait `Uwu: LL | fn baz() {} | ^^^^^^^^^^^^ -error: aborting due to 5 previous errors +error: aborting due to 3 previous errors -Some errors have detailed explanations: E0049, E0053. -For more information about an error, try `rustc --explain E0049`. +For more information about this error, try `rustc --explain E0053`. diff --git a/src/test/ui/generic-associated-types/const_params_have_right_type.rs b/src/test/ui/generic-associated-types/const_params_have_right_type.rs index 675132587bfdd..138d332eed432 100644 --- a/src/test/ui/generic-associated-types/const_params_have_right_type.rs +++ b/src/test/ui/generic-associated-types/const_params_have_right_type.rs @@ -6,7 +6,7 @@ trait Trait { impl Trait for () { type Foo = u32; - //~^ error: associated type `Foo` has an incompatible const parameter type + //~^ error: type `Foo` has an incompatible const parameter type } fn main() {} diff --git a/src/test/ui/generic-associated-types/const_params_have_right_type.stderr b/src/test/ui/generic-associated-types/const_params_have_right_type.stderr index 62353180c6796..4ec6c19f54bcf 100644 --- a/src/test/ui/generic-associated-types/const_params_have_right_type.stderr +++ b/src/test/ui/generic-associated-types/const_params_have_right_type.stderr @@ -1,4 +1,4 @@ -error[E0053]: associated type `Foo` has an incompatible const parameter type for trait +error[E0053]: type `Foo` has an incompatible const parameter type for trait --> $DIR/const_params_have_right_type.rs:8:14 | LL | type Foo = u32; From fea1d765033eada386ffc1684d47c00a48d104f1 Mon Sep 17 00:00:00 2001 From: Ellen Date: Thu, 5 May 2022 17:44:54 +0100 Subject: [PATCH 3/5] make `compare_generic_param_kinds` errors consistent --- .../rustc_typeck/src/check/compare_method.rs | 166 +++++------------- .../mismatched_ty_const_in_trait_impl.rs | 18 +- .../mismatched_ty_const_in_trait_impl.stderr | 75 +++++--- .../ui/const-generics/issues/issue-86820.rs | 9 +- .../const-generics/issues/issue-86820.stderr | 21 +-- .../const_params_have_right_type.rs | 2 +- .../const_params_have_right_type.stderr | 17 +- 7 files changed, 142 insertions(+), 166 deletions(-) diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index c810be16bf846..590131e4f6ae1 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -7,10 +7,10 @@ use rustc_hir::intravisit; use rustc_hir::{GenericParamKind, ImplItemKind, TraitItemKind}; use rustc_infer::infer::{self, InferOk, TyCtxtInferExt}; use rustc_infer::traits::util; -use rustc_middle::ty; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::subst::{InternalSubsts, Subst}; use rustc_middle::ty::util::ExplicitSelf; +use rustc_middle::ty::{self, DefIdTree}; use rustc_middle::ty::{GenericParamDefKind, ToPredicate, TyCtxt}; use rustc_span::Span; use rustc_trait_selection::traits::error_reporting::InferCtxtExt; @@ -48,7 +48,7 @@ crate fn compare_impl_method<'tcx>( return; } - if let Err(_) = compare_generic_param_kinds(tcx, impl_m, trait_m, trait_item_span) { + if let Err(_) = compare_generic_param_kinds(tcx, impl_m, trait_m) { return; } @@ -973,7 +973,6 @@ fn compare_generic_param_kinds<'tcx>( tcx: TyCtxt<'tcx>, impl_item: &ty::AssocItem, trait_item: &ty::AssocItem, - trait_item_span: Option, ) -> Result<(), ErrorGuaranteed> { assert_eq!(impl_item.kind, trait_item.kind); @@ -986,123 +985,54 @@ fn compare_generic_param_kinds<'tcx>( }) }; - let get_param_span = |param: &ty::GenericParamDef| match tcx.hir().get_if_local(param.def_id) { - Some(hir::Node::GenericParam(hir::GenericParam { span, .. })) => Some(span), - _ => None, - }; + for (param_impl, param_trait) in + iter::zip(ty_const_params_of(impl_item.def_id), ty_const_params_of(trait_item.def_id)) + { + use GenericParamDefKind::*; + if match (¶m_impl.kind, ¶m_trait.kind) { + (Const { .. }, Const { .. }) + if tcx.type_of(param_impl.def_id) != tcx.type_of(param_trait.def_id) => + { + true + } + (Const { .. }, Type { .. }) | (Type { .. }, Const { .. }) => true, + // this is exhaustive so that anyone adding new generic param kinds knows + // to make sure this error is reported for them. + (Const { .. }, Const { .. }) | (Type { .. }, Type { .. }) => false, + (Lifetime { .. }, _) | (_, Lifetime { .. }) => unreachable!(), + } { + let make_param_message = |prefix: &str, param: &ty::GenericParamDef| match param.kind { + Const { .. } => { + format!("{} const parameter with type `{}`", prefix, tcx.type_of(param.def_id)) + } + Type { .. } => format!("{} type parameter", prefix), + Lifetime { .. } => unreachable!(), + }; - let get_param_ident = |param: &ty::GenericParamDef| match tcx.hir().get_if_local(param.def_id) { - Some(hir::Node::GenericParam(hir::GenericParam { name, .. })) => match name { - hir::ParamName::Plain(ident) => Some(ident), - _ => None, - }, - other => bug!( - "expected GenericParam, found {:?}", - other.map_or_else(|| "nothing".to_string(), |n| format!("{:?}", n)) - ), - }; + let param_impl_span = tcx.def_span(param_impl.def_id); + let param_trait_span = tcx.def_span(param_trait.def_id); - let ty_const_params_impl = ty_const_params_of(impl_item.def_id); - let ty_const_params_trait = ty_const_params_of(trait_item.def_id); - let assoc_item_str = assoc_item_kind_str(&impl_item); + let mut err = struct_span_err!( + tcx.sess, + param_impl_span, + E0053, + "{} `{}` has an incompatible generic parameter for trait: `{}`", + assoc_item_kind_str(&impl_item), + trait_item.name, + &tcx.def_path_str(tcx.parent(trait_item.def_id)) + ); - for (param_impl, param_trait) in iter::zip(ty_const_params_impl, ty_const_params_trait) { - use GenericParamDefKind::*; - match (¶m_impl.kind, ¶m_trait.kind) { - (Const { .. }, Const { .. }) => { - let impl_ty = tcx.type_of(param_impl.def_id); - let trait_ty = tcx.type_of(param_trait.def_id); - if impl_ty != trait_ty { - let param_impl_span = get_param_span(param_impl).unwrap(); - let param_impl_ident = get_param_ident(param_impl); - let param_trait_span = get_param_span(param_trait); - - let mut err = struct_span_err!( - tcx.sess, - *param_impl_span, - E0053, - "{} `{}` has an incompatible const parameter type for trait", - assoc_item_str, - trait_item.name, - ); - err.span_note( - param_trait_span.map_or_else( - || trait_item_span.unwrap_or(*param_impl_span), - |span| *span, - ), - &format!( - "the const parameter{} has type `{}`, but the declaration \ - in trait `{}` has type `{}`", - ¶m_impl_ident - .map_or_else(|| "".to_string(), |ident| format!(" `{ident}`")), - impl_ty, - tcx.def_path_str(trait_item.def_id), - trait_ty - ), - ); - let reported = err.emit(); - return Err(reported); - } - } - (Const { .. }, Type { .. }) => { - let impl_ty = tcx.type_of(param_impl.def_id); - let param_impl_span = get_param_span(param_impl).unwrap(); - let param_impl_ident = get_param_ident(param_impl); - let param_trait_span = get_param_span(param_trait); - - let mut err = struct_span_err!( - tcx.sess, - *param_impl_span, - E0053, - "{} `{}` has an incompatible generic parameter for trait", - assoc_item_str, - trait_item.name, - ); - err.span_note( - param_trait_span - .map_or_else(|| trait_item_span.unwrap_or(*param_impl_span), |span| *span), - &format!( - "the trait impl specifies{} a const parameter of type `{}`, but the declaration \ - in trait `{}` requires it is a type parameter", - ¶m_impl_ident - .map_or_else(|| "".to_string(), |ident| format!(" `{ident}` is")), - impl_ty, - tcx.def_path_str(trait_item.def_id), - ), - ); - let reported = err.emit(); - return Err(reported); - } - (Type { .. }, Const { .. }) => { - let trait_ty = tcx.type_of(param_trait.def_id); - let param_impl_span = get_param_span(param_impl).unwrap(); - let param_impl_ident = get_param_ident(param_impl); - let param_trait_span = get_param_span(param_trait); - - let mut err = struct_span_err!( - tcx.sess, - *param_impl_span, - E0053, - "{} `{}` has an incompatible generic parameter for trait", - assoc_item_str, - trait_item.name, - ); - err.span_note( - param_trait_span - .map_or_else(|| trait_item_span.unwrap_or(*param_impl_span), |span| *span), - &format!( - "the trait impl specifies{} a type parameter, but the declaration \ - in trait `{}` requires it is a const parameter of type `{}`", - ¶m_impl_ident - .map_or_else(|| "".to_string(), |ident| format!(" `{ident}` is")), - tcx.def_path_str(trait_item.def_id), - trait_ty, - ), - ); - let reported = err.emit(); - return Err(reported); - } - _ => (), + let trait_header_span = tcx.def_ident_span(tcx.parent(trait_item.def_id)).unwrap(); + err.span_label(trait_header_span, ""); + err.span_label(param_trait_span, make_param_message("expected", param_trait)); + + let impl_header_span = + tcx.sess.source_map().guess_head_span(tcx.def_span(tcx.parent(impl_item.def_id))); + err.span_label(impl_header_span, ""); + err.span_label(param_impl_span, make_param_message("found", param_impl)); + + let reported = err.emit(); + return Err(reported); } } @@ -1228,7 +1158,7 @@ crate fn compare_ty_impl<'tcx>( let _: Result<(), ErrorGuaranteed> = (|| { compare_number_of_generics(tcx, impl_ty, impl_ty_span, trait_ty, trait_item_span)?; - compare_generic_param_kinds(tcx, impl_ty, trait_ty, trait_item_span)?; + compare_generic_param_kinds(tcx, impl_ty, trait_ty)?; let sp = tcx.def_span(impl_ty.def_id); compare_type_predicate_entailment(tcx, impl_ty, sp, trait_ty, impl_trait_ref)?; diff --git a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs index fd57060d5e7c4..5c9323261a973 100644 --- a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs +++ b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs @@ -19,7 +19,23 @@ trait Uwu { } impl Uwu for () { fn baz() {} - //~^ error: method `baz` has an incompatible const parameter type for trait + //~^ error: method `baz` has an incompatible generic parameter for trait +} + +trait Aaaaaa { + fn bbbb() {} +} +impl Aaaaaa for () { + fn bbbb() {} + //~^ error: method `bbbb` has an incompatible generic parameter for trait +} + +trait Names { + fn abcd() {} +} +impl Names for () { + fn abcd() {} + //~^ error: method `abcd` has an incompatible generic parameter for trait } fn main() {} diff --git a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr index cea8f4e50b067..a1ec8adec76a3 100644 --- a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr +++ b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr @@ -1,39 +1,68 @@ -error[E0053]: method `foo` has an incompatible generic parameter for trait +error[E0053]: method `foo` has an incompatible generic parameter for trait: `Trait` --> $DIR/mismatched_ty_const_in_trait_impl.rs:5:12 | -LL | fn foo() {} - | ^^^^^^^^^^^^ - | -note: the trait impl specifies `M` is a const parameter of type `u64`, but the declaration in trait `Trait::foo` requires it is a type parameter - --> $DIR/mismatched_ty_const_in_trait_impl.rs:2:12 - | +LL | trait Trait { + | ----- LL | fn foo() {} - | ^ + | - expected type parameter +LL | } +LL | impl Trait for () { + | ----------------- +LL | fn foo() {} + | ^^^^^^^^^^^^ found const parameter with type `u64` -error[E0053]: method `bar` has an incompatible generic parameter for trait +error[E0053]: method `bar` has an incompatible generic parameter for trait: `Other` --> $DIR/mismatched_ty_const_in_trait_impl.rs:13:12 | -LL | fn bar() {} - | ^ - | -note: the trait impl specifies `T` is a type parameter, but the declaration in trait `Other::bar` requires it is a const parameter of type `u8` - --> $DIR/mismatched_ty_const_in_trait_impl.rs:10:12 - | +LL | trait Other { + | ----- LL | fn bar() {} - | ^^^^^^^^^^^ + | ----------- expected const parameter with type `u8` +LL | } +LL | impl Other for () { + | ----------------- +LL | fn bar() {} + | ^ found type parameter -error[E0053]: method `baz` has an incompatible const parameter type for trait +error[E0053]: method `baz` has an incompatible generic parameter for trait: `Uwu` --> $DIR/mismatched_ty_const_in_trait_impl.rs:21:12 | +LL | trait Uwu { + | --- +LL | fn baz() {} + | ------------ expected const parameter with type `u32` +LL | } +LL | impl Uwu for () { + | --------------- LL | fn baz() {} - | ^^^^^^^^^^^^ + | ^^^^^^^^^^^^ found const parameter with type `i32` + +error[E0053]: method `bbbb` has an incompatible generic parameter for trait: `Aaaaaa` + --> $DIR/mismatched_ty_const_in_trait_impl.rs:29:13 | -note: the const parameter `N` has type `i32`, but the declaration in trait `Uwu::baz` has type `u32` - --> $DIR/mismatched_ty_const_in_trait_impl.rs:18:12 +LL | trait Aaaaaa { + | ------ +LL | fn bbbb() {} + | ------------ expected const parameter with type `u32` +LL | } +LL | impl Aaaaaa for () { + | ------------------ +LL | fn bbbb() {} + | ^ found type parameter + +error[E0053]: method `abcd` has an incompatible generic parameter for trait: `Names` + --> $DIR/mismatched_ty_const_in_trait_impl.rs:37:13 | -LL | fn baz() {} - | ^^^^^^^^^^^^ +LL | trait Names { + | ----- +LL | fn abcd() {} + | - expected type parameter +LL | } +LL | impl Names for () { + | ----------------- +LL | fn abcd() {} + | ^^^^^^^^^^^^ found const parameter with type `u32` -error: aborting due to 3 previous errors +error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0053`. diff --git a/src/test/ui/const-generics/issues/issue-86820.rs b/src/test/ui/const-generics/issues/issue-86820.rs index 04650403c6baf..9bcb8e1aeed39 100644 --- a/src/test/ui/const-generics/issues/issue-86820.rs +++ b/src/test/ui/const-generics/issues/issue-86820.rs @@ -1,6 +1,6 @@ // Regression test for the ICE described in #86820. -#![allow(unused,dead_code)] +#![allow(unused, dead_code)] use std::ops::BitAnd; const C: fn() = || is_set(); @@ -9,13 +9,12 @@ fn is_set() { } trait Bits { - fn bit(self) -> bool; - //~^ NOTE: the const parameter `I` has type `usize`, but the declaration in trait `Bits::bit` has type `u8` + fn bit(self) -> bool; } impl Bits for u8 { - fn bit(self) -> bool { - //~^ ERROR: method `bit` has an incompatible const parameter type for trait [E0053] + fn bit(self) -> bool { + //~^ ERROR: method `bit` has an incompatible generic parameter for trait: `Bits` [E0053] let i = 1 << I; let mask = u8::from(i); mask & self == mask diff --git a/src/test/ui/const-generics/issues/issue-86820.stderr b/src/test/ui/const-generics/issues/issue-86820.stderr index f7b8d80eeca7f..4d54d654c12d1 100644 --- a/src/test/ui/const-generics/issues/issue-86820.stderr +++ b/src/test/ui/const-generics/issues/issue-86820.stderr @@ -1,14 +1,15 @@ -error[E0053]: method `bit` has an incompatible const parameter type for trait - --> $DIR/issue-86820.rs:17:12 +error[E0053]: method `bit` has an incompatible generic parameter for trait: `Bits` + --> $DIR/issue-86820.rs:16:12 | -LL | fn bit(self) -> bool { - | ^^^^^^^^^^^^^^^ - | -note: the const parameter `I` has type `usize`, but the declaration in trait `Bits::bit` has type `u8` - --> $DIR/issue-86820.rs:12:12 - | -LL | fn bit(self) -> bool; - | ^^^^^^^^^^^^ +LL | trait Bits { + | ---- +LL | fn bit(self) -> bool; + | ----------- expected const parameter with type `u8` +... +LL | impl Bits for u8 { + | ---------------- +LL | fn bit(self) -> bool { + | ^^^^^^^^^^^^^^ found const parameter with type `usize` error: aborting due to previous error diff --git a/src/test/ui/generic-associated-types/const_params_have_right_type.rs b/src/test/ui/generic-associated-types/const_params_have_right_type.rs index 138d332eed432..6bed8e3aff975 100644 --- a/src/test/ui/generic-associated-types/const_params_have_right_type.rs +++ b/src/test/ui/generic-associated-types/const_params_have_right_type.rs @@ -6,7 +6,7 @@ trait Trait { impl Trait for () { type Foo = u32; - //~^ error: type `Foo` has an incompatible const parameter type + //~^ error: type `Foo` has an incompatible generic parameter for trait } fn main() {} diff --git a/src/test/ui/generic-associated-types/const_params_have_right_type.stderr b/src/test/ui/generic-associated-types/const_params_have_right_type.stderr index 4ec6c19f54bcf..9b8eddaff41bc 100644 --- a/src/test/ui/generic-associated-types/const_params_have_right_type.stderr +++ b/src/test/ui/generic-associated-types/const_params_have_right_type.stderr @@ -1,14 +1,15 @@ -error[E0053]: type `Foo` has an incompatible const parameter type for trait +error[E0053]: type `Foo` has an incompatible generic parameter for trait: `Trait` --> $DIR/const_params_have_right_type.rs:8:14 | -LL | type Foo = u32; - | ^^^^^^^^^^^^ - | -note: the const parameter `N` has type `u64`, but the declaration in trait `Trait::Foo` has type `u8` - --> $DIR/const_params_have_right_type.rs:4:14 - | +LL | trait Trait { + | ----- LL | type Foo; - | ^^^^^^^^^^^ + | ----------- expected const parameter with type `u8` +... +LL | impl Trait for () { + | ----------------- +LL | type Foo = u32; + | ^^^^^^^^^^^^ found const parameter with type `u64` error: aborting due to previous error From 2819b2d8c91ac7d3b366c3c76dca37d2b7b2b5d5 Mon Sep 17 00:00:00 2001 From: Ellen Date: Fri, 6 May 2022 11:37:22 +0100 Subject: [PATCH 4/5] wording tweaks --- .../rustc_typeck/src/check/compare_method.rs | 4 ++-- .../mismatched_ty_const_in_trait_impl.stderr | 22 +++++++++---------- .../ui/const-generics/issues/issue-86820.rs | 2 +- .../const-generics/issues/issue-86820.stderr | 6 ++--- .../const_params_have_right_type.stderr | 6 ++--- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index 590131e4f6ae1..3e15079552ceb 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -1003,7 +1003,7 @@ fn compare_generic_param_kinds<'tcx>( } { let make_param_message = |prefix: &str, param: &ty::GenericParamDef| match param.kind { Const { .. } => { - format!("{} const parameter with type `{}`", prefix, tcx.type_of(param.def_id)) + format!("{} const parameter of type `{}`", prefix, tcx.type_of(param.def_id)) } Type { .. } => format!("{} type parameter", prefix), Lifetime { .. } => unreachable!(), @@ -1016,7 +1016,7 @@ fn compare_generic_param_kinds<'tcx>( tcx.sess, param_impl_span, E0053, - "{} `{}` has an incompatible generic parameter for trait: `{}`", + "{} `{}` has an incompatible generic parameter for trait `{}`", assoc_item_kind_str(&impl_item), trait_item.name, &tcx.def_path_str(tcx.parent(trait_item.def_id)) diff --git a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr index a1ec8adec76a3..3455f2c8ea97b 100644 --- a/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr +++ b/src/test/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr @@ -1,4 +1,4 @@ -error[E0053]: method `foo` has an incompatible generic parameter for trait: `Trait` +error[E0053]: method `foo` has an incompatible generic parameter for trait `Trait` --> $DIR/mismatched_ty_const_in_trait_impl.rs:5:12 | LL | trait Trait { @@ -9,48 +9,48 @@ LL | } LL | impl Trait for () { | ----------------- LL | fn foo() {} - | ^^^^^^^^^^^^ found const parameter with type `u64` + | ^^^^^^^^^^^^ found const parameter of type `u64` -error[E0053]: method `bar` has an incompatible generic parameter for trait: `Other` +error[E0053]: method `bar` has an incompatible generic parameter for trait `Other` --> $DIR/mismatched_ty_const_in_trait_impl.rs:13:12 | LL | trait Other { | ----- LL | fn bar() {} - | ----------- expected const parameter with type `u8` + | ----------- expected const parameter of type `u8` LL | } LL | impl Other for () { | ----------------- LL | fn bar() {} | ^ found type parameter -error[E0053]: method `baz` has an incompatible generic parameter for trait: `Uwu` +error[E0053]: method `baz` has an incompatible generic parameter for trait `Uwu` --> $DIR/mismatched_ty_const_in_trait_impl.rs:21:12 | LL | trait Uwu { | --- LL | fn baz() {} - | ------------ expected const parameter with type `u32` + | ------------ expected const parameter of type `u32` LL | } LL | impl Uwu for () { | --------------- LL | fn baz() {} - | ^^^^^^^^^^^^ found const parameter with type `i32` + | ^^^^^^^^^^^^ found const parameter of type `i32` -error[E0053]: method `bbbb` has an incompatible generic parameter for trait: `Aaaaaa` +error[E0053]: method `bbbb` has an incompatible generic parameter for trait `Aaaaaa` --> $DIR/mismatched_ty_const_in_trait_impl.rs:29:13 | LL | trait Aaaaaa { | ------ LL | fn bbbb() {} - | ------------ expected const parameter with type `u32` + | ------------ expected const parameter of type `u32` LL | } LL | impl Aaaaaa for () { | ------------------ LL | fn bbbb() {} | ^ found type parameter -error[E0053]: method `abcd` has an incompatible generic parameter for trait: `Names` +error[E0053]: method `abcd` has an incompatible generic parameter for trait `Names` --> $DIR/mismatched_ty_const_in_trait_impl.rs:37:13 | LL | trait Names { @@ -61,7 +61,7 @@ LL | } LL | impl Names for () { | ----------------- LL | fn abcd() {} - | ^^^^^^^^^^^^ found const parameter with type `u32` + | ^^^^^^^^^^^^ found const parameter of type `u32` error: aborting due to 5 previous errors diff --git a/src/test/ui/const-generics/issues/issue-86820.rs b/src/test/ui/const-generics/issues/issue-86820.rs index 9bcb8e1aeed39..ae4bd943fd415 100644 --- a/src/test/ui/const-generics/issues/issue-86820.rs +++ b/src/test/ui/const-generics/issues/issue-86820.rs @@ -14,7 +14,7 @@ trait Bits { impl Bits for u8 { fn bit(self) -> bool { - //~^ ERROR: method `bit` has an incompatible generic parameter for trait: `Bits` [E0053] + //~^ ERROR: method `bit` has an incompatible generic parameter for trait `Bits` [E0053] let i = 1 << I; let mask = u8::from(i); mask & self == mask diff --git a/src/test/ui/const-generics/issues/issue-86820.stderr b/src/test/ui/const-generics/issues/issue-86820.stderr index 4d54d654c12d1..3a9cd957f35e7 100644 --- a/src/test/ui/const-generics/issues/issue-86820.stderr +++ b/src/test/ui/const-generics/issues/issue-86820.stderr @@ -1,15 +1,15 @@ -error[E0053]: method `bit` has an incompatible generic parameter for trait: `Bits` +error[E0053]: method `bit` has an incompatible generic parameter for trait `Bits` --> $DIR/issue-86820.rs:16:12 | LL | trait Bits { | ---- LL | fn bit(self) -> bool; - | ----------- expected const parameter with type `u8` + | ----------- expected const parameter of type `u8` ... LL | impl Bits for u8 { | ---------------- LL | fn bit(self) -> bool { - | ^^^^^^^^^^^^^^ found const parameter with type `usize` + | ^^^^^^^^^^^^^^ found const parameter of type `usize` error: aborting due to previous error diff --git a/src/test/ui/generic-associated-types/const_params_have_right_type.stderr b/src/test/ui/generic-associated-types/const_params_have_right_type.stderr index 9b8eddaff41bc..89c993dee5e69 100644 --- a/src/test/ui/generic-associated-types/const_params_have_right_type.stderr +++ b/src/test/ui/generic-associated-types/const_params_have_right_type.stderr @@ -1,15 +1,15 @@ -error[E0053]: type `Foo` has an incompatible generic parameter for trait: `Trait` +error[E0053]: type `Foo` has an incompatible generic parameter for trait `Trait` --> $DIR/const_params_have_right_type.rs:8:14 | LL | trait Trait { | ----- LL | type Foo; - | ----------- expected const parameter with type `u8` + | ----------- expected const parameter of type `u8` ... LL | impl Trait for () { | ----------------- LL | type Foo = u32; - | ^^^^^^^^^^^^ found const parameter with type `u64` + | ^^^^^^^^^^^^ found const parameter of type `u64` error: aborting due to previous error From e4b8ed5aff7d25aa21c4c4d6bba8850c6dcaaaac Mon Sep 17 00:00:00 2001 From: Ellen Date: Fri, 6 May 2022 11:40:18 +0100 Subject: [PATCH 5/5] move closure down --- .../rustc_typeck/src/check/compare_method.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index 3e15079552ceb..a83924d4636c5 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -1001,14 +1001,6 @@ fn compare_generic_param_kinds<'tcx>( (Const { .. }, Const { .. }) | (Type { .. }, Type { .. }) => false, (Lifetime { .. }, _) | (_, Lifetime { .. }) => unreachable!(), } { - let make_param_message = |prefix: &str, param: &ty::GenericParamDef| match param.kind { - Const { .. } => { - format!("{} const parameter of type `{}`", prefix, tcx.type_of(param.def_id)) - } - Type { .. } => format!("{} type parameter", prefix), - Lifetime { .. } => unreachable!(), - }; - let param_impl_span = tcx.def_span(param_impl.def_id); let param_trait_span = tcx.def_span(param_trait.def_id); @@ -1022,6 +1014,14 @@ fn compare_generic_param_kinds<'tcx>( &tcx.def_path_str(tcx.parent(trait_item.def_id)) ); + let make_param_message = |prefix: &str, param: &ty::GenericParamDef| match param.kind { + Const { .. } => { + format!("{} const parameter of type `{}`", prefix, tcx.type_of(param.def_id)) + } + Type { .. } => format!("{} type parameter", prefix), + Lifetime { .. } => unreachable!(), + }; + let trait_header_span = tcx.def_ident_span(tcx.parent(trait_item.def_id)).unwrap(); err.span_label(trait_header_span, ""); err.span_label(param_trait_span, make_param_message("expected", param_trait));