Skip to content

confusing diagnostic when variable name equals function name #53841

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

Closed
matthiaskrgr opened this issue Aug 30, 2018 · 2 comments · Fixed by #117924
Closed

confusing diagnostic when variable name equals function name #53841

matthiaskrgr opened this issue Aug 30, 2018 · 2 comments · Fixed by #117924
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

rustc 1.30.0-nightly (02cb8f2a4 2018-08-29)
code:

struct VersionInfo {
    commit_hash: Option<String>,
}

impl VersionInfo {
    pub fn new() -> VersionInfo {
        let commit_hash: Option<String>;

        if false {
            commit_hash = commit_hash();
        } else {
            commit_hash = Some(String::from("bla"));
        }

        VersionInfo {
            commit_hash,
        }
    }
}


fn commit_hash() -> Option<String> {
    Some("hi".to_string())
}

This causes a very confusing error message

error[E0618]: expected function, found enum variant `commit_hash`
  --> rustc_tools_util/src/lib.rs:11:27
   |
8  |         let commit_hash: Option<String>;
   |             ----------- `commit_hash` defined here
...
11 |             commit_hash = commit_hash();
   |                           ^^^^^^^^^^^^^ not a function
help: `commit_hash` is a unit variant, you need to write it without the parenthesis
   |
11 |             commit_hash = commit_hash;
   |                           ^^^^^^^^^^^
error: aborting due to previous error

First, the message talks about "enum variant", does it mean struct variant?
Second, I don't get why it does not compile anyway.
If I rename commit_hash() to get_commit_hash() it builds, but why can't it figure out that that a var and function are different things even if they have the same name?

Fun fact:
this doesn't build either

    pub fn new() -> VersionInfo {
        let commit_hash: Option<String>;
        commit_hash = commit_hash();

        VersionInfo {
            commit_hash,
        }
    }

but this does:

    pub fn new() -> VersionInfo {
        let commit_hash: Option<String> = commit_hash();

        VersionInfo {
            commit_hash,
        }
    }

Shouldn't the 2 snippets behave identical?

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 30, 2018
@estebank
Copy link
Contributor

estebank commented Aug 30, 2018

Shouldn't the 2 snippets behave identical?

They are not because on the second case the local binding commit_hash doesn't exist yet, so the mod level commit_hash function is not yet shadowed. In the other case, the function commit_hash has been shadowed by the local binding, so it fails to compile.

First, the message talks about "enum variant", does it mean struct variant?

No, it's just confused thinking that commit_variant is Option::None.


The error message should mention the shadowing of the function.

@estebank estebank added D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-resolve Area: Name/path resolution done by `rustc_resolve` specifically labels Jan 25, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Apr 12, 2020
@matthiaskrgr
Copy link
Member Author

Just ran into this again with a more simple case :(

fn a() {}

fn main() {
    let a: bool = false;
    if a {
        a();
    }
}

IMO this should compile because we know that fn a() is of type Fn .. and not bool so if a querying the let a variable is the only thing that makes sense here..?

The error message is not really helping imo

error[E0618]: expected function, found `bool`
 --> src/main.rs:7:9
  |
5 |     let a: bool = false;
  |         - `a` has type `bool`
6 |     if a {
7 |         a();
  |         ^--
  |         |
  |         call expression requires function

I wonder if type ascription could solve this:
if a: bool {

estebank added a commit to estebank/rust that referenced this issue Nov 15, 2023
When a local binding shadows a function that is then called, this local
binding will cause an E0618 error. We now point not only at the binding
definition, but also at the locally defined function of the same name.

```
error[E0618]: expected function, found `&str`
  --> $DIR/issue-22468.rs:3:13
   |
LL |     let foo = "bar";
   |         --- `foo` has type `&str`
LL |     let x = foo("baz");
   |             ^^^-------
   |             |
   |             call expression requires function
...
LL | fn foo(file: &str) -> bool {
   | -------------------------- this function of the same name is avalable here, but it shadowed by the local binding of the same name
```

Fix rust-lang#53841
estebank added a commit to estebank/rust that referenced this issue Nov 15, 2023
When a local binding shadows a function that is then called, this local
binding will cause an E0618 error. We now point not only at the binding
definition, but also at the locally defined function of the same name.

```
error[E0618]: expected function, found `&str`
  --> $DIR/issue-22468.rs:3:13
   |
LL |     let foo = "bar";
   |         --- `foo` has type `&str`
LL |     let x = foo("baz");
   |             ^^^-------
   |             |
   |             call expression requires function
...
LL | fn foo(file: &str) -> bool {
   | -------------------------- this function of the same name is avalable here, but it shadowed by the local binding of the same name
```

Fix rust-lang#53841
estebank added a commit to estebank/rust that referenced this issue Nov 15, 2023
When a local binding shadows a function that is then called, this local
binding will cause an E0618 error. We now point not only at the binding
definition, but also at the locally defined function of the same name.

```
error[E0618]: expected function, found `&str`
  --> $DIR/issue-22468.rs:3:13
   |
LL |     let foo = "bar";
   |         --- `foo` has type `&str`
LL |     let x = foo("baz");
   |             ^^^-------
   |             |
   |             call expression requires function
...
LL | fn foo(file: &str) -> bool {
   | -------------------------- this function of the same name is avalable here, but it shadowed by the local binding of the same name
```

Fix rust-lang#53841
estebank added a commit to estebank/rust that referenced this issue Nov 15, 2023
When a local binding shadows a function that is then called, this local
binding will cause an E0618 error. We now point not only at the binding
definition, but also at the locally defined function of the same name.

```
error[E0618]: expected function, found `&str`
  --> $DIR/issue-22468.rs:3:13
   |
LL |     let foo = "bar";
   |         --- `foo` has type `&str`
LL |     let x = foo("baz");
   |             ^^^-------
   |             |
   |             call expression requires function
...
LL | fn foo(file: &str) -> bool {
   | -------------------------- this function of the same name is avalable here, but it shadowed by the local binding of the same name
```

Fix rust-lang#53841
estebank added a commit to estebank/rust that referenced this issue Nov 18, 2023
When a local binding shadows a function that is then called, this local
binding will cause an E0618 error. We now point not only at the binding
definition, but also at the locally defined function of the same name.

```
error[E0618]: expected function, found `&str`
  --> $DIR/issue-22468.rs:3:13
   |
LL |     let foo = "bar";
   |         --- `foo` has type `&str`
LL |     let x = foo("baz");
   |             ^^^-------
   |             |
   |             call expression requires function
...
LL | fn foo(file: &str) -> bool {
   | -------------------------- this function of the same name is avalable here, but it shadowed by the local binding of the same name
```

Fix rust-lang#53841
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 18, 2023
When a local binding shadows a fn, point at fn def in call failure

When a local binding shadows a function that is then called, this local binding will cause an E0618 error. We now point not only at the binding definition, but also at the locally defined function of the same name.

```
error[E0618]: expected function, found `&str`
  --> $DIR/issue-22468.rs:3:13
   |
LL |     let foo = "bar";
   |         --- `foo` has type `&str`
LL |     let x = foo("baz");
   |             ^^^-------
   |             |
   |             call expression requires function
...
LL | fn foo(file: &str) -> bool {
   | -------------------------- this function of the same name is available here, but it shadowed by the local binding of the same name
```

Fix rust-lang#53841
@bors bors closed this as completed in 7141399 Nov 18, 2023
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 A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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.

3 participants