-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Finalize repeat expr inference behaviour with inferred repeat counts #139635
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
BoxyUwU
wants to merge
6
commits into
rust-lang:master
Choose a base branch
from
BoxyUwU:no_order_dependent_copy_checks
base: master
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.
+348
−147
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0378db5
Don't allow repeat expr count inference side effects to propagate
BoxyUwU 5ff95e9
Properly test whether repeat expr checks are pre/post integer fallback
BoxyUwU 3ca085c
GAI logic on stable too
BoxyUwU 99c0909
Check for element being `const` before resolving repeat count
BoxyUwU e71b496
Anon consts cant appear as repeat expr elements
BoxyUwU 6296fda
nits
BoxyUwU 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
72 changes: 72 additions & 0 deletions
72
tests/ui/repeat-expr/copy-check-const-element-uninferred-count.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,72 @@ | ||
#![feature(generic_arg_infer)] | ||
|
||
// Test when deferring repeat expr copy checks to end of typechecking whether elements | ||
// that are const items allow for repeat counts to go uninferred without an error being | ||
// emitted if they would later wind up inferred by integer fallback. | ||
// | ||
// This test should be updated if we wind up deferring repeat expr checks until *after* | ||
// integer fallback as the point of the test is not *specifically* about integer fallback | ||
// but rather about the behaviour of `const` element exprs. | ||
|
||
trait Trait<const N: usize> {} | ||
|
||
// We impl `Trait` for both `i32` and `u32` to avoid being able | ||
// to prove `?int: Trait<?n>` from there only being one impl. | ||
impl Trait<2> for i32 {} | ||
impl Trait<2> for u32 {} | ||
|
||
fn tie_and_make_goal<const N: usize, T: Trait<N>>(_: &T, _: &[String; N]) {} | ||
|
||
fn const_block() { | ||
// Deferred repeat expr `String; ?n` | ||
let a = [const { String::new() }; _]; | ||
|
||
// `?int: Trait<?n>` goal | ||
tie_and_make_goal(&1, &a); | ||
|
||
// If repeat expr checks structurally resolve the `?n`s before checking if the | ||
// element is a `const` then we would error here. Otherwise we avoid doing so, | ||
// integer fallback occurs, allowing `?int: Trait<?n>` goals to make progress, | ||
// inferring the repeat counts (to `2` but that doesn't matter as the element is `const`). | ||
} | ||
|
||
fn const_item() { | ||
const MY_CONST: String = String::new(); | ||
|
||
// Deferred repeat expr `String; ?n` | ||
let a = [MY_CONST; _]; | ||
|
||
// `?int: Trait<?n>` goal | ||
tie_and_make_goal(&1, &a); | ||
|
||
// ... same as `const_block` | ||
} | ||
|
||
fn assoc_const() { | ||
trait Dummy { | ||
const ASSOC: String; | ||
} | ||
impl Dummy for () { | ||
const ASSOC: String = String::new(); | ||
} | ||
|
||
// Deferred repeat expr `String; ?n` | ||
let a = [<() as Dummy>::ASSOC; _]; | ||
|
||
// `?int: Trait<?n>` goal | ||
tie_and_make_goal(&1, &a); | ||
|
||
// ... same as `const_block` | ||
} | ||
|
||
fn const_block_but_uninferred() { | ||
// Deferred repeat expr `String; ?n` | ||
let a = [const { String::new() }; _]; | ||
//~^ ERROR: type annotations needed for `[String; _]` | ||
|
||
// Even if we don't structurally resolve the repeat count as part of repeat expr | ||
// checks, we still error on the repeat count being uninferred as we require all | ||
// types/consts to be inferred by the end of type checking. | ||
} | ||
|
||
fn main() {} |
15 changes: 15 additions & 0 deletions
15
tests/ui/repeat-expr/copy-check-const-element-uninferred-count.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,15 @@ | ||
error[E0284]: type annotations needed for `[String; _]` | ||
--> $DIR/copy-check-const-element-uninferred-count.rs:64:9 | ||
| | ||
LL | let a = [const { String::new() }; _]; | ||
| ^ ---------------------------- type must be known at this point | ||
| | ||
= note: the length of array `[String; _]` must be type `usize` | ||
help: consider giving `a` an explicit type, where the placeholders `_` are specified | ||
| | ||
LL | let a: [_; _] = [const { String::new() }; _]; | ||
| ++++++++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0284`. |
64 changes: 40 additions & 24 deletions
64
tests/ui/repeat-expr/copy-check-deferred-after-fallback.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 |
---|---|---|
@@ -1,37 +1,53 @@ | ||
#![feature(generic_arg_infer)] | ||
|
||
// Test that would start passing if we defer repeat expr copy checks to end of | ||
// typechecking and they're checked after integer fallback occurs. We accomplish | ||
// this by contriving a situation where integer fallback allows progress to be | ||
// made on a trait goal that infers the length of a repeat expr. | ||
// Test when deferring repeat expr copy checks to end of typechecking whether they're | ||
// checked before integer fallback occurs or not. We accomplish this by having a repeat | ||
// count that can only be inferred after integer fallback has occured. This test will | ||
// pass if we were to check repeat exprs after integer fallback. | ||
|
||
use std::marker::PhantomData; | ||
|
||
struct NotCopy; | ||
struct Foo<T>(PhantomData<T>); | ||
|
||
// We impl Copy/Clone for multiple (but not all) substitutions | ||
// to ensure that `Foo<?int>: Copy` can't be proven on the basis | ||
// of there only being one applying impl. | ||
impl Clone for Foo<u32> { | ||
fn clone(&self) -> Self { | ||
Foo(PhantomData) | ||
} | ||
} | ||
impl Clone for Foo<i32> { | ||
fn clone(&self) -> Self { | ||
Foo(PhantomData) | ||
} | ||
} | ||
impl Copy for Foo<u32> {} | ||
impl Copy for Foo<i32> {} | ||
|
||
trait Trait<const N: usize> {} | ||
|
||
impl Trait<2> for u32 {} | ||
// We impl `Trait` for both `i32` and `u32` to avoid being able | ||
// to prove `?int: Trait<?n>` from there only being one impl. | ||
impl Trait<1> for i32 {} | ||
impl Trait<2> for u32 {} | ||
|
||
fn make_goal<T: Trait<N>, const N: usize>(_: &T, _: [NotCopy; N]) {} | ||
fn tie_and_make_goal<const N: usize, T: Trait<N>>(_: &T, _: &[Foo<T>; N]) {} | ||
|
||
fn main() { | ||
let a = 1; | ||
let b = [NotCopy; _]; | ||
//~^ ERROR: type annotations needed | ||
|
||
// a is of type `?y` | ||
// b is of type `[NotCopy; ?x]` | ||
// there is a goal ?y: Trait<?x>` with two candidates: | ||
// - `i32: Trait<1>`, ?y=i32 ?x=1 which doesnt require `NotCopy: Copy` | ||
// - `u32: Trait<2>` ?y=u32 ?x=2 which requires `NotCopy: Copy` | ||
make_goal(&a, b); | ||
|
||
// final repeat expr checks: | ||
// | ||
// `NotCopy; ?x` | ||
// - succeeds if fallback happens before repeat exprs as `i32: Trait<?x>` infers `?x=1` | ||
// - fails if repeat expr checks happen first as `?x` is unconstrained so cannot be | ||
// structurally resolved | ||
// Deferred repeat expr `Foo<?int>; ?n` | ||
let b = [Foo(PhantomData); _]; | ||
//~^ ERROR: type annotations needed for `[Foo<{integer}>; _]` | ||
|
||
// Introduces a `?int: Trait<?n>` goal | ||
tie_and_make_goal(&a, &b); | ||
|
||
// If fallback doesn't occur: | ||
// - `Foo<?int>; ?n`is ambig as repeat count is unknown -> error | ||
|
||
// If fallback occurs: | ||
// - `?int` inferred to `i32` | ||
// - `?int: Trait<?n>` becomes `i32: Trait<?n>` wihhc infers `?n=1` | ||
// - Repeat expr check `Foo<?int>; ?n` is now `Foo<i32>; 1` | ||
// - `Foo<i32>; 1` doesn't require `Foo<i32>: Copy` | ||
} |
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.
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.
I changed this to an explicit match instead of the
to_target_usize().is_none_or()
as theis_none_or
felt like somewhat subtle logic given the soundness sensitive nature of how we should be handling each variant 🤔