Skip to content

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

Closed

Description

@ekr

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`.

Activity

  1. Stebalien commented on Dec 15, 2016

    @Stebalien
    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!");
    }
  2. ekr commented on Dec 15, 2016

    @ekr
    Author

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

  3. Stebalien commented on Dec 15, 2016

    @Stebalien
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions