Skip to content
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

Misplaced "type of this value must be known ..." error #36598

Closed
bnordbo opened this issue Sep 20, 2016 · 1 comment · Fixed by #48028
Closed

Misplaced "type of this value must be known ..." error #36598

bnordbo opened this issue Sep 20, 2016 · 1 comment · Fixed by #48028
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bnordbo
Copy link

bnordbo commented Sep 20, 2016

In a transformation sequence, I got a type inference error on the first function call, while the error was actually later (and below) in the chain. This was very confusing, in particular since the offending line was not shown in the error message. This could be a duplicate of #36495, or a more general issue not related to type inference.

As it should, rustc 1.11.0 on OS X fails to build the following code:

fn fail(v: Vec<u32>) -> Option<Box<Vec<u32>>> {
    v.iter()
        .map(|x| Some(*x))
        .collect()
        .map(Box::new)
}

fn main() {
    fail(vec![1, 2, 3]);
}

The error message is:

/tmp/err.rs:2:5: 5:23 error: the type of this value must be known in this context
/tmp/err.rs:2     v.iter()
                  ^
error: aborting due to previous error

On nightly the error report is nicer, but no more helpful. The fix, which is obvious once one decides to ignore the line indication from rustc, is to add a type annotation to collect():

fn fail(v: Vec<u32>) -> Option<Box<Vec<u32>>> {
    v.iter()
        .map(|x| Some(*x))
        .collect::<Option<_>>()
        .map(Box::new)
}

fn main() {
    fail(vec![1, 2, 3]);
}

After poking around a bit, I suspect that this may be a more general issue related to how rustc determines the offending line number. For instance, the error message from the compiler has the same problem, while the error is "mismatched types":

fn fail(v: Vec<u32>) -> Option<Vec<u32>> {
    v.iter()
        .map(|x| Some(*x))
}

fn main() {
    fail(vec![1, 2, 3]);
}
/tmp/err.rs:2:5: 3:27 error: mismatched types [E0308]
/tmp/err.rs:2     v.iter()
                  ^

[...]

While this is the kind of thing that only bites you once, it can utterly confuse a beginner. It is particularly perplexing in more complex, multi-line expressions. One could of course argue that the error location is correct as it points to the flawed expression, but that does not really help. Perhaps indicating where in the expression the error lies would be better, e.g (using the new and much better error message style from nightly)?

error: the type of this value must be known in this context
 --> /tmp/err.rs:2:5
  |
2 |     v.iter()
3 |         .map(|x| Some(*x))
4 |         .collect()
  |         ^

error: aborting due to previous error
@apasel422 apasel422 added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 20, 2016
@steveklabnik steveklabnik added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 9, 2017
@leonardo-m
Copy link

@Mark-Simulacrum has closed issue #43233, so now this is the additional problem that should be fixed by #36598 (otherwise I'll reopen #43233):

This code:

fn sum_and_show(data: &[u32]) -> String {
    data.iter().sum().to_string()
}
fn main() {
    let s: String = sum_and_show(&[1, 2, 3]);
}

In rustc 1.20.0-nightly (b2c0707 2017-07-13) gives the error message:

error[E0619]: the type of this value must be known in this context
 --> ...\test.rs:2:5
  |
2 |     data.iter().sum().to_string()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0619]: the type of this value must be known in this context
 --> ...\test.rs:2:5
  |
2 |     data.iter().sum().to_string()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The error message doesn't point to the exact location of the problem. The correct code is:

data.iter().sum::<u32>().to_string()

So probably I'd like an error message like:

error[E0619]: the type of this value must be known in this context
 --> ...\test.rs:2:5
  |
2 |     data.iter().sum().to_string()
  |                 ^^^^^

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
zackmdavis added a commit to zackmdavis/rust that referenced this issue Feb 6, 2018
Previously, when the type of a method receiver could not be determined,
the error message would, potentially confusingly, highlight the span of
the entire method call.

Resolves rust-lang#36598, resolves rust-lang#42234.
Manishearth added a commit to Manishearth/rust that referenced this issue Feb 7, 2018
…own_type, r=estebank

correct E0619 span re method call receivers whose type must be known

Previously, when the type of a method receiver could not be determined,
the error message would, potentially confusingly, highlight the span of
the entire method call.

![unknown_receiver_type](https://user-images.githubusercontent.com/1076988/35838930-a595b17c-0aa2-11e8-9364-6b8e2329f051.png)

Resolves rust-lang#36598, resolves rust-lang#42234.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants