Skip to content

Commit e6485ed

Browse files
committed
Auto merge of #105733 - compiler-errors:ty-ct-late-flags, r=cjgillot
Add type flags support for `Ty` and `Const` late-bound variables I've been working on `for<T>` binders, and these will eventually be useful.
2 parents 8ea62f6 + 3c41003 commit e6485ed

File tree

7 files changed

+42
-14
lines changed

7 files changed

+42
-14
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
19121912
}
19131913
let pred = obligation.predicate;
19141914
// Match the existing behavior.
1915-
if pred.is_global() && !pred.has_late_bound_regions() {
1915+
if pred.is_global() && !pred.has_late_bound_vars() {
19161916
let pred = self.normalize(span, None, pred);
19171917
let hir_node = tcx.hir().find(self.body_id);
19181918

compiler/rustc_middle/src/ty/erase_regions.rs

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

compiler/rustc_middle/src/ty/flags.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,18 @@ impl FlagComputation {
5959
{
6060
let mut computation = FlagComputation::new();
6161

62-
if !value.bound_vars().is_empty() {
63-
computation.flags = computation.flags | TypeFlags::HAS_RE_LATE_BOUND;
62+
for bv in value.bound_vars() {
63+
match bv {
64+
ty::BoundVariableKind::Ty(_) => {
65+
computation.flags |= TypeFlags::HAS_TY_LATE_BOUND;
66+
}
67+
ty::BoundVariableKind::Region(_) => {
68+
computation.flags |= TypeFlags::HAS_RE_LATE_BOUND;
69+
}
70+
ty::BoundVariableKind::Const => {
71+
computation.flags |= TypeFlags::HAS_CT_LATE_BOUND;
72+
}
73+
}
6474
}
6575

6676
f(&mut computation, value.skip_binder());
@@ -131,6 +141,7 @@ impl FlagComputation {
131141

132142
&ty::Bound(debruijn, _) => {
133143
self.add_bound_var(debruijn);
144+
self.add_flags(TypeFlags::HAS_TY_LATE_BOUND);
134145
}
135146

136147
&ty::Placeholder(..) => {
@@ -303,6 +314,7 @@ impl FlagComputation {
303314
}
304315
ty::ConstKind::Bound(debruijn, _) => {
305316
self.add_bound_var(debruijn);
317+
self.add_flags(TypeFlags::HAS_CT_LATE_BOUND);
306318
}
307319
ty::ConstKind::Param(_) => {
308320
self.add_flags(TypeFlags::HAS_CT_PARAM);

compiler/rustc_middle/src/ty/visit.rs

+8
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
165165
fn has_late_bound_regions(&self) -> bool {
166166
self.has_type_flags(TypeFlags::HAS_RE_LATE_BOUND)
167167
}
168+
/// True if there are any late-bound non-region variables
169+
fn has_non_region_late_bound(&self) -> bool {
170+
self.has_type_flags(TypeFlags::HAS_LATE_BOUND - TypeFlags::HAS_RE_LATE_BOUND)
171+
}
172+
/// True if there are any late-bound variables
173+
fn has_late_bound_vars(&self) -> bool {
174+
self.has_type_flags(TypeFlags::HAS_LATE_BOUND)
175+
}
168176

169177
/// Indicates whether this value still has parameters/placeholders/inference variables
170178
/// which could be replaced later, in a way that would change the results of `impl`

compiler/rustc_symbol_mangling/src/v0.rs

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ impl<'tcx> SymbolMangler<'tcx> {
206206
where
207207
T: TypeVisitable<'tcx>,
208208
{
209+
// FIXME(non-lifetime-binders): What to do here?
209210
let regions = if value.has_late_bound_regions() {
210211
self.tcx.collect_referenced_late_bound_regions(value)
211212
} else {

compiler/rustc_trait_selection/src/traits/select/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
755755
// contain the "'static" lifetime (any other lifetime
756756
// would either be late-bound or local), so it is guaranteed
757757
// to outlive any other lifetime
758-
if pred.0.is_global() && !pred.0.has_late_bound_regions() {
758+
if pred.0.is_global() && !pred.0.has_late_bound_vars() {
759759
Ok(EvaluatedToOk)
760760
} else {
761761
Ok(EvaluatedToOkModuloRegions)
@@ -1785,9 +1785,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
17851785
// Check if a bound would previously have been removed when normalizing
17861786
// the param_env so that it can be given the lowest priority. See
17871787
// #50825 for the motivation for this.
1788-
let is_global = |cand: &ty::PolyTraitPredicate<'tcx>| {
1789-
cand.is_global() && !cand.has_late_bound_regions()
1790-
};
1788+
let is_global =
1789+
|cand: &ty::PolyTraitPredicate<'tcx>| cand.is_global() && !cand.has_late_bound_vars();
17911790

17921791
// (*) Prefer `BuiltinCandidate { has_nested: false }`, `PointeeCandidate`,
17931792
// `DiscriminantKindCandidate`, `ConstDestructCandidate`

compiler/rustc_type_ir/src/lib.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,30 @@ bitflags! {
241241
/// Basically anything but `ReLateBound` and `ReErased`.
242242
const HAS_FREE_REGIONS = 1 << 14;
243243

244-
/// Does this have any `ReLateBound` regions? Used to check
245-
/// if a global bound is safe to evaluate.
244+
/// Does this have any `ReLateBound` regions?
246245
const HAS_RE_LATE_BOUND = 1 << 15;
246+
/// Does this have any `Bound` types?
247+
const HAS_TY_LATE_BOUND = 1 << 16;
248+
/// Does this have any `ConstKind::Bound` consts?
249+
const HAS_CT_LATE_BOUND = 1 << 17;
250+
/// Does this have any bound variables?
251+
/// Used to check if a global bound is safe to evaluate.
252+
const HAS_LATE_BOUND = TypeFlags::HAS_RE_LATE_BOUND.bits
253+
| TypeFlags::HAS_TY_LATE_BOUND.bits
254+
| TypeFlags::HAS_CT_LATE_BOUND.bits;
247255

248256
/// Does this have any `ReErased` regions?
249-
const HAS_RE_ERASED = 1 << 16;
257+
const HAS_RE_ERASED = 1 << 18;
250258

251259
/// Does this value have parameters/placeholders/inference variables which could be
252260
/// replaced later, in a way that would change the results of `impl` specialization?
253-
const STILL_FURTHER_SPECIALIZABLE = 1 << 17;
261+
const STILL_FURTHER_SPECIALIZABLE = 1 << 19;
254262

255263
/// Does this value have `InferTy::FreshTy/FreshIntTy/FreshFloatTy`?
256-
const HAS_TY_FRESH = 1 << 18;
264+
const HAS_TY_FRESH = 1 << 20;
257265

258266
/// Does this value have `InferConst::Fresh`?
259-
const HAS_CT_FRESH = 1 << 19;
267+
const HAS_CT_FRESH = 1 << 21;
260268
}
261269
}
262270

0 commit comments

Comments
 (0)