From 3e9a8e577a041a334079d3206ec72a5d31a59c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Thu, 8 Feb 2024 14:08:58 +0100 Subject: [PATCH] Properly check constrainedness of gen params in the presence of weak alias types --- .../src/collect/predicates_of.rs | 2 +- .../src/constrained_generic_params.rs | 39 ++++++++++++++----- .../rustc_hir_analysis/src/impl_wf_check.rs | 4 +- .../src/impl_wf_check/min_specialization.rs | 8 ++-- .../ui/lazy-type-alias/constrained-params.rs | 27 +++++++++++++ .../unconstrained-param-due-to-overflow.rs | 8 ++++ ...unconstrained-param-due-to-overflow.stderr | 9 +++++ .../lazy-type-alias/unconstrained-params.rs | 12 ++++++ .../unconstrained-params.stderr | 9 +++++ 9 files changed, 101 insertions(+), 17 deletions(-) create mode 100644 tests/ui/lazy-type-alias/constrained-params.rs create mode 100644 tests/ui/lazy-type-alias/unconstrained-param-due-to-overflow.rs create mode 100644 tests/ui/lazy-type-alias/unconstrained-param-due-to-overflow.stderr create mode 100644 tests/ui/lazy-type-alias/unconstrained-params.rs create mode 100644 tests/ui/lazy-type-alias/unconstrained-params.stderr diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 05755f98f202d..351ac2eb7702e 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -307,7 +307,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen tcx, &mut predicates, trait_ref, - &mut cgp::parameters_for_impl(self_ty, trait_ref), + &mut cgp::parameters_for_impl(tcx, self_ty, trait_ref), ); } diff --git a/compiler/rustc_hir_analysis/src/constrained_generic_params.rs b/compiler/rustc_hir_analysis/src/constrained_generic_params.rs index 05efad3ccb313..98b7073bc331e 100644 --- a/compiler/rustc_hir_analysis/src/constrained_generic_params.rs +++ b/compiler/rustc_hir_analysis/src/constrained_generic_params.rs @@ -1,4 +1,5 @@ use rustc_data_structures::fx::FxHashSet; +use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor}; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::Span; @@ -27,12 +28,13 @@ impl From for Parameter { /// Returns the set of parameters constrained by the impl header. pub fn parameters_for_impl<'tcx>( + tcx: TyCtxt<'tcx>, impl_self_ty: Ty<'tcx>, impl_trait_ref: Option>, ) -> FxHashSet { let vec = match impl_trait_ref { - Some(tr) => parameters_for(&tr, false), - None => parameters_for(&impl_self_ty, false), + Some(tr) => parameters_for(tcx, &tr, false), + None => parameters_for(tcx, &impl_self_ty, false), }; vec.into_iter().collect() } @@ -43,26 +45,43 @@ pub fn parameters_for_impl<'tcx>( /// of parameters whose values are needed in order to constrain `ty` - these /// differ, with the latter being a superset, in the presence of projections. pub fn parameters_for<'tcx>( + tcx: TyCtxt<'tcx>, t: &impl TypeVisitable>, include_nonconstraining: bool, ) -> Vec { - let mut collector = ParameterCollector { parameters: vec![], include_nonconstraining }; + let mut collector = + ParameterCollector { tcx, parameters: vec![], include_nonconstraining, depth: 0 }; t.visit_with(&mut collector); collector.parameters } -struct ParameterCollector { +struct ParameterCollector<'tcx> { + tcx: TyCtxt<'tcx>, parameters: Vec, include_nonconstraining: bool, + depth: usize, } -impl<'tcx> TypeVisitor> for ParameterCollector { +impl<'tcx> TypeVisitor> for ParameterCollector<'tcx> { fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow { match *t.kind() { - ty::Alias(..) if !self.include_nonconstraining => { - // projections are not injective + ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, _) + if !self.include_nonconstraining => + { + // Projections are not injective in general. return ControlFlow::Continue(()); } + ty::Alias(ty::Weak, alias) if !self.include_nonconstraining => { + if !self.tcx.recursion_limit().value_within_limit(self.depth) { + // Other constituent types may still constrain some generic params, consider + // ` (Overflow, T)` for example. Therefore we want to continue instead of + // breaking. Only affects diagnostics. + return ControlFlow::Continue(()); + } + self.depth += 1; + let ty = self.tcx.type_of(alias.def_id).instantiate(self.tcx, alias.args); + return ensure_sufficient_stack(|| ty.visit_with(self)); + } ty::Param(data) => { self.parameters.push(Parameter::from(data)); } @@ -82,7 +101,7 @@ impl<'tcx> TypeVisitor> for ParameterCollector { fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow { match c.kind() { ty::ConstKind::Unevaluated(..) if !self.include_nonconstraining => { - // Constant expressions are not injective + // Constant expressions are not injective in general. return c.ty().visit_with(self); } ty::ConstKind::Param(data) => { @@ -201,12 +220,12 @@ pub fn setup_constraining_predicates<'tcx>( // `<::Baz as Iterator>::Output = ::Output` // Then the projection only applies if `T` is known, but it still // does not determine `U`. - let inputs = parameters_for(&projection.projection_ty, true); + let inputs = parameters_for(tcx, &projection.projection_ty, true); let relies_only_on_inputs = inputs.iter().all(|p| input_parameters.contains(p)); if !relies_only_on_inputs { continue; } - input_parameters.extend(parameters_for(&projection.term, false)); + input_parameters.extend(parameters_for(tcx, &projection.term, false)); } else { continue; } diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check.rs b/compiler/rustc_hir_analysis/src/impl_wf_check.rs index c072891e2952e..b4cec1d9882b6 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check.rs @@ -94,7 +94,7 @@ fn enforce_impl_params_are_constrained( let impl_predicates = tcx.predicates_of(impl_def_id); let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).map(ty::EarlyBinder::instantiate_identity); - let mut input_parameters = cgp::parameters_for_impl(impl_self_ty, impl_trait_ref); + let mut input_parameters = cgp::parameters_for_impl(tcx, impl_self_ty, impl_trait_ref); cgp::identify_constrained_generic_params( tcx, impl_predicates, @@ -111,7 +111,7 @@ fn enforce_impl_params_are_constrained( match item.kind { ty::AssocKind::Type => { if item.defaultness(tcx).has_value() { - cgp::parameters_for(&tcx.type_of(def_id).instantiate_identity(), true) + cgp::parameters_for(tcx, &tcx.type_of(def_id).instantiate_identity(), true) } else { vec![] } diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs index c8dedb0f37194..bd4fce81377d4 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs @@ -266,15 +266,15 @@ fn unconstrained_parent_impl_args<'tcx>( continue; } - unconstrained_parameters.extend(cgp::parameters_for(&projection_ty, true)); + unconstrained_parameters.extend(cgp::parameters_for(tcx, &projection_ty, true)); - for param in cgp::parameters_for(&projected_ty, false) { + for param in cgp::parameters_for(tcx, &projected_ty, false) { if !unconstrained_parameters.contains(¶m) { constrained_params.insert(param.0); } } - unconstrained_parameters.extend(cgp::parameters_for(&projected_ty, true)); + unconstrained_parameters.extend(cgp::parameters_for(tcx, &projected_ty, true)); } } @@ -312,7 +312,7 @@ fn check_duplicate_params<'tcx>( parent_args: &Vec>, span: Span, ) -> Result<(), ErrorGuaranteed> { - let mut base_params = cgp::parameters_for(parent_args, true); + let mut base_params = cgp::parameters_for(tcx, parent_args, true); base_params.sort_by_key(|param| param.0); if let (_, [duplicate, ..]) = base_params.partition_dedup() { let param = impl1_args[duplicate.0 as usize]; diff --git a/tests/ui/lazy-type-alias/constrained-params.rs b/tests/ui/lazy-type-alias/constrained-params.rs new file mode 100644 index 0000000000000..7ac84b9d96f21 --- /dev/null +++ b/tests/ui/lazy-type-alias/constrained-params.rs @@ -0,0 +1,27 @@ +// check-pass + +#![feature(lazy_type_alias)] +#![allow(incomplete_features)] + +type Injective = Local; +struct Local(T); + +impl Injective { + fn take(_: T) {} +} + +trait Trait { + type Out; + fn produce() -> Self::Out; +} + +impl Trait for Injective { + type Out = T; + fn produce() -> Self::Out { T::default() } +} + +fn main() { + Injective::take(0); + let _: String = Injective::produce(); + let _: bool = Local::produce(); +} diff --git a/tests/ui/lazy-type-alias/unconstrained-param-due-to-overflow.rs b/tests/ui/lazy-type-alias/unconstrained-param-due-to-overflow.rs new file mode 100644 index 0000000000000..eceefa719ec80 --- /dev/null +++ b/tests/ui/lazy-type-alias/unconstrained-param-due-to-overflow.rs @@ -0,0 +1,8 @@ +#![feature(lazy_type_alias)] +#![allow(incomplete_features)] + +impl Loop {} //~ ERROR the type parameter `T` is not constrained + +type Loop = Loop; + +fn main() {} diff --git a/tests/ui/lazy-type-alias/unconstrained-param-due-to-overflow.stderr b/tests/ui/lazy-type-alias/unconstrained-param-due-to-overflow.stderr new file mode 100644 index 0000000000000..9af6f5dda0b34 --- /dev/null +++ b/tests/ui/lazy-type-alias/unconstrained-param-due-to-overflow.stderr @@ -0,0 +1,9 @@ +error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates + --> $DIR/unconstrained-param-due-to-overflow.rs:4:6 + | +LL | impl Loop {} + | ^ unconstrained type parameter + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0207`. diff --git a/tests/ui/lazy-type-alias/unconstrained-params.rs b/tests/ui/lazy-type-alias/unconstrained-params.rs new file mode 100644 index 0000000000000..d58938b30701c --- /dev/null +++ b/tests/ui/lazy-type-alias/unconstrained-params.rs @@ -0,0 +1,12 @@ +#![feature(lazy_type_alias)] +#![allow(incomplete_features)] + +impl NotInjective {} //~ ERROR the type parameter `T` is not constrained + +type NotInjective = Local<::Out>; +struct Local(T); + +trait Discard { type Out; } +impl Discard for T { type Out = (); } + +fn main() {} diff --git a/tests/ui/lazy-type-alias/unconstrained-params.stderr b/tests/ui/lazy-type-alias/unconstrained-params.stderr new file mode 100644 index 0000000000000..3c52a06c319db --- /dev/null +++ b/tests/ui/lazy-type-alias/unconstrained-params.stderr @@ -0,0 +1,9 @@ +error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates + --> $DIR/unconstrained-params.rs:4:6 + | +LL | impl NotInjective {} + | ^ unconstrained type parameter + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0207`.