Skip to content

Commit 803c7ba

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. - Avoid `is_break` in one place for consistency with nearby code. 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 803c7ba

3 files changed

Lines changed: 27 additions & 31 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: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use tracing::{debug, instrument};
5555

5656
use crate::inherent::*;
5757
use crate::visit::{TypeVisitable, TypeVisitableExt as _};
58-
use crate::{self as ty, BoundVarIndexKind, Interner, TypeFlags};
58+
use crate::{self as ty, BoundVarIndexKind, Interner};
5959

6060
/// This trait is implemented for every type that can be folded,
6161
/// providing the skeleton of the traversal.
@@ -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_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_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_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_regions() { c.super_fold_with(self) } else { c }
565555
}
566556
}

compiler/rustc_type_ir/src/visit.rs

Lines changed: 9 additions & 8 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,13 +367,12 @@ 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 }) == ControlFlow::Break(FoundFlags)
369371
}
370372

371373
fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
372-
self.visit_with(&mut HasEscapingVarsVisitor { outer_index: binder }).is_break()
374+
self.visit_with(&mut HasEscapingVarsVisitor { outer_index: binder })
375+
== ControlFlow::Break(FoundEscapingVars)
373376
}
374377

375378
fn error_reported(&self) -> Result<(), I::ErrorGuaranteed> {
@@ -438,8 +441,7 @@ impl<I: Interner> TypeVisitor<I> for HasTypeFlagsVisitor {
438441
#[inline]
439442
fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
440443
// Note: no `super_visit_with` call.
441-
let flags = t.flags();
442-
if flags.intersects(self.flags) {
444+
if t.flags().intersects(self.flags) {
443445
ControlFlow::Break(FoundFlags)
444446
} else {
445447
ControlFlow::Continue(())
@@ -449,8 +451,7 @@ impl<I: Interner> TypeVisitor<I> for HasTypeFlagsVisitor {
449451
#[inline]
450452
fn visit_region(&mut self, r: I::Region) -> Self::Result {
451453
// Note: no `super_visit_with` call, as usual for `Region`.
452-
let flags = r.flags();
453-
if flags.intersects(self.flags) {
454+
if r.flags().intersects(self.flags) {
454455
ControlFlow::Break(FoundFlags)
455456
} else {
456457
ControlFlow::Continue(())

0 commit comments

Comments
 (0)