|
| 1 | +use rustc_ast::token; |
| 2 | +use rustc_ast::tokenstream::{DelimSpan, DelimSpacing, Spacing, TokenStream, TokenTree}; |
| 3 | +use rustc_errors::ErrorGuaranteed; |
| 4 | +use rustc_expand::base::{AttrProcMacro, ExtCtxt}; |
| 5 | +use rustc_span::symbol::{sym, Symbol}; |
| 6 | +use rustc_span::Span; |
| 7 | + |
| 8 | +pub struct ExpandRequires; |
| 9 | + |
| 10 | +impl AttrProcMacro for ExpandRequires { |
| 11 | + fn expand<'cx>( |
| 12 | + &self, |
| 13 | + ecx: &'cx mut ExtCtxt<'_>, |
| 14 | + span: Span, |
| 15 | + annotation: TokenStream, |
| 16 | + annotated: TokenStream, |
| 17 | + ) -> Result<TokenStream, ErrorGuaranteed> { |
| 18 | + expand_requires_tts(ecx, span, annotation, annotated) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +fn expand_requires_tts( |
| 23 | + _ecx: &mut ExtCtxt<'_>, |
| 24 | + attr_span: Span, |
| 25 | + annotation: TokenStream, |
| 26 | + annotated: TokenStream, |
| 27 | +) -> Result<TokenStream, ErrorGuaranteed> { |
| 28 | + let mut new_tts = Vec::with_capacity(annotated.len()); |
| 29 | + let mut cursor = annotated.into_trees(); |
| 30 | + |
| 31 | + // XXX this will break if you have a `{ ... }` syntactically prior to the fn body, |
| 32 | + // e.g. if a `const { ... }` can occur in a where clause. We need to do something |
| 33 | + // smarter for injecting this code into the right place. |
| 34 | + while let Some(tt) = cursor.next_ref() { |
| 35 | + if let TokenTree::Delimited(sp, spacing, delim @ token::Delimiter::Brace, inner_ts) = tt { |
| 36 | + let revised_tt = TokenTree::Delimited( |
| 37 | + *sp, *spacing, *delim, |
| 38 | + |
| 39 | + // XXX a constructed ast doesn't actually *carry* |
| 40 | + // tokens from which to build a TokenStream. |
| 41 | + // So instead, use the original annotation directly. |
| 42 | + // TokenStream::from_ast(&ast_for_invoke) |
| 43 | + |
| 44 | + std::iter::once(TokenTree::token_alone(token::Ident(sym::core, token::IdentIsRaw::No), attr_span)) |
| 45 | + .chain(std::iter::once(TokenTree::token_alone(token::PathSep, attr_span))) |
| 46 | + .chain(std::iter::once(TokenTree::token_alone(token::Ident(sym::intrinsics, token::IdentIsRaw::No), attr_span))) |
| 47 | + .chain(std::iter::once(TokenTree::token_alone(token::PathSep, attr_span))) |
| 48 | + .chain(std::iter::once(TokenTree::token_alone(token::Ident(sym::contract_check, token::IdentIsRaw::No), attr_span))) |
| 49 | + .chain(std::iter::once(TokenTree::Delimited( |
| 50 | + DelimSpan::from_single(attr_span), |
| 51 | + DelimSpacing { open: Spacing::JointHidden, close: Spacing::JointHidden }, |
| 52 | + token::Delimiter::Parenthesis, |
| 53 | + TokenStream::new( |
| 54 | + std::iter::once(TokenTree::token_alone(token::BinOp(token::BinOpToken::Or), attr_span)) |
| 55 | + .chain(std::iter::once(TokenTree::token_alone(token::BinOp(token::BinOpToken::Or), attr_span))) |
| 56 | + .chain(std::iter::once(TokenTree::Delimited(DelimSpan::from_single(attr_span), |
| 57 | + DelimSpacing { open: Spacing::JointHidden, close: Spacing::JointHidden }, |
| 58 | + token::Delimiter::Brace, |
| 59 | + annotation))) |
| 60 | + .chain(std::iter::once(TokenTree::token_alone(token::Comma, attr_span))) |
| 61 | + .chain(std::iter::once(TokenTree::token_alone(token::Literal(token::Lit::new(token::LitKind::Str, Symbol::intern("contract failure"), None)), attr_span))) |
| 62 | + .collect() |
| 63 | + )))) |
| 64 | + .chain(TokenStream::token_alone(token::Semi, attr_span).trees().cloned()) |
| 65 | + .chain(inner_ts.trees().cloned()) |
| 66 | + .collect()); |
| 67 | + new_tts.push(revised_tt); |
| 68 | + break; |
| 69 | + } else { |
| 70 | + new_tts.push(tt.clone()); |
| 71 | + continue; |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + // Above we injected the intrinsic call. Now just copy over all the other token trees. |
| 76 | + while let Some(tt) = cursor.next_ref() { |
| 77 | + new_tts.push(tt.clone()); |
| 78 | + } |
| 79 | + Ok(TokenStream::new(new_tts)) |
| 80 | +} |
0 commit comments