Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply noundef attribute to all scalar types which do not permit raw init #94157

Merged
merged 2 commits into from
Feb 28, 2022

Conversation

erikdesjardins
Copy link
Contributor

Beyond &/&mut/Box, this covers char, enum discriminants, NonZero*, etc.
All such types currently cause a Miri error if left uninitialized,
and an invalid_value lint in cases like mem::uninitialized::<char>().

Note that this does not change whether or not it is UB for u64 (or
other integer types with no invalid values) to be undef.

Fixes (partially) #74378.

r? @ghost (blocked on #94127)

@rustbot label S-blocked

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. labels Feb 19, 2022
@nikic
Copy link
Contributor

nikic commented Feb 26, 2022

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 26, 2022
@bors
Copy link
Contributor

bors commented Feb 26, 2022

⌛ Trying commit fa8c9719d2749752db1d388d043c177f9b6eb62a with merge f71dbc524c99e197ea8d54be1a5801bdab7c37aa...

@bors
Copy link
Contributor

bors commented Feb 26, 2022

☀️ Try build successful - checks-actions
Build commit: f71dbc524c99e197ea8d54be1a5801bdab7c37aa (f71dbc524c99e197ea8d54be1a5801bdab7c37aa)

@rust-timer
Copy link
Collaborator

Queued f71dbc524c99e197ea8d54be1a5801bdab7c37aa with parent 8c9640e, future comparison URL.

compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
src/test/codegen/call-metadata.rs Show resolved Hide resolved
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f71dbc524c99e197ea8d54be1a5801bdab7c37aa): comparison url.

Summary: This benchmark run did not return any relevant results. 6 results were found to be statistically significant but too small to be relevant.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR led to changes in compiler perf.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf -perf-regression

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Feb 26, 2022
Beyond `&`/`&mut`/`Box`, this covers `char`, discriminants, `NonZero*`, etc.
All such types currently cause a Miri error if left uninitialized,
and an `invalid_value` lint in cases like `mem::uninitialized::<char>()`

Note that this _does not_ change whether or not it is UB for `u64` (or
other integer types with no invalid values) to be undef.
@nikic
Copy link
Contributor

nikic commented Feb 26, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Feb 26, 2022

📌 Commit 5979b68 has been approved by nikic

@bors bors 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. S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. labels Feb 26, 2022
@bors
Copy link
Contributor

bors commented Feb 27, 2022

⌛ Testing commit 5979b68 with merge d77ce1b08ed049da00de734d8928cf3166b77210...

@bors
Copy link
Contributor

bors commented Feb 27, 2022

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 27, 2022
@rust-log-analyzer

This comment has been minimized.

@nikic
Copy link
Contributor

nikic commented Feb 27, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Feb 27, 2022

📌 Commit 45ee3fc has been approved by nikic

@bors bors 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 Feb 27, 2022
@bors
Copy link
Contributor

bors commented Feb 27, 2022

⌛ Testing commit 45ee3fc with merge 9fbff89...

@bors
Copy link
Contributor

bors commented Feb 28, 2022

☀️ Test successful - checks-actions
Approved by: nikic
Pushing 9fbff89 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 28, 2022
@bors bors merged commit 9fbff89 into rust-lang:master Feb 28, 2022
@rustbot rustbot added this to the 1.61.0 milestone Feb 28, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9fbff89): comparison url.

Summary: This benchmark run shows 5 relevant improvements 🎉 to instruction counts.

  • Arithmetic mean of relevant improvements: -1.0%
  • Largest improvement in instruction counts: -1.7% on incr-full builds of coercions debug

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

@@ -3053,6 +3053,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
return;
}

// Scalars which have invalid values cannot be undef.
if !scalar.is_always_valid(self) {
attrs.set(ArgAttribute::NoUndef);
Copy link
Member

Choose a reason for hiding this comment

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

With this logic, can't we remove some of the logic from #93670 since those types also are "not always valid"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially thought the same, but then we don't put noundef on Option<&T/&mut T/Box<T>>>, since they have no invalid values.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I didn't realize those would get the attribute.

OTOH that still means we are missing Option<NonZero*>, so maybe there's a more general way to handle this by special-casing enums? Like, if the scalar has Multiple layout, then that one scalar contains its discriminant and hence we can definitely set NoUndef? (This might miss newtypes around such enums, not sure if that's a problem.)

Copy link
Contributor Author

@erikdesjardins erikdesjardins Mar 4, 2022

Choose a reason for hiding this comment

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

You're right.

I'm a bit hesistant to add more complex logic here. I think the missing cases are fairly rare, just Option<NonZero*/NonNull> (and equivalent types), and enums with exactly 256 (or 65536, etc.) variants. In the long run we can switch to the uninit tracking added by #94527 and handle these cases without having to inspect the shape of the layout at all.

Copy link
Member

Choose a reason for hiding this comment

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

Sure. :) I just hope we won't forget to clean up that duplication of logic in two different places ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

7 participants