Skip to content

confusing diagnostic when variable name equals function name #53841

Closed
@matthiaskrgr

Description

@matthiaskrgr

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-resolveArea: Name/path resolution done by `rustc_resolve` specificallyC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions