Closed
Description
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
Labels
Area: Messages for errors, warnings, and lintsArea: Name/path resolution done by `rustc_resolve` specificallyCategory: An issue proposing an enhancement or a PR with one.Diagnostics: An error or lint that needs small tweaks.Relevant to the compiler team, which will review and decide on the PR/issue.