Skip to content

Move rustc_middle::ty::Const to rustc_type_ir Part 2 - #163258

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

Jamesbarford wants to merge 8 commits into
rust-lang:mainfrom
Jamesbarford:chore/move-const-pt2

Conversation

@Jamesbarford

@Jamesbarford Jamesbarford commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Split by commit;

  • Move all methods from ConstExt from rustc_middle, creating small helpers to aid this. Subsequently delete ConstExt.
  • Remove ConstExt from imports in compiler.
  • Remove ConstExt from imports in clippy.

r? lcnr

@rustbot

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

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in rustc_ty_utils::consts.rs

cc @BoxyUwU

Some changes occurred in exhaustiveness checking

cc @Nadrieril

Some changes occurred in match checking

cc @Nadrieril

HIR ty lowering was modified

cc @fmease

Some changes occurred in match lowering

cc @Nadrieril

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

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 / Miri interpreter

cc @rust-lang/miri

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

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

@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. labels Sep 24, 2026
@rust-log-analyzer

This comment has been minimized.

@RalfJung

Copy link
Copy Markdown
Member

Move Const from rustc_middle to rustc_type_ir

This is ty::Const, not mir::Const, right? Would be good to clarify so these PRs are easier to interpret. :)

@Jamesbarford Jamesbarford changed the title Move Const from rustc_middle to rustc_type_ir Part 2 Move rustc_middle::ty::Const to rustc_type_ir Part 2 Sep 25, 2026
Comment thread compiler/rustc_middle/src/ty/consts/valtree.rs
let valtree =
ty::ValTree::from_scalar_int(tcx, ScalarInt::try_from_uint(bits, size).unwrap());
ty::Const::new_value(tcx, valtree, ty)
}

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

this function is kind of scuffed :< I don't want this in rustc_type_ir if I am honest. More generally TypingEnv is in a weird state and I'd like to keep this out of rustc_type_ir for now 🤔 can we maybe keep this in an extension trait for now?

View changes since the review

@Jamesbarford Jamesbarford Sep 29, 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.

Hmm what about on Interner;

fn const_from_bits(self, bits: u128, typing_env: Self::TypingEnv, ty: Self::Ty) -> Const<Self>;

Then keep the implementation in the rustc_middle's interner implementation? That way we don't need to resurrect the ConstExt and the scuffed parts stay in rustc_middle? Obviously happy to revert if that's a terrible idea.

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've given it a go in this commit; 39c689e, doesn't feel too bad as it uses a few interner methods

Comment thread compiler/rustc_type_ir/src/inherent.rs Outdated
} else {
const_v.try_to_leaf().map(|s| s.to_target_usize(self))
}
}

@lcnr lcnr Sep 25, 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 exactly does this need to be on the interner? I guess the to_target_usize is the actually relevant part? 🤔

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.

Yes s.to_target_usize() to the relevant part. I've now made to_target_usize a method on ValueConst in inherent.rs. Could be less bad than bolting another helper method on to Interner?

I've done so in; bcc98fa

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

lgtm modulo minor nits

View changes since this review

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_codegen_llvm/src/debuginfo

cc @Walnut356

@rustbot

rustbot commented Oct 6, 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-log-analyzer

This comment has been minimized.

@@ -228,13 +227,21 @@ impl<'tcx> Value<'tcx> {
}

impl<'tcx> rustc_type_ir::inherent::ValueConst<TyCtxt<'tcx>> for Value<'tcx> {

@lcnr lcnr Oct 6, 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.

future work, we should move Value into rustc_type_ir

View changes since the review

@lcnr

lcnr commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

my only question is const_from_bits /Const::from_bits. If this method only uses rustc_type_ir internal stuff, maybe we should have it on Const after all 🤔

Why do we need the TypingEnv extension trait again?

this is partially out of cache for me unfortunately 🤔

@Jamesbarford

Copy link
Copy Markdown
Contributor Author

const_from_bits

  • const_from_bits exists so we aren't using TypingEnv in rustc_type_ir though maybe we could put something on the inherent trait for it to implement in rustc_middle?
  • The inherent trait is for I::TypingEnv::fully_monomorphized() so we can use it in Const::from_bool(...) and Const::from_target_usize(...)

@lcnr

lcnr commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

That's inconsistent, is it not. If we need to use TypingEnv::fully_monomorphized for two other functions, then const_from_bits being outside to avoid that doesn't really work, does it?

Either have all these functions outside of rustc_type_ir or all of them in it I'd say

@Jamesbarford

Copy link
Copy Markdown
Contributor Author

That's inconsistent, is it not. If we need to use TypingEnv::fully_monomorphized for two other functions, then const_from_bits being outside to avoid that doesn't really work, does it?

Either have all these functions outside of rustc_type_ir or all of them in it I'd say

Good point, I've made a fn layout_size(...) on trait TypingEnv and moved fn from_bits(...) 0bf6c48

@rust-bors

rust-bors Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #164052) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants