-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
check if len of array const arg matches the expected len of the type when lowering to valtree #158587
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
Merged
+119
−3
Merged
check if len of array const arg matches the expected len of the type when lowering to valtree #158587
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //@ known-bug: #160553 | ||
| //@ compile-flags: -Copt-level=0 | ||
| #![allow(incomplete_features)] | ||
| #![feature(adt_const_params, min_generic_const_args, macroless_generic_const_args)] | ||
| #![feature(generic_const_parameter_types)] | ||
|
|
||
| trait Trait { | ||
| type const LEN: usize; | ||
| } | ||
|
|
||
| struct S; | ||
| impl Trait for S { | ||
| type const LEN: usize = 2; | ||
| } | ||
|
|
||
| fn foo<T: Trait, const A: [u8; <T as Trait>::LEN]>() -> [u8; <T as Trait>::LEN] { | ||
| A | ||
| } | ||
|
|
||
| fn bar<T: Trait>() -> [u8; <T as Trait>::LEN] { | ||
| foo::<T, { [1, 2, 3] }>() | ||
| } | ||
|
|
||
| fn main() { | ||
| bar::<S>(); | ||
| } |
39 changes: 39 additions & 0 deletions
39
tests/ui/const-generics/mgca/array-const-arg-len-mismatch.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,39 @@ | ||
| //! Regression test for #155168 | ||
| //! | ||
| //! Ensure that providing an array const arg with the wrong number of elements | ||
| //! doesn't ICE or silently cause UB. | ||
| #![expect(incomplete_features)] | ||
| #![feature(adt_const_params, min_generic_const_args, macroless_generic_const_args)] | ||
| #![feature(unsized_const_params, generic_const_parameter_types)] | ||
|
|
||
| use std::marker::ConstParamTy_; | ||
|
|
||
| fn foo<T: ConstParamTy_, const N: usize, const M: [T; N]>() -> [T; N] { | ||
| M | ||
| } | ||
|
|
||
| fn bar<const A: [u8; 2]>() {} | ||
|
|
||
| trait Trait { | ||
| type const LEN: usize; | ||
| } | ||
|
|
||
| struct S; | ||
| impl Trait for S { | ||
| type const LEN: usize = 3; | ||
| } | ||
|
|
||
| fn baz<const A: [u8; <S as Trait>::LEN]>() {} | ||
|
|
||
| fn main() { | ||
| foo::<u8, 2, { [] }>(); | ||
| //~^ ERROR: expected array with 2 elements, found 0 elements | ||
| foo::<u8, 2, { [0, 0, 0] }>(); | ||
| //~^ ERROR: expected array with 2 elements, found 3 elements | ||
| bar::<{ [] }>(); | ||
| //~^ ERROR: expected array with 2 elements, found 0 elements | ||
| bar::<{ [1, 2, 3] }>(); | ||
| //~^ ERROR: expected array with 2 elements, found 3 elements | ||
| baz::<{ [42] }>(); | ||
| //~^ ERROR: expected array with 3 elements, found 1 elements | ||
| } |
32 changes: 32 additions & 0 deletions
32
tests/ui/const-generics/mgca/array-const-arg-len-mismatch.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,32 @@ | ||
| error: expected array with 2 elements, found 0 elements | ||
| --> $DIR/array-const-arg-len-mismatch.rs:29:20 | ||
| | | ||
| LL | foo::<u8, 2, { [] }>(); | ||
| | ^^ | ||
|
|
||
| error: expected array with 2 elements, found 3 elements | ||
| --> $DIR/array-const-arg-len-mismatch.rs:31:20 | ||
| | | ||
| LL | foo::<u8, 2, { [0, 0, 0] }>(); | ||
| | ^^^^^^^^^ | ||
|
|
||
| error: expected array with 2 elements, found 0 elements | ||
| --> $DIR/array-const-arg-len-mismatch.rs:33:13 | ||
| | | ||
| LL | bar::<{ [] }>(); | ||
| | ^^ | ||
|
|
||
| error: expected array with 2 elements, found 3 elements | ||
| --> $DIR/array-const-arg-len-mismatch.rs:35:13 | ||
| | | ||
| LL | bar::<{ [1, 2, 3] }>(); | ||
| | ^^^^^^^^^ | ||
|
|
||
| error: expected array with 3 elements, found 1 elements | ||
| --> $DIR/array-const-arg-len-mismatch.rs:37:13 | ||
| | | ||
| LL | baz::<{ [42] }>(); | ||
| | ^^^^ | ||
|
|
||
| error: aborting due to 5 previous errors | ||
|
|
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.
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.
IMO it should catch more cases if len is normalized, e.g.
type const LEN(I haven't checked deeply so it may be caught by somewhere already)View changes since the review
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.
Thanks, I changed it to call
try_normalize_erasing_regionswith an emptyParamEnvon the expected len and added a test for it, so now something likefn baz<const A: [u8; <S as Trait>::LEN]>() {}produces the correct error. Something likefn baz<T: Trait, const A: [u8; <T as Trait>::LEN]>() {}still ICEs if too long/produces UB if too short, however, since we can't resolveTto an actual type: trying to dotry_evaluate_constwith a non-emptyParamEnvled to cycle errors.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.
Could you open another issue and add an ICE test for that case (if not exists yet)? At least we should track it.
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.
Opened #160553 and added a corresponding test to the
crashes/dir