-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Resolve long compile times when evaluating always valid constants #67667
Merged
bors
merged 1 commit into
rust-lang:master
from
wesleywiser:speed_up_trivially_valid_constants
Dec 30, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/test/ui/consts/const-eval/validate_uninhabited_zsts.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#![feature(const_fn)] | ||
#![feature(const_transmute)] | ||
|
||
const fn foo() -> ! { | ||
unsafe { std::mem::transmute(()) } | ||
//~^ WARN any use of this value will cause an error [const_err] | ||
//~| WARN the type `!` does not permit zero-initialization [invalid_value] | ||
} | ||
|
||
#[derive(Clone, Copy)] | ||
enum Empty { } | ||
|
||
#[warn(const_err)] | ||
const FOO: [Empty; 3] = [foo(); 3]; | ||
|
||
#[warn(const_err)] | ||
const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; | ||
//~^ ERROR it is undefined behavior to use this value | ||
//~| WARN the type `Empty` does not permit zero-initialization | ||
|
||
fn main() { | ||
FOO; | ||
BAR; | ||
} |
52 changes: 52 additions & 0 deletions
52
src/test/ui/consts/const-eval/validate_uninhabited_zsts.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
warning: any use of this value will cause an error | ||
--> $DIR/validate_uninhabited_zsts.rs:5:14 | ||
| | ||
LL | unsafe { std::mem::transmute(()) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| entering unreachable code | ||
| inside call to `foo` at $DIR/validate_uninhabited_zsts.rs:14:26 | ||
... | ||
LL | const FOO: [Empty; 3] = [foo(); 3]; | ||
| ----------------------------------- | ||
| | ||
note: lint level defined here | ||
--> $DIR/validate_uninhabited_zsts.rs:13:8 | ||
| | ||
LL | #[warn(const_err)] | ||
| ^^^^^^^^^ | ||
|
||
error[E0080]: it is undefined behavior to use this value | ||
--> $DIR/validate_uninhabited_zsts.rs:17:1 | ||
| | ||
LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type | ||
| | ||
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. | ||
|
||
warning: the type `!` does not permit zero-initialization | ||
--> $DIR/validate_uninhabited_zsts.rs:5:14 | ||
| | ||
LL | unsafe { std::mem::transmute(()) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| this code causes undefined behavior when executed | ||
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done | ||
| | ||
= note: `#[warn(invalid_value)]` on by default | ||
= note: The never type (`!`) has no valid value | ||
|
||
warning: the type `Empty` does not permit zero-initialization | ||
--> $DIR/validate_uninhabited_zsts.rs:17:35 | ||
| | ||
LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| this code causes undefined behavior when executed | ||
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done | ||
| | ||
= note: 0-variant enums have no valid value | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// build-pass | ||
// ignore-32bit | ||
|
||
#[derive(Clone, Copy)] | ||
struct Foo; | ||
|
||
fn main() { | ||
let _ = [(); 4_000_000_000]; | ||
let _ = [0u8; 4_000_000_000]; | ||
let _ = [Foo; 4_000_000_000]; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
side note: if you remove this constant and its use, you do get a hard error on the use of
FOO
, butBAR
is what we are actually interested in testing here, because it causes validation to error instead of const eval.