diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check.rs b/compiler/rustc_hir_analysis/src/impl_wf_check.rs index 5cc1ec71757be..0056ceff6f529 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check.rs @@ -11,7 +11,7 @@ use crate::constrained_generic_params as cgp; use min_specialization::check_min_specialization; -use rustc_data_structures::fx::FxHashSet; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_errors::{codes::*, struct_span_code_err}; use rustc_hir::def::DefKind; use rustc_hir::def_id::LocalDefId; @@ -88,14 +88,36 @@ fn enforce_impl_params_are_constrained( impl_trait_ref.error_reported()?; - let mut input_parameters = cgp::parameters_for_impl(tcx, impl_self_ty, impl_trait_ref); + let mut constrained_parameters = cgp::parameters_for_impl(tcx, impl_self_ty, impl_trait_ref); cgp::identify_constrained_generic_params( tcx, impl_predicates, impl_trait_ref, - &mut input_parameters, + &mut constrained_parameters, ); + // Reasons each generic is unconstrained, or Other if not present + let mut unconstrained_reasons = FxHashMap::default(); + for (clause, _) in impl_predicates.predicates { + if let Some(projection) = clause.as_projection_clause() { + let mentioned_params = cgp::parameters_for(tcx, projection.term().skip_binder(), true); + let is_clause_circular = + Some(projection.required_poly_trait_ref(tcx).skip_binder()) == impl_trait_ref; + + for param in mentioned_params { + if is_clause_circular { + // Potentially override BoundToUnconstrainedProjection + unconstrained_reasons.insert(param, UnconstrainedReason::BoundCircularly); + } else { + // Don't override BoundCircularly + unconstrained_reasons + .entry(param) + .or_insert(UnconstrainedReason::BoundToUnconstrainedProjection); + } + } + } + } + // Disallow unconstrained lifetimes, but only if they appear in assoc types. let lifetimes_in_associated_types: FxHashSet<_> = tcx .associated_item_def_ids(impl_def_id) @@ -115,42 +137,56 @@ fn enforce_impl_params_are_constrained( }) .collect(); + let impl_kind = match impl_trait_ref { + Some(_) => ImplKind::ImplTrait, + None => ImplKind::InherentImpl, + }; + let mut res = Ok(()); for param in &impl_generics.own_params { + let cgp_param = cgp::Parameter(param.index); + let unconstrained_reason = + unconstrained_reasons.get(&cgp_param).copied().unwrap_or_default(); + match param.kind { // Disallow ANY unconstrained type parameters. ty::GenericParamDefKind::Type { .. } => { let param_ty = ty::ParamTy::for_def(param); - if !input_parameters.contains(&cgp::Parameter::from(param_ty)) { + if !constrained_parameters.contains(&cgp_param) { res = Err(report_unused_parameter( tcx, tcx.def_span(param.def_id), "type", param_ty.name, + impl_kind, + unconstrained_reason, )); } } ty::GenericParamDefKind::Lifetime => { - let param_lt = cgp::Parameter::from(param.to_early_bound_region_data()); - if lifetimes_in_associated_types.contains(¶m_lt) && // (*) - !input_parameters.contains(¶m_lt) + if lifetimes_in_associated_types.contains(&cgp_param) && // (*) + !constrained_parameters.contains(&cgp_param) { res = Err(report_unused_parameter( tcx, tcx.def_span(param.def_id), "lifetime", param.name, + impl_kind, + unconstrained_reason, )); } } ty::GenericParamDefKind::Const { .. } => { let param_ct = ty::ParamConst::for_def(param); - if !input_parameters.contains(&cgp::Parameter::from(param_ct)) { + if !constrained_parameters.contains(&cgp_param) { res = Err(report_unused_parameter( tcx, tcx.def_span(param.def_id), "const", param_ct.name, + impl_kind, + unconstrained_reason, )); } } @@ -178,11 +214,30 @@ fn enforce_impl_params_are_constrained( // used elsewhere are not projected back out. } +#[derive(Copy, Clone)] +enum ImplKind { + ImplTrait, + InherentImpl, +} + +#[derive(Copy, Clone, Default)] +enum UnconstrainedReason { + /// Not used in the impl trait, self type, nor bound to any projection + #[default] + Other, + /// Bound to a projection, but the LHS was not constrained + BoundToUnconstrainedProjection, + /// Bound to a projection, but the LHS is the trait being implemented + BoundCircularly, +} + fn report_unused_parameter( tcx: TyCtxt<'_>, span: Span, kind: &str, name: Symbol, + impl_kind: ImplKind, + unconstrained_reason: UnconstrainedReason, ) -> ErrorGuaranteed { let mut err = struct_span_code_err!( tcx.dcx(), @@ -194,6 +249,38 @@ fn report_unused_parameter( name ); err.span_label(span, format!("unconstrained {kind} parameter")); + + match impl_kind { + ImplKind::ImplTrait => { + err.note(format!( + "to constrain `{name}`, use it in the implemented trait, in the self type, \ + or in an equality with an associated type" + )); + } + ImplKind::InherentImpl => { + err.note(format!( + "to constrain `{name}`, use it in the self type, \ + or in an equality with an associated type" + )); + } + } + + match unconstrained_reason { + UnconstrainedReason::BoundToUnconstrainedProjection => { + err.note(format!( + "`{name}` is bound to an associated type, \ + but the reference to the associated type itself uses unconstrained generic parameters" + )); + } + UnconstrainedReason::BoundCircularly => { + err.note(format!( + "`{name}` is bound to an associated type, \ + but the associated type is circularly defined in this impl" + )); + } + UnconstrainedReason::Other => {} + } + if kind == "const" { err.note( "expressions using a const parameter must map each value to a distinct output value", @@ -202,5 +289,6 @@ fn report_unused_parameter( "proving the result of expressions other than the parameter are unique is not supported", ); } + err.emit() } diff --git a/tests/rustdoc-ui/not-wf-ambiguous-normalization.stderr b/tests/rustdoc-ui/not-wf-ambiguous-normalization.stderr index 55ab144b92419..677c8f4e05a92 100644 --- a/tests/rustdoc-ui/not-wf-ambiguous-normalization.stderr +++ b/tests/rustdoc-ui/not-wf-ambiguous-normalization.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl Allocator for DefaultAllocator { | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/rustdoc-ui/synthetic-auto-trait-impls/unconstrained-param-in-impl-ambiguity.stderr b/tests/rustdoc-ui/synthetic-auto-trait-impls/unconstrained-param-in-impl-ambiguity.stderr index 38d1a537fe458..91e34fbf1bc49 100644 --- a/tests/rustdoc-ui/synthetic-auto-trait-impls/unconstrained-param-in-impl-ambiguity.stderr +++ b/tests/rustdoc-ui/synthetic-auto-trait-impls/unconstrained-param-in-impl-ambiguity.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `Q` is not constrained by the impl trait, self | LL | unsafe impl Send for Inner {} | ^ unconstrained type parameter + | + = note: to constrain `Q`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/ui/associated-types/issue-26262.stderr b/tests/ui/associated-types/issue-26262.stderr index 90e2d0d930164..639f0f6667117 100644 --- a/tests/ui/associated-types/issue-26262.stderr +++ b/tests/ui/associated-types/issue-26262.stderr @@ -3,12 +3,16 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl S { | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the self type, or in an equality with an associated type error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates --> $DIR/issue-26262.rs:17:6 | LL | impl<'a,T: Trait2<'a>> Trait1<>::Foo> for T { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 2 previous errors diff --git a/tests/ui/async-await/in-trait/unconstrained-impl-region.stderr b/tests/ui/async-await/in-trait/unconstrained-impl-region.stderr index 66819d1fcf7db..f6ba561a3792c 100644 --- a/tests/ui/async-await/in-trait/unconstrained-impl-region.stderr +++ b/tests/ui/async-await/in-trait/unconstrained-impl-region.stderr @@ -19,6 +19,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a> Actor for () { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 2 previous errors diff --git a/tests/ui/async-await/issues/issue-78654.full.stderr b/tests/ui/async-await/issues/issue-78654.full.stderr index 0d12a948c68bd..a7c3f8e79ad89 100644 --- a/tests/ui/async-await/issues/issue-78654.full.stderr +++ b/tests/ui/async-await/issues/issue-78654.full.stderr @@ -10,6 +10,7 @@ error[E0207]: the const parameter `H` is not constrained by the impl trait, self LL | impl Foo { | ^^^^^^^^^^^^^^^^ unconstrained const parameter | + = note: to constrain `H`, use it in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/async-await/issues/issue-78654.min.stderr b/tests/ui/async-await/issues/issue-78654.min.stderr index 0d12a948c68bd..a7c3f8e79ad89 100644 --- a/tests/ui/async-await/issues/issue-78654.min.stderr +++ b/tests/ui/async-await/issues/issue-78654.min.stderr @@ -10,6 +10,7 @@ error[E0207]: the const parameter `H` is not constrained by the impl trait, self LL | impl Foo { | ^^^^^^^^^^^^^^^^ unconstrained const parameter | + = note: to constrain `H`, use it in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/const-generics/ice-unexpected-inference-var-122549.stderr b/tests/ui/const-generics/ice-unexpected-inference-var-122549.stderr index afad3388145c7..8366f956546d3 100644 --- a/tests/ui/const-generics/ice-unexpected-inference-var-122549.stderr +++ b/tests/ui/const-generics/ice-unexpected-inference-var-122549.stderr @@ -52,6 +52,7 @@ error[E0207]: the const parameter `N` is not constrained by the impl trait, self LL | impl<'a, T, const N: usize> Iterator for ConstChunksExact<'a, T, {}> { | ^^^^^^^^^^^^^^ unconstrained const parameter | + = note: to constrain `N`, use it in the implemented trait, in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/const-generics/issues/issue-68366.full.stderr b/tests/ui/const-generics/issues/issue-68366.full.stderr index 3363a895e4725..c65f0361bea08 100644 --- a/tests/ui/const-generics/issues/issue-68366.full.stderr +++ b/tests/ui/const-generics/issues/issue-68366.full.stderr @@ -16,6 +16,7 @@ error[E0207]: the const parameter `N` is not constrained by the impl trait, self LL | impl Collatz<{Some(N)}> {} | ^^^^^^^^^^^^^^ unconstrained const parameter | + = note: to constrain `N`, use it in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported @@ -25,6 +26,7 @@ error[E0207]: the const parameter `N` is not constrained by the impl trait, self LL | impl Foo {} | ^^^^^^^^^^^^^^ unconstrained const parameter | + = note: to constrain `N`, use it in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/const-generics/issues/issue-68366.min.stderr b/tests/ui/const-generics/issues/issue-68366.min.stderr index 276f91e76dd42..4a3f5106a2bad 100644 --- a/tests/ui/const-generics/issues/issue-68366.min.stderr +++ b/tests/ui/const-generics/issues/issue-68366.min.stderr @@ -25,6 +25,7 @@ error[E0207]: the const parameter `N` is not constrained by the impl trait, self LL | impl Collatz<{Some(N)}> {} | ^^^^^^^^^^^^^^ unconstrained const parameter | + = note: to constrain `N`, use it in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported @@ -34,6 +35,7 @@ error[E0207]: the const parameter `N` is not constrained by the impl trait, self LL | impl Foo {} | ^^^^^^^^^^^^^^ unconstrained const parameter | + = note: to constrain `N`, use it in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/consts/rustc-impl-const-stability.stderr b/tests/ui/consts/rustc-impl-const-stability.stderr index ba8e6c1555ce2..ce5e1f977a7bb 100644 --- a/tests/ui/consts/rustc-impl-const-stability.stderr +++ b/tests/ui/consts/rustc-impl-const-stability.stderr @@ -13,6 +13,7 @@ error[E0207]: the const parameter `host` is not constrained by the impl trait, s LL | impl const Default for Data { | ^^^^^ unconstrained const parameter | + = note: to constrain `host`, use it in the implemented trait, in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/error-codes/E0207.stderr b/tests/ui/error-codes/E0207.stderr index 01d7c41854412..493b7695e7672 100644 --- a/tests/ui/error-codes/E0207.stderr +++ b/tests/ui/error-codes/E0207.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl Foo { | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/ui/generic-associated-types/bugs/issue-87735.stderr b/tests/ui/generic-associated-types/bugs/issue-87735.stderr index d80050652389d..4619109793c60 100644 --- a/tests/ui/generic-associated-types/bugs/issue-87735.stderr +++ b/tests/ui/generic-associated-types/bugs/issue-87735.stderr @@ -3,6 +3,9 @@ error[E0207]: the type parameter `U` is not constrained by the impl trait, self | LL | impl<'b, T, U> AsRef2 for Foo | ^ unconstrained type parameter + | + = note: to constrain `U`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: `U` is bound to an associated type, but the reference to the associated type itself uses unconstrained generic parameters error[E0309]: the parameter type `U` may not live long enough --> $DIR/issue-87735.rs:34:21 diff --git a/tests/ui/generic-associated-types/bugs/issue-88526.stderr b/tests/ui/generic-associated-types/bugs/issue-88526.stderr index 5da3e3ff64ab9..01ed5928243b6 100644 --- a/tests/ui/generic-associated-types/bugs/issue-88526.stderr +++ b/tests/ui/generic-associated-types/bugs/issue-88526.stderr @@ -3,6 +3,9 @@ error[E0207]: the type parameter `I` is not constrained by the impl trait, self | LL | impl<'q, Q, I, F> A for TestB | ^ unconstrained type parameter + | + = note: to constrain `I`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: `I` is bound to an associated type, but the reference to the associated type itself uses unconstrained generic parameters error[E0309]: the parameter type `F` may not live long enough --> $DIR/issue-88526.rs:16:18 diff --git a/tests/ui/generic-associated-types/gat-trait-path-generic-type-arg.stderr b/tests/ui/generic-associated-types/gat-trait-path-generic-type-arg.stderr index cb2b9f32bfe72..f274fc34eac42 100644 --- a/tests/ui/generic-associated-types/gat-trait-path-generic-type-arg.stderr +++ b/tests/ui/generic-associated-types/gat-trait-path-generic-type-arg.stderr @@ -18,6 +18,8 @@ error[E0207]: the type parameter `T1` is not constrained by the impl trait, self | LL | impl Foo for T { | ^^ unconstrained type parameter + | + = note: to constrain `T1`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 3 previous errors diff --git a/tests/ui/impl-trait/in-trait/unconstrained-lt.stderr b/tests/ui/impl-trait/in-trait/unconstrained-lt.stderr index 4c5a42c0b4b47..24d00663a2090 100644 --- a/tests/ui/impl-trait/in-trait/unconstrained-lt.stderr +++ b/tests/ui/impl-trait/in-trait/unconstrained-lt.stderr @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a, T> Foo for T { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/issues/issue-87340.stderr b/tests/ui/impl-trait/issues/issue-87340.stderr index 1be4087be4242..aad8cd8509a0b 100644 --- a/tests/ui/impl-trait/issues/issue-87340.stderr +++ b/tests/ui/impl-trait/issues/issue-87340.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl X for () { | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0282]: type annotations needed --> $DIR/issue-87340.rs:11:23 diff --git a/tests/ui/impl-unused-rps-in-assoc-type.stderr b/tests/ui/impl-unused-rps-in-assoc-type.stderr index ef61fa4be4830..beb077dbdec4f 100644 --- a/tests/ui/impl-unused-rps-in-assoc-type.stderr +++ b/tests/ui/impl-unused-rps-in-assoc-type.stderr @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a> Fun for Holder { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/ui/impl-unused-tps-inherent.stderr b/tests/ui/impl-unused-tps-inherent.stderr index 43f63cf968cf5..d24d4ca1a054e 100644 --- a/tests/ui/impl-unused-tps-inherent.stderr +++ b/tests/ui/impl-unused-tps-inherent.stderr @@ -3,12 +3,16 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl MyType { | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the self type, or in an equality with an associated type error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates --> $DIR/impl-unused-tps-inherent.rs:17:8 | LL | impl MyType1 { | ^ unconstrained type parameter + | + = note: to constrain `U`, use it in the self type, or in an equality with an associated type error: aborting due to 2 previous errors diff --git a/tests/ui/impl-unused-tps.stderr b/tests/ui/impl-unused-tps.stderr index af427cb5f3e3c..0c9140c6caa4e 100644 --- a/tests/ui/impl-unused-tps.stderr +++ b/tests/ui/impl-unused-tps.stderr @@ -25,30 +25,42 @@ error[E0207]: the type parameter `U` is not constrained by the impl trait, self | LL | impl Foo for [isize;1] { | ^ unconstrained type parameter + | + = note: to constrain `U`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates --> $DIR/impl-unused-tps.rs:31:8 | LL | impl Bar for T { | ^ unconstrained type parameter + | + = note: to constrain `U`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates --> $DIR/impl-unused-tps.rs:39:8 | LL | impl Bar for T | ^ unconstrained type parameter + | + = note: to constrain `U`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: `U` is bound to an associated type, but the associated type is circularly defined in this impl error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates --> $DIR/impl-unused-tps.rs:47:8 | LL | impl Foo for T | ^ unconstrained type parameter + | + = note: to constrain `U`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0207]: the type parameter `V` is not constrained by the impl trait, self type, or predicates --> $DIR/impl-unused-tps.rs:47:10 | LL | impl Foo for T | ^ unconstrained type parameter + | + = note: to constrain `V`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: `V` is bound to an associated type, but the reference to the associated type itself uses unconstrained generic parameters error: aborting due to 7 previous errors diff --git a/tests/ui/issues/issue-16562.stderr b/tests/ui/issues/issue-16562.stderr index efbd7f712a45a..07c1f58e6be2f 100644 --- a/tests/ui/issues/issue-16562.stderr +++ b/tests/ui/issues/issue-16562.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl Collection for Col { | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/ui/issues/issue-22886.stderr b/tests/ui/issues/issue-22886.stderr index a04fa677f9ec5..de6f0db1c7c83 100644 --- a/tests/ui/issues/issue-22886.stderr +++ b/tests/ui/issues/issue-22886.stderr @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a> Iterator for Newtype { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/ui/issues/issue-29861.stderr b/tests/ui/issues/issue-29861.stderr index a25cbf0515d84..1011caedf752f 100644 --- a/tests/ui/issues/issue-29861.stderr +++ b/tests/ui/issues/issue-29861.stderr @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a, T: 'a> MakeRef2 for T { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0716]: temporary value dropped while borrowed --> $DIR/issue-29861.rs:16:43 diff --git a/tests/ui/issues/issue-35139.stderr b/tests/ui/issues/issue-35139.stderr index 875af70483224..54e2fb2ff28b5 100644 --- a/tests/ui/issues/issue-35139.stderr +++ b/tests/ui/issues/issue-35139.stderr @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a> MethodType for MTFn { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/ui/lazy-type-alias/unconstrained-params-in-impl-due-to-overflow.stderr b/tests/ui/lazy-type-alias/unconstrained-params-in-impl-due-to-overflow.stderr index bcffa02ddd424..45ec6d29096c4 100644 --- a/tests/ui/lazy-type-alias/unconstrained-params-in-impl-due-to-overflow.stderr +++ b/tests/ui/lazy-type-alias/unconstrained-params-in-impl-due-to-overflow.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl Loop {} | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the self type, or in an equality with an associated type error[E0275]: overflow normalizing the type alias `Loop` --> $DIR/unconstrained-params-in-impl-due-to-overflow.rs:6:16 diff --git a/tests/ui/lazy-type-alias/unconstrained-params-in-impl.stderr b/tests/ui/lazy-type-alias/unconstrained-params-in-impl.stderr index 2419c78cba8d9..d67d950224d9e 100644 --- a/tests/ui/lazy-type-alias/unconstrained-params-in-impl.stderr +++ b/tests/ui/lazy-type-alias/unconstrained-params-in-impl.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl NotInjective {} | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/const-impl-requires-const-trait.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/const-impl-requires-const-trait.stderr index d20404e63b3fb..234c0ce26a8b1 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/const-impl-requires-const-trait.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/const-impl-requires-const-trait.stderr @@ -16,6 +16,7 @@ error[E0207]: the const parameter `host` is not constrained by the impl trait, s LL | impl const A for () {} | ^^^^^ unconstrained const parameter | + = note: to constrain `host`, use it in the implemented trait, in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/const_derives/derive-const-non-const-type.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/const_derives/derive-const-non-const-type.stderr index 4b009446dbc00..61124f4047987 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/const_derives/derive-const-non-const-type.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/const_derives/derive-const-non-const-type.stderr @@ -10,6 +10,7 @@ LL | #[derive_const(Default)] error[E0207]: the const parameter `host` is not constrained by the impl trait, self type, or predicates | + = note: to constrain `host`, use it in the implemented trait, in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/const_derives/derive-const-use.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/const_derives/derive-const-use.stderr index e45c1a1f46fb6..0780760079cae 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/const_derives/derive-const-use.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/const_derives/derive-const-use.stderr @@ -19,6 +19,7 @@ error[E0207]: the const parameter `host` is not constrained by the impl trait, s LL | impl const Default for A { | ^^^^^ unconstrained const parameter | + = note: to constrain `host`, use it in the implemented trait, in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported @@ -34,6 +35,7 @@ LL | #[derive_const(Default, PartialEq)] error[E0207]: the const parameter `host` is not constrained by the impl trait, self type, or predicates | + = note: to constrain `host`, use it in the implemented trait, in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/span-bug-issue-121418.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/span-bug-issue-121418.stderr index 3ff1efb59883d..1dab189391736 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/span-bug-issue-121418.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/span-bug-issue-121418.stderr @@ -14,6 +14,7 @@ error[E0207]: the const parameter `host` is not constrained by the impl trait, s LL | impl const dyn T { | ^^^^^ unconstrained const parameter | + = note: to constrain `host`, use it in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/spec-effectvar-ice.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/spec-effectvar-ice.stderr index d18063f8d3d88..5408153e9c6ea 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/spec-effectvar-ice.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/spec-effectvar-ice.stderr @@ -34,6 +34,7 @@ error[E0207]: the const parameter `host` is not constrained by the impl trait, s LL | impl const Foo for T {} | ^^^^^ unconstrained const parameter | + = note: to constrain `host`, use it in the implemented trait, in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported @@ -43,6 +44,7 @@ error[E0207]: the const parameter `host` is not constrained by the impl trait, s LL | impl const Foo for T where T: const Specialize {} | ^^^^^ unconstrained const parameter | + = note: to constrain `host`, use it in the implemented trait, in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-default-body-stability.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-default-body-stability.stderr index 62c4bc3b7ae60..acd5eb066f286 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-default-body-stability.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-default-body-stability.stderr @@ -22,6 +22,7 @@ error[E0207]: the const parameter `host` is not constrained by the impl trait, s LL | impl const Try for T { | ^^^^^ unconstrained const parameter | + = note: to constrain `host`, use it in the implemented trait, in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported @@ -31,6 +32,7 @@ error[E0207]: the const parameter `host` is not constrained by the impl trait, s LL | impl const FromResidual for T { | ^^^^^ unconstrained const parameter | + = note: to constrain `host`, use it in the implemented trait, in the self type, or in an equality with an associated type = note: expressions using a const parameter must map each value to a distinct output value = note: proving the result of expressions other than the parameter are unique is not supported diff --git a/tests/ui/specialization/min_specialization/ice-const-not-fully-resolved-113045.stderr b/tests/ui/specialization/min_specialization/ice-const-not-fully-resolved-113045.stderr index acbdb9b0a308e..1c451178b3305 100644 --- a/tests/ui/specialization/min_specialization/ice-const-not-fully-resolved-113045.stderr +++ b/tests/ui/specialization/min_specialization/ice-const-not-fully-resolved-113045.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `Unconstrained` is not constrained by the impl | LL | impl<'a, Unconstrained> X for [(); 0] {} | ^^^^^^^^^^^^^ unconstrained type parameter + | + = note: to constrain `Unconstrained`, use it in the implemented trait, in the self type, or in an equality with an associated type error: specialization impl does not specialize any associated items --> $DIR/ice-const-not-fully-resolved-113045.rs:11:1 diff --git a/tests/ui/traits/associated_type_bound/116464-invalid-assoc-type-suggestion-in-trait-impl.stderr b/tests/ui/traits/associated_type_bound/116464-invalid-assoc-type-suggestion-in-trait-impl.stderr index 06e2fa5d4d1fd..05ab8fbb6b348 100644 --- a/tests/ui/traits/associated_type_bound/116464-invalid-assoc-type-suggestion-in-trait-impl.stderr +++ b/tests/ui/traits/associated_type_bound/116464-invalid-assoc-type-suggestion-in-trait-impl.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `S` is not constrained by the impl trait, self | LL | impl Trait for i32 { | ^ unconstrained type parameter + | + = note: to constrain `S`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0107]: trait takes 1 generic argument but 2 generic arguments were supplied --> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:15:12 diff --git a/tests/ui/type-alias-impl-trait/assoc-type-lifetime-unconstrained.stderr b/tests/ui/type-alias-impl-trait/assoc-type-lifetime-unconstrained.stderr index e6b94c525ff23..a7b75a5ac9452 100644 --- a/tests/ui/type-alias-impl-trait/assoc-type-lifetime-unconstrained.stderr +++ b/tests/ui/type-alias-impl-trait/assoc-type-lifetime-unconstrained.stderr @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a, I> UnwrapItemsExt for I { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0792]: expected generic lifetime parameter, found `'_` --> $DIR/assoc-type-lifetime-unconstrained.rs:22:9 diff --git a/tests/ui/type-alias-impl-trait/ice-failed-to-resolve-instance-for-110696.stderr b/tests/ui/type-alias-impl-trait/ice-failed-to-resolve-instance-for-110696.stderr index a39f77ce17f61..de8dbcd0e5000 100644 --- a/tests/ui/type-alias-impl-trait/ice-failed-to-resolve-instance-for-110696.stderr +++ b/tests/ui/type-alias-impl-trait/ice-failed-to-resolve-instance-for-110696.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl>>, U> MyIndex> for Scope { | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/ui/type-alias-impl-trait/impl-with-unconstrained-param.stderr b/tests/ui/type-alias-impl-trait/impl-with-unconstrained-param.stderr index bb0e11d314c33..ac4bda0061b89 100644 --- a/tests/ui/type-alias-impl-trait/impl-with-unconstrained-param.stderr +++ b/tests/ui/type-alias-impl-trait/impl-with-unconstrained-param.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl X for () { | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0282]: type annotations needed --> $DIR/impl-with-unconstrained-param.rs:14:23 diff --git a/tests/ui/type-alias-impl-trait/issue-74244.stderr b/tests/ui/type-alias-impl-trait/issue-74244.stderr index d2b50ffd86b59..def3b5bf77480 100644 --- a/tests/ui/type-alias-impl-trait/issue-74244.stderr +++ b/tests/ui/type-alias-impl-trait/issue-74244.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl Allocator for DefaultAllocator { | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0282]: type annotations needed --> $DIR/issue-74244.rs:16:13 diff --git a/tests/ui/type-alias-impl-trait/issue-74761-2.stderr b/tests/ui/type-alias-impl-trait/issue-74761-2.stderr index 26babc29000c0..ef85b17751651 100644 --- a/tests/ui/type-alias-impl-trait/issue-74761-2.stderr +++ b/tests/ui/type-alias-impl-trait/issue-74761-2.stderr @@ -3,12 +3,16 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a, 'b> A for () { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0207]: the lifetime parameter `'b` is not constrained by the impl trait, self type, or predicates --> $DIR/issue-74761-2.rs:7:10 | LL | impl<'a, 'b> A for () { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'b`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0792]: expected generic lifetime parameter, found `'_` --> $DIR/issue-74761-2.rs:12:28 diff --git a/tests/ui/type-alias-impl-trait/issue-74761.stderr b/tests/ui/type-alias-impl-trait/issue-74761.stderr index a4826c293467e..a29443ed325d9 100644 --- a/tests/ui/type-alias-impl-trait/issue-74761.stderr +++ b/tests/ui/type-alias-impl-trait/issue-74761.stderr @@ -3,12 +3,16 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a, 'b> A for () { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0207]: the lifetime parameter `'b` is not constrained by the impl trait, self type, or predicates --> $DIR/issue-74761.rs:7:10 | LL | impl<'a, 'b> A for () { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'b`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0792]: expected generic lifetime parameter, found `'_` --> $DIR/issue-74761.rs:12:28 diff --git a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-unconstrained-lifetime.stderr b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-unconstrained-lifetime.stderr index 5f9c56f1ca9d0..d4c1c4ed91da1 100644 --- a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-unconstrained-lifetime.stderr +++ b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-unconstrained-lifetime.stderr @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a, I: Iterator> Trait for (i32, I) { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0792]: expected generic lifetime parameter, found `'_` --> $DIR/type-alias-impl-trait-unconstrained-lifetime.rs:14:9 diff --git a/tests/ui/type-alias-impl-trait/unconstrained-impl-param.stderr b/tests/ui/type-alias-impl-trait/unconstrained-impl-param.stderr index 6206f169c5b9e..c191fe97aa693 100644 --- a/tests/ui/type-alias-impl-trait/unconstrained-impl-param.stderr +++ b/tests/ui/type-alias-impl-trait/unconstrained-impl-param.stderr @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, | LL | impl<'a> Trait for Opaque<&'a str> { | ^^ unconstrained lifetime parameter + | + = note: to constrain `'a`, use it in the implemented trait, in the self type, or in an equality with an associated type error: aborting due to 1 previous error diff --git a/tests/ui/typeck/issue-13853-5.stderr b/tests/ui/typeck/issue-13853-5.stderr index 388d5ec746ce6..cab3ba9fe3741 100644 --- a/tests/ui/typeck/issue-13853-5.stderr +++ b/tests/ui/typeck/issue-13853-5.stderr @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self | LL | impl<'a, T: Deserializable> Deserializable for &'a str { | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type error[E0308]: mismatched types --> $DIR/issue-13853-5.rs:9:70 diff --git a/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-const-generic.rs b/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-const-generic.rs new file mode 100644 index 0000000000000..a8f2d02637703 --- /dev/null +++ b/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-const-generic.rs @@ -0,0 +1,17 @@ +trait Foo1 {} + +trait Foo2 { + type Bar; +} + +impl Foo1 for u32 where [u32; N]: Clone {} +//~^ ERROR: the const parameter `N` is not constrained + +impl Foo1 for String where T: Iterator {} +//~^ ERROR: the type parameter `T` is not constrained +//~| ERROR: the const parameter `N` is not constrained + +impl Foo2 for T where T: Foo2 { type Bar = [u32; N]; } +//~^ ERROR: the const parameter `N` is not constrained + +fn main() {} diff --git a/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-const-generic.stderr b/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-const-generic.stderr new file mode 100644 index 0000000000000..a3545ba84e36e --- /dev/null +++ b/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-const-generic.stderr @@ -0,0 +1,43 @@ +error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates + --> $DIR/mentioned-but-unconstrained-const-generic.rs:7:6 + | +LL | impl Foo1 for u32 where [u32; N]: Clone {} + | ^^^^^^^^^^^^^^ unconstrained const parameter + | + = note: to constrain `N`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: expressions using a const parameter must map each value to a distinct output value + = note: proving the result of expressions other than the parameter are unique is not supported + +error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates + --> $DIR/mentioned-but-unconstrained-const-generic.rs:10:6 + | +LL | impl Foo1 for String where T: Iterator {} + | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type + +error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates + --> $DIR/mentioned-but-unconstrained-const-generic.rs:10:9 + | +LL | impl Foo1 for String where T: Iterator {} + | ^^^^^^^^^^^^^^ unconstrained const parameter + | + = note: to constrain `N`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: `N` is bound to an associated type, but the reference to the associated type itself uses unconstrained generic parameters + = note: expressions using a const parameter must map each value to a distinct output value + = note: proving the result of expressions other than the parameter are unique is not supported + +error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates + --> $DIR/mentioned-but-unconstrained-const-generic.rs:14:9 + | +LL | impl Foo2 for T where T: Foo2 { type Bar = [u32; N]; } + | ^^^^^^^^^^^^^^ unconstrained const parameter + | + = note: to constrain `N`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: `N` is bound to an associated type, but the associated type is circularly defined in this impl + = note: expressions using a const parameter must map each value to a distinct output value + = note: proving the result of expressions other than the parameter are unique is not supported + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0207`. diff --git a/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-type.rs b/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-type.rs new file mode 100644 index 0000000000000..3946a83f174b9 --- /dev/null +++ b/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-type.rs @@ -0,0 +1,24 @@ +trait Foo1 {} + +trait Foo2 { + type Bar; +} + +trait Foo3 { + type Bar; +} + +impl Foo1 for u32 where T: Clone {} +//~^ ERROR: the type parameter `T` is not constrained + +impl Foo1 for String where T: Iterator {} +//~^ ERROR: the type parameter `T` is not constrained +//~| ERROR: the type parameter `U` is not constrained + +impl Foo2 for T where T: Foo2 { type Bar = U; } +//~^ ERROR: the type parameter `U` is not constrained + +impl Foo3 for T where T: Foo3> { type Bar = Option; } +//~^ ERROR: the type parameter `U` is not constrained + +fn main() {} diff --git a/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-type.stderr b/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-type.stderr new file mode 100644 index 0000000000000..1467e479853b2 --- /dev/null +++ b/tests/ui/unconstrained-generic-parameters/mentioned-but-unconstrained-type.stderr @@ -0,0 +1,46 @@ +error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates + --> $DIR/mentioned-but-unconstrained-type.rs:11:6 + | +LL | impl Foo1 for u32 where T: Clone {} + | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type + +error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates + --> $DIR/mentioned-but-unconstrained-type.rs:14:6 + | +LL | impl Foo1 for String where T: Iterator {} + | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type + +error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates + --> $DIR/mentioned-but-unconstrained-type.rs:14:9 + | +LL | impl Foo1 for String where T: Iterator {} + | ^ unconstrained type parameter + | + = note: to constrain `U`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: `U` is bound to an associated type, but the reference to the associated type itself uses unconstrained generic parameters + +error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates + --> $DIR/mentioned-but-unconstrained-type.rs:18:9 + | +LL | impl Foo2 for T where T: Foo2 { type Bar = U; } + | ^ unconstrained type parameter + | + = note: to constrain `U`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: `U` is bound to an associated type, but the associated type is circularly defined in this impl + +error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates + --> $DIR/mentioned-but-unconstrained-type.rs:21:9 + | +LL | impl Foo3 for T where T: Foo3> { type Bar = Option; } + | ^ unconstrained type parameter + | + = note: to constrain `U`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: `U` is bound to an associated type, but the associated type is circularly defined in this impl + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0207`. diff --git a/tests/ui/unconstrained-generic-parameters/unused-const-generic.rs b/tests/ui/unconstrained-generic-parameters/unused-const-generic.rs new file mode 100644 index 0000000000000..e6acb79999d1a --- /dev/null +++ b/tests/ui/unconstrained-generic-parameters/unused-const-generic.rs @@ -0,0 +1,14 @@ +struct Foo; + +impl Foo {} +//~^ ERROR: the const parameter `N` is not constrained + +impl Default for Foo { + //~^ ERROR: the const parameter `N` is not constrained + fn default() -> Self { Foo } +} + +// This one isn't an error +fn foo() {} + +fn main() {} diff --git a/tests/ui/unconstrained-generic-parameters/unused-const-generic.stderr b/tests/ui/unconstrained-generic-parameters/unused-const-generic.stderr new file mode 100644 index 0000000000000..95dd7b7d566f9 --- /dev/null +++ b/tests/ui/unconstrained-generic-parameters/unused-const-generic.stderr @@ -0,0 +1,23 @@ +error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates + --> $DIR/unused-const-generic.rs:3:6 + | +LL | impl Foo {} + | ^^^^^^^^^^^^^^ unconstrained const parameter + | + = note: to constrain `N`, use it in the self type, or in an equality with an associated type + = note: expressions using a const parameter must map each value to a distinct output value + = note: proving the result of expressions other than the parameter are unique is not supported + +error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates + --> $DIR/unused-const-generic.rs:6:6 + | +LL | impl Default for Foo { + | ^^^^^^^^^^^^^^ unconstrained const parameter + | + = note: to constrain `N`, use it in the implemented trait, in the self type, or in an equality with an associated type + = note: expressions using a const parameter must map each value to a distinct output value + = note: proving the result of expressions other than the parameter are unique is not supported + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0207`. diff --git a/tests/ui/unconstrained-generic-parameters/unused-type-generic.rs b/tests/ui/unconstrained-generic-parameters/unused-type-generic.rs new file mode 100644 index 0000000000000..ba64d4e5b6ccf --- /dev/null +++ b/tests/ui/unconstrained-generic-parameters/unused-type-generic.rs @@ -0,0 +1,14 @@ +struct Foo; + +impl Foo {} +//~^ ERROR: the type parameter `T` is not constrained + +impl Default for Foo { + //~^ ERROR: the type parameter `T` is not constrained + fn default() -> Self { Foo } +} + +// This one isn't an error +fn foo() {} + +fn main() {} diff --git a/tests/ui/unconstrained-generic-parameters/unused-type-generic.stderr b/tests/ui/unconstrained-generic-parameters/unused-type-generic.stderr new file mode 100644 index 0000000000000..9491e923e2a78 --- /dev/null +++ b/tests/ui/unconstrained-generic-parameters/unused-type-generic.stderr @@ -0,0 +1,19 @@ +error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates + --> $DIR/unused-type-generic.rs:3:6 + | +LL | impl Foo {} + | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the self type, or in an equality with an associated type + +error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates + --> $DIR/unused-type-generic.rs:6:6 + | +LL | impl Default for Foo { + | ^ unconstrained type parameter + | + = note: to constrain `T`, use it in the implemented trait, in the self type, or in an equality with an associated type + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0207`.