Skip to content

Move Region from rustc_middle to rustc_type_ir#154989

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
Jamesbarford:chore/move-region-to-ir
Jul 21, 2026
Merged

Move Region from rustc_middle to rustc_type_ir#154989
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
Jamesbarford:chore/move-region-to-ir

Conversation

@Jamesbarford

@Jamesbarford Jamesbarford commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

View all comments

General notes

Probably best reviewed commit by commit. I've tried where to make the changes as isolated as possible.

  • For the pub struct Region if we use Interned<...> in the definition then we'd need to add a lifetime to the Interner trait. As such I opted for a type InternedRegionKind which is Interned<'tcx, RegionKind<'tcx>> in the implementation. To allow calling the kind() method I implemented inherent::IntoKind for Interned<'tcx, T>.
  • All methods from the Region trait in inherent have been moved to compiler/rustc_type_ir/src/sty/mod.rs.
  • Had to implement Lift manually for Interned<RegionKind> (I::InternedRegionKind) which is a no-op.
  • I'm not sure the implementation of kind() is correct or even should be implemented for Interned<...>?
  • Add a small abstraction needed for decode to still re-intern Region and moved this along with other encode/decode implementations into a serialize file.
error[E0210]: type parameter `E` must be used as the type parameter for some local type (e.g., `MyStruct<E>`)
   --> compiler/rustc_middle/src/ty/codec.rs:162:12
    |
162 | impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Region<'tcx> {
    |            ^ type parameter `E` must be used as the type parameter for some local type
    |
    = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
    = note: only traits defined in the current crate can be implemented for a type parameter

error[E0210]: type parameter `D` must be used as the type parameter for some local type (e.g., `MyStruct<D>`)
   --> compiler/rustc_middle/src/ty/codec.rs:301:12
    |
301 | impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Region<'tcx> {
    |            ^ type parameter `D` must be used as the type parameter for some local type
    |
    = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
    = note: only traits defined in the current crate can be implemented for a type parameter

compiler/rustc_middle/src/ty/region.rs

