-
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
Defer repeat expr Copy
checks to end of type checking
#137045
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
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() {} |
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 | ||
} |
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`. |
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` | ||
} |
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`. |
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); | ||
} |
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(); _]; | ||
} |
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); | ||
} |
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)
?