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

Bad compile error with fun bar(&foo: Foo) syntax #38371

Closed
ekr opened this issue Dec 14, 2016 · 3 comments
Closed

Bad compile error with fun bar(&foo: Foo) syntax #38371

ekr opened this issue Dec 14, 2016 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@ekr
Copy link

ekr commented Dec 14, 2016

See the following code fragment:

struct Foo {
}

fn foo(&foo: Foo) {  // illegal syntax
}

fn main() {
    println!("Hello, world!");
}

This generates the following confusing error:

15:29:19|~/tmp/rust_compile_error (master)$ cargo build
   Compiling rust_compile_error v0.1.0 (file:///Users/ekr/tmp/rust_compile_error)
error[E0308]: mismatched types
 --> src/main.rs:4:8
  |
4 | fn foo(&foo: Foo) {
  |        ^^^^ expected struct `Foo`, found reference
  |
  = note: expected type `Foo`
  = note:    found type `&_`

error: aborting due to previous error

error: Could not compile `rust_compile_error`.
@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 14, 2016
@Stebalien
Copy link
Contributor

Well, that code is actually syntactically correct, it just doesn't type-check.

For example, this works:

#[derive(Copy, Clone)]
struct Foo;

fn foo(&foo: &Foo) { // legal syntax
}

fn main() {
    println!("Hello, world!");
}

And is equivalent to:

#[derive(Copy, Clone)]
struct Foo;

fn foo(foo_ref: &Foo) {
    let foo = *foo_ref;
}

fn main() {
    println!("Hello, world!");
}

@ekr
Copy link
Author

ekr commented Dec 15, 2016

OK. I was just following @dherman's instructions to file here, so I'll defer to those who know better than me.

@Stebalien
Copy link
Contributor

@ekr yeah, reporting here was the right thing to do. This is confusing and deserves a "did you mean foo: &Foo?".

bors added a commit that referenced this issue Jan 12, 2017
Suggest solutions for `fn foo(&foo: Foo)`

For a given file:

```rust
struct Foo {}

fn foo(&foo: Foo) {}
```

suggest:

```
error[E0308]: mismatched types
 --> file.rs:3:8
  |
3 | fn foo(&foo: Foo) {}
  |        ^^^^ expected struct `Foo`, found reference
  |
  = note: expected type `Foo`
  = note:    found type `&_`
  = help: did you mean `foo: &Foo`?
```

Fix #38371.
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
Projects
None yet
Development

No branches or pull requests

3 participants