Skip to content

Commit fd068fd

Browse files
committed
Some fold/visit tweaks.
- Introduce `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 522e0ac commit fd068fd

3 files changed

Lines changed: 20 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: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,7 @@ pub trait TypeVisitableExt<I: Interner>: TypeVisitable<I> {
363363

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

371369
fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
@@ -438,8 +436,7 @@ impl<I: Interner> TypeVisitor<I> for HasTypeFlagsVisitor {
438436
#[inline]
439437
fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
440438
// Note: no `super_visit_with` call.
441-
let flags = t.flags();
442-
if flags.intersects(self.flags) {
439+
if t.flags().intersects(self.flags) {
443440
ControlFlow::Break(FoundFlags)
444441
} else {
445442
ControlFlow::Continue(())
@@ -449,8 +446,7 @@ impl<I: Interner> TypeVisitor<I> for HasTypeFlagsVisitor {
449446
#[inline]
450447
fn visit_region(&mut self, r: I::Region) -> Self::Result {
451448
// Note: no `super_visit_with` call, as usual for `Region`.
452-
let flags = r.flags();
453-
if flags.intersects(self.flags) {
449+
if r.flags().intersects(self.flags) {
454450
ControlFlow::Break(FoundFlags)
455451
} else {
456452
ControlFlow::Continue(())

0 commit comments

Comments
 (0)