-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-inferenceArea: Type inferenceArea: Type inferenceA-type-systemArea: Type systemArea: Type systemC-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.T-langRelevant to the language teamRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Description
I don't know this issue should be feature request
or bug report
.
I have code like this:
#[derive(Debug, Fail)]
#[fail(display = "Target can't open: {:?}", path)]
struct NotExists {
path: PathBuf
}
#[derive(Debug, Fail)]
#[fail(display = "Target is not file: {:?}", path)]
struct NotFile {
path: PathBuf
}
fn foo() -> Result<(), failure::Error> {
let path = PathBuf::from("Rust/is/the/best/language");
if path.exists() {
Err(NotExists { path })?
}
if path.is_dir() {
Err(NotFile { path })?
}
unimplemented!()
}
The compiler error is:
24 | Err(NotExists { path })?
| ---- value moved here
25 | }
26 | if path.is_dir() {
| ^^^^ value used here after move
Obviously, Err(foo)?
equals return Err(foo.into());
, that means there should be no use after moved
error.
There are two other ways to achieve the goal:
if path.exists() {
Err(NotExists { path })?
} else if path.is_dir() {
Err(NotFile { path })?
} else {
unimplemented!()
}
or
if path.exists() {
return Err(NotExists { path }.into());
}
I think with control flow analysis, compiler should be able to pass my origin code(Err(foo)?
version).
jonhoo
Metadata
Metadata
Assignees
Labels
A-inferenceArea: Type inferenceArea: Type inferenceA-type-systemArea: Type systemArea: Type systemC-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.T-langRelevant to the language teamRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.