Generally anything that needed to access fields on TyCtxt have been made methods on the interner.

  • interner.lifetimes.anon_re_bounds.get(debruijn.as_usize()) for getting an interned lifetime, is now a trait method on Interner; fn get_anon_re_bounds_lifetime(...)
  • fn intern_region(...) now a trait method on Interner
  • interner.lifetimes.anon_re_canonical_bounds.get(debruijn.as_usize()) required
    for fn new_canonical_bound(...). Is now fn get_anon_re_canonical_bounds_lifetime(...)
  • interner.lifetimes.re_static has become fn get_re_static_lifetime() for method fn new_static((..)

r? @lcnr

@rustbot

rustbot commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator

changes to the core type system

cc @lcnr

HIR ty lowering was modified

cc @fmease

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Apr 8, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

Comment thread compiler/rustc_type_ir/src/sty/mod.rs Outdated
// Use a pre-interned one when possible.
if let BoundRegion { var, kind: BoundRegionKind::Anon } = bound_region
// This
&& let Some(inner) = interner.get_anon_re_bounds_lifetime(debruijn.as_usize())

@lcnr lcnr Apr 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

i feel like these interning opts should not be in rustc_type_ir, so maybe add Interner::intern_bound_region and call that directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread compiler/rustc_type_ir/src/sty/mod.rs Outdated
#[inline]
pub fn new_canonical_bound(interner: I, var: BoundVar) -> Self {
// Use a pre-interned one when possible.
if let Some(re) = interner.get_anon_re_canonical_bounds_lifetime(var.as_usize()) {

@lcnr lcnr Apr 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lcnr lcnr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully get the changes to InternerDecoder

we should keep Region<I>: Display around by implementing it via IrPrint 🤔 having RegionDisplay seems unnecessary, we already have this IrPrint handling for other types, don't we?

IntoDiagArg is defined outside of rustc_middle? Maybe we should make it generic over I as well?

View changes since this review

Comment thread compiler/rustc_type_ir/src/binder.rs Outdated
Canonical,
}

impl<U: Interner> Lift<U> for BoundVarIndexKind {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surprising that this isn't/can't be derived or ignored in the derive

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

39c966a, by adding back nop_lift! { region; Region<'a> => Region<'tcx> } in compiler/rustc_middle/src/ty/context.rs it takes care of all of the comments about Lift

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this impl still exists 🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still relevant

Comment thread compiler/rustc_type_ir/src/binder.rs Outdated
Comment thread compiler/rustc_type_ir/src/inherent.rs Outdated

impl<I: Interner> Eq for RegionKind<I> {}

impl<I: Interner, U: Interner> Lift<U> for RegionKind<I>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also not derived?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still relevant?

Comment thread compiler/rustc_middle/src/ty/structural_impls.rs Outdated
Comment thread compiler/rustc_middle/src/ty/structural_impls.rs
@lcnr

lcnr commented Apr 9, 2026

Copy link
Copy Markdown
Contributor
  • For the pub struct Region if we use Interned<...> in the definition then we'd need to add a lifetime to the Interner trait. As such I opted for a type InternedRegionKind which is Interned<'tcx, RegionKind<'tcx>> in the implementation. To allow calling the kind() method I implemented inherent::IntoKind for Interned<'tcx, T>.

Interned itself is a type from rustc_middle anyways, is it not? 🤔 I would expect us to instead of type Interned<T> to trait Interner and have region be struct Region<I: Interner>(I::Interned<RegionKind>)

  • Had to implement Lift manually.

do we have some Lift_Generic derive or whatever? I feel like we should?

@Jamesbarford
Jamesbarford force-pushed the chore/move-region-to-ir branch from fc9ac0c to 39c966a Compare April 9, 2026 10:19
@rustbot

rustbot commented Apr 9, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Apr 9, 2026
@Jamesbarford
Jamesbarford force-pushed the chore/move-region-to-ir branch from 39c966a to 7c825ed Compare April 9, 2026 14:12
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Jamesbarford
Jamesbarford force-pushed the chore/move-region-to-ir branch from 6de045f to 89ecd54 Compare April 10, 2026 11:43
Comment thread compiler/rustc_middle/src/ty/context/impl_interner.rs
@rust-bors

This comment has been minimized.

@Jamesbarford
Jamesbarford force-pushed the chore/move-region-to-ir branch from 89ecd54 to 706e15e Compare April 14, 2026 11:51
@rustbot

This comment has been minimized.

@rustbot

rustbot commented Apr 14, 2026

Copy link
Copy Markdown
Collaborator

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

@rustbot rustbot added the T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. label Apr 14, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_middle/src/ty/region.rs Outdated
}
impl<'tcx, T: std::fmt::Debug + Clone + Copy> rustc_type_ir::intern::Interned<TyCtxt<'tcx>>
for Interned<'tcx, T>
{

@lcnr lcnr Jul 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be in region as it applies to all interned things

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4bc330e 👍

Comment thread compiler/rustc_type_ir/src/binder.rs Outdated
Canonical,
}

impl<U: Interner> Lift<U> for BoundVarIndexKind {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still relevant

Comment thread compiler/rustc_type_ir/src/interner.rs Outdated

fn lift_region_raw<I: Interner>(self, region: I::InternedRegionKind) -> Self::InternedRegionKind
where
I::InternedRegionKind: Lift<Self, Lifted = Self::InternedRegionKind>,

@lcnr lcnr Jul 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this instead of deriving Lift for Region (so that it just recursively lifts its field)

View changes since the review

@Jamesbarford Jamesbarford Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ba50d0a 👍

Comment thread compiler/rustc_middle/src/ty/region.rs Outdated

pub fn type_flags(self) -> TypeFlags {
fn type_flags(self) -> TypeFlags {
let mut flags = TypeFlags::empty();

@lcnr lcnr Jul 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that method should be unused 🤔 more generally, rustc_type_ir duplicated some of the methods we also implement here, can you go through the methods on the extension trait and check whether we have the same method as an inherent method?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5222b87 👍

@Jamesbarford
Jamesbarford force-pushed the chore/move-region-to-ir branch from 5222b87 to 2254fbc Compare July 21, 2026 11:07
@lcnr

lcnr commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

@bors r+ rollup=never

@rust-bors

rust-bors Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 2254fbc has been approved by lcnr

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 21, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Contributor

@bors p=6
Schedulign

@rust-bors

This comment has been minimized.

@rust-bors rust-bors Bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 21, 2026
@rust-bors

rust-bors Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: lcnr
Duration: 3h 39m 10s
Pushing 0e29c21 to main...

@github-actions

Copy link
Copy Markdown
Contributor
What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 79a4b00 (parent) -> 0e29c21 (this PR)

Test differences

Show 129 test diffs

129 doctest diffs were found. These are ignored, as they are noisy.

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 0e29c21d93abc9bdc330182be1fbd33cfd5fee68 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. aarch64-apple-macos-26: 2h 24m -> 3h 27m (+43.3%)
  2. x86_64-gnu-miri: 1h 36m -> 1h 1m (-36.6%)
  3. dist-ohos-x86_64: 58m 33s -> 1h 19m (+35.1%)
  4. pr-check-1: 33m 46s -> 22m 15s (-34.1%)
  5. x86_64-mingw-2: 2h 33m -> 1h 42m (-33.1%)
  6. i686-gnu-1: 1h 51m -> 2h 26m (+31.8%)
  7. x86_64-gnu-nopt: 2h 24m -> 1h 39m (-30.8%)
  8. tidy: 2m 28s -> 1m 46s (-28.5%)
  9. arm-android: 1h 41m -> 1h 13m (-27.9%)
  10. dist-android: 28m 26s -> 20m 41s (-27.3%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (0e29c21): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (secondary -0.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.9% [4.9%, 4.9%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.5% [-5.6%, -1.4%] 2
All ❌✅ (primary) - - 0

Cycles

This perf run didn't have relevant results for this metric.

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 489.717s -> 495.463s (1.17%)
Artifact size: 388.55 MiB -> 387.85 MiB (-0.18%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants