Skip to content

Distinguish null reference production from null pointer dereference in UB checks#158796

Open
raushan728 wants to merge 1 commit into
rust-lang:mainfrom
raushan728:issues/154044
Open

Distinguish null reference production from null pointer dereference in UB checks#158796
raushan728 wants to merge 1 commit into
rust-lang:mainfrom
raushan728:issues/154044

Conversation

@raushan728

@raushan728 raushan728 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Closes #154044

This change distinguishes null reference production from actual null pointer dereferences in the UB checks.

Producing a reference from a null pointer (for example, &*ptr) is undefined behavior even if no memory is dereferenced. This change introduces a dedicated AssertKind::NullReferenceConstructed and runtime panic so these cases produce a more accurate diagnostic while preserving the existing panic for actual null pointer dereferences.

Summary

  • Add AssertKind::NullReferenceConstructed.
  • Emit it for null reference production in the MIR null check pass.
  • Add panic_null_reference_constructed and its lang item.
  • Keep NullPointerDereference for actual dereferences.
  • Update the affected UI tests.

@rustbot

rustbot commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @vakaras

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 the CTFE machinery

cc @RalfJung, @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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 5, 2026
@rustbot

rustbot commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

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

@rustbot

rustbot commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Requested reviewer is already assigned to this pull request.

Please choose another assignee.

Comment thread tests/ui/mir/null/borrowed_mut_null.rs Outdated
//@ run-crash
//@ compile-flags: -C debug-assertions
//@ error-pattern: null pointer dereference occurred
//@ error-pattern: null reference formed

@RalfJung RalfJung Jul 5, 2026

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.

"formed" is not a verb we use for this anywhere else I think. The reference calls it "producing" an invalid value, maybe use that?

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.

thanks! i'll update the message to use produced to exact match the terminology.

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.

done!

Introduce a dedicated panic for null reference production to distinguish
producing a reference from a null pointer from an actual null pointer
dereference.

This emits a dedicated panic message for null reference production while
preserving the existing panic for actual dereferences.
@raushan728 raushan728 changed the title Distinguish null reference formation from null pointer dereference in UB checks Distinguish null reference production from null pointer dereference in UB checks Jul 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic message for forming null reference in debug builds misleading

4 participants