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.

@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

@lcnr

lcnr commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

@bors r+ rollup=iffy

@rust-bors

rust-bors Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 0a7563b 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-review Status: Awaiting review from the assignee but also interested parties. labels Sep 22, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 22, 2026
… r=lcnr

Move `Const` from `rustc_middle` to `rustc_type_ir`

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
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 22, 2026
… r=lcnr

Move `Const` from `rustc_middle` to `rustc_type_ir`

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
@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 22, 2026
@rust-bors

This comment has been minimized.

@lcnr

lcnr commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

@bors delegate+

@rust-bors

rust-bors Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

✌️ @Jamesbarford, you can now approve this pull request!

If @lcnr told you to "r=me" after making some further change, then please make that change and post @bors r=lcnr.

View changes since this delegation.

@rustbot

rustbot commented Sep 22, 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.

@Jamesbarford

Copy link
Copy Markdown
Contributor Author

@bors r=lcnr

@rust-bors

rust-bors Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 5e56705 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 Sep 22, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 22, 2026
… r=lcnr

Move `Const` from `rustc_middle` to `rustc_type_ir`

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
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 22, 2026
… r=lcnr

Move `Const` from `rustc_middle` to `rustc_type_ir`

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
rust-bors Bot pushed a commit that referenced this pull request Sep 22, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #162628 (Move `Const` from `rustc_middle` to `rustc_type_ir`)
 - #159589 (Avoid leaking opaque hidden types via auto trait candidates)
 - #147790 (constify comparison traits on sliced types)
 - #162325 (powerpc64-ibm-aix: fix cfg(target_abi) value)
 - #162786 (regression test for GCE inherent projection ICE)
 - #163065 (windows Dir::rename: remove incorrect is_dir query)
 - #163138 (feat(time): add `Duration::{WEEK, DAY, HOUR, MINUTE}`)
 - #163162 (Remove unused `make3.sh` CI script)

This branch has not been deployed

No deployments
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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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