diff options
| author | bors <bors@rust-lang.org> | 2024-02-18 03:58:56 +0000 | 
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-02-18 03:58:56 +0000 | 
| commit | d3df8ff85121146f2ac5e863e0c9eaba4bf35d32 (patch) | |
| tree | 1093deb2936e81e75ec907c00b8021463cb4286f /compiler/rustc_hir_analysis/src/constrained_generic_params.rs | |
| parent | 1f8e824f111c972c9df8dbb378d87c33f67bbad4 (diff) | |
| parent | fde4556785ac5e07302a48fcea7ead71e711d5f4 (diff) | |
| download | rust-d3df8ff85121146f2ac5e863e0c9eaba4bf35d32.tar.gz rust-d3df8ff85121146f2ac5e863e0c9eaba4bf35d32.zip | |
Auto merge of #120780 - fmease:lta-in-impls, r=oli-obk
Properly deal with weak alias types as self types of impls Fixes #114216. Fixes #116100. Not super happy about the two ad hoc “normalization” implementations for weak alias types: 1. In `inherent_impls`: The “peeling”, normalization to [“WHNF”][whnf]: Semantically that's exactly what we want (neither proper normalization nor shallow normalization would be correct here). Basically a weak alias type is “nominal” (well...^^) if the WHNF is nominal. [#97974](https://github.com/rust-lang/rust/pull/97974) followed the same approach. 2. In `constrained_generic_params`: Generic parameters are constrained by a weak alias type if the corresp. “normalized” type constrains them (where we only normalize *weak* alias types not arbitrary ones). Weak alias types are injective if the corresp. “normalized” type is injective. Both have ad hoc overflow detection mechanisms. **Coherence** is handled in #117164. r? `@oli-obk` or types [whnf]: https://en.wikipedia.org/wiki/Lambda_calculus_definition#Weak_head_normal_form
Diffstat (limited to 'compiler/rustc_hir_analysis/src/constrained_generic_params.rs')
| -rw-r--r-- | compiler/rustc_hir_analysis/src/constrained_generic_params.rs | 43 | 
1 files changed, 33 insertions, 10 deletions
| diff --git a/compiler/rustc_hir_analysis/src/constrained_generic_params.rs b/compiler/rustc_hir_analysis/src/constrained_generic_params.rs index 05efad3ccb3..4ce43bb4887 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<ty::ParamConst> 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<ty::TraitRef<'tcx>>, ) -> FxHashSet<Parameter> { 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,47 @@ 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<TyCtxt<'tcx>>, include_nonconstraining: bool, ) -> Vec<Parameter> { - 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<Parameter>, include_nonconstraining: bool, + depth: usize, } -impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector { +impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector<'tcx> { fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> { 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 + // `<T> (Overflow, T)` for example. Therefore we want to continue instead of + // breaking. Only affects diagnostics. + return ControlFlow::Continue(()); + } + self.depth += 1; + return ensure_sufficient_stack(|| { + self.tcx + .type_of(alias.def_id) + .instantiate(self.tcx, alias.args) + .visit_with(self) + }); + } ty::Param(data) => { self.parameters.push(Parameter::from(data)); } @@ -82,7 +105,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector { fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> { 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 +224,12 @@ pub fn setup_constraining_predicates<'tcx>( // `<<T as Bar>::Baz as Iterator>::Output = <U as Iterator>::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; } | 
