Rust: Misleading error message on typo "Type ascription"

Created on 13 Jun 2016  路  7Comments  路  Source: rust-lang/rust

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
A-diagnostics C-enhancement D-confusing D-verbose P-low T-compiler

Most helpful comment

From the two issues I just closed:

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

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

All 7 comments

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

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

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

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

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

From the two issues I just closed:

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

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

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);
}

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?

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 https://github.com/rust-lang/rust/issues/61326.

The case in the first comment is not handled correctly at all (related https://github.com/rust-lang/rust/issues/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

https://github.com/rust-lang/rust/issues/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.

https://github.com/rust-lang/rust/issues/70382 brings up a specific case of bad output when recovering some paths.

Was this page helpful?
0 / 5 - 0 ratings