diff options
| author | Jakub Beránek <berykubik@gmail.com> | 2025-08-04 17:24:18 +0200 |
|---|---|---|
| committer | Jakub Beránek <berykubik@gmail.com> | 2025-08-15 12:06:20 +0200 |
| commit | e935a155c2078185f0705bb972a38953e770e393 (patch) | |
| tree | 86f4aa3d98fa77383bf0a2fb57dff0aac7eb7af1 | |
| parent | f39085bb372f00b5b6849001e947727c9dc77ba3 (diff) | |
| download | rust-e935a155c2078185f0705bb972a38953e770e393.tar.gz rust-e935a155c2078185f0705bb972a38953e770e393.zip | |
Create unstable `From` builtin macro and register it
| -rw-r--r-- | compiler/rustc_builtin_macros/src/deriving/from.rs | 13 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/deriving/mod.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/lib.rs | 1 | ||||
| -rw-r--r-- | library/core/src/macros/mod.rs | 11 | ||||
| -rw-r--r-- | library/core/src/prelude/v1.rs | 7 |
5 files changed, 33 insertions, 0 deletions
diff --git a/compiler/rustc_builtin_macros/src/deriving/from.rs b/compiler/rustc_builtin_macros/src/deriving/from.rs new file mode 100644 index 00000000000..79fd4caa440 --- /dev/null +++ b/compiler/rustc_builtin_macros/src/deriving/from.rs @@ -0,0 +1,13 @@ +use rustc_ast as ast; +use rustc_expand::base::{Annotatable, ExtCtxt}; +use rustc_span::{Span, sym}; + +pub(crate) fn expand_deriving_from( + cx: &ExtCtxt<'_>, + span: Span, + mitem: &ast::MetaItem, + item: &Annotatable, + push: &mut dyn FnMut(Annotatable), + is_const: bool, +) { +} diff --git a/compiler/rustc_builtin_macros/src/deriving/mod.rs b/compiler/rustc_builtin_macros/src/deriving/mod.rs index 1edc2965def..cee6952fa34 100644 --- a/compiler/rustc_builtin_macros/src/deriving/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/mod.rs @@ -23,6 +23,7 @@ pub(crate) mod clone; pub(crate) mod coerce_pointee; pub(crate) mod debug; pub(crate) mod default; +pub(crate) mod from; pub(crate) mod hash; #[path = "cmp/eq.rs"] diff --git a/compiler/rustc_builtin_macros/src/lib.rs b/compiler/rustc_builtin_macros/src/lib.rs index 7bc448a9acb..86a4927f390 100644 --- a/compiler/rustc_builtin_macros/src/lib.rs +++ b/compiler/rustc_builtin_macros/src/lib.rs @@ -139,6 +139,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) { PartialEq: partial_eq::expand_deriving_partial_eq, PartialOrd: partial_ord::expand_deriving_partial_ord, CoercePointee: coerce_pointee::expand_deriving_coerce_pointee, + From: from::expand_deriving_from, } let client = rustc_proc_macro::bridge::client::Client::expand1(rustc_proc_macro::quote); diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index db8b527d593..dea459e83bc 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -1768,4 +1768,15 @@ pub(crate) mod builtin { pub macro deref($pat:pat) { builtin # deref($pat) } + + /// Derive macro generating an impl of the trait `From`. + /// Currently, it can only be used on single-field structs. + // Note that the macro is in a different module than the `From` trait, + // to avoid triggering an unstable feature being used if someone imports + // `std::convert::From`. + #[rustc_builtin_macro] + #[unstable(feature = "derive_from", issue = "144889")] + pub macro From($item: item) { + /* compiler built-in */ + } } diff --git a/library/core/src/prelude/v1.rs b/library/core/src/prelude/v1.rs index a4be66b90ca..d8d82afb0e6 100644 --- a/library/core/src/prelude/v1.rs +++ b/library/core/src/prelude/v1.rs @@ -117,3 +117,10 @@ pub use crate::macros::builtin::deref; reason = "`type_alias_impl_trait` has open design concerns" )] pub use crate::macros::builtin::define_opaque; + +#[unstable( + feature = "derive_from", + issue = "144889", + reason = "`derive(From)` is unstable" +)] +pub use crate::macros::builtin::From; |
