Open
Description
Feature gate: #![feature(proc_macro_expand)]
This is a tracking issue for the TokenStream::expand_expr
function for use in proc-macros. This function allows eagerly expanding TokenStreams containing expressions from within a proc-macro, such that they can be modified, giving proc-macros similar functionality to built-in macros like format_args!
, concat!
and include!
.
Public API
impl TokenStream {
pub fn expand_expr(&self) -> Result<TokenStream, ExpandError>;
}
#[non_exhaustive]
pub struct ExpandError;
impl Debug for ExpandError { ... }
impl Display for ExpandError { ... }
impl Error for ExpandError {}
impl !Send for ExpandError {}
impl !Sync for ExpandError {}
Steps / History
- Implementation for literal expressions: proc_macro: Add an expand_expr method to TokenStream #87264
- Generalization for other expression types: TODO
- (optional) Generalization to other AST types: TODO
- Final comment period (FCP)
- Stabilization PR
Unresolved Questions
- What types of expressions beyond literals should be supported?
- Should expanding non-expression macros (e.g. types, items, etc.) be supported?
- Should certain macros (e.g. those with
#[allow_internal_unstable]
) be expandable or left unexpanded? - How can we evolve which macros are expanded or not within the rustc stability model? Can downstream crates opt-in to additional macro expansion on nightly channels? Should an additional argument be provided to customize what should be expanded?
- Should compilation be allowed to proceed after an expand method errors?
- Are there concerns about stabilizing the span information generated by internal macros like
include!
? - How can we properly preserve span information when expanding expression macros?