Skip to content

Implement #[derive(From)] #144922

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Implement #[derive(From)] #144922

wants to merge 4 commits into from

Conversation

Kobzol
Copy link
Member

@Kobzol Kobzol commented Aug 4, 2025

Implements the #[derive(From)] feature (tracking issue, RFC 1).

It allows deriving the From impl on structs and tuple structs with exactly one field. Some implementation notes:

  • I wasn't exactly sure which spans to use in the derive generating code, so I just used span everywhere. 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.
  • I had to solve an import stability problem, where if I just added the unstable macro From to core::convert, previously working code like use std::convert::From would 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 (?).
  • I had to add Ty::AstTy, because the from function receives an argument with the type of the single field, and the existing variants of the Ty enum couldn't represent an arbitrary type.

Footnotes

  1. Note that the RFC has gone through the FCP, just rfcbot hasn't realized it yet, it should be merged soon.

@rustbot
Copy link
Collaborator

rustbot commented Aug 4, 2025

r? @SparrowLii

rustbot has assigned @SparrowLii.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 4, 2025
@Kobzol Kobzol added the F-derive_from Feature gate for #[derive(From)] built-in derive macro (RFC 3809). label Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-derive_from Feature gate for #[derive(From)] built-in derive macro (RFC 3809). S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants