Skip to content

Commit

Permalink
Unrolled build for rust-lang#118964
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#118964 - compiler-errors:resolve, r=aliemjay

Opportunistically resolve region var in canonicalizer (instead of resolving root var)

See comment in `compiler/rustc_type_ir/src/infcx.rs`.

The **root** infer region for a given region vid may not actually be nameable from the universe of the original vid. That means that the assertion in the canonicalizer was too strict, since the `EagerResolver` that we use before canonicalizing is doing only as much resolving as it can.

This replaces `resolve_lt_var` and `probe_lt_var` in the `rustc_type_ir` API with `opportunistic_resolve_lt_var`, which acts as you expect it should. I left a FIXME that complains about the inconsistency.

This test is really gnarly, but I have no idea how to minimize it, since it seems to kind of just be coincidental that it triggered this issue. I hope the underlying root cause is easy enough to understand, though.

r? `@lcnr` or `@aliemjay`

Fixes rust-lang#118950
  • Loading branch information
rust-timer committed Dec 15, 2023
2 parents cca2bda + 146e345 commit 3bc3445
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 35 deletions.
12 changes: 2 additions & 10 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,13 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
self.probe_ty_var(vid).ok()
}

fn root_lt_var(&self, vid: ty::RegionVid) -> ty::RegionVid {
self.root_region_var(vid)
}

fn probe_lt_var(&self, vid: ty::RegionVid) -> Option<ty::Region<'tcx>> {
fn opportunistic_resolve_lt_var(&self, vid: ty::RegionVid) -> Option<ty::Region<'tcx>> {
let re = self
.inner
.borrow_mut()
.unwrap_region_constraints()
.opportunistic_resolve_var(self.tcx, vid);
if re.is_var() { None } else { Some(re) }
if *re == ty::ReVar(vid) { None } else { Some(re) }
}

fn root_ct_var(&self, vid: ConstVid) -> ConstVid {
Expand Down Expand Up @@ -1367,10 +1363,6 @@ impl<'tcx> InferCtxt<'tcx> {
self.inner.borrow_mut().type_variables().root_var(var)
}

pub fn root_region_var(&self, var: ty::RegionVid) -> ty::RegionVid {
self.inner.borrow_mut().unwrap_region_constraints().root_var(var)
}

pub fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
self.inner.borrow_mut().const_unification_table().find(var).vid
}
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_infer/src/infer/region_constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,6 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
}
}

pub fn root_var(&mut self, vid: ty::RegionVid) -> ty::RegionVid {
let mut ut = self.unification_table_mut(); // FIXME(rust-lang/ena#42): unnecessary mut
ut.find(vid).vid
}

fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'tcx> {
match t {
Glb => &mut self.glbs,
Expand Down
8 changes: 1 addition & 7 deletions compiler/rustc_next_trait_solver/src/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,10 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>

ty::ReVar(vid) => {
assert_eq!(
self.infcx.root_lt_var(vid),
vid,
"region vid should have been resolved fully before canonicalization"
);
assert_eq!(
self.infcx.probe_lt_var(vid),
self.infcx.opportunistic_resolve_lt_var(vid),
None,
"region vid should have been resolved fully before canonicalization"
);

match self.canonicalize_mode {
CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
CanonicalizeMode::Response { .. } => {
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_type_ir/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ impl<I: Interner> InferCtxtLike for NoInfcx<I> {
None
}

fn root_lt_var(&self, vid: I::InferRegion) -> I::InferRegion {
vid
}

fn probe_lt_var(&self, _vid: I::InferRegion) -> Option<I::Region> {
fn opportunistic_resolve_lt_var(&self, _vid: I::InferRegion) -> Option<I::Region> {
None
}

Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_type_ir/src/infcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ pub trait InferCtxtLike {
lt: <Self::Interner as Interner>::InferRegion,
) -> Option<UniverseIndex>;

/// Resolve `InferRegion` to its root `InferRegion`.
fn root_lt_var(
&self,
vid: <Self::Interner as Interner>::InferRegion,
) -> <Self::Interner as Interner>::InferRegion;

/// Resolve `InferRegion` to its inferred region, if it has been equated with a non-infer region.
fn probe_lt_var(
/// Resolve `InferRegion` to its inferred region, if it has been equated with
/// a non-infer region.
///
/// FIXME: This has slightly different semantics than `{probe,resolve}_{ty,ct}_var`,
/// that has to do with the fact unlike `Ty` or `Const` vars, in rustc, we may
/// not always be able to *name* the root region var from the universe of the
/// var we're trying to resolve. That's why it's called *opportunistic*.
fn opportunistic_resolve_lt_var(
&self,
vid: <Self::Interner as Interner>::InferRegion,
) -> Option<<Self::Interner as Interner>::Region>;
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/traits/next-solver/issue-118950-root-region.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// compile-flags: -Znext-solver
//
// This is a gnarly test but I don't know how to minimize it, frankly.

#![feature(lazy_type_alias)]
//~^ WARN the feature `lazy_type_alias` is incomplete

trait ToUnit<'a> {
type Unit;
}

trait Overlap<T> {}

type Assoc<'a, T> = <*const T as ToUnit<'a>>::Unit;

impl<T> Overlap<T> for T {}

impl<T> Overlap<for<'a> fn(Assoc<'a, T>)> for T where Missing: Overlap<T> {}
//~^ ERROR conflicting implementations of trait `Overlap<fn(_)>` for type `fn(_)`
//~| ERROR cannot find type `Missing` in this scope

fn main() {}
36 changes: 36 additions & 0 deletions tests/ui/traits/next-solver/issue-118950-root-region.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error[E0412]: cannot find type `Missing` in this scope
--> $DIR/issue-118950-root-region.rs:18:55
|
LL | impl<T> Overlap<for<'a> fn(Assoc<'a, T>)> for T where Missing: Overlap<T> {}
| ^^^^^^^ not found in this scope

warning: the feature `lazy_type_alias` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/issue-118950-root-region.rs:5:12
|
LL | #![feature(lazy_type_alias)]
| ^^^^^^^^^^^^^^^
|
= note: see issue #112792 <https://github.com/rust-lang/rust/issues/112792> for more information
= note: `#[warn(incomplete_features)]` on by default

WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Weak, AliasTy { args: [ReBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a), 'a) }), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) })
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Weak, AliasTy { args: [RePlaceholder(!1_BoundRegion { var: 0, kind: BrNamed(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a), 'a) }), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) })
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Weak, AliasTy { args: [ReBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a), 'a) }), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) })
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Weak, AliasTy { args: [RePlaceholder(!1_BoundRegion { var: 0, kind: BrNamed(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a), 'a) }), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) })
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Weak, AliasTy { args: [ReBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a), 'a) }), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) })
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Weak, AliasTy { args: [RePlaceholder(!1_BoundRegion { var: 0, kind: BrNamed(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a), 'a) }), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) })
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Weak, AliasTy { args: [ReBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a), 'a) }), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) })
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Weak, AliasTy { args: [RePlaceholder(!1_BoundRegion { var: 0, kind: BrNamed(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a), 'a) }), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) })
error[E0119]: conflicting implementations of trait `Overlap<fn(_)>` for type `fn(_)`
--> $DIR/issue-118950-root-region.rs:18:1
|
LL | impl<T> Overlap<T> for T {}
| ------------------------ first implementation here
LL |
LL | impl<T> Overlap<for<'a> fn(Assoc<'a, T>)> for T where Missing: Overlap<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `fn(_)`

error: aborting due to 2 previous errors; 1 warning emitted

Some errors have detailed explanations: E0119, E0412.
For more information about an error, try `rustc --explain E0119`.

0 comments on commit 3bc3445

Please sign in to comment.