Skip to content

Move Const from rustc_middle to rustc_type_ir - #162628

Open
Jamesbarford wants to merge 5 commits into
rust-lang:mainfrom
Jamesbarford:chore/move-const-pt1
Open

Jamesbarford wants to merge 5 commits into
rust-lang:mainfrom
Jamesbarford:chore/move-const-pt1

Conversation

@Jamesbarford

@Jamesbarford Jamesbarford commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

View all comments

Split by commit;

  • Firstly move the type and methods
  • From I::Const -> Const<I>
  • Import ConstExt in all places that require the extension trait methods in compiler
  • Import ConstExt in all places that require the extension trait methods in clippy

r? @lcnr

@rustbot

rustbot commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

Some changes occurred in match lowering

cc @Nadrieril

Some changes occurred in match checking

cc @Nadrieril

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

cc @rust-lang/clippy

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred in rustc_ty_utils::consts.rs

cc @BoxyUwU

Some changes occurred in exhaustiveness checking

cc @Nadrieril

changes to the core type system

cc @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

HIR ty lowering was modified

cc @fmease

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. PG-exploit-mitigations Project group: Exploit mitigations 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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Sep 11, 2026
// its pointee is valid for the entire lifetime of the target `TyCtxt`.
unsafe { mem::transmute(self) }
}
}

@lcnr lcnr Sep 11, 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 manual impls instead of the macro here again?

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.

nop_lift does;

 assert!(tcx.interners.$set.contains_pointer_to(&InternedInSet(&*self.0.0)));

Whereas we need;

assert!(tcx.interners.const_.contains_pointer_to(&InternedInSet(&*self.0)));

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.

hmm, why does moving Const change this access pattern from .0.0 to just .0 🤔 that's not immediately obvious to me.

Please add that as a comment if it can't be avoided

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.

I'm not sure why;

nop_lift! { const_; Const<'a> => Const<'tcx> }

Given the following definitions

// rustc_type_ir/src/sty/consts.rs
pub struct Const<I: Interner>(pub I::InternedConstKind);

// rustc_middle/rustc_middle/src/ty/context/impl_interner.rs
type InternedConstKind = Interned<'tcx, WithCachedTypeInfo<ty::ConstKind<'tcx>>>;

// rustc_middle/src/ty/consts.rs
pub type Const<'tcx> = ir::Const<TyCtxt<'tcx>>;

Walking through how I think the above would work, which could be wrong, I'd have thought the following code snippet would be true;

pub type Const<'tcx> = struct Const<TyCtxt<'tcx>>(pub TyCtxt<'tcx>::InternedConst);

// which in turn becomes
pub type Const<'tcx> = struct Const<TyCtxt<'tcx>>(pub  Interned<'tcx, WithCachedTypeInfo<ty::ConstKind<'tcx>>>);

Which is the same as what we have before all be it the definition is composed from different modules and associated types. The rust-analyser LSP I have setup agrees with with me that my intuition is correct.

However I get a bunch of cascading errors. Of which this one seems the most likely culprit. So I did what the compiler error told me to do; implement Lift for WithCachedTypeInfo<...>.

error[E0277]: the trait bound `Interned<'tcx, _>: Lift<TyCtxt<'tcx>>` is not satisfied
    --> compiler/rustc_middle/src/ty/context.rs:1894:54
     |
1894 | struct InternedInSet<'tcx, T: ?Sized + PointeeSized>(&'tcx T);
     |                                                      ^^^^^^^ unsatisfied trait bound
     |
help: the trait `Lift<TyCtxt<'tcx>>` is not implemented for `Interned<'tcx, rustc_type_ir::WithCachedTypeInfo<rustc_type_ir::ConstKind<context::TyCtxt<'tcx>>>>`
      but trait `Lift<TyCtxt<'_>>` is implemented for `Interned<'_, rustc_type_ir::RegionKind<context::TyCtxt<'_>>>`
    --> compiler/rustc_middle/src/ty/context.rs:1721:1

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.

3b8480e adds the comment;

// `rustc_type_ir::Const<I>` is only the generic wrapper; lifting it delegates
// to `I::InternedConstKind`, so the concrete interned const representation
// must itself implement `Lift`.

It's interesting that when I expanded the macro rust-analyser was able to pick up self.0.0. This confused me probably more than it should have done.

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.

