Skip to content

Commit dd4ce84

Browse files
committed
Some fold/visit tweaks.
- Introduce `HAS_REGIONS`/`has_regions`, which is true if any regions are present. - Add `fold_clauses` to `Shifter` and `RegionFolder` for consistency. - Simplify `has_type_flags`. - Remove unnecessary local variables in `HasTypeFlagsVisitor` methods. - Use `|=` operator in one place. LLM disclosure: Claude Code suggested these changes when I asked it to review `fold.rs` and `visit.rs`. I verified the correctness of the suggestions and made the changes by hand.
1 parent 3b5dd18 commit dd4ce84

3 files changed

Lines changed: 24 additions & 29 deletions

File tree

compiler/rustc_type_ir/src/flags.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ bitflags::bitflags! {
118118
/// Does this have any `ReErased` regions?
119119
const HAS_RE_ERASED = 1 << 21;
120120

121+
/// Does this have any regions of any kind?
122+
const HAS_REGIONS = TypeFlags::HAS_FREE_REGIONS.bits()
123+
| TypeFlags::HAS_RE_BOUND.bits()
124+
| TypeFlags::HAS_RE_ERASED.bits();
125+
121126
/// Does this value have parameters/placeholders/inference variables which could be
122127
/// replaced later, in a way that would change the results of `impl` specialization?
123128
const STILL_FURTHER_SPECIALIZABLE = TypeFlags::HAS_TY_PARAM.bits()
@@ -192,7 +197,7 @@ impl<I: Interner> FlagComputation<I> {
192197
}
193198

194199
fn add_flags(&mut self, flags: TypeFlags) {
195-
self.flags = self.flags | flags;
200+
self.flags |= flags;
196201
}
197202

198203
/// indicates that `self` refers to something at binding level `binder`

compiler/rustc_type_ir/src/fold.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ impl<I: Interner> TypeFolder<I> for Shifter<I> {
433433
fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
434434
if p.has_vars_bound_at_or_above(self.current_index) { p.super_fold_with(self) } else { p }
435435
}
436+
437+
fn fold_clauses(&mut self, c: I::Clauses) -> I::Clauses {
438+
if c.has_vars_bound_at_or_above(self.current_index) { c.super_fold_with(self) } else { c }
439+
}
436440
}
437441

438442
pub fn shift_region<I: Interner>(cx: I, region: I::Region, amount: u32) -> I::Region {
@@ -535,32 +539,18 @@ where
535539
}
536540

537541
fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
538-
if t.has_type_flags(
539-
TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_RE_BOUND | TypeFlags::HAS_RE_ERASED,
540-
) {
541-
t.super_fold_with(self)
542-
} else {
543-
t
544-
}
542+
if t.has_type_flags(TypeFlags::HAS_REGIONS) { t.super_fold_with(self) } else { t }
545543
}
546544

547545
fn fold_const(&mut self, ct: I::Const) -> I::Const {
548-
if ct.has_type_flags(
549-
TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_RE_BOUND | TypeFlags::HAS_RE_ERASED,
550-
) {
551-
ct.super_fold_with(self)
552-
} else {
553-
ct
554-
}
546+
if ct.has_type_flags(TypeFlags::HAS_REGIONS) { ct.super_fold_with(self) } else { ct }
555547
}
556548

557549
fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
558-
if p.has_type_flags(
559-
TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_RE_BOUND | TypeFlags::HAS_RE_ERASED,
560-
) {
561-
p.super_fold_with(self)
562-
} else {
563-
p
564-
}
550+
if p.has_type_flags(TypeFlags::HAS_REGIONS) { p.super_fold_with(self) } else { p }
551+
}
552+
553+
fn fold_clauses(&mut self, c: I::Clauses) -> I::Clauses {
554+
if c.has_type_flags(TypeFlags::HAS_REGIONS) { c.super_fold_with(self) } else { c }
565555
}
566556
}

compiler/rustc_type_ir/src/visit.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ pub trait TypeVisitableExt<I: Interner>: TypeVisitable<I> {
285285
self.has_type_flags(TypeFlags::HAS_PARAM - TypeFlags::HAS_RE_PARAM)
286286
}
287287

288+
fn has_regions(&self) -> bool {
289+
self.has_type_flags(TypeFlags::HAS_REGIONS)
290+
}
291+
288292
fn has_infer_regions(&self) -> bool {
289293
self.has_type_flags(TypeFlags::HAS_RE_INFER)
290294
}
@@ -363,9 +367,7 @@ pub trait TypeVisitableExt<I: Interner>: TypeVisitable<I> {
363367

364368
impl<I: Interner, T: TypeVisitable<I>> TypeVisitableExt<I> for T {
365369
fn has_type_flags(&self, flags: TypeFlags) -> bool {
366-
let res =
367-
self.visit_with(&mut HasTypeFlagsVisitor { flags }) == ControlFlow::Break(FoundFlags);
368-
res
370+
self.visit_with(&mut HasTypeFlagsVisitor { flags }).is_break()
369371
}
370372

371373
fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
@@ -438,8 +440,7 @@ impl<I: Interner> TypeVisitor<I> for HasTypeFlagsVisitor {
438440
#[inline]
439441
fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
440442
// Note: no `super_visit_with` call.
441-
let flags = t.flags();
442-
if flags.intersects(self.flags) {
443+
if t.flags().intersects(self.flags) {
443444
ControlFlow::Break(FoundFlags)
444445
} else {
445446
ControlFlow::Continue(())
@@ -449,8 +450,7 @@ impl<I: Interner> TypeVisitor<I> for HasTypeFlagsVisitor {
449450
#[inline]
450451
fn visit_region(&mut self, r: I::Region) -> Self::Result {
451452
// Note: no `super_visit_with` call, as usual for `Region`.
452-
let flags = r.flags();
453-
if flags.intersects(self.flags) {
453+
if r.flags().intersects(self.flags) {
454454
ControlFlow::Break(FoundFlags)
455455
} else {
456456
ControlFlow::Continue(())

0 commit comments

Comments
 (0)