Skip to content

const-check: clarify when and how we can rely on non-UB assumptions - #164088

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
RalfJung:const-check-ub
Oct 12, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
RalfJung:const-check-ub

Conversation

@RalfJung

@RalfJung RalfJung commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Turns out const-checking relies on &x being immutable. This is the "most obvious" property we get from the aliasing model, and we emit LLVM attributes and have MIR-opts that rely on it. I just didn't realize that taking away this UB would actually be a breaking change; there is code that runs on stable that we'd have to stop accepting if we took away this UB:

struct NotConstDestruct;

impl Drop for NotConstDestruct {
    fn drop(&mut self) {
        eprintln!("this is not const");
    }
}

const _: () = {
    let x = None::<NotConstDestruct>;
    let ptr = std::ptr::from_ref(&x);
    unsafe { ptr.cast_mut().write(Some(NotConstDestruct)) };
};

This means that I accidentally fixed a hole in const-checking when I added & immutability checking to const-eval.

r? @oli-obk
Cc @rust-lang/opsem @tmiasko

@rustbot

rustbot commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to constck

cc @fee1-dead

Some changes occurred to the CTFE machinery

cc @oli-obk, @lcnr

@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. labels Oct 10, 2026
/// Normally, we would determine what qualifications apply to each type and error when an illegal
/// operation is performed on such a type. However, this was found to be too imprecise, especially
/// in the presence of `enum`s. If only a single variant of an enum has a certain qualification, we
/// needn't reject code unless it actually constructs and operates on the qualified variant.

@tmiasko tmiasko Oct 10, 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 would be an additional example of non-UB assumption. This kind of reasoning presupposes that producing an invalid values don't surprisingly change an active variant.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point! Here's another ICE.

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 also demonstrates that it relies on subobject provenance, since write could have been to any other field.

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

@rust-bors

rust-bors Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 0e50382 has been approved by oli-obk

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 Oct 12, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 12, 2026
Rollup of 5 pull requests

Successful merges:

 - #163981 (New solver opts for `diesel`)
 - #164088 (const-check: clarify when and how we can rely on non-UB assumptions)
 - #164105 (Remove unnecessary indirection around `hir::ConstArg`)
 - #164181 (Tweak output on unused/unaccessible local macro)
 - #164186 (Update actions/upload-artifact action to v7.0.2)
rust-bors Bot pushed a commit that referenced this pull request Oct 12, 2026
Rollup of 5 pull requests

Successful merges:

 - #163981 (New solver opts for `diesel`)
 - #164088 (const-check: clarify when and how we can rely on non-UB assumptions)
 - #164105 (Remove unnecessary indirection around `hir::ConstArg`)
 - #164181 (Tweak output on unused/unaccessible local macro)
 - #164186 (Update actions/upload-artifact action to v7.0.2)
@rust-bors
rust-bors Bot merged commit f2f26bc into rust-lang:main Oct 12, 2026
14 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Oct 12, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 12, 2026
Rollup merge of #164088 - RalfJung:const-check-ub, r=oli-obk

const-check: clarify when and how we can rely on non-UB assumptions

Turns out const-checking relies on `&x` being immutable. This is the "most obvious" property we get from the aliasing model, and we emit LLVM attributes and have MIR-opts that rely on it. I just didn't realize that taking away this UB would actually be a breaking change; there is code that runs on stable that we'd have to stop accepting if we took away this UB:
```rust
struct NotConstDestruct;

impl Drop for NotConstDestruct {
    fn drop(&mut self) {
        eprintln!("this is not const");
    }
}

const _: () = {
    let x = None::<NotConstDestruct>;
    let ptr = std::ptr::from_ref(&x);
    unsafe { ptr.cast_mut().write(Some(NotConstDestruct)) };
};
```
This means that I accidentally fixed a hole in const-checking when I added `&` immutability checking to const-eval.

r? @oli-obk
Cc @rust-lang/opsem @tmiasko
@RalfJung
RalfJung deleted the const-check-ub branch October 15, 2026 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

4 participants