hmm, confusing. Can you change this to a "FIXME: unclear why exactly the macro doesn't work"?

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

// Things stored inside of tys
type ErrorGuaranteed: Copy + Debug + Hash + Eq;
type ErrorGuaranteed: Copy + Debug + Hash + Eq + TypeVisitable<Self>;

@lcnr lcnr Sep 11, 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.

instead mark with type_visitable(ignored) 🤔

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.

I was able to completely get rid of it

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 does this bound then exist

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.

I think I was wrong, I've detailed it here; #162628 (comment), but will also comment in the code

Comment thread compiler/rustc_type_ir/src/interner.rs Outdated
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_type_ir/src/ty_info.rs

@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.

nits, otherwise this is looking good

View changes since this review

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

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

cc @bjorn3

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

cc @antoyo, @GuillaumeGomez

@rust-bors

This comment has been minimized.

@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.

alright, r=me after rebase + final nit then

View changes since this review

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Jamesbarford

Copy link
Copy Markdown
Contributor Author

So from one of the rebases I've done, I was getting a lot of failing UI tests (narrowed to one error I've pasted in below). Tracing the error to the changes, the only implementation changes were in the implementation of TypeVisitable for Const.

ErrorGuaranteed was no longer being visited and instead returned V::Result::output(). The same is true for ValueConst, although that doesn't seem to require visiting.

Putting the implementation back for ErrorGuaranteed has made the tests pass again (locally, 🤞 for the pipeline). I suspect this is because the flags and the structural visitor had become inconsistent: the flags still indicated that an error was present, but the visitor could no longer reach the corresponding ErrorGuaranteed.

thread 'rustc' (2161986) panicked at compiler/rustc_type_ir/src/visit.rs:402:17:
type flags said there was an error, but now there is not
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/cbae9b4cae2b108f6a3d18cfe6075714bb739463/library/std/src/panicking.rs:679:5
   1: core::panicking::panic_fmt
             at /rustc/cbae9b4cae2b108f6a3d18cfe6075714bb739463/library/core/src/panicking.rs:80:14
   2: <T as rustc_type_ir::visit::TypeVisitableExt<I>>::error_reported
   3: rustc_infer::infer::InferCtxt::deeply_resolve_ignoring_regions
   4: rustc_hir_typeck::expr::<impl rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
   5: rustc_hir_typeck::expr::<impl rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation
   6: rustc_hir_typeck::fn_ctxt::_impl::<impl rustc_hir_typeck::fn_ctxt::FnCtxt>::with_breakable_ctxt
   7: rustc_hir_typeck::fn_ctxt::checks::<impl rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
   8: rustc_hir_typeck::expr::<impl rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
   9: rustc_hir_typeck::expr::<impl rustc_hir_typeck::fn_ctxt::FnCtxt>::check_return_or_body_tail
  10: rustc_hir_typeck::check::check_fn
  11: rustc_hir_typeck::typeck_with_inspect
      [... omitted 2 frames ...]
  12: rustc_middle::query::calls::TyCtxtEnsureOk::typeck
  13: rustc_middle::hir::map::<impl rustc_middle::ty::context::TyCtxt>::par_hir_body_owners::{{closure}}
  14: rustc_data_structures::sync::parallel::par_for_each_in
  15: rustc_hir_analysis::check_crate
  16: rustc_interface::passes::analysis
      [... omitted 2 frames ...]
  17: std::thread::local::LocalKey<T>::with
  18: rustc_middle::ty::context::TyCtxt::create_global_ctxt
  19: rustc_interface::passes::create_and_enter_global_ctxt
  20: scoped_tls::ScopedKey<T>::set
  21: rustc_span::create_session_globals_then
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

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-bors

This comment has been minimized.

type Consts: Copy + Debug + Hash + Eq + SliceLike<Item = Const<Self>> + Default;
type ParamConst: Copy + Debug + Hash + Eq + ParamLike;
type ValueConst: ValueConst<Self>;
type ValueConst: ValueConst<Self> + TypeFoldable<Self> + Display;

@lcnr lcnr Sep 17, 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.

and this bound, instead of a type_visible(skip)

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.

I'm not sure I follow here?

The type needs to be foldable so we can implement TypeSuperFoldable<I> for Const<I>, which is done manually. I've grepped the codebase and can't find type_visible, is this a new thing that I should add?

If I remove TypeFoldable<Self> declaring the type as;

type ValueConst: ValueConst<Self> + Display;

And remove the branch in the code for folding ValueConst I get an error (below). Maybe there's something I'm missing?

error: internal compiler error: compiler/rustc_borrowck/src/universal_regions.rs:970:36: cannot convert `'{erased}` to a region vid


thread 'rustc' (280854) panicked at compiler/rustc_borrowck/src/universal_regions.rs:970:36:
cannot convert `'{erased}` to a region vid
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: rustc_span::macros::bug_impl
   3: rustc_borrowck::universal_regions::UniversalRegionIndices::to_region_vid::{{closure}}
   4: rustc_borrowck::universal_regions::UniversalRegions::to_region_vid
   5: <rustc_borrowck::type_check::relate_tys::NllTypeRelating as rustc_type_ir::relate::TypeRelation<rustc_middle::ty::context::TyCtxt>>::regions
   6: rustc_type_ir::relate::structurally_relate_tys
   7: rustc_type_ir::relate::combine::super_combine_tys
   8: <rustc_borrowck::type_check::relate_tys::NllTypeRelating as rustc_type_ir::relate::TypeRelation<rustc_middle::ty::context::TyCtxt>>::tys
   9: rustc_borrowck::type_check::relate_tys::<impl rustc_borrowck::type_check::TypeChecker>::relate_types
  10: <rustc_borrowck::type_check::TypeChecker as rustc_middle::mir::visit::Visitor>::visit_terminator
  11: <rustc_borrowck::type_check::TypeChecker as rustc_middle::mir::visit::Visitor>::visit_body
  12: rustc_borrowck::type_check::type_check
  13: rustc_borrowck::borrowck_collect_region_constraints
  14: rustc_borrowck::root_cx::BorrowCheckRootCtxt::do_mir_borrowck
  15: rustc_borrowck::mir_borrowck
      [... omitted 2 frames ...]
  16: rustc_middle::query::calls::query_ensure_ok
  17: rustc_middle::hir::map::<impl rustc_middle::ty::context::TyCtxt>::par_hir_body_owners::{{closure}}
  18: rustc_data_structures::sync::parallel::par_for_each_in
  19: rustc_session::utils::<impl rustc_session::session::Session>::time
  20: rustc_interface::passes::analysis
      [... omitted 2 frames ...]
  21: std::thread::local::LocalKey<T>::with
  22: rustc_middle::ty::context::TyCtxt::create_global_ctxt
  23: rustc_interface::passes::create_and_enter_global_ctxt
  24: scoped_tls::ScopedKey<T>::set
  25: rustc_span::create_session_globals_then
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: the compiler unexpectedly panicked. This is a bug

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: please attach the file at `/data_nvme1n1/Documents/aux/rust/rustc-ice-2026-09-17T13_20_45-280839.txt` to your bug report

note: rustc 1.100.0-dev running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -Z embed-metadata=no -C opt-level=3 -C embed-bitcode=no -C codegen-units=1 -C embed-bitcode=yes -Z unstable-options -C force-frame-pointers=non-leaf -C debug-assertions=on -C overflow-checks=off -C symbol-mangling-version=legacy -Z annotate-moves -Z unstable-options -Z macro-backtrace -C split-debuginfo=off -C force-frame-pointers=true -C prefer-dynamic -C llvm-args=-import-instr-limit=10 -Z inline-mir -Z inline-mir-preserve-debug -Z mir_strip_debuginfo=locals-in-tiny-functions -C link-args=-Wl,-z,origin -C link-args=-Wl,-rpath,$ORIGIN/../lib -C lto=off -Z crate-attr=doc(html_root_url="https://doc.rust-lang.org/nightly/") -Z binary-dep-depinfo -Z force-unstable-if-unmarked

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [mir_borrowck] borrow-checking `str::traits::<impl at library/core/src/str/traits.rs:897:1: 897:22>::from_str`
#1 [analysis] running analysis passes on crate `core`
end of query stack
error: could not compile `core` (lib)

@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.

final nits, then r=me

View changes since this review

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

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. PG-exploit-mitigations Project group: Exploit mitigations 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. 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