Repository navigation
Implement #[derive(From)] - #144922
Implement #[derive(From)]#144922
#[derive(From)]#144922Conversation
|
r? @SparrowLii rustbot has assigned @SparrowLii. Use |
commented
Aug 10, 2025
|
☔ The latest upstream changes (presumably #145146) made this pull request unmergeable. Please resolve the merge conflicts. |
left a comment
There was a problem hiding this comment.
On the right track, but a few things need fixing.
There is a test called tests/ui/deriving/deriving-all-codegen.rs which tests the output of all builtin derives. It's really useful to have a test that shows the actual output, so that minor changes can be seen.
From should be added to that file. A lot of the structs don't have a single field, it would be worth adding/moving some of the ones from deriving-from.rs to it.
The derive(...) lists are in mostly-alphabetical order, so I would put From in between Default and Hash.
@rustbot author
commented
Aug 15, 2025
|
I tried to resolve review remarks, and improve the error message(s). Before fixing tests, I'd like to know your opinion on the current errors. |
commented
Aug 15, 2025
|
Changes to the code generated for builtin derived traits. cc @nnethercote |
This comment has been minimized.
This comment has been minimized.
commented
Aug 15, 2025
|
Good progress. The new error messages look good, thanks. |
commented
Aug 15, 2025
|
Ok: squash the final 7 commits together, undo the formatting changes to |
commented
Aug 15, 2025
|
@bors r=nnethercote |
Implements the
#[derive(From)]feature (tracking issue, RFC).It allows deriving the
Fromimpl on structs and tuple structs with exactly one field. Some implementation notes:spaneverywhere. I don't know if it's the Right Thing To Do. In particular the errors when#[derive(From)]is used on a struct with an unsized field are weirdly duplicated.macro Fromtocore::convert, previously working code likeuse std::convert::Fromwould suddenly require an unstable feature gate, because rustc would think that you're trying to import the unstable macro. @petrochenkov suggested that I add the macro the the core prelude instead. This has worked well, although it only works in edition 2021+. Not sure if I botched the prelude somehow and it should live elsewhere (?).Ty::AstTy, because thefromfunction receives an argument with the type of the single field, and the existing variants of theTyenum couldn't represent an arbitrary type.