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

Misleading error message on typo "Type ascription" #34255

Closed
danielpclark opened this issue Jun 13, 2016 · 8 comments
Closed

Misleading error message on typo "Type ascription" #34255

danielpclark opened this issue Jun 13, 2016 · 8 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@danielpclark
Copy link

danielpclark commented Jun 13, 2016

This had me googling for answers. But I had entered a colon instead of a semi-colon at the end of a line. Nothing in my search results pointed that out.

src/chop_basename.rs:26:3: 29:29 error: type ascription is experimental (see issue #23416)
src/chop_basename.rs:26   parts.push(CString::new(basename).unwrap().into_raw()):
src/chop_basename.rs:27   // END BASENAME
src/chop_basename.rs:28   
src/chop_basename.rs:29   RubyArray::from_vec(parts)
src/lib.rs:23:1: 23:30 note: in this expansion of include!
src/chop_basename.rs:26:3: 29:29 help: add #![feature(type_ascription)] to the crate attributes to enable
error: aborting due to previous error
@apasel422 apasel422 added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 13, 2016
@shepmaster
Copy link
Member

Another from Stack Overflow (missing struct name in constructor):

impl Reactor {
    pub fn new() -> Self {
        input_cells: Vec::new()
    }
}

@gsingh93
Copy link
Contributor

gsingh93 commented Mar 3, 2017

Same issue here (accidentally typed Foo:foo() instead of Foo::foo()):

struct Foo {}
impl Foo {
    fn foo() {}
}

fn main() {
    Foo:foo();
}

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

From the two issues I just closed:

fn foo() {
    let two = 2:
    let two_squared = two * two;
}

fn bar() {
x : i32 = 0;
}

@scampi
Copy link
Contributor

scampi commented Jul 16, 2018

I was doing some refactoring, going from a tuple struct to a struct. I forgot to replace the parenthesis in the main with curly brackets, and got that confusing error:

error: expected type, found `42`
 --> src/main.rs:8:24
  |
8 |     Test::Drill(field: 42);
  |                        ^^ expecting a type here because of type ascription
enum Test {
    Drill {
        field: i32,
    }
}

fn main() {
    Test::Drill(field: 42);
}

@tokideveloper
Copy link

I was also confused by that strange error message. Here is my example: Compiling

fn main() {
    let s = String:from("Just a string.");
    println!("{}", s);
}

using rustc 1.29.1 leads to

error: expected type, found `"Just a string."`
 --> src/main.rs:2:25
  |
2 |     let s = String:from("Just a string.");
  |                         ^^^^^^^^^^^^^^^^ expecting a type here because of type ascription

A little line could really help:

error: expected type, found `"Just a string."`
 --> src/main.rs:2:25
  |
2 |     let s = String:from("Just a string.");
  |                         ^^^^^^^^^^^^^^^^ expecting a type here because of type ascription
                      - help: Did you mean `::`?

Would that be feasible to implement?
Have I overseen any obstacles towards it?

@estebank
Copy link
Contributor

estebank commented Mar 13, 2019

Foo:foo()/String:from("Just a string."); is handled in #59150.

let two = 2: case is already handled.

x : i32 = 0; is handled in the PR above.

Test::Drill(field: 42); case is not but I have a couple of ideas on some signaling the compiler can carry to handle it better. Edit #62791 now points at the colon, but we could identify that a struct variant literal was intended. Related #61326.

The case in the first comment is not handled correctly at all (related #73941):

error[E0425]: cannot find value `foo` in this scope
 --> file4.rs:5:9
  |
5 |         foo: Vec::new()
  |         ^^^
  |         |
  |         `self` value is a keyword only available in methods with `self` parameter
  |         help: try: `self.foo`

error[E0658]: type ascription is experimental (see issue #23416)
 --> file4.rs:5:9
  |
5 |         foo: Vec::new()
  |         ^^^^^^^^^^^^^^^
  |
  = help: add #![feature(type_ascription)] to the crate attributes to enable

error: parenthesized type parameters may only be used with a `Fn` trait
 --> file4.rs:5:22
  |
5 |         foo: Vec::new()
  |                      ^^
  |
  = note: #[deny(parenthesized_params_in_types_and_modules)] on by default
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>

error[E0107]: wrong number of type arguments: expected 1, found 0
 --> file4.rs:5:14
  |
5 |         foo: Vec::new()
  |              ^^^^^^^^^^ expected 1 type argument

error: aborting due to 5 previous errors

Update: the output for the last case has changed slightly, but it is still not great:

error[E0425]: cannot find value `input_cells` in this scope
 --> src/main.rs:6:9
  |
6 |         input_cells: Vec::new()
  |         ^^^^^^^^^^^ a field by this name exists in `Self`

error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
 --> src/main.rs:6:27
  |
6 |         input_cells: Vec::new()
  |                           ^^^^^ only `Fn` traits may use parentheses

error[E0107]: wrong number of type arguments: expected 1, found 0
 --> src/main.rs:6:22
  |
6 |         input_cells: Vec::new()
  |                      ^^^^^^^^^^ expected 1 type argument

Centril added a commit to Centril/rust that referenced this issue Mar 26, 2019
Centril added a commit to Centril/rust that referenced this issue Mar 26, 2019
Centril added a commit to Centril/rust that referenced this issue Mar 26, 2019
Centril added a commit to Centril/rust that referenced this issue Jul 21, 2019
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jul 23, 2019
@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 22, 2020
@estebank
Copy link
Contributor

estebank commented Jul 2, 2020

#73777 brought up the possibility of issues when it comes to typos in macro calls. I feel that the approach of leaving this to macro authors like in that ticket is reasonable enough.

#70382 brings up a specific case of bad output when recovering some paths.

bors added a commit to rust-lang-ci/rust that referenced this issue Aug 10, 2020
…rochenkov

Clean up errors in typeck and resolve

* Tweak ordering of suggestions
* Do not suggest similarly named enclosing item
* Point at item definition in foreign crates
* Add missing primary label

CC rust-lang#34255.
estebank added a commit to estebank/rust that referenced this issue Oct 7, 2020
This approach lives exclusively in the parser, so struct expr bodies
that are syntactically correct on their own but are otherwise incorrect
will still emit confusing errors, like in the following case:

```rust
fn foo() -> Foo {
    bar: Vec::new()
}
```

```
error[E0425]: cannot find value `bar` in this scope
 --> src/file.rs:5:5
  |
5 |     bar: Vec::new()
  |     ^^^ expecting a type here because of type ascription

error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
 --> src/file.rs:5:15
  |
5 |     bar: Vec::new()
  |               ^^^^^ only `Fn` traits may use parentheses

error[E0107]: wrong number of type arguments: expected 1, found 0
 --> src/file.rs:5:10
  |
5 |     bar: Vec::new()
  |          ^^^^^^^^^^ expected 1 type argument
  ```

If that field had a trailing comma, that would be a parse error and it
would trigger the new, more targetted, error:

```
error: struct literal body without path
 --> file.rs:4:17
  |
4 |   fn foo() -> Foo {
  |  _________________^
5 | |     bar: Vec::new(),
6 | | }
  | |_^
  |
help: you might have forgotten to add the struct literal inside the block
  |
4 | fn foo() -> Foo { Path {
5 |     bar: Vec::new(),
6 | } }
  |
```

Partially address last part of rust-lang#34255.
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 8, 2020
Detect blocks that could be struct expr bodies

This approach lives exclusively in the parser, so struct expr bodies
that are syntactically correct on their own but are otherwise incorrect
will still emit confusing errors, like in the following case:

```rust
fn foo() -> Foo {
    bar: Vec::new()
}
```

```
error[E0425]: cannot find value `bar` in this scope
 --> src/file.rs:5:5
  |
5 |     bar: Vec::new()
  |     ^^^ expecting a type here because of type ascription

error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
 --> src/file.rs:5:15
  |
5 |     bar: Vec::new()
  |               ^^^^^ only `Fn` traits may use parentheses

error[E0107]: wrong number of type arguments: expected 1, found 0
 --> src/file.rs:5:10
  |
5 |     bar: Vec::new()
  |          ^^^^^^^^^^ expected 1 type argument
  ```

If that field had a trailing comma, that would be a parse error and it
would trigger the new, more targetted, error:

```
error: struct literal body without path
 --> file.rs:4:17
  |
4 |   fn foo() -> Foo {
  |  _________________^
5 | |     bar: Vec::new(),
6 | | }
  | |_^
  |
help: you might have forgotten to add the struct literal inside the block
  |
4 | fn foo() -> Foo { Path {
5 |     bar: Vec::new(),
6 | } }
  |
```

Partially address last remaining part of rust-lang#34255.
estebank added a commit to estebank/rust that referenced this issue Sep 3, 2021
…ct` literal

Address part of rust-lang#34255.

Potential improvement: silence the other knock down errors in
`issue-34255-1.rs`.
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 4, 2021
…-fire, r=wesleywiser

Detect bare blocks with type ascription that were meant to be a `struct` literal

Address part of rust-lang#34255.

Potential improvement: silence the other knock down errors in `issue-34255-1.rs`.
@estebank
Copy link
Contributor

estebank commented Sep 5, 2021

There are two outstanding issues:

As such, I consider this ticket addressed 😄

@estebank estebank closed this as completed Sep 5, 2021
calebcartwright pushed a commit to calebcartwright/rust that referenced this issue Oct 20, 2021
…ct` literal

Address part of rust-lang#34255.

Potential improvement: silence the other knock down errors in
`issue-34255-1.rs`.
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. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants