diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2018-07-03 15:36:31 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2018-07-16 07:58:06 -0700 |
| commit | 65f3007fa8a08daf77f2b8382a56eb80cb277131 (patch) | |
| tree | 49537e475912785d0492cae1f7c3767b3ade7634 /src/libsyntax | |
| parent | 3d5753fda1ee8f729da1061e931e13b043f479a5 (diff) | |
| download | rust-65f3007fa8a08daf77f2b8382a56eb80cb277131.tar.gz rust-65f3007fa8a08daf77f2b8382a56eb80cb277131.zip | |
rustc: Stabilize much of the `proc_macro` feature
This commit stabilizes some of the `proc_macro` language feature as well as a number of APIs in the `proc_macro` crate as [previously discussed][1]. This means that on stable Rust you can now define custom procedural macros which operate as attributes attached to items or `macro_rules!`-like bang-style invocations. This extends the suite of currently stable procedural macros, custom derives, with custom attributes and custom bang macros. Note though that despite the stabilization in this commit procedural macros are still not usable on stable Rust. To stabilize that we'll need to stabilize at least part of the `use_extern_macros` feature. Currently you can define a procedural macro attribute but you can't import it to call it! A summary of the changes made in this PR (as well as the various consequences) is: * The `proc_macro` language and library features are now stable. * Other APIs not stabilized in the `proc_macro` crate are now named under a different feature, such as `proc_macro_diagnostic` or `proc_macro_span`. * A few checks in resolution for `proc_macro` being enabled have switched over to `use_extern_macros` being enabled. This means that code using `#![feature(proc_macro)]` today will likely need to move to `#![feature(use_extern_macros)]`. It's intended that this PR, once landed, will be followed up with an attempt to stabilize a small slice of `use_extern_macros` just for procedural macros to make this feature 100% usable on stable. [1]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/feature_gate.rs | 37 |
2 files changed, 15 insertions, 30 deletions
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 281ebaff272..1241e230b26 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1073,7 +1073,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { return attrs; } - if self.cx.ecfg.proc_macro_enabled() { + if self.cx.ecfg.use_extern_macros_enabled() { attr = find_attr_invoc(&mut attrs); } traits = collect_derives(&mut self.cx, &mut attrs); @@ -1096,7 +1096,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { return attrs; } - if self.cx.ecfg.proc_macro_enabled() { + if self.cx.ecfg.use_extern_macros_enabled() { attr = find_attr_invoc(&mut attrs); } attrs @@ -1406,7 +1406,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> { foreign_item: ast::ForeignItem) -> SmallVector<ast::ForeignItem> { let (attr, traits, foreign_item) = self.classify_item(foreign_item); - let explain = if self.cx.ecfg.proc_macro_enabled() { + let explain = if self.cx.ecfg.use_extern_macros_enabled() { feature_gate::EXPLAIN_PROC_MACROS_IN_EXTERN } else { feature_gate::EXPLAIN_MACROS_IN_EXTERN @@ -1592,7 +1592,7 @@ impl<'feat> ExpansionConfig<'feat> { fn enable_trace_macros = trace_macros, fn enable_allow_internal_unstable = allow_internal_unstable, fn enable_custom_derive = custom_derive, - fn proc_macro_enabled = proc_macro, + fn use_extern_macros_enabled = use_extern_macros, fn macros_in_extern_enabled = macros_in_extern, fn proc_macro_mod = proc_macro_mod, fn proc_macro_gen = proc_macro_gen, diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index f033c5006c5..1a73096505f 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -39,13 +39,6 @@ use symbol::{keywords, Symbol}; use std::{env, path}; macro_rules! set { - (proc_macro) => {{ - fn f(features: &mut Features, span: Span) { - features.declared_lib_features.push((Symbol::intern("proc_macro"), span)); - features.proc_macro = true; - } - f as fn(&mut Features, Span) - }}; ($field: ident) => {{ fn f(features: &mut Features, _: Span) { features.$field = true; @@ -303,9 +296,6 @@ declare_features! ( // rustc internal (active, abi_unadjusted, "1.16.0", None, None), - // Procedural macros 2.0. - (active, proc_macro, "1.16.0", Some(38356), Some(Edition::Edition2018)), - // Declarative macros 2.0 (`macro`). (active, decl_macro, "1.17.0", Some(39412), None), @@ -626,6 +616,8 @@ declare_features! ( (accepted, global_allocator, "1.28.0", Some(27389), None), // Allows `#[repr(transparent)]` attribute on newtype structs (accepted, repr_transparent, "1.28.0", Some(43036), None), + // Defining procedural macros in `proc-macro` crates + (accepted, proc_macro, "1.29.0", Some(38356), None), ); // If you change this, please modify src/doc/unstable-book as well. You must @@ -1033,15 +1025,8 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG ("windows_subsystem", Whitelisted, Ungated), - ("proc_macro_attribute", Normal, Gated(Stability::Unstable, - "proc_macro", - "attribute proc macros are currently unstable", - cfg_fn!(proc_macro))), - - ("proc_macro", Normal, Gated(Stability::Unstable, - "proc_macro", - "function-like proc macros are currently unstable", - cfg_fn!(proc_macro))), + ("proc_macro_attribute", Normal, Ungated), + ("proc_macro", Normal, Ungated), ("rustc_derive_registrar", Normal, Gated(Stability::Unstable, "rustc_derive_registrar", @@ -1542,7 +1527,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } - if self.context.features.proc_macro && attr::is_known(attr) { + if self.context.features.use_extern_macros && attr::is_known(attr) { return } @@ -1990,7 +1975,7 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute], /// A collector for mutually exclusive and interdependent features and their flag spans. #[derive(Default)] struct FeatureChecker { - proc_macro: Option<Span>, + use_extern_macros: Option<Span>, custom_attribute: Option<Span>, } @@ -1999,9 +1984,9 @@ impl FeatureChecker { // the branching can be eliminated by modifying `set!()` to set these spans // only for the features that need to be checked for mutual exclusion. fn collect(&mut self, features: &Features, span: Span) { - if features.proc_macro { - // If self.proc_macro is None, set to Some(span) - self.proc_macro = self.proc_macro.or(Some(span)); + if features.use_extern_macros { + // If self.use_extern_macros is None, set to Some(span) + self.use_extern_macros = self.use_extern_macros.or(Some(span)); } if features.custom_attribute { @@ -2010,8 +1995,8 @@ impl FeatureChecker { } fn check(self, handler: &Handler) { - if let (Some(pm_span), Some(ca_span)) = (self.proc_macro, self.custom_attribute) { - handler.struct_span_err(pm_span, "Cannot use `#![feature(proc_macro)]` and \ + if let (Some(pm_span), Some(ca_span)) = (self.use_extern_macros, self.custom_attribute) { + handler.struct_span_err(pm_span, "Cannot use `#![feature(use_extern_macros)]` and \ `#![feature(custom_attribute)] at the same time") .span_note(ca_span, "`#![feature(custom_attribute)]` declared here") .emit(); |
