Skip to content

Commit f64d028

Browse files
committed
Auto merge of #117589 - compiler-errors:global-vars-bug, r=jackh726
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 #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 #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 #117056. r? types **(priority is kinda high on a review here given beta becomes stable on November 16.)**
2 parents c3ae470 + 32294fc commit f64d028

File tree

5 files changed

+47
-32
lines changed

5 files changed

+47
-32
lines changed

compiler/rustc_middle/src/ty/erase_regions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ impl<'tcx> TyCtxt<'tcx> {
2020
where
2121
T: TypeFoldable<TyCtxt<'tcx>>,
2222
{
23-
// If there's nothing to erase avoid performing the query at all
24-
if !value.has_type_flags(TypeFlags::HAS_LATE_BOUND | TypeFlags::HAS_FREE_REGIONS) {
23+
// If there's nothing to erase or anonymize, avoid performing the query at all
24+
if !value.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
2525
return value;
2626
}
2727
debug!("erase_regions({:?})", value);

compiler/rustc_middle/src/ty/flags.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,6 @@ impl FlagComputation {
3434
result.flags
3535
}
3636

37-
pub fn bound_var_flags(vars: &ty::List<ty::BoundVariableKind>) -> FlagComputation {
38-
let mut computation = FlagComputation::new();
39-
40-
for bv in vars {
41-
match bv {
42-
ty::BoundVariableKind::Ty(_) => {
43-
computation.flags |= TypeFlags::HAS_TY_LATE_BOUND;
44-
}
45-
ty::BoundVariableKind::Region(_) => {
46-
computation.flags |= TypeFlags::HAS_RE_LATE_BOUND;
47-
}
48-
ty::BoundVariableKind::Const => {
49-
computation.flags |= TypeFlags::HAS_CT_LATE_BOUND;
50-
}
51-
}
52-
}
53-
54-
computation
55-
}
56-
5737
fn add_flags(&mut self, flags: TypeFlags) {
5838
self.flags = self.flags | flags;
5939
}
@@ -77,7 +57,11 @@ impl FlagComputation {
7757
where
7858
F: FnOnce(&mut Self, T),
7959
{
80-
let mut computation = FlagComputation::bound_var_flags(value.bound_vars());
60+
let mut computation = FlagComputation::new();
61+
62+
if !value.bound_vars().is_empty() {
63+
computation.add_flags(TypeFlags::HAS_BINDER_VARS);
64+
}
8165

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

compiler/rustc_middle/src/ty/visit.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -494,15 +494,11 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for HasTypeFlagsVisitor {
494494
&mut self,
495495
t: &Binder<'tcx, T>,
496496
) -> ControlFlow<Self::BreakTy> {
497-
// If we're looking for any of the HAS_*_LATE_BOUND flags, we need to
498-
// additionally consider the bound vars on the binder itself, even if
499-
// the contents of a the binder (e.g. a `TraitRef`) doesn't reference
500-
// the bound vars.
501-
if self.flags.intersects(TypeFlags::HAS_LATE_BOUND) {
502-
let bound_var_flags = FlagComputation::bound_var_flags(t.bound_vars());
503-
if bound_var_flags.flags.intersects(self.flags) {
504-
return ControlFlow::Break(FoundFlags);
505-
}
497+
// If we're looking for the HAS_BINDER_VARS flag, check if the
498+
// binder has vars. This won't be present in the binder's bound
499+
// value, so we need to check here too.
500+
if self.flags.intersects(TypeFlags::HAS_BINDER_VARS) && !t.bound_vars().is_empty() {
501+
return ControlFlow::Break(FoundFlags);
506502
}
507503

508504
t.super_visit_with(self)

compiler/rustc_type_ir/src/flags.rs

+3
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,8 @@ bitflags! {
115115

116116
/// Does this have `Coroutine` or `CoroutineWitness`?
117117
const HAS_TY_COROUTINE = 1 << 23;
118+
119+
/// Does this have any binders with bound vars (e.g. that need to be anonymized)?
120+
const HAS_BINDER_VARS = 1 << 24;
118121
}
119122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// check-pass
2+
3+
trait Foo {
4+
type Assoc;
5+
6+
fn do_it(_: &Self::Assoc)
7+
where
8+
for<'a> Self: Baz<'a>;
9+
}
10+
11+
trait Baz<'a>: Foo {}
12+
13+
impl Foo for () {
14+
type Assoc = Inherent;
15+
16+
// Ensure that the `for<'a> Self: Baz<'a>` predicate, which has
17+
// a supertrait `for<'a> Self: Foo`, does not cause us to fail
18+
// to normalize `Self::Assoc`.
19+
fn do_it(x: &Self::Assoc)
20+
where
21+
for<'a> Self: Baz<'a>,
22+
{
23+
x.inherent();
24+
}
25+
}
26+
27+
struct Inherent;
28+
impl Inherent {
29+
fn inherent(&self) {}
30+
}
31+
32+
fn main() {}

0 commit comments

Comments
 (0)