Skip to content

Move Region from rustc_middle to rustc_type_ir#154989

Open
Jamesbarford wants to merge 18 commits intorust-lang:mainfrom
Jamesbarford:chore/move-region-to-ir
Open

Move Region from rustc_middle to rustc_type_ir#154989
Jamesbarford wants to merge 18 commits intorust-lang:mainfrom
Jamesbarford:chore/move-region-to-ir

Conversation

@Jamesbarford
Copy link
Copy Markdown
Contributor

@Jamesbarford Jamesbarford commented Apr 8, 2026

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.
  • I'm not sure the implementation of kind() is correct or even should be implemented for Interned<...>?
  • In order for the struct to be printable in pretty.rs I've used an intermediary struct; RegionDisplay so we can still access ty::tls::with
  • Similarly there is an intermediary struct for diagnostics; RegionDiagArg
  • Moved the impls to the owning crate and add a small abstraction needed for decode to still re-intern Region
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
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 8, 2026

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.

// 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())
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 9, 2026

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.

#[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()) {
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 9, 2026

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.

Copy link
Copy Markdown
Contributor

@lcnr lcnr left a comment

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


fn lift_to_interner(self, interner: U) -> Option<Self::Lifted> {
Some(BoundRegion { var: self.var, kind: self.kind.lift_to_interner(interner)? })
}
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.

same here


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?

// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// write!(f, "{:?}", self.kind())
// }
// }
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.

?

rustc_hir::Safety,
rustc_middle::mir::ConstValue,
rustc_span::ErrorGuaranteed,
rustc_span::Symbol,
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.

the alternative would be to restrict the region lift impl to not actually change these types

@lcnr
Copy link
Copy Markdown
Contributor

lcnr commented Apr 9, 2026

  • 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
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 9, 2026

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
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 9, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

The job x86_64-gnu-gcc failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- [run-make] tests/run-make-cargo/rustc-crates-on-stable stdout ----

