-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Defer repeat expr Copy
checks to end of type checking
#137045
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,6 +199,15 @@ fn typeck_with_inspect<'tcx>( | |
fcx.write_ty(id, expected_type); | ||
}; | ||
|
||
// Whether to check repeat exprs before/after inference fallback is somewhat arbitrary of a decision | ||
// as neither option is strictly more permissive than the other. However, we opt to check repeat exprs | ||
// first as errors from not having inferred array lengths yet seem less confusing than errors from inference | ||
// fallback arbitrarily inferring something incompatible with `Copy` inference side effects. | ||
// | ||
// This should also be forwards compatible with moving repeat expr checks to a custom goal kind or using | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel somewhat hesitant to add a custom goal kind for repeat expr checks but I do think it would be "optimal" behaviour-wise as the goals could stall on the repeat count infer var and be deferred ~as long as necessary while also not delaying inference constraints from them any later than is needed. |
||
// marker traits in the future. | ||
fcx.check_repeat_exprs(); | ||
|
||
fcx.type_inference_fallback(); | ||
|
||
// Even though coercion casts provide type hints, we check casts after fallback for | ||
|
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 |
---|---|---|
@@ -1,11 +1,16 @@ | ||
static _MAYBE_STRINGS: [Option<String>; 5] = [None; 5]; | ||
//~^ ERROR the trait bound `String: Copy` is not satisfied | ||
|
||
fn main() { | ||
// should hint to create an inline `const` block | ||
// or to create a new `const` item | ||
// should hint to create an inline `const` block | ||
// or to create a new `const` item | ||
fn foo() { | ||
let _strings: [String; 5] = [String::new(); 5]; | ||
//~^ ERROR the trait bound `String: Copy` is not satisfied | ||
} | ||
|
||
fn bar() { | ||
let _maybe_strings: [Option<String>; 5] = [None; 5]; | ||
//~^ ERROR the trait bound `String: Copy` is not satisfied | ||
} | ||
|
||
fn main() {} |
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/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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![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. | ||
|
||
use std::marker::PhantomData; | ||
|
||
struct NotCopy; | ||
|
||
trait Trait<const N: usize> {} | ||
|
||
impl Trait<2> for u32 {} | ||
impl Trait<1> for i32 {} | ||
|
||
fn make_goal<T: Trait<N>, const N: usize>(_: &T, _: [NotCopy; 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 | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/ui/repeat-expr/copy-check-deferred-after-fallback.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,14 @@ | ||
error[E0282]: type annotations needed for `[NotCopy; _]` | ||
--> $DIR/copy-check-deferred-after-fallback.rs:21:9 | ||
| | ||
LL | let b = [NotCopy; _]; | ||
| ^ ------- type must be known at this point | ||
| | ||
help: consider giving `b` an explicit type, where the value of const parameter `N` is specified | ||
| | ||
LL | let b: [_; N] = [NotCopy; _]; | ||
| ++++++++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0282`. |
59 changes: 59 additions & 0 deletions
59
tests/ui/repeat-expr/copy-check-deferred-before-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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//@ check-pass | ||
|
||
#![feature(generic_arg_infer)] | ||
|
||
// Test that if we defer repeat expr copy checks to end of typechecking they're | ||
// checked before integer fallback occurs. We accomplish this by contriving a | ||
// situation where we have a goal that can be proven either via another repeat expr | ||
// check or by integer fallback. In the integer fallback case an array length would | ||
// be inferred to `2` requiring `NotCopy: Copy`, and in the repeat expr case it would | ||
// be inferred to `1`. | ||
|
||
use std::marker::PhantomData; | ||
|
||
struct NotCopy; | ||
|
||
struct Foo<T>(PhantomData<T>); | ||
|
||
impl Clone for Foo<u32> { | ||
fn clone(&self) -> Self { | ||
Foo(PhantomData) | ||
} | ||
} | ||
|
||
impl Copy for Foo<u32> {} | ||
|
||
fn tie<T>(_: &T, _: [Foo<T>; 2]) {} | ||
|
||
trait Trait<const N: usize> {} | ||
|
||
impl Trait<2> for i32 {} | ||
impl Trait<1> for u32 {} | ||
|
||
fn make_goal<T: Trait<N>, const N: usize>(_: &T, _: [NotCopy; N]) {} | ||
|
||
fn main() { | ||
let a = 1; | ||
let b: [Foo<_>; 2] = [Foo(PhantomData); _]; | ||
tie(&a, b); | ||
let c = [NotCopy; _]; | ||
|
||
// a is of type `?y` | ||
// b is of type `[Foo<?y>; 2]` | ||
// c is of type `[NotCopy; ?x]` | ||
// there is a goal ?y: Trait<?x>` with two candidates: | ||
// - `i32: Trait<2>`, ?y=i32 ?x=2 which requires `NotCopy: Copy` when expr checks happen | ||
// - `u32: Trait<1>` ?y=u32 ?x=1 which doesnt require `NotCopy: Copy` | ||
make_goal(&a, c); | ||
|
||
// final repeat expr checks: | ||
// | ||
// `Foo<?y>; 2` | ||
// - Foo<?y>: Copy | ||
// - requires ?y=u32 | ||
// | ||
// `NotCopy; ?x` | ||
// - fails if fallback happens before repeat exprs as `i32: Trait<?x>` infers `?x=2` | ||
// - succeeds if repeat expr checks happen first as `?y=u32` means `u32: Trait<?x>` | ||
// infers `?x=1` | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/ui/repeat-expr/copy-inference-side-effects-are-lazy.gai.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,17 @@ | ||
error[E0282]: type annotations needed for `[Foo<_>; 2]` | ||
--> $DIR/copy-inference-side-effects-are-lazy.rs:22:9 | ||
| | ||
LL | let x = [Foo(PhantomData); 2]; | ||
| ^ | ||
LL | | ||
LL | _ = extract(x).max(2); | ||
| ---------- type must be known at this point | ||
| | ||
help: consider giving `x` an explicit type, where the type for type parameter `T` is specified | ||
| | ||
LL | let x: [Foo<T>; 2] = [Foo(PhantomData); 2]; | ||
| +++++++++++++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0282`. |
25 changes: 25 additions & 0 deletions
25
tests/ui/repeat-expr/copy-inference-side-effects-are-lazy.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,25 @@ | ||
//@revisions: current gai | ||
//@[current] check-pass | ||
|
||
#![cfg_attr(gai, feature(generic_arg_infer))] | ||
|
||
use std::marker::PhantomData; | ||
|
||
struct Foo<T>(PhantomData<T>); | ||
|
||
impl Clone for Foo<u8> { | ||
fn clone(&self) -> Self { | ||
Foo(PhantomData) | ||
} | ||
} | ||
impl Copy for Foo<u8> {} | ||
|
||
fn extract<T, const N: usize>(_: [Foo<T>; N]) -> T { | ||
loop {} | ||
} | ||
|
||
fn main() { | ||
let x = [Foo(PhantomData); 2]; | ||
//[gai]~^ ERROR: type annotations needed | ||
_ = extract(x).max(2); | ||
} |
File renamed without changes.
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,6 @@ | ||
//@ check-pass | ||
#![feature(generic_arg_infer)] | ||
|
||
fn main() { | ||
let a: [_; 1] = [String::new(); _]; | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/ui/repeat-expr/no-conservative-copy-impl-requirement.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,20 @@ | ||
#![feature(generic_arg_infer)] | ||
|
||
struct Foo<const N: usize>; | ||
|
||
impl Clone for Foo<1> { | ||
fn clone(&self) -> Self { | ||
Foo | ||
} | ||
} | ||
impl Copy for Foo<1> {} | ||
|
||
fn unify<const N: usize>(_: &[Foo<N>; N]) { | ||
loop {} | ||
} | ||
|
||
fn main() { | ||
let x = &[Foo::<_>; _]; | ||
//~^ ERROR: type annotations needed for `&[Foo<_>; _]` | ||
_ = unify(x); | ||
} |
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.
There's no
fcx.demand_subtype
for consts yet.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.
You could just
self.at(..).eq(ct, err)
?