Skip to content
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

Add default value for properties (takes a function instead of an expression) #881

Merged
merged 4 commits into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions crates/macro/src/derive_props/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ impl PropField {
// Hacks to avoid misleading error message.
quote_spanned! {span=>
#name: {
let none: Option<#ty> = None;
match true {
false => none,
true => Some(#default())
#[allow(unreachable_code)]
false => {
let __unreachable: #ty = ::std::unreachable!();
__unreachable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need an assign here? I think you can just do false => ::std::unreachable!() right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The false arm must return the correct type, otherwise Rust would think that it has the same type as #default(), and might show that misleading message: "help: try using a conversion method: "foo".to_string()".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm your explanation makes sense but I had it working with unimplemented like that. This is fine though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was wrong about that. I did not test it before leaving the comment. It seems that false => ::std::unreachable!() does work.

},
true => #default()
}
.unwrap()
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/derive_props/fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ error[E0308]: match arms have incompatible types
| |
| expected struct `std::string::String`, found i32
| `match` arms have incompatible types
| this is found to be of type `std::option::Option<std::string::String>`
| this is found to be of type `std::string::String`
|
= note: expected type `std::option::Option<std::string::String>`
found type `std::option::Option<i32>`
= note: expected type `std::string::String`
found type `i32`