Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interpret region vars as universals in implied bounds query #109388

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions compiler/rustc_traits/src/implied_outlives_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! [`rustc_trait_selection::traits::query::type_op::implied_outlives_bounds`].

use rustc_infer::infer::canonical::{self, Canonical};
use rustc_infer::infer::canonical::{CanonicalVarInfo, CanonicalVarKind};
use rustc_infer::infer::outlives::components::{push_outlives_components, Component};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::query::OutlivesBound;
Expand All @@ -20,17 +21,63 @@ pub(crate) fn provide(p: &mut Providers) {
*p = Providers { implied_outlives_bounds, ..*p };
}

struct MapRegionsToUniversals<'tcx> {
tcx: TyCtxt<'tcx>,
query_max_universe: ty::UniverseIndex,
}

impl<'tcx> MapRegionsToUniversals<'tcx> {
fn map<K>(tcx: TyCtxt<'tcx>, value: Canonical<'tcx, K>) -> (Canonical<'tcx, K>, Self) {
let variables = value.variables.iter().zip(0u32..).map(|(var, idx)| match var.kind {
CanonicalVarKind::Region(universe) => {
let name = ty::BoundRegionKind::BrAnon(idx, None);
let placeholder = ty::Placeholder { universe, name };
CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderRegion(placeholder) }
}
CanonicalVarKind::PlaceholderRegion(_) => bug!("unimplemented: placeholder region"),

CanonicalVarKind::Ty(_) => bug!("unexpected ty var"),
CanonicalVarKind::PlaceholderTy(_) => var,

CanonicalVarKind::Const(..) => bug!("unexpected const var"),
CanonicalVarKind::PlaceholderConst(..) => var,
});
let variables = tcx.mk_canonical_var_infos_from_iter(variables);
let query_max_universe = value.max_universe;

(Canonical { variables, ..value }, Self { tcx, query_max_universe })
}

fn reverse_map<K>(&self, value: Canonical<'tcx, K>) -> Canonical<'tcx, K> {
let variables = value.variables.iter().map(|var| match var.kind {
CanonicalVarKind::PlaceholderRegion(ty::Placeholder { universe, .. })
if self.query_max_universe.can_name(universe) =>
{
CanonicalVarInfo { kind: CanonicalVarKind::Region(universe) }
}
CanonicalVarKind::Region(_) | CanonicalVarKind::PlaceholderRegion(_) => var,

CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => var,
CanonicalVarKind::Const(..) | CanonicalVarKind::PlaceholderConst(..) => var,
});
let variables = self.tcx.mk_canonical_var_infos_from_iter(variables);
Canonical { variables, ..value }
}
}

fn implied_outlives_bounds<'tcx>(
tcx: TyCtxt<'tcx>,
goal: CanonicalTyGoal<'tcx>,
) -> Result<
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>,
NoSolution,
> {
tcx.infer_ctxt().enter_canonical_trait_query(&goal, |ocx, key| {
) -> Fallible<&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>> {
let (goal, map) = MapRegionsToUniversals::map(tcx, goal);

let result = tcx.infer_ctxt().enter_canonical_trait_query(&goal, |ocx, key| {
let (param_env, ty) = key.into_parts();
compute_implied_outlives_bounds(ocx, param_env, ty)
})
})?;

let result = MapRegionsToUniversals::reverse_map(&map, result.clone());
Ok(tcx.arena.alloc(result))
}

fn compute_implied_outlives_bounds<'tcx>(
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/implied-bounds/normalization-preserve-equality.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// check-pass
// issue: #106569

struct Equal<'a, 'b>(&'a &'b (), &'b &'a ()); // implies 'a == 'b

trait Trait { type Ty; }

impl<'x> Trait for Equal<'x, 'x> { type Ty = (); }

fn test1<'a, 'b>(_: (<Equal<'a, 'b> as Trait>::Ty, Equal<'a, 'b>)) {
let _ = None::<Equal<'a, 'b>>;
}

fn test2<'a, 'b>(_: <Equal<'a, 'b> as Trait>::Ty, _: Equal<'a, 'b>) {
let _ = None::<Equal<'a, 'b>>;
}

fn main() {}