diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2023-04-16 10:14:27 +0000 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2023-04-16 17:39:52 +0000 |
| commit | 8fc1d68413b98cd50e6e55b6e33145ccd584b6a1 (patch) | |
| tree | 9b3a8a9c3f0a0134f9b2de663bfcb9548e63a6b4 | |
| parent | 8889c6fa0ea8b2045205bc1a4550260c34be98c6 (diff) | |
| download | rust-8fc1d68413b98cd50e6e55b6e33145ccd584b6a1.tar.gz rust-8fc1d68413b98cd50e6e55b6e33145ccd584b6a1.zip | |
Account for variance in outlives verification.
| -rw-r--r-- | compiler/rustc_infer/src/infer/outlives/components.rs | 40 | ||||
| -rw-r--r-- | compiler/rustc_infer/src/infer/outlives/verify.rs | 9 | ||||
| -rw-r--r-- | tests/ui/impl-trait/issue-108592.stderr | 21 |
3 files changed, 45 insertions, 25 deletions
diff --git a/compiler/rustc_infer/src/infer/outlives/components.rs b/compiler/rustc_infer/src/infer/outlives/components.rs index ff23087fe8d..cb63d2f18b6 100644 --- a/compiler/rustc_infer/src/infer/outlives/components.rs +++ b/compiler/rustc_infer/src/infer/outlives/components.rs @@ -143,7 +143,7 @@ fn compute_components<'tcx>( // through and constrain Pi. let mut subcomponents = smallvec![]; let mut subvisited = SsoHashSet::new(); - compute_components_recursive(tcx, ty.into(), &mut subcomponents, &mut subvisited); + compute_alias_components_recursive(tcx, ty, &mut subcomponents, &mut subvisited); out.push(Component::EscapingAlias(subcomponents.into_iter().collect())); } } @@ -193,7 +193,43 @@ fn compute_components<'tcx>( /// /// This should not be used to get the components of `parent` itself. /// Use [push_outlives_components] instead. -pub(super) fn compute_components_recursive<'tcx>( +pub(super) fn compute_alias_components_recursive<'tcx>( + tcx: TyCtxt<'tcx>, + alias_ty: Ty<'tcx>, + out: &mut SmallVec<[Component<'tcx>; 4]>, + visited: &mut SsoHashSet<GenericArg<'tcx>>, +) { + let ty::Alias(kind, alias_ty) = alias_ty.kind() else { bug!() }; + let opt_variances = if *kind == ty::Opaque { tcx.variances_of(alias_ty.def_id) } else { &[] }; + for (index, child) in alias_ty.substs.iter().enumerate() { + if opt_variances.get(index) == Some(&ty::Bivariant) { + continue; + } + if !visited.insert(child) { + continue; + } + match child.unpack() { + GenericArgKind::Type(ty) => { + compute_components(tcx, ty, out, visited); + } + GenericArgKind::Lifetime(lt) => { + // Ignore late-bound regions. + if !lt.is_late_bound() { + out.push(Component::Region(lt)); + } + } + GenericArgKind::Const(_) => { + compute_components_recursive(tcx, child, out, visited); + } + } + } +} + +/// Collect [Component]s for *all* the substs of `parent`. +/// +/// This should not be used to get the components of `parent` itself. +/// Use [push_outlives_components] instead. +fn compute_components_recursive<'tcx>( tcx: TyCtxt<'tcx>, parent: GenericArg<'tcx>, out: &mut SmallVec<[Component<'tcx>; 4]>, diff --git a/compiler/rustc_infer/src/infer/outlives/verify.rs b/compiler/rustc_infer/src/infer/outlives/verify.rs index bae246418b0..e1cb53bc71d 100644 --- a/compiler/rustc_infer/src/infer/outlives/verify.rs +++ b/compiler/rustc_infer/src/infer/outlives/verify.rs @@ -1,4 +1,4 @@ -use crate::infer::outlives::components::{compute_components_recursive, Component}; +use crate::infer::outlives::components::{compute_alias_components_recursive, Component}; use crate::infer::outlives::env::RegionBoundPairs; use crate::infer::region_constraints::VerifyIfEq; use crate::infer::VerifyBound; @@ -130,7 +130,12 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> { // see the extensive comment in projection_must_outlive let recursive_bound = { let mut components = smallvec![]; - compute_components_recursive(self.tcx, alias_ty_as_ty.into(), &mut components, visited); + compute_alias_components_recursive( + self.tcx, + alias_ty_as_ty.into(), + &mut components, + visited, + ); self.bound_from_components(&components, visited) }; diff --git a/tests/ui/impl-trait/issue-108592.stderr b/tests/ui/impl-trait/issue-108592.stderr deleted file mode 100644 index 3fbf0b266c3..00000000000 --- a/tests/ui/impl-trait/issue-108592.stderr +++ /dev/null @@ -1,21 +0,0 @@ -error[E0428]: the name `test` is defined multiple times - --> $DIR/issue-108592.rs:17:1 - | -LL | fn test() { - | --------- previous definition of the value `test` here -... -LL | fn test(_: &Opaque<'_>) { - | ^^^^^^^^^^^^^^^^^^^^^^^ `test` redefined here - | - = note: `test` must be defined only once in the value namespace of this module - -error[E0601]: `main` function not found in crate `issue_108592` - --> $DIR/issue-108592.rs:20:2 - | -LL | } - | ^ consider adding a `main` function to `$DIR/issue-108592.rs` - -error: aborting due to 2 previous errors - -Some errors have detailed explanations: E0428, E0601. -For more information about an error, try `rustc --explain E0428`. |