error: rmake recipe failed to complete
status: exit status: 1
command: cd "/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-make-cargo/rustc-crates-on-stable/rmake_out" && env -u RUSTFLAGS -u __RUSTC_DEBUG_ASSERTIONS_ENABLED -u __STD_DEBUG_ASSERTIONS_ENABLED -u __STD_REMAP_DEBUGINFO_ENABLED AR="ar" BUILD_ROOT="/checkout/obj/build/x86_64-unknown-linux-gnu" CARGO="/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools-bin/cargo" CC="cc" CC_DEFAULT_FLAGS="-ffunction-sections -fdata-sections -fPIC -m64" CXX="c++" CXX_DEFAULT_FLAGS="-ffunction-sections -fdata-sections -fPIC -m64" HOST_RUSTC_DYLIB_PATH="/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" LD_LIBRARY_PATH="/checkout/obj/build/x86_64-unknown-linux-gnu/bootstrap-tools/x86_64-unknown-linux-gnu/release/deps:/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/lib/rustlib/x86_64-unknown-linux-gnu/lib" LD_LIB_PATH_ENVVAR="LD_LIBRARY_PATH" LLVM_BIN_DIR="/checkout/obj/build/x86_64-unknown-linux-gnu/ci-llvm/bin" LLVM_COMPONENTS="aarch64 aarch64asmparser aarch64codegen aarch64desc aarch64disassembler aarch64info aarch64utils abi aggressiveinstcombine all all-targets amdgpu amdgpuasmparser amdgpucodegen amdgpudesc amdgpudisassembler amdgpuinfo amdgputargetmca amdgpuutils analysis arm armasmparser armcodegen armdesc armdisassembler arminfo armutils asmparser asmprinter avr avrasmparser avrcodegen avrdesc avrdisassembler avrinfo binaryformat bitreader bitstreamreader bitwriter bpf bpfasmparser bpfcodegen bpfdesc bpfdisassembler bpfinfo cas cfguard cgdata codegen codegentypes core coroutines coverage csky cskyasmparser cskycodegen cskydesc cskydisassembler cskyinfo debuginfobtf debuginfocodeview debuginfodwarf debuginfodwarflowlevel debuginfogsym debuginfologicalview debuginfomsf debuginfopdb demangle dlltooldriver dtlto dwarfcfichecker dwarflinker dwarflinkerclassic dwarflinkerparallel dwp engine executionengine extensions filecheck frontendatomic frontenddirective frontenddriver frontendhlsl frontendoffloading frontendopenacc frontendopenmp fuzzercli fuzzmutate globalisel hexagon hexagonasmparser hexagoncodegen hexagondesc hexagondisassembler hexagoninfo hipstdpar instcombine instrumentation interfacestub interpreter ipo irprinter irreader jitlink libdriver lineeditor linker loongarch loongarchasmparser loongarchcodegen loongarchdesc loongarchdisassembler loongarchinfo lto m68k m68kasmparser m68kcodegen m68kdesc m68kdisassembler m68kinfo mc mca mcdisassembler mcjit mcparser mips mipsasmparser mipscodegen mipsdesc mipsdisassembler mipsinfo mirparser msp430 msp430asmparser msp430codegen msp430desc msp430disassembler msp430info native nativecodegen nvptx nvptxcodegen nvptxdesc nvptxinfo objcarcopts objcopy object objectyaml option orcdebugging orcjit orcshared orctargetprocess passes plugins powerpc powerpcasmparser powerpccodegen powerpcdesc powerpcdisassembler powerpcinfo profiledata remarks riscv riscvasmparser riscvcodegen riscvdesc riscvdisassembler riscvinfo riscvtargetmca runtimedyld sandboxir scalaropts selectiondag sparc sparcasmparser sparccodegen sparcdesc sparcdisassembler sparcinfo support supportlsp symbolize systemz systemzasmparser systemzcodegen systemzdesc systemzdisassembler systemzinfo tablegen target targetparser telemetry textapi textapibinaryreader transformutils vectorize webassembly webassemblyasmparser webassemblycodegen webassemblydesc webassemblydisassembler webassemblyinfo webassemblyutils windowsdriver windowsmanifest x86 x86asmparser x86codegen x86desc x86disassembler x86info x86targetmca xray xtensa xtensaasmparser xtensacodegen xtensadesc xtensadisassembler xtensainfo" LLVM_FILECHECK="/checkout/obj/build/x86_64-unknown-linux-gnu/ci-llvm/bin/FileCheck" PYTHON="/usr/bin/python3" RUSTC="/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" RUSTDOC="/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustdoc" SOURCE_ROOT="/checkout" TARGET="x86_64-unknown-linux-gnu" TARGET_EXE_DYLIB_PATH="/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" __BOOTSTRAP_JOBS="4" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-make-cargo/rustc-crates-on-stable/rmake"
stdout: none
--- stderr -------------------------------
command failed at line 41
LD_LIBRARY_PATH="/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-make-cargo/rustc-crates-on-stable/rmake_out:/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib:/checkout/obj/build/x86_64-unknown-linux-gnu/bootstrap-tools/x86_64-unknown-linux-gnu/release/deps:/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/lib/rustlib/x86_64-unknown-linux-gnu/lib" RUSTC="/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" RUSTC_STAGE="0" RUSTFLAGS="-Zallow-features=" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools-bin/cargo" "build" "--manifest-path" "/checkout/Cargo.toml" "--no-default-features" "--target-dir" "target" "-p" "rustc_type_ir" "-p" "rustc_next_trait_solver" "-p" "rustc_pattern_analysis" "-p" "rustc_lexer" "-p" "rustc_abi" "-p" "rustc_parse_format" "-p" "rustc_hashes"
output status: `exit status: 101`
=== STDOUT ===



=== STDERR ===
    Blocking waiting for file lock on package cache
---
   |
25 | impl HomogeneousAggregate {
   | ------------------------- method in this implementation
...
38 |     fn merge(self, other: HomogeneousAggregate) -> Result<HomogeneousAggregate, Heterogeneous> {
   |        ^^^^^
   |
   = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

   Compiling rustc_type_ir v0.0.0 (/checkout/compiler/rustc_type_ir)

For more information how to resolve CI failures of this job, visit this link.

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

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. 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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants