Skip to content

Rip out rustc_layout_scalar_valid_range_* attribute support#155433

Open
oli-obk wants to merge 6 commits intorust-lang:mainfrom
oli-obk:bye-bye-long-attribute
Open

Rip out rustc_layout_scalar_valid_range_* attribute support#155433
oli-obk wants to merge 6 commits intorust-lang:mainfrom
oli-obk:bye-bye-long-attribute

Conversation

@oli-obk
Copy link
Copy Markdown
Contributor

@oli-obk oli-obk commented Apr 17, 2026

View all comments

And either removes tests for it or replaces the uses with pattern types.

primarily fixes #135996

fixes #147761
fixes #133652

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 17, 2026

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred to the CTFE machinery

cc @RalfJung, @lcnr

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

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

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

This PR changes a file inside tests/crashes. If a crash was fixed, please move into the corresponding ui subdir and add 'Fixes #' to the PR description to autoclose the issue upon merge.

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. labels Apr 17, 2026
@rustbot rustbot added 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. labels Apr 17, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 17, 2026

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 72 candidates
  • Random selection from 18 candidates

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

Happy to review this
Love the branch name 🤣

r? me

Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

Please revert the rust-analyzer changes. r-a needs to support old compilers, and also it doesn't even properly support pattern types yet.

View changes since this review

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 17, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 17, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 17, 2026
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the bye-bye-long-attribute branch from c6053aa to cc13e2c Compare April 17, 2026 19:20
@mejrs
Copy link
Copy Markdown
Contributor

mejrs commented Apr 17, 2026

There is some prose in the dev guide that will no longer apply.

The other complicated safety check is for writes to fields of layout constrained structs (such as NonNull). These are found by looking for the borrow or assignment expression and then visiting the subexpression being borrowed or assigned with a separate visitor.

https://rustc-dev-guide.rust-lang.org/unsafety-checking.html?other-checks-involving-unsafe

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@oli-obk oli-obk force-pushed the bye-bye-long-attribute branch from cc13e2c to 663f77e Compare April 21, 2026 07:08
@rustbot

This comment has been minimized.

@oli-obk oli-obk force-pushed the bye-bye-long-attribute branch from 663f77e to c01a244 Compare April 21, 2026 07:09
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 21, 2026

The rustc-dev-guide subtree was changed. If this PR only touches the dev guide consider submitting a PR directly to rust-lang/rustc-dev-guide otherwise thank you for updating the dev guide with your changes.

cc @BoxyUwU, @tshepang

@rustbot rustbot added the A-rustc-dev-guide Area: rustc-dev-guide label Apr 21, 2026
@oli-obk oli-obk force-pushed the bye-bye-long-attribute branch from c01a244 to 07e6d54 Compare April 21, 2026 07:18
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the bye-bye-long-attribute branch from 07e6d54 to cbd6780 Compare April 21, 2026 09:42
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the bye-bye-long-attribute branch from cbd6780 to 4fb465f Compare April 21, 2026 12:16
@rustbot

This comment has been minimized.

Copy link
Copy Markdown
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Comment on lines +1513 to +1520
// *After* all of this, check further information stored in the layout.
// On leaf types like `!` or empty enums, this will raise the error.
// This means that for types wrapping such a type, we won't ever get here, but it's
// just the simplest way to check for this case.
//
// FIXME: We could avoid some redundant checks here. For newtypes wrapping
// scalars, we do the same check on every "level" (e.g., first we check
// MyNewtype and then the scalar in there).
// the fields of MyNewtype, and then we check MyNewType again).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems mostly outdated now, and the FIXME is resolved because there are no longer any redundant checks.

The only question that remains is why we need an is_uninhabited check here. Can this even be reached? Maybe it can be turned into an assertion that just double-checks that indeed we caught all uninhabited types above?

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.

the uninhabited check is still reachable and it's sufficiently unrelated to pattern types and layout attributes that I've decided to leave the fixme and try it in a separate PR once this one lands

Comment on lines -1498 to -1528
match val.layout.backend_repr {
BackendRepr::Scalar(scalar_layout) => {
if !scalar_layout.is_uninit_valid() {
// There is something to check here.
// We read directly via `ecx` since the read cannot fail -- we already read
// this field above when recursing into the field.
let scalar = self.ecx.read_scalar(val)?;
self.visit_scalar(scalar, scalar_layout)?;
}
}
BackendRepr::ScalarPair(a_layout, b_layout) => {
// We can only proceed if *both* scalars need to be initialized.
// FIXME: find a way to also check ScalarPair when one side can be uninit but
// the other must be init.
if !a_layout.is_uninit_valid() && !b_layout.is_uninit_valid() {
// We read directly via `ecx` since the read cannot fail -- we already read
// this field above when recursing into the field.
let (a, b) = self.ecx.read_immediate(val)?.to_scalar_pair();
self.visit_scalar(a, a_layout)?;
self.visit_scalar(b, b_layout)?;
}
}
BackendRepr::SimdVector { .. } | BackendRepr::SimdScalableVector { .. } => {
// No checks here, we assume layout computation gets this right.
// (This is harder to check since Miri does not represent these as `Immediate`. We
// also cannot use field projections since this might be a newtype around a vector.)
}
BackendRepr::Memory { .. } => {
// Nothing to do.
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This crucially relies on the fact that layout computation will never make a type's layout more restrictive than a field. Is there some way we can debug-assert this to avoid soundness bugs in the future where we emit range information to LLVM that Miri never checks?

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 readded it as an assertion for now, there's probably something we can do nicer

// we won't see optimizations actually breaking such programs.
ty::PatternKind::Or(_patterns) => {}
}
match val.layout.backend_repr {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
match val.layout.backend_repr {
// FIXME(pattern_types): Instead of checking the resulting layout, we should be checking
// the pattern directly.
match val.layout.backend_repr {

Fine for now but structurally it'd make a lot more sense to check the pattern rather than the layout (and then somehow have a sanity check that the layout does not add extra restrictions not covered by the pattern).

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.

it does, but I haven't figured out how to do that without just repeating a lot of logic. Tho I guess we can just not look at whether is_uninit_valid is set, because if your pattern type is the full range of what the base type allows, that's so rare, that the small perf hit is fine to take

@oli-obk oli-obk force-pushed the bye-bye-long-attribute branch from 4fb465f to 28fa32d Compare April 21, 2026 15:28
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

r? RalfJung
Since you seem to be reviewing this already, and I'm a bit busy

@rustbot rustbot assigned RalfJung and unassigned JonathanBrouwer Apr 21, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 21, 2026

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

@RalfJung
Copy link
Copy Markdown
Member

Sorry, that won't work. I have too many other things on my plate already. I only looked at the const-eval part and left some comments related to that.

@rustbot reroll

@rustbot rustbot assigned ShoyuVanilla and unassigned RalfJung Apr 21, 2026
Copy link
Copy Markdown
Member

@ShoyuVanilla ShoyuVanilla left a comment

Choose a reason for hiding this comment

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

The other changes look straightforward to me, though I'm not fully confident about the const-eval part. Overall this looks good, but would it make sense to get a second look from someone more familiar with this area?

View changes since this review

Comment thread tests/ui/consts/const-eval/ub-ref-ptr.rs
@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented Apr 22, 2026

I'm not fully confident about the const-eval part. Overall this looks good, but would it make sense to get a second look from someone more familiar with this area?

Ralf already reviewed the const eval parts and I addressed those

@RalfJung
Copy link
Copy Markdown
Member

Yeah I will take a look at the const-eval parts again to review the revision.

Comment thread src/doc/rustc-dev-guide/src/unsafety-checking.md
@rust-bors

This comment has been minimized.

@oli-obk oli-obk force-pushed the bye-bye-long-attribute branch from 28fa32d to e993ad9 Compare April 22, 2026 12:26
@rustbot
Copy link
Copy Markdown
Collaborator

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

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-rustc-dev-guide Area: rustc-dev-guide S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Projects

None yet

10 participants