syntax: Improve documentation of SyntaxExtension

This commit is contained in:
Vadim Petrochenkov 2019-06-07 01:59:27 +03:00
parent 8edbbacbca
commit 0468eb63ad

View File

@ -552,60 +552,78 @@ pub fn article(self) -> &'static str {
/// An enum representing the different kinds of syntax extensions. /// An enum representing the different kinds of syntax extensions.
pub enum SyntaxExtension { pub enum SyntaxExtension {
/// A trivial "extension" that does nothing, only keeps the attribute and marks it as known. /// A token-based function-like macro.
NonMacroAttr { mark_used: bool },
/// A syntax extension that is attached to an item and modifies it
/// in-place. Also allows decoration, i.e., creating new items.
MultiModifier(Box<dyn MultiItemModifier + sync::Sync + sync::Send>),
/// A function-like procedural macro. TokenStream -> TokenStream.
ProcMacro { ProcMacro {
/// An expander with signature TokenStream -> TokenStream.
expander: Box<dyn ProcMacro + sync::Sync + sync::Send>, expander: Box<dyn ProcMacro + sync::Sync + sync::Send>,
/// Whitelist of unstable features that are treated as stable inside this macro /// Whitelist of unstable features that are treated as stable inside this macro.
allow_internal_unstable: Option<Lrc<[Symbol]>>, allow_internal_unstable: Option<Lrc<[Symbol]>>,
/// Edition of the crate in which this macro is defined.
edition: Edition, edition: Edition,
}, },
/// An attribute-like procedural macro. TokenStream, TokenStream -> TokenStream. /// An AST-based function-like macro.
/// The first TokenSteam is the attribute, the second is the annotated item.
/// Allows modification of the input items and adding new items, similar to
/// MultiModifier, but uses TokenStreams, rather than AST nodes.
AttrProcMacro(Box<dyn AttrProcMacro + sync::Sync + sync::Send>, Edition),
/// A normal, function-like syntax extension.
///
/// `bytes!` is a `NormalTT`.
NormalTT { NormalTT {
/// An expander with signature TokenStream -> AST.
expander: Box<dyn TTMacroExpander + sync::Sync + sync::Send>, expander: Box<dyn TTMacroExpander + sync::Sync + sync::Send>,
/// Some info about the macro's definition point.
def_info: Option<(ast::NodeId, Span)>, def_info: Option<(ast::NodeId, Span)>,
/// Hygienic properties of identifiers produced by this macro.
transparency: Transparency, transparency: Transparency,
/// Whether the contents of the macro can /// Whitelist of unstable features that are treated as stable inside this macro.
/// directly use `#[unstable]` things.
///
/// Only allows things that require a feature gate in the given whitelist
allow_internal_unstable: Option<Lrc<[Symbol]>>, allow_internal_unstable: Option<Lrc<[Symbol]>>,
/// Whether the contents of the macro can use `unsafe` /// Suppresses the `unsafe_code` lint for code produced by this macro.
/// without triggering the `unsafe_code` lint.
allow_internal_unsafe: bool, allow_internal_unsafe: bool,
/// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`) /// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`) for this macro.
/// for a given macro.
local_inner_macros: bool, local_inner_macros: bool,
/// The macro's feature name if it is unstable, and the stability feature /// The macro's feature name and tracking issue number if it is unstable.
unstable_feature: Option<(Symbol, u32)>, unstable_feature: Option<(Symbol, u32)>,
/// Edition of the crate in which the macro is defined /// Edition of the crate in which this macro is defined.
edition: Edition, edition: Edition,
}, },
/// An attribute-like procedural macro. TokenStream -> TokenStream. /// A token-based attribute macro.
/// The input is the annotated item. AttrProcMacro(
/// Allows generating code to implement a Trait for a given struct /// An expander with signature (TokenStream, TokenStream) -> TokenStream.
/// or enum item. /// The first TokenSteam is the attribute itself, the second is the annotated item.
ProcMacroDerive(Box<dyn MultiItemModifier + sync::Sync + sync::Send>, /// The produced TokenSteam replaces the input TokenSteam.
Vec<Symbol> /* inert attribute names */, Edition), Box<dyn AttrProcMacro + sync::Sync + sync::Send>,
/// Edition of the crate in which this macro is defined.
Edition,
),
/// An attribute-like procedural macro that derives a builtin trait. /// An AST-based attribute macro.
BuiltinDerive(Box<dyn MultiItemModifier + sync::Sync + sync::Send>), MultiModifier(
/// An expander with signature (AST, AST) -> AST.
/// The first AST fragment is the attribute itself, the second is the annotated item.
/// The produced AST fragment replaces the input AST fragment.
Box<dyn MultiItemModifier + sync::Sync + sync::Send>,
),
/// A trivial attribute "macro" that does nothing,
/// only keeps the attribute and marks it as known.
NonMacroAttr {
/// Suppresses the `unused_attributes` lint for this attribute.
mark_used: bool,
},
/// A token-based derive macro.
ProcMacroDerive(
/// An expander with signature TokenStream -> TokenStream (not yet).
/// The produced TokenSteam is appended to the input TokenSteam.
Box<dyn MultiItemModifier + sync::Sync + sync::Send>,
/// Names of helper attributes registered by this macro.
Vec<Symbol>,
/// Edition of the crate in which this macro is defined.
Edition,
),
/// An AST-based derive macro.
BuiltinDerive(
/// An expander with signature AST -> AST.
/// The produced AST fragment is appended to the input AST fragment.
Box<dyn MultiItemModifier + sync::Sync + sync::Send>,
),
} }
impl SyntaxExtension { impl SyntaxExtension {