Repository navigation
Always try to evaluate default field values and lint if it is too generic #163235
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
Open
estebank
wants to merge
8
commits into
rust-lang:main
Choose a base branch
from
estebank:default-field-values-lint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aff8f13
Always try to evaluate default field values and lint if it is too gen…
estebank 282b810
Suggest `expect` instead of `allow`, so that the annotation is only p…
estebank 3000f74
Use proposed strategy of never evaluating default field values for ty…
estebank 1435c41
review comments
estebank 702470b
Update test comments
estebank 1788cb4
Add more cases to test
estebank c9ed30f
Eager "requires mono" check
estebank 4531f72
Account for macro calls in default field values
estebank 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
37 changes: 37 additions & 0 deletions
37
tests/ui/structs/default-field-values/field-references-param-accurate-span.rs
This file contains hidden or 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,37 @@ | ||
| //@ build-fail | ||
| // https://github.com/rust-lang/rust/issues/146496 | ||
| #![feature(default_field_values)] | ||
|
|
||
| struct Z<const X: usize> { | ||
| // Ensure that proper context is shown in lint. | ||
| multiline_field: | ||
| () | ||
| = { //~ WARN default value | ||
| f::<X>(); | ||
| panic!(); | ||
| //~^ ERROR: explicit panic | ||
| //~| ERROR: explicit panic | ||
| }, | ||
| } | ||
|
|
||
| pub const fn f<const N: usize>() { | ||
| // *If* we const evaluated `Z.multiline_field` at definition, but then bailed because `f` needs | ||
| // to be const evaluated, commenting out this line would suddenly allow `f` to be evaluated and | ||
| // cause the panic in `multiline_field` to be reached. | ||
| let _ = [0u8; N]; | ||
| } | ||
|
|
||
| const fn const_use_generically<const X: usize>() { | ||
| let x: Z<X> = Z { .. }; | ||
| } | ||
|
|
||
| fn use_generically<const X: usize>() { | ||
| let x: Z<X> = Z { .. }; | ||
| } | ||
|
|
||
| fn main() { | ||
| let x: Z<1> = Z { .. }; | ||
| use_generically::<2>(); | ||
| const_use_generically::<3>(); | ||
| const { const_use_generically::<4>() }; | ||
| } |
73 changes: 73 additions & 0 deletions
73
tests/ui/structs/default-field-values/field-references-param-accurate-span.stderr
This file contains hidden or 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,73 @@ | ||
| warning: field `multiline_field` has a default value that is only checked when a value of `Z` is constructed | ||
| --> $DIR/field-references-param-accurate-span.rs:9:15 | ||
| | | ||
| LL | struct Z<const X: usize> { | ||
| LL | // Ensure that proper context is shown in lint. | ||
| LL | multiline_field: | ||
| LL | () | ||
| LL | = { | ||
| | _______________^ | ||
| LL | | f::<X>(); | ||
| LL | | panic!(); | ||
| LL | | | ||
| LL | | | ||
| LL | | }, | ||
| | |_____________^ this can't be const-evaluated until use | ||
| | | ||
| = help: structs and enums with type and const parameters only evaluate their default field values during construction, not eagerly when declared | ||
| = note: `#[warn(unevaluated_default_field_value)]` on by default | ||
| help: if this behavior is acceptable, allow the lint and preferably write a test relying on the default value | ||
| | | ||
| LL + #[expect(unevaluated_default_field_value)] | ||
| LL | struct Z<const X: usize> { | ||
| | | ||
|
|
||
| error[E0080]: evaluation panicked: explicit panic | ||
| --> $DIR/field-references-param-accurate-span.rs:11:17 | ||
| | | ||
| LL | panic!(); | ||
| | ^^^^^^^^ evaluation of `Z::<1>::multiline_field::{constant#0}` failed here | ||
|
|
||
| note: erroneous constant encountered | ||
| --> $DIR/field-references-param-accurate-span.rs:33:19 | ||
| | | ||
| LL | let x: Z<1> = Z { .. }; | ||
| | ^^^^^^^^ | ||
|
|
||
| note: erroneous constant encountered | ||
| --> $DIR/field-references-param-accurate-span.rs:33:19 | ||
| | | ||
| LL | let x: Z<1> = Z { .. }; | ||
| | ^^^^^^^^ | ||
| | | ||
| = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
|
||
| error[E0080]: evaluation panicked: explicit panic | ||
| --> $DIR/field-references-param-accurate-span.rs:11:17 | ||
| | | ||
| LL | panic!(); | ||
| | ^^^^^^^^ evaluation of `Z::<4>::multiline_field::{constant#0}` failed here | ||
|
|
||
| note: erroneous constant encountered | ||
| --> $DIR/field-references-param-accurate-span.rs:25:19 | ||
| | | ||
| LL | let x: Z<X> = Z { .. }; | ||
| | ^^^^^^^^ | ||
|
|
||
| note: erroneous constant encountered | ||
| --> $DIR/field-references-param-accurate-span.rs:36:5 | ||
| | | ||
| LL | const { const_use_generically::<4>() }; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| note: erroneous constant encountered | ||
| --> $DIR/field-references-param-accurate-span.rs:36:5 | ||
| | | ||
| LL | const { const_use_generically::<4>() }; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
|
||
| error: aborting due to 2 previous errors; 1 warning emitted | ||
|
|
||
| For more information about this error, try `rustc --explain E0080`. |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.