-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Labeled line for "here the type of var
is inferred to be" can be wrong
#106590
Comments
Similarly confusing output: use std::collections::HashMap;
fn main() {
let mut x = HashMap::<u32, _>::new();
x.insert(1i32, Default::default());
}
Specifically:
|
For the second case it can be guarded against by breaking on an earlier iteration of the suggestion logic. For the original we'll need to add logic to handle field access explicitly. |
I don't think the original issue is fixed by adding logic to handle field accesses explicitly. There are a number of other ways of using an expression that can constrain it partially, for example: #[derive(Copy, Clone)]
struct Wrap<T, S>(T, S);
fn main() {
let s = Wrap(None, None);
constrain::<i32, _>(s);
constrain::<_, i32>(s);
let z: Wrap<Option<i32>, Option<&str>> = s;
}
fn constrain<T, S>(_: Wrap<Option<T>, Option<S>>) {} Results in:
The issue here is that the heuristic is just "did the type of the expression change at all" -- not "did the type of the expression change in a way to that causes the type error down the road". |
You're right, and that might be solved by evaluating that the current |
I actually implemented a change to do something similar, which fixes the examples (1) and (3) above, but the code is a bit too tangled up with the special casing for method calls and argument mismatches and it breaks the |
Shame. We could rework the logic for methods to be more consistent with the others, which might help :-/ |
…ush, r=estebank Fix escaping inference var ICE in `point_at_expr_source_of_inferred_type` Fixes rust-lang#107158 `point_at_expr_source_of_inferred_type` uses `lookup_probe` to adjust the self type of a method receiver -- but that method returns inference variables from inside a probe. That means that the ty vars are no longer valid, so we can't use any infcx methods on them. Also, pass some extra span info to hack a quick solution to bad labels, resulting in this diagnostic improvement: ```rust fn example2() { let mut x = vec![1]; x.push(""); } ``` ```diff error[E0308]: mismatched types --> src/main.rs:5:12 | 5 | x.push(""); | ---- ^^ | | | | | expected integer, found `&str` - | | this is of type `&'static str`, which causes `x` to be inferred as `Vec<{integer}>` | arguments to this method are incorrect ``` (since that "which causes `x` to be inferred as `Vec<{integer}>` part is wrong) r? `@estebank` (we really should make this code better in general, cc rust-lang#106590, but that's a bit bigger issue that needs some more thinking about)
…ush, r=estebank Fix escaping inference var ICE in `point_at_expr_source_of_inferred_type` Fixes rust-lang#107158 `point_at_expr_source_of_inferred_type` uses `lookup_probe` to adjust the self type of a method receiver -- but that method returns inference variables from inside a probe. That means that the ty vars are no longer valid, so we can't use any infcx methods on them. Also, pass some extra span info to hack a quick solution to bad labels, resulting in this diagnostic improvement: ```rust fn example2() { let mut x = vec![1]; x.push(""); } ``` ```diff error[E0308]: mismatched types --> src/main.rs:5:12 | 5 | x.push(""); | ---- ^^ | | | | | expected integer, found `&str` - | | this is of type `&'static str`, which causes `x` to be inferred as `Vec<{integer}>` | arguments to this method are incorrect ``` (since that "which causes `x` to be inferred as `Vec<{integer}>` part is wrong) r? ``@estebank`` (we really should make this code better in general, cc rust-lang#106590, but that's a bit bigger issue that needs some more thinking about)
…ush, r=estebank Fix escaping inference var ICE in `point_at_expr_source_of_inferred_type` Fixes rust-lang#107158 `point_at_expr_source_of_inferred_type` uses `lookup_probe` to adjust the self type of a method receiver -- but that method returns inference variables from inside a probe. That means that the ty vars are no longer valid, so we can't use any infcx methods on them. Also, pass some extra span info to hack a quick solution to bad labels, resulting in this diagnostic improvement: ```rust fn example2() { let mut x = vec![1]; x.push(""); } ``` ```diff error[E0308]: mismatched types --> src/main.rs:5:12 | 5 | x.push(""); | ---- ^^ | | | | | expected integer, found `&str` - | | this is of type `&'static str`, which causes `x` to be inferred as `Vec<{integer}>` | arguments to this method are incorrect ``` (since that "which causes `x` to be inferred as `Vec<{integer}>` part is wrong) r? `@estebank` (we really should make this code better in general, cc rust-lang#106590, but that's a bit bigger issue that needs some more thinking about)
Given the following code: playground
The current output is:
Ideally the output should look like:
The problem here is that while
s.0 = ..
is the first expression to constrainWrap<_, _>
, it's not the expression which constrainsWrap<_, _>
in such a way that causes the type error, so pointing to it is possibly misleading -- possibly even more confusing if the lines are further separated.The text was updated successfully, but these errors were encountered: