Skip to content

Commit

Permalink
Auto merge of rust-lang#117589 - compiler-errors:global-vars-bug, r=j…
Browse files Browse the repository at this point in the history
…ackh726

Make sure that predicates with unmentioned bound vars are still considered global in the old solver

In the old solver, we consider predicates with late-bound vars to not be "global":
https://github.com/rust-lang/rust/blob/9c8a2694fadf3900c4d7880f6357cee60e9aa39b/compiler/rustc_trait_selection/src/traits/select/mod.rs#L1840-L1844

The implementation of `has_late_bound_vars` was modified in rust-lang#115834 so that we'd properly anonymize binders that had late-bound vars but didn't reference them. This fixed an ICE.

However, this also led to a behavioral change in rust-lang#117056 (comment) for a couple of crates, which now consider `for<'a> GL33: Shader` (note the binder var that is *not* used in the predicate) to not be "global". This forces associated types to not be normalizable due to the old trait solver being dumb.

This PR distinguishes types which *reference* late-bound vars and binders which *have* late-bound vars. The latter is represented with the new type flag `TypeFlags::HAS_BINDER_VARS`, which is used when we only care about knowing whether binders have vars in their bound var list (even if they're not used, like for binder anonymization).

This should fix (after beta backport) the `luminance-gl` and `luminance-webgl` crates in rust-lang#117056.

r? types
**(priority is kinda high on a review here given beta becomes stable on November 16.)**
  • Loading branch information
bors committed Nov 5, 2023
2 parents c3ae470 + 32294fc commit f64d028
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 32 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/erase_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl<'tcx> TyCtxt<'tcx> {
where
T: TypeFoldable<TyCtxt<'tcx>>,
{
// If there's nothing to erase avoid performing the query at all
if !value.has_type_flags(TypeFlags::HAS_LATE_BOUND | TypeFlags::HAS_FREE_REGIONS) {
// If there's nothing to erase or anonymize, avoid performing the query at all
if !value.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
return value;
}
debug!("erase_regions({:?})", value);
Expand Down
26 changes: 5 additions & 21 deletions compiler/rustc_middle/src/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,6 @@ impl FlagComputation {
result.flags
}

pub fn bound_var_flags(vars: &ty::List<ty::BoundVariableKind>) -> FlagComputation {
let mut computation = FlagComputation::new();

for bv in vars {
match bv {
ty::BoundVariableKind::Ty(_) => {
computation.flags |= TypeFlags::HAS_TY_LATE_BOUND;
}
ty::BoundVariableKind::Region(_) => {
computation.flags |= TypeFlags::HAS_RE_LATE_BOUND;
}
ty::BoundVariableKind::Const => {
computation.flags |= TypeFlags::HAS_CT_LATE_BOUND;
}
}
}

computation
}

fn add_flags(&mut self, flags: TypeFlags) {
self.flags = self.flags | flags;
}
Expand All @@ -77,7 +57,11 @@ impl FlagComputation {
where
F: FnOnce(&mut Self, T),
{
let mut computation = FlagComputation::bound_var_flags(value.bound_vars());
let mut computation = FlagComputation::new();

if !value.bound_vars().is_empty() {
computation.add_flags(TypeFlags::HAS_BINDER_VARS);
}

f(&mut computation, value.skip_binder());

Expand Down
14 changes: 5 additions & 9 deletions compiler/rustc_middle/src/ty/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,11 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for HasTypeFlagsVisitor {
&mut self,
t: &Binder<'tcx, T>,
) -> ControlFlow<Self::BreakTy> {
// If we're looking for any of the HAS_*_LATE_BOUND flags, we need to
// additionally consider the bound vars on the binder itself, even if
// the contents of a the binder (e.g. a `TraitRef`) doesn't reference
// the bound vars.
if self.flags.intersects(TypeFlags::HAS_LATE_BOUND) {
let bound_var_flags = FlagComputation::bound_var_flags(t.bound_vars());
if bound_var_flags.flags.intersects(self.flags) {
return ControlFlow::Break(FoundFlags);
}
// If we're looking for the HAS_BINDER_VARS flag, check if the
// binder has vars. This won't be present in the binder's bound
// value, so we need to check here too.
if self.flags.intersects(TypeFlags::HAS_BINDER_VARS) && !t.bound_vars().is_empty() {
return ControlFlow::Break(FoundFlags);
}

t.super_visit_with(self)
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_type_ir/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,8 @@ bitflags! {

/// Does this have `Coroutine` or `CoroutineWitness`?
const HAS_TY_COROUTINE = 1 << 23;

/// Does this have any binders with bound vars (e.g. that need to be anonymized)?
const HAS_BINDER_VARS = 1 << 24;
}
}
32 changes: 32 additions & 0 deletions tests/ui/late-bound-lifetimes/predicate-is-global.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// check-pass

trait Foo {
type Assoc;

fn do_it(_: &Self::Assoc)
where
for<'a> Self: Baz<'a>;
}

trait Baz<'a>: Foo {}

impl Foo for () {
type Assoc = Inherent;

// Ensure that the `for<'a> Self: Baz<'a>` predicate, which has
// a supertrait `for<'a> Self: Foo`, does not cause us to fail
// to normalize `Self::Assoc`.
fn do_it(x: &Self::Assoc)
where
for<'a> Self: Baz<'a>,
{
x.inherent();
}
}

struct Inherent;
impl Inherent {
fn inherent(&self) {}
}

fn main() {}

0 comments on commit f64d028

Please sign in to comment.