-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Fix accidental type inference in array coercion #140283
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
Conversation
|
rustbot has assigned @petrochenkov. Use |
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.
Please add a test to demonstrate the effect of this change. Especially since with this change, we would be accepting more code than on master (this is the snippet in the original issue):
fn foo() {}
fn bar() {}
fn main() {
let _a = if true { foo } else { bar };
let _b = vec![foo, bar];
let _c = [foo, bar];
d(if true { foo } else { bar });
e(vec![foo, bar]);
f([foo, bar]); // <- this PR now accepts this
}
fn d<T>(_: T) {}
fn e<T>(_: Vec<T>) {}
fn f<T>(_: [T; 2]) {}whereas on master this snippet does not compile w/
error[E0308]: mismatched types
--> src/main.rs:10:7
|
10 | f([foo, bar]);
| - ^^^^^^^^^^ expected `[fn() {foo}; 2]`, found `[fn(); 2]`
| |
| arguments to this function are incorrect
|
= note: expected array `[fn() {foo}; 2]`
found array `[fn(); 2]`
note: function defined here
--> src/main.rs:15:4
|
15 | fn f<T>(_: [T; 2]) {}
| ^ ---------
For more information about this error, try `rustc --explain E0308`.
I'm surprised there are no ui test diffs.
|
r? types |
|
There're some similar errors, but I'm unsure whether it's okay to allow these code. The Rust Reference. fn foo() {}
fn bar() {}
fn main() {
let block_var = 'a: { // Easy to fix, but not specified by the Rust Reference.
if false {
break 'a foo;
}
break 'a bar;
};
let loop_var = loop { // Easy to fix, but not specified by the Rust Reference.
if false {
break foo;
}
break bar;
};
let closure_var = || { // More complicated. But this should work according to the Rust Reference.
if false {
return foo;
}
return bar;
};
} |
Yea I think these all should work, too. Please fix the easy ones and add tests for all of them if we don't already have any |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
fdbaf03 to
b31fa29
Compare
|
Turns out the ‘easy’ cases aren’t so easy after all. I can't fix the inference regressions for now, but I might revisit them later once I understand type inference better. |
|
@rustbot ready |
|
I am not going to get to this in the next 10 days. I'll need to review the general state of inference here and write a T-types FCP text |
|
☔ The latest upstream changes (presumably #141716) made this pull request unmergeable. Please resolve the merge conflicts. |
|
Array expressions normally lub their element expressions' types to ensure that things like This PR changes that to instead fall back to "not knowing" that the argument type is array of infer var, but just having an infer var for the entire argument. Thus we typeck the array expression normally, lubbing the element expressions, and then in the end comparing the array expression's type with the array of infer var type. Things like fn foo() {}
fn bar() {}
fn f<T>(_: [T; 2]) {}
f([foo, bar]);and struct Foo;
struct Bar;
trait Trait {}
impl Trait for Foo {}
impl Trait for Bar {}
fn f<T>(_: [T; 2]) {}
f([&Foo, &Bar as &dyn Trait]);@rfcbot merge |
|
Team member @oli-obk has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
|
I think that was previously tried upthread somewhere and it broke a lot of stuff, i can't remember where though. but note that other places in inference that use CoerceMany already have such an issue. e.g. |
I don't think the specific variant I am suggesting (checking if |
You're right that it's insufficient. I think it's not observable now. Since coercion only works for the outmost type, the code will fail lub coercion anyway. But the coercion might need the expectation type to make progress even if it contains ty vars. |
let _: Vec<Box<dyn Fn(isize) -> _>> = vec![
Box::new(|x| (x as u8)),
Box::new(|x| (x as i16 as u8)),
];This fails with log: |
|
Ah, yep. That one. I ran across that one too, and I think was the one that made me just not change array coercion in my testing (370fc9b#diff-502fc8fe28ac40b5ad34931ece468fa6755533b1aeb4ce89c528718ec9cef106R1684), because I don't actually know how to solve that. |
|
Alright, so, I'm good to land the PR as-is, and it's been FCPed. Can you rebase? |
ec874ee to
9ccce41
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
9ccce41 to
b235cc2
Compare
|
I see the date that this was opened and am terribly sorry that you've waited this long @adwinwhite. That being said, I appreciate you opening this and seeing it through :) Overall here, even though I am doing other experimentation in this area, these changes here align with the principles that I am trying to achieve (that the LUB inference is "somewhat" independent from the "expected type" - and also doesn't directly result in any additional order-dependent behavior). @bors r+ rollup=never |
This comment has been minimized.
This comment has been minimized.
What is this?This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.Comparing 842bd5b (parent) -> 35a31ba (this PR) Test differencesShow 14 test diffsStage 1
Stage 2
Additionally, 10 doctest diffs were found. These are ignored, as they are noisy. Job group index
Test dashboardRun cargo run --manifest-path src/ci/citool/Cargo.toml -- \
test-dashboard 35a31ba763976907cd38ba88743a3aefbaf8ffff --output-dir test-dashboardAnd then open Job duration changes
How to interpret the job duration changes?Job durations can vary a lot, based on the actual runner instance |
|
Finished benchmarking commit (35a31ba): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)This benchmark run did not return any relevant results for this metric. CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 473.781s -> 476.214s (0.51%) |
Fix accidental type inference in array coercion Fixes rust-lang/rust#136420. If the expectation of array element is a type variable, we should avoid resolving it to the first element's type and wait until LUB coercion is completed. We create a free type variable instead which is only used in this `CoerceMany`. [`check_expr_match`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/_match.rs#L72) and [`check_expr_if`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expr.rs#L1329) where `CoerceMany` is also used do the [same](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expectation.rs#L50). ### [FCP Proposal](rust-lang/rust#140283 (comment)): > Array expressions normally lub their element expressions' types to ensure that things like `[5, 5_u8]` work and don't result in type mismatches. When invoking a generic function `fn foo<T>(_: [T; N])` with an array expression, we end up with an infer var for the element type of the array in the signature. So when typecking the first array element we compare its type with the infer var and thus subsequently require all other elements to be the same type. > > This PR changes that to instead fall back to "not knowing" that the argument type is array of infer var, but just having an infer var for the entire argument. Thus we typeck the array expression normally, lubbing the element expressions, and then in the end comparing the array expression's type with the array of infer var type. > > Things like > > ```rust > fn foo() {} > fn bar() {} > fn f<T>(_: [T; 2]) {} > > f([foo, bar]); > ``` > > and > > ```rust > struct Foo; > struct Bar; > trait Trait {} > impl Trait for Foo {} > impl Trait for Bar {} > fn f<T>(_: [T; 2]) {} > > f([&Foo, &Bar as &dyn Trait]); > ``` ### Remaining inconsistency with `if` and `match`(rust-lang/rust#145048): The typeck of array always uses the element coercion target type as the expectation of element exprs while `if` and `match` use `NoExpectation` if the expected type is an infer var. This causes that array doesn't support nested coercion. ```rust fn foo() {} fn bar() {} fn main() { let _ = [foo, if false { bar } else { foo }]; // type mismatch when trying to coerce `bar` into `foo` in if-then branch coercion. } ``` But we can't simply change this behavior to be the same as `if` and `match` since [many code](rust-lang/rust#140283 (comment)) depends on using the first element's type as expectation.
Fix accidental type inference in array coercion Fixes rust-lang/rust#136420. If the expectation of array element is a type variable, we should avoid resolving it to the first element's type and wait until LUB coercion is completed. We create a free type variable instead which is only used in this `CoerceMany`. [`check_expr_match`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/_match.rs#L72) and [`check_expr_if`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expr.rs#L1329) where `CoerceMany` is also used do the [same](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expectation.rs#L50). ### [FCP Proposal](rust-lang/rust#140283 (comment)): > Array expressions normally lub their element expressions' types to ensure that things like `[5, 5_u8]` work and don't result in type mismatches. When invoking a generic function `fn foo<T>(_: [T; N])` with an array expression, we end up with an infer var for the element type of the array in the signature. So when typecking the first array element we compare its type with the infer var and thus subsequently require all other elements to be the same type. > > This PR changes that to instead fall back to "not knowing" that the argument type is array of infer var, but just having an infer var for the entire argument. Thus we typeck the array expression normally, lubbing the element expressions, and then in the end comparing the array expression's type with the array of infer var type. > > Things like > > ```rust > fn foo() {} > fn bar() {} > fn f<T>(_: [T; 2]) {} > > f([foo, bar]); > ``` > > and > > ```rust > struct Foo; > struct Bar; > trait Trait {} > impl Trait for Foo {} > impl Trait for Bar {} > fn f<T>(_: [T; 2]) {} > > f([&Foo, &Bar as &dyn Trait]); > ``` ### Remaining inconsistency with `if` and `match`(rust-lang/rust#145048): The typeck of array always uses the element coercion target type as the expectation of element exprs while `if` and `match` use `NoExpectation` if the expected type is an infer var. This causes that array doesn't support nested coercion. ```rust fn foo() {} fn bar() {} fn main() { let _ = [foo, if false { bar } else { foo }]; // type mismatch when trying to coerce `bar` into `foo` in if-then branch coercion. } ``` But we can't simply change this behavior to be the same as `if` and `match` since [many code](rust-lang/rust#140283 (comment)) depends on using the first element's type as expectation.
Fix accidental type inference in array coercion Fixes rust-lang/rust#136420. If the expectation of array element is a type variable, we should avoid resolving it to the first element's type and wait until LUB coercion is completed. We create a free type variable instead which is only used in this `CoerceMany`. [`check_expr_match`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/_match.rs#L72) and [`check_expr_if`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expr.rs#L1329) where `CoerceMany` is also used do the [same](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expectation.rs#L50). ### [FCP Proposal](rust-lang/rust#140283 (comment)): > Array expressions normally lub their element expressions' types to ensure that things like `[5, 5_u8]` work and don't result in type mismatches. When invoking a generic function `fn foo<T>(_: [T; N])` with an array expression, we end up with an infer var for the element type of the array in the signature. So when typecking the first array element we compare its type with the infer var and thus subsequently require all other elements to be the same type. > > This PR changes that to instead fall back to "not knowing" that the argument type is array of infer var, but just having an infer var for the entire argument. Thus we typeck the array expression normally, lubbing the element expressions, and then in the end comparing the array expression's type with the array of infer var type. > > Things like > > ```rust > fn foo() {} > fn bar() {} > fn f<T>(_: [T; 2]) {} > > f([foo, bar]); > ``` > > and > > ```rust > struct Foo; > struct Bar; > trait Trait {} > impl Trait for Foo {} > impl Trait for Bar {} > fn f<T>(_: [T; 2]) {} > > f([&Foo, &Bar as &dyn Trait]); > ``` ### Remaining inconsistency with `if` and `match`(rust-lang/rust#145048): The typeck of array always uses the element coercion target type as the expectation of element exprs while `if` and `match` use `NoExpectation` if the expected type is an infer var. This causes that array doesn't support nested coercion. ```rust fn foo() {} fn bar() {} fn main() { let _ = [foo, if false { bar } else { foo }]; // type mismatch when trying to coerce `bar` into `foo` in if-then branch coercion. } ``` But we can't simply change this behavior to be the same as `if` and `match` since [many code](rust-lang/rust#140283 (comment)) depends on using the first element's type as expectation.
Fix accidental type inference in array coercion Fixes rust-lang/rust#136420. If the expectation of array element is a type variable, we should avoid resolving it to the first element's type and wait until LUB coercion is completed. We create a free type variable instead which is only used in this `CoerceMany`. [`check_expr_match`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/_match.rs#L72) and [`check_expr_if`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expr.rs#L1329) where `CoerceMany` is also used do the [same](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expectation.rs#L50). ### [FCP Proposal](rust-lang/rust#140283 (comment)): > Array expressions normally lub their element expressions' types to ensure that things like `[5, 5_u8]` work and don't result in type mismatches. When invoking a generic function `fn foo<T>(_: [T; N])` with an array expression, we end up with an infer var for the element type of the array in the signature. So when typecking the first array element we compare its type with the infer var and thus subsequently require all other elements to be the same type. > > This PR changes that to instead fall back to "not knowing" that the argument type is array of infer var, but just having an infer var for the entire argument. Thus we typeck the array expression normally, lubbing the element expressions, and then in the end comparing the array expression's type with the array of infer var type. > > Things like > > ```rust > fn foo() {} > fn bar() {} > fn f<T>(_: [T; 2]) {} > > f([foo, bar]); > ``` > > and > > ```rust > struct Foo; > struct Bar; > trait Trait {} > impl Trait for Foo {} > impl Trait for Bar {} > fn f<T>(_: [T; 2]) {} > > f([&Foo, &Bar as &dyn Trait]); > ``` ### Remaining inconsistency with `if` and `match`(rust-lang/rust#145048): The typeck of array always uses the element coercion target type as the expectation of element exprs while `if` and `match` use `NoExpectation` if the expected type is an infer var. This causes that array doesn't support nested coercion. ```rust fn foo() {} fn bar() {} fn main() { let _ = [foo, if false { bar } else { foo }]; // type mismatch when trying to coerce `bar` into `foo` in if-then branch coercion. } ``` But we can't simply change this behavior to be the same as `if` and `match` since [many code](rust-lang/rust#140283 (comment)) depends on using the first element's type as expectation.
Fix accidental type inference in array coercion Fixes rust-lang/rust#136420. If the expectation of array element is a type variable, we should avoid resolving it to the first element's type and wait until LUB coercion is completed. We create a free type variable instead which is only used in this `CoerceMany`. [`check_expr_match`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/_match.rs#L72) and [`check_expr_if`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expr.rs#L1329) where `CoerceMany` is also used do the [same](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expectation.rs#L50). ### [FCP Proposal](rust-lang/rust#140283 (comment)): > Array expressions normally lub their element expressions' types to ensure that things like `[5, 5_u8]` work and don't result in type mismatches. When invoking a generic function `fn foo<T>(_: [T; N])` with an array expression, we end up with an infer var for the element type of the array in the signature. So when typecking the first array element we compare its type with the infer var and thus subsequently require all other elements to be the same type. > > This PR changes that to instead fall back to "not knowing" that the argument type is array of infer var, but just having an infer var for the entire argument. Thus we typeck the array expression normally, lubbing the element expressions, and then in the end comparing the array expression's type with the array of infer var type. > > Things like > > ```rust > fn foo() {} > fn bar() {} > fn f<T>(_: [T; 2]) {} > > f([foo, bar]); > ``` > > and > > ```rust > struct Foo; > struct Bar; > trait Trait {} > impl Trait for Foo {} > impl Trait for Bar {} > fn f<T>(_: [T; 2]) {} > > f([&Foo, &Bar as &dyn Trait]); > ``` ### Remaining inconsistency with `if` and `match`(rust-lang/rust#145048): The typeck of array always uses the element coercion target type as the expectation of element exprs while `if` and `match` use `NoExpectation` if the expected type is an infer var. This causes that array doesn't support nested coercion. ```rust fn foo() {} fn bar() {} fn main() { let _ = [foo, if false { bar } else { foo }]; // type mismatch when trying to coerce `bar` into `foo` in if-then branch coercion. } ``` But we can't simply change this behavior to be the same as `if` and `match` since [many code](rust-lang/rust#140283 (comment)) depends on using the first element's type as expectation.
Fix accidental type inference in array coercion Fixes rust-lang/rust#136420. If the expectation of array element is a type variable, we should avoid resolving it to the first element's type and wait until LUB coercion is completed. We create a free type variable instead which is only used in this `CoerceMany`. [`check_expr_match`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/_match.rs#L72) and [`check_expr_if`](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expr.rs#L1329) where `CoerceMany` is also used do the [same](https://github.com/rust-lang/rust/blob/847e3ee6b0e614937eee4e6d8f61094411eadcc0/compiler/rustc_hir_typeck/src/expectation.rs#L50). ### [FCP Proposal](rust-lang/rust#140283 (comment)): > Array expressions normally lub their element expressions' types to ensure that things like `[5, 5_u8]` work and don't result in type mismatches. When invoking a generic function `fn foo<T>(_: [T; N])` with an array expression, we end up with an infer var for the element type of the array in the signature. So when typecking the first array element we compare its type with the infer var and thus subsequently require all other elements to be the same type. > > This PR changes that to instead fall back to "not knowing" that the argument type is array of infer var, but just having an infer var for the entire argument. Thus we typeck the array expression normally, lubbing the element expressions, and then in the end comparing the array expression's type with the array of infer var type. > > Things like > > ```rust > fn foo() {} > fn bar() {} > fn f<T>(_: [T; 2]) {} > > f([foo, bar]); > ``` > > and > > ```rust > struct Foo; > struct Bar; > trait Trait {} > impl Trait for Foo {} > impl Trait for Bar {} > fn f<T>(_: [T; 2]) {} > > f([&Foo, &Bar as &dyn Trait]); > ``` ### Remaining inconsistency with `if` and `match`(rust-lang/rust#145048): The typeck of array always uses the element coercion target type as the expectation of element exprs while `if` and `match` use `NoExpectation` if the expected type is an infer var. This causes that array doesn't support nested coercion. ```rust fn foo() {} fn bar() {} fn main() { let _ = [foo, if false { bar } else { foo }]; // type mismatch when trying to coerce `bar` into `foo` in if-then branch coercion. } ``` But we can't simply change this behavior to be the same as `if` and `match` since [many code](rust-lang/rust#140283 (comment)) depends on using the first element's type as expectation.
Fixes #136420.
If the expectation of array element is a type variable, we should avoid resolving it to the first element's type and wait until LUB coercion is completed.
We create a free type variable instead which is only used in this
CoerceMany.check_expr_matchandcheck_expr_ifwhereCoerceManyis also used do the same.FCP Proposal:
Remaining inconsistency with
ifandmatch(#145048):The typeck of array always uses the element coercion target type as the expectation of element exprs while
ifandmatchuseNoExpectationif the expected type is an infer var.This causes that array doesn't support nested coercion.
But we can't simply change this behavior to be the same as
ifandmatchsince many code depends on using the first element's type as expectation.