Account for variance in outlives verification.

This commit is contained in:
Camille GILLOT 2023-04-16 10:14:27 +00:00
parent 8889c6fa0e
commit 8fc1d68413
3 changed files with 45 additions and 25 deletions

View file

@ -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]>,

View file

@ -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)
};

View file

@ -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`.