2014-02-11 22:43:23 -08:00
|
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2017-03-16 10:23:33 +00:00
|
|
|
|
use ast::{self, Block, Ident, NodeId, PatKind, Path};
|
2017-03-08 23:13:35 +00:00
|
|
|
|
use ast::{MacStmtStyle, StmtKind, ItemKind};
|
2016-06-22 08:03:42 +00:00
|
|
|
|
use attr::{self, HasAttrs};
|
2018-08-18 12:14:03 +02:00
|
|
|
|
use source_map::{ExpnInfo, MacroBang, MacroAttribute, dummy_spanned, respan};
|
2018-07-20 18:04:02 -07:00
|
|
|
|
use config::StripUnconfigured;
|
suggestion applicabilities for libsyntax and librustc, run-rustfix tests
Consider this a down payment on #50723. To recap, an `Applicability`
enum was recently (#50204) added, to convey to Rustfix and other tools
whether we think it's OK for them to blindly apply the suggestion, or
whether to prompt a human for guidance (because the suggestion might
contain placeholders that we can't infer, or because we think it has a
sufficiently high probability of being wrong even though it's—
presumably—right often enough to be worth emitting in the first place).
When a suggestion is marked as `MaybeIncorrect`, we try to use comments
to indicate precisely why (although there are a few places where we just
say `// speculative` because the present author's subjective judgement
balked at the idea that the suggestion has no false positives).
The `run-rustfix` directive is opporunistically set on some relevant UI
tests (and a couple tests that were in the `test/ui/suggestions`
directory, even if the suggestions didn't originate in librustc or
libsyntax). This is less trivial than it sounds, because a surprising
number of test files aren't equipped to be tested as fixed even when
they contain successfully fixable errors, because, e.g., there are more,
not-directly-related errors after fixing. Some test files need an
attribute or underscore to avoid unused warnings tripping up the "fixed
code is still producing diagnostics" check despite the fixes being
correct; this is an interesting contrast-to/inconsistency-with the
behavior of UI tests (which secretly pass `-A unused`), a behavior which
we probably ought to resolve one way or the other (filed issue #50926).
A few suggestion labels are reworded (e.g., to avoid phrasing it as a
question, which which is discouraged by the style guidelines listed in
`.span_suggestion`'s doc-comment).
2018-05-19 14:52:24 -07:00
|
|
|
|
use errors::{Applicability, FatalError};
|
2012-09-04 11:37:29 -07:00
|
|
|
|
use ext::base::*;
|
2017-02-02 07:01:15 +00:00
|
|
|
|
use ext::derive::{add_derived_markers, collect_derives};
|
2018-04-28 02:08:16 +03:00
|
|
|
|
use ext::hygiene::{self, Mark, SyntaxContext};
|
2017-02-01 21:03:09 +10:30
|
|
|
|
use ext::placeholders::{placeholder, PlaceholderExpander};
|
2018-02-24 19:11:06 -08:00
|
|
|
|
use feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err};
|
2014-07-02 23:16:01 -07:00
|
|
|
|
use fold;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
use fold::*;
|
2018-05-09 15:03:02 -07:00
|
|
|
|
use parse::{DirectoryOwnership, PResult, ParseSess};
|
2017-03-29 01:55:01 +00:00
|
|
|
|
use parse::token::{self, Token};
|
2016-09-06 17:57:58 +12:00
|
|
|
|
use parse::parser::Parser;
|
2014-09-13 19:06:01 +03:00
|
|
|
|
use ptr::P;
|
2018-08-05 12:04:56 +02:00
|
|
|
|
use OneVector;
|
2017-02-01 21:03:09 +10:30
|
|
|
|
use symbol::Symbol;
|
2016-11-16 08:21:52 +00:00
|
|
|
|
use symbol::keywords;
|
2017-12-14 08:09:19 +01:00
|
|
|
|
use syntax_pos::{Span, DUMMY_SP, FileName};
|
2017-11-14 01:24:36 +02:00
|
|
|
|
use syntax_pos::hygiene::ExpnFormat;
|
2017-03-29 01:55:01 +00:00
|
|
|
|
use tokenstream::{TokenStream, TokenTree};
|
2018-05-09 15:03:02 -07:00
|
|
|
|
use visit::{self, Visitor};
|
2011-08-05 13:06:11 -07:00
|
|
|
|
|
2018-08-18 13:55:43 +03:00
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2017-09-21 22:37:00 -05:00
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::Read;
|
2018-07-23 02:52:51 +03:00
|
|
|
|
use std::{iter, mem};
|
2016-09-01 06:44:54 +00:00
|
|
|
|
use std::rc::Rc;
|
2017-12-14 08:09:19 +01:00
|
|
|
|
use std::path::PathBuf;
|
2016-08-31 09:02:45 +00:00
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
macro_rules! ast_fragments {
|
2018-06-23 01:05:07 +03:00
|
|
|
|
(
|
|
|
|
|
$($Kind:ident($AstTy:ty) {
|
|
|
|
|
$kind_name:expr;
|
2018-06-15 21:49:00 -05:00
|
|
|
|
// FIXME: HACK: this should be `$(one ...)?` and `$(many ...)?` but `?` macro
|
|
|
|
|
// repetition was removed from 2015 edition in #51587 because of ambiguities.
|
|
|
|
|
$(one fn $fold_ast:ident; fn $visit_ast:ident;)*
|
|
|
|
|
$(many fn $fold_ast_elt:ident; fn $visit_ast_elt:ident;)*
|
2018-06-23 01:05:07 +03:00
|
|
|
|
fn $make_ast:ident;
|
|
|
|
|
})*
|
|
|
|
|
) => {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
/// A fragment of AST that can be produced by a single macro expansion.
|
|
|
|
|
/// Can also serve as an input and intermediate result for macro expansion operations.
|
2018-06-23 01:05:07 +03:00
|
|
|
|
pub enum AstFragment {
|
|
|
|
|
OptExpr(Option<P<ast::Expr>>),
|
|
|
|
|
$($Kind($AstTy),)*
|
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
|
|
|
|
|
/// "Discriminant" of an AST fragment.
|
2016-09-23 09:32:58 +00:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
2018-06-23 01:05:07 +03:00
|
|
|
|
pub enum AstFragmentKind {
|
|
|
|
|
OptExpr,
|
|
|
|
|
$($Kind,)*
|
|
|
|
|
}
|
2016-08-27 05:27:59 +00:00
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
impl AstFragmentKind {
|
2016-09-23 09:32:58 +00:00
|
|
|
|
pub fn name(self) -> &'static str {
|
2016-08-27 05:27:59 +00:00
|
|
|
|
match self {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::OptExpr => "expression",
|
2018-06-23 01:05:07 +03:00
|
|
|
|
$(AstFragmentKind::$Kind => $kind_name,)*
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-24 06:12:54 +00:00
|
|
|
|
|
2018-07-10 21:06:26 +02:00
|
|
|
|
fn make_from<'a>(self, result: Box<dyn MacResult + 'a>) -> Option<AstFragment> {
|
2016-08-27 05:27:59 +00:00
|
|
|
|
match self {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::OptExpr =>
|
|
|
|
|
result.make_expr().map(Some).map(AstFragment::OptExpr),
|
2018-06-23 01:05:07 +03:00
|
|
|
|
$(AstFragmentKind::$Kind => result.$make_ast().map(AstFragment::$Kind),)*
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-17 11:02:42 +00:00
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
impl AstFragment {
|
2016-08-29 05:32:41 +00:00
|
|
|
|
pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
|
2016-08-27 05:27:59 +00:00
|
|
|
|
match self {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragment::OptExpr(expr) => expr,
|
|
|
|
|
_ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-23 01:05:07 +03:00
|
|
|
|
|
|
|
|
|
$(pub fn $make_ast(self) -> $AstTy {
|
2016-08-27 05:27:59 +00:00
|
|
|
|
match self {
|
2018-06-23 01:05:07 +03:00
|
|
|
|
AstFragment::$Kind(ast) => ast,
|
2018-06-20 02:08:08 +03:00
|
|
|
|
_ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
2018-06-23 01:05:07 +03:00
|
|
|
|
})*
|
2016-05-19 09:45:37 +00:00
|
|
|
|
|
2016-08-29 05:32:41 +00:00
|
|
|
|
pub fn fold_with<F: Folder>(self, folder: &mut F) -> Self {
|
2016-08-27 05:27:59 +00:00
|
|
|
|
match self {
|
2018-06-23 01:05:07 +03:00
|
|
|
|
AstFragment::OptExpr(expr) =>
|
|
|
|
|
AstFragment::OptExpr(expr.and_then(|expr| folder.fold_opt_expr(expr))),
|
|
|
|
|
$($(AstFragment::$Kind(ast) =>
|
2018-06-15 21:49:00 -05:00
|
|
|
|
AstFragment::$Kind(folder.$fold_ast(ast)),)*)*
|
2018-06-23 01:05:07 +03:00
|
|
|
|
$($(AstFragment::$Kind(ast) =>
|
|
|
|
|
AstFragment::$Kind(ast.into_iter()
|
|
|
|
|
.flat_map(|ast| folder.$fold_ast_elt(ast))
|
2018-06-15 21:49:00 -05:00
|
|
|
|
.collect()),)*)*
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
|
pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
|
2016-09-07 23:21:59 +00:00
|
|
|
|
match *self {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragment::OptExpr(Some(ref expr)) => visitor.visit_expr(expr),
|
|
|
|
|
AstFragment::OptExpr(None) => {}
|
2018-06-15 21:49:00 -05:00
|
|
|
|
$($(AstFragment::$Kind(ref ast) => visitor.$visit_ast(ast),)*)*
|
2018-06-23 01:05:07 +03:00
|
|
|
|
$($(AstFragment::$Kind(ref ast) => for ast_elt in &ast[..] {
|
|
|
|
|
visitor.$visit_ast_elt(ast_elt);
|
2018-06-15 21:49:00 -05:00
|
|
|
|
})*)*
|
2016-09-07 23:21:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
|
|
|
|
impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
|
|
|
|
|
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
2018-06-23 01:05:07 +03:00
|
|
|
|
self.expand_fragment(AstFragment::OptExpr(Some(expr))).make_opt_expr()
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2018-06-23 01:05:07 +03:00
|
|
|
|
$($(fn $fold_ast(&mut self, ast: $AstTy) -> $AstTy {
|
|
|
|
|
self.expand_fragment(AstFragment::$Kind(ast)).$make_ast()
|
2018-06-15 21:49:00 -05:00
|
|
|
|
})*)*
|
2018-06-23 01:05:07 +03:00
|
|
|
|
$($(fn $fold_ast_elt(&mut self, ast_elt: <$AstTy as IntoIterator>::Item) -> $AstTy {
|
2018-08-13 22:15:16 +03:00
|
|
|
|
self.expand_fragment(AstFragment::$Kind(smallvec![ast_elt])).$make_ast()
|
2018-06-15 21:49:00 -05:00
|
|
|
|
})*)*
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2016-09-23 09:32:58 +00:00
|
|
|
|
|
|
|
|
|
impl<'a> MacResult for ::ext::tt::macro_rules::ParserAnyMacro<'a> {
|
2018-06-23 01:05:07 +03:00
|
|
|
|
$(fn $make_ast(self: Box<::ext::tt::macro_rules::ParserAnyMacro<'a>>)
|
|
|
|
|
-> Option<$AstTy> {
|
|
|
|
|
Some(self.make(AstFragmentKind::$Kind).$make_ast())
|
2016-09-23 09:32:58 +00:00
|
|
|
|
})*
|
|
|
|
|
}
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
ast_fragments! {
|
2018-06-23 01:05:07 +03:00
|
|
|
|
Expr(P<ast::Expr>) { "expression"; one fn fold_expr; fn visit_expr; fn make_expr; }
|
|
|
|
|
Pat(P<ast::Pat>) { "pattern"; one fn fold_pat; fn visit_pat; fn make_pat; }
|
|
|
|
|
Ty(P<ast::Ty>) { "type"; one fn fold_ty; fn visit_ty; fn make_ty; }
|
2018-08-05 12:04:56 +02:00
|
|
|
|
Stmts(OneVector<ast::Stmt>) { "statement"; many fn fold_stmt; fn visit_stmt; fn make_stmts; }
|
|
|
|
|
Items(OneVector<P<ast::Item>>) { "item"; many fn fold_item; fn visit_item; fn make_items; }
|
|
|
|
|
TraitItems(OneVector<ast::TraitItem>) {
|
2018-06-23 01:05:07 +03:00
|
|
|
|
"trait item"; many fn fold_trait_item; fn visit_trait_item; fn make_trait_items;
|
|
|
|
|
}
|
2018-08-05 12:04:56 +02:00
|
|
|
|
ImplItems(OneVector<ast::ImplItem>) {
|
2018-06-23 01:05:07 +03:00
|
|
|
|
"impl item"; many fn fold_impl_item; fn visit_impl_item; fn make_impl_items;
|
|
|
|
|
}
|
2018-08-05 12:04:56 +02:00
|
|
|
|
ForeignItems(OneVector<ast::ForeignItem>) {
|
2018-06-23 01:05:07 +03:00
|
|
|
|
"foreign item"; many fn fold_foreign_item; fn visit_foreign_item; fn make_foreign_items;
|
|
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
impl AstFragmentKind {
|
|
|
|
|
fn dummy(self, span: Span) -> Option<AstFragment> {
|
2017-12-26 16:47:32 +09:00
|
|
|
|
self.make_from(DummyResult::any(span))
|
2016-06-11 22:59:33 +00:00
|
|
|
|
}
|
2016-09-02 06:14:38 +00:00
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(self, items: I)
|
|
|
|
|
-> AstFragment {
|
2018-04-17 23:19:21 -07:00
|
|
|
|
let mut items = items.into_iter();
|
2016-09-02 06:14:38 +00:00
|
|
|
|
match self {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::Items =>
|
|
|
|
|
AstFragment::Items(items.map(Annotatable::expect_item).collect()),
|
|
|
|
|
AstFragmentKind::ImplItems =>
|
|
|
|
|
AstFragment::ImplItems(items.map(Annotatable::expect_impl_item).collect()),
|
|
|
|
|
AstFragmentKind::TraitItems =>
|
|
|
|
|
AstFragment::TraitItems(items.map(Annotatable::expect_trait_item).collect()),
|
|
|
|
|
AstFragmentKind::ForeignItems =>
|
|
|
|
|
AstFragment::ForeignItems(items.map(Annotatable::expect_foreign_item).collect()),
|
|
|
|
|
AstFragmentKind::Stmts =>
|
|
|
|
|
AstFragment::Stmts(items.map(Annotatable::expect_stmt).collect()),
|
|
|
|
|
AstFragmentKind::Expr => AstFragment::Expr(
|
2018-04-17 23:19:21 -07:00
|
|
|
|
items.next().expect("expected exactly one expression").expect_expr()
|
|
|
|
|
),
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::OptExpr =>
|
|
|
|
|
AstFragment::OptExpr(items.next().map(Annotatable::expect_expr)),
|
|
|
|
|
AstFragmentKind::Pat | AstFragmentKind::Ty =>
|
2018-04-17 23:19:21 -07:00
|
|
|
|
panic!("patterns and types aren't annotatable"),
|
2016-09-02 06:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-16 10:09:23 +00:00
|
|
|
|
}
|
2015-01-27 01:22:12 +01:00
|
|
|
|
|
2017-11-14 01:24:36 +02:00
|
|
|
|
fn macro_bang_format(path: &ast::Path) -> ExpnFormat {
|
|
|
|
|
// We don't want to format a path using pretty-printing,
|
|
|
|
|
// `format!("{}", path)`, because that tries to insert
|
|
|
|
|
// line-breaks and is slow.
|
|
|
|
|
let mut path_str = String::with_capacity(64);
|
|
|
|
|
for (i, segment) in path.segments.iter().enumerate() {
|
|
|
|
|
if i != 0 {
|
|
|
|
|
path_str.push_str("::");
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-18 03:53:41 +03:00
|
|
|
|
if segment.ident.name != keywords::CrateRoot.name() &&
|
|
|
|
|
segment.ident.name != keywords::DollarCrate.name()
|
2017-11-14 01:24:36 +02:00
|
|
|
|
{
|
2018-05-26 15:12:38 +03:00
|
|
|
|
path_str.push_str(&segment.ident.as_str())
|
2017-11-14 01:24:36 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MacroBang(Symbol::intern(&path_str))
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-27 05:27:59 +00:00
|
|
|
|
pub struct Invocation {
|
2016-09-07 23:21:59 +00:00
|
|
|
|
pub kind: InvocationKind,
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fragment_kind: AstFragmentKind,
|
2017-03-01 23:48:16 +00:00
|
|
|
|
pub expansion_data: ExpansionData,
|
2016-09-02 06:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
|
pub enum InvocationKind {
|
2016-09-02 06:14:38 +00:00
|
|
|
|
Bang {
|
|
|
|
|
mac: ast::Mac,
|
|
|
|
|
ident: Option<Ident>,
|
|
|
|
|
span: Span,
|
|
|
|
|
},
|
|
|
|
|
Attr {
|
2017-02-02 07:01:15 +00:00
|
|
|
|
attr: Option<ast::Attribute>,
|
2017-03-08 23:13:35 +00:00
|
|
|
|
traits: Vec<Path>,
|
2016-09-02 06:14:38 +00:00
|
|
|
|
item: Annotatable,
|
|
|
|
|
},
|
2017-02-01 21:03:09 +10:30
|
|
|
|
Derive {
|
2017-03-08 23:13:35 +00:00
|
|
|
|
path: Path,
|
2017-02-01 21:03:09 +10:30
|
|
|
|
item: Annotatable,
|
|
|
|
|
},
|
2011-07-06 15:22:23 -07:00
|
|
|
|
}
|
2013-07-29 17:25:00 -07:00
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
|
impl Invocation {
|
2018-07-12 13:24:59 +03:00
|
|
|
|
pub fn span(&self) -> Span {
|
2016-09-07 23:21:59 +00:00
|
|
|
|
match self.kind {
|
|
|
|
|
InvocationKind::Bang { span, .. } => span,
|
2017-02-02 07:01:15 +00:00
|
|
|
|
InvocationKind::Attr { attr: Some(ref attr), .. } => attr.span,
|
2017-03-08 23:13:35 +00:00
|
|
|
|
InvocationKind::Attr { attr: None, .. } => DUMMY_SP,
|
|
|
|
|
InvocationKind::Derive { ref path, .. } => path.span,
|
2016-09-07 23:21:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
|
pub struct MacroExpander<'a, 'b:'a> {
|
2014-03-27 15:39:48 -07:00
|
|
|
|
pub cx: &'a mut ExtCtxt<'b>,
|
2016-09-06 05:42:45 +00:00
|
|
|
|
monotonic: bool, // c.f. `cx.monotonic_expander()`
|
2014-12-14 15:42:41 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'b> MacroExpander<'a, 'b> {
|
2016-09-06 05:42:45 +00:00
|
|
|
|
pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
|
2016-09-12 09:47:54 +00:00
|
|
|
|
MacroExpander { cx: cx, monotonic: monotonic }
|
2014-12-14 15:42:41 +13:00
|
|
|
|
}
|
2016-05-16 10:09:23 +00:00
|
|
|
|
|
2016-09-29 00:22:46 +00:00
|
|
|
|
pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
|
|
|
|
|
let mut module = ModuleData {
|
2016-11-16 08:21:52 +00:00
|
|
|
|
mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
|
2018-08-18 12:14:09 +02:00
|
|
|
|
directory: match self.cx.source_map().span_to_unmapped_path(krate.span) {
|
2017-12-14 08:09:19 +01:00
|
|
|
|
FileName::Real(path) => path,
|
|
|
|
|
other => PathBuf::from(other.to_string()),
|
|
|
|
|
},
|
2016-09-29 00:22:46 +00:00
|
|
|
|
};
|
|
|
|
|
module.directory.pop();
|
2017-09-21 22:37:00 -05:00
|
|
|
|
self.cx.root_path = module.directory.clone();
|
2016-09-29 00:22:46 +00:00
|
|
|
|
self.cx.current_expansion.module = Rc::new(module);
|
2018-02-24 19:11:06 -08:00
|
|
|
|
self.cx.current_expansion.crate_span = Some(krate.span);
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
2017-04-20 16:13:13 -07:00
|
|
|
|
let orig_mod_span = krate.module.inner;
|
|
|
|
|
|
2018-08-13 22:15:16 +03:00
|
|
|
|
let krate_item = AstFragment::Items(smallvec![P(ast::Item {
|
2016-09-21 09:20:42 +00:00
|
|
|
|
attrs: krate.attrs,
|
|
|
|
|
span: krate.span,
|
|
|
|
|
node: ast::ItemKind::Mod(krate.module),
|
|
|
|
|
ident: keywords::Invalid.ident(),
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2018-03-10 17:45:47 +03:00
|
|
|
|
vis: respan(krate.span.shrink_to_lo(), ast::VisibilityKind::Public),
|
2017-07-10 17:44:46 -07:00
|
|
|
|
tokens: None,
|
2018-08-13 22:15:16 +03:00
|
|
|
|
})]);
|
2016-09-21 09:20:42 +00:00
|
|
|
|
|
2018-06-23 01:05:07 +03:00
|
|
|
|
match self.expand_fragment(krate_item).make_items().pop().map(P::into_inner) {
|
2017-04-20 16:13:13 -07:00
|
|
|
|
Some(ast::Item { attrs, node: ast::ItemKind::Mod(module), .. }) => {
|
2016-09-21 09:20:42 +00:00
|
|
|
|
krate.attrs = attrs;
|
|
|
|
|
krate.module = module;
|
|
|
|
|
},
|
2017-04-20 16:13:13 -07:00
|
|
|
|
None => {
|
|
|
|
|
// Resolution failed so we return an empty expansion
|
|
|
|
|
krate.attrs = vec![];
|
|
|
|
|
krate.module = ast::Mod {
|
|
|
|
|
inner: orig_mod_span,
|
|
|
|
|
items: vec![],
|
|
|
|
|
};
|
|
|
|
|
},
|
2016-09-06 01:45:23 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2017-05-05 21:49:59 -07:00
|
|
|
|
self.cx.trace_macros_diag();
|
2016-09-02 09:12:47 +00:00
|
|
|
|
krate
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
// Fully expand all macro invocations in this AST fragment.
|
2018-06-23 01:05:07 +03:00
|
|
|
|
fn expand_fragment(&mut self, input_fragment: AstFragment) -> AstFragment {
|
2016-09-07 23:21:59 +00:00
|
|
|
|
let orig_expansion_data = self.cx.current_expansion.clone();
|
|
|
|
|
self.cx.current_expansion.depth = 0;
|
|
|
|
|
|
2018-06-23 19:27:01 +03:00
|
|
|
|
// Collect all macro invocations and replace them with placeholders.
|
2018-06-20 02:08:08 +03:00
|
|
|
|
let (fragment_with_placeholders, mut invocations)
|
|
|
|
|
= self.collect_invocations(input_fragment, &[]);
|
2018-06-23 19:27:01 +03:00
|
|
|
|
|
|
|
|
|
// Optimization: if we resolve all imports now,
|
|
|
|
|
// we'll be able to immediately resolve most of imported macros.
|
2016-11-10 10:11:25 +00:00
|
|
|
|
self.resolve_imports();
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
2018-08-19 15:30:23 +02:00
|
|
|
|
// Resolve paths in all invocations and produce output expanded fragments for them, but
|
2018-06-23 19:27:01 +03:00
|
|
|
|
// do not insert them into our input AST fragment yet, only store in `expanded_fragments`.
|
|
|
|
|
// The output fragments also go through expansion recursively until no invocations are left.
|
|
|
|
|
// Unresolved macros produce dummy outputs as a recovery measure.
|
|
|
|
|
invocations.reverse();
|
2018-06-20 02:08:08 +03:00
|
|
|
|
let mut expanded_fragments = Vec::new();
|
2018-08-18 13:55:43 +03:00
|
|
|
|
let mut derives: FxHashMap<Mark, Vec<_>> = FxHashMap::default();
|
2016-10-11 03:41:48 +00:00
|
|
|
|
let mut undetermined_invocations = Vec::new();
|
|
|
|
|
let (mut progress, mut force) = (false, !self.monotonic);
|
|
|
|
|
loop {
|
2018-08-03 02:30:03 +03:00
|
|
|
|
let invoc = if let Some(invoc) = invocations.pop() {
|
2016-10-11 03:41:48 +00:00
|
|
|
|
invoc
|
|
|
|
|
} else {
|
2016-11-10 10:11:25 +00:00
|
|
|
|
self.resolve_imports();
|
|
|
|
|
if undetermined_invocations.is_empty() { break }
|
2016-10-11 03:41:48 +00:00
|
|
|
|
invocations = mem::replace(&mut undetermined_invocations, Vec::new());
|
|
|
|
|
force = !mem::replace(&mut progress, false);
|
|
|
|
|
continue
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let scope =
|
|
|
|
|
if self.monotonic { invoc.expansion_data.mark } else { orig_expansion_data.mark };
|
2018-08-15 03:51:12 +03:00
|
|
|
|
let ext = match self.cx.resolver.resolve_macro_invocation(&invoc, scope, force) {
|
2016-10-11 03:41:48 +00:00
|
|
|
|
Ok(ext) => Some(ext),
|
|
|
|
|
Err(Determinacy::Determined) => None,
|
|
|
|
|
Err(Determinacy::Undetermined) => {
|
|
|
|
|
undetermined_invocations.push(invoc);
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
progress = true;
|
2016-09-07 23:21:59 +00:00
|
|
|
|
let ExpansionData { depth, mark, .. } = invoc.expansion_data;
|
|
|
|
|
self.cx.current_expansion = invoc.expansion_data.clone();
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
2016-09-19 07:27:20 +00:00
|
|
|
|
self.cx.current_expansion.mark = scope;
|
2017-02-02 07:01:15 +00:00
|
|
|
|
// FIXME(jseyfried): Refactor out the following logic
|
2018-06-20 02:08:08 +03:00
|
|
|
|
let (expanded_fragment, new_invocations) = if let Some(ext) = ext {
|
2017-02-02 07:01:15 +00:00
|
|
|
|
if let Some(ext) = ext {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
let dummy = invoc.fragment_kind.dummy(invoc.span()).unwrap();
|
|
|
|
|
let fragment = self.expand_invoc(invoc, &*ext).unwrap_or(dummy);
|
|
|
|
|
self.collect_invocations(fragment, &[])
|
2017-02-02 07:01:15 +00:00
|
|
|
|
} else if let InvocationKind::Attr { attr: None, traits, item } = invoc.kind {
|
2017-12-26 16:47:32 +09:00
|
|
|
|
if !item.derive_allowed() {
|
|
|
|
|
let attr = attr::find_by_name(item.attrs(), "derive")
|
2017-09-21 20:29:29 -07:00
|
|
|
|
.expect("`derive` attribute should exist");
|
|
|
|
|
let span = attr.span;
|
|
|
|
|
let mut err = self.cx.mut_span_err(span,
|
|
|
|
|
"`derive` may only be applied to \
|
|
|
|
|
structs, enums and unions");
|
|
|
|
|
if let ast::AttrStyle::Inner = attr.style {
|
|
|
|
|
let trait_list = traits.iter()
|
2018-07-27 11:11:18 +02:00
|
|
|
|
.map(|t| t.to_string()).collect::<Vec<_>>();
|
2017-09-21 20:29:29 -07:00
|
|
|
|
let suggestion = format!("#[derive({})]", trait_list.join(", "));
|
suggestion applicabilities for libsyntax and librustc, run-rustfix tests
Consider this a down payment on #50723. To recap, an `Applicability`
enum was recently (#50204) added, to convey to Rustfix and other tools
whether we think it's OK for them to blindly apply the suggestion, or
whether to prompt a human for guidance (because the suggestion might
contain placeholders that we can't infer, or because we think it has a
sufficiently high probability of being wrong even though it's—
presumably—right often enough to be worth emitting in the first place).
When a suggestion is marked as `MaybeIncorrect`, we try to use comments
to indicate precisely why (although there are a few places where we just
say `// speculative` because the present author's subjective judgement
balked at the idea that the suggestion has no false positives).
The `run-rustfix` directive is opporunistically set on some relevant UI
tests (and a couple tests that were in the `test/ui/suggestions`
directory, even if the suggestions didn't originate in librustc or
libsyntax). This is less trivial than it sounds, because a surprising
number of test files aren't equipped to be tested as fixed even when
they contain successfully fixable errors, because, e.g., there are more,
not-directly-related errors after fixing. Some test files need an
attribute or underscore to avoid unused warnings tripping up the "fixed
code is still producing diagnostics" check despite the fixes being
correct; this is an interesting contrast-to/inconsistency-with the
behavior of UI tests (which secretly pass `-A unused`), a behavior which
we probably ought to resolve one way or the other (filed issue #50926).
A few suggestion labels are reworded (e.g., to avoid phrasing it as a
question, which which is discouraged by the style guidelines listed in
`.span_suggestion`'s doc-comment).
2018-05-19 14:52:24 -07:00
|
|
|
|
err.span_suggestion_with_applicability(
|
|
|
|
|
span, "try an outer attribute", suggestion,
|
|
|
|
|
// We don't 𝑘𝑛𝑜𝑤 that the following item is an ADT
|
|
|
|
|
Applicability::MaybeIncorrect
|
|
|
|
|
);
|
2017-09-21 20:29:29 -07:00
|
|
|
|
}
|
|
|
|
|
err.emit();
|
2017-08-22 19:22:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-12 21:55:41 +02:00
|
|
|
|
let item = self.fully_configure(item)
|
2017-03-03 09:23:59 +00:00
|
|
|
|
.map_attrs(|mut attrs| { attrs.retain(|a| a.path != "derive"); attrs });
|
2017-02-02 07:01:15 +00:00
|
|
|
|
let item_with_markers =
|
2017-03-17 04:04:41 +00:00
|
|
|
|
add_derived_markers(&mut self.cx, item.span(), &traits, item.clone());
|
2018-07-21 22:43:31 +03:00
|
|
|
|
let derives = derives.entry(invoc.expansion_data.mark).or_default();
|
2017-02-02 07:01:15 +00:00
|
|
|
|
|
2017-03-08 23:13:35 +00:00
|
|
|
|
for path in &traits {
|
2017-03-22 08:39:51 +00:00
|
|
|
|
let mark = Mark::fresh(self.cx.current_expansion.mark);
|
2017-02-02 07:01:15 +00:00
|
|
|
|
derives.push(mark);
|
2018-08-15 03:51:12 +03:00
|
|
|
|
let item = match self.cx.resolver.resolve_macro_path(
|
|
|
|
|
path, MacroKind::Derive, Mark::root(), &[], false) {
|
2017-02-02 07:01:15 +00:00
|
|
|
|
Ok(ext) => match *ext {
|
2017-08-07 10:22:28 +02:00
|
|
|
|
BuiltinDerive(..) => item_with_markers.clone(),
|
2017-02-02 07:01:15 +00:00
|
|
|
|
_ => item.clone(),
|
|
|
|
|
},
|
|
|
|
|
_ => item.clone(),
|
|
|
|
|
};
|
|
|
|
|
invocations.push(Invocation {
|
2017-03-08 23:13:35 +00:00
|
|
|
|
kind: InvocationKind::Derive { path: path.clone(), item: item },
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fragment_kind: invoc.fragment_kind,
|
2017-02-02 07:01:15 +00:00
|
|
|
|
expansion_data: ExpansionData {
|
2017-08-06 22:54:09 -07:00
|
|
|
|
mark,
|
2017-02-02 07:01:15 +00:00
|
|
|
|
..invoc.expansion_data.clone()
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
let fragment = invoc.fragment_kind
|
2017-02-02 07:01:15 +00:00
|
|
|
|
.expect_from_annotatables(::std::iter::once(item_with_markers));
|
2018-06-20 02:08:08 +03:00
|
|
|
|
self.collect_invocations(fragment, derives)
|
2017-02-02 07:01:15 +00:00
|
|
|
|
} else {
|
|
|
|
|
unreachable!()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
self.collect_invocations(invoc.fragment_kind.dummy(invoc.span()).unwrap(), &[])
|
2016-09-07 23:21:59 +00:00
|
|
|
|
};
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
if expanded_fragments.len() < depth {
|
|
|
|
|
expanded_fragments.push(Vec::new());
|
2016-09-02 03:35:59 +00:00
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
expanded_fragments[depth - 1].push((mark, expanded_fragment));
|
2016-09-12 09:47:54 +00:00
|
|
|
|
if !self.cx.ecfg.single_step {
|
2016-09-02 09:12:47 +00:00
|
|
|
|
invocations.extend(new_invocations.into_iter().rev());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
|
self.cx.current_expansion = orig_expansion_data;
|
|
|
|
|
|
2018-06-23 19:27:01 +03:00
|
|
|
|
// Finally incorporate all the expanded macros into the input AST fragment.
|
2016-09-06 05:42:45 +00:00
|
|
|
|
let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
while let Some(expanded_fragments) = expanded_fragments.pop() {
|
|
|
|
|
for (mark, expanded_fragment) in expanded_fragments.into_iter().rev() {
|
2017-02-02 07:01:15 +00:00
|
|
|
|
let derives = derives.remove(&mark).unwrap_or_else(Vec::new);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
placeholder_expander.add(NodeId::placeholder_from_mark(mark),
|
|
|
|
|
expanded_fragment, derives);
|
2016-09-02 03:35:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fragment_with_placeholders.fold_with(&mut placeholder_expander)
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 10:11:25 +00:00
|
|
|
|
fn resolve_imports(&mut self) {
|
|
|
|
|
if self.monotonic {
|
|
|
|
|
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
|
|
|
|
|
self.cx.resolver.resolve_imports();
|
|
|
|
|
self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-23 19:27:01 +03:00
|
|
|
|
/// Collect all macro invocations reachable at this time in this AST fragment, and replace
|
|
|
|
|
/// them with "placeholders" - dummy macro invocations with specially crafted `NodeId`s.
|
|
|
|
|
/// Then call into resolver that builds a skeleton ("reduced graph") of the fragment and
|
|
|
|
|
/// prepares data for resolving paths of macro invocations.
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fn collect_invocations(&mut self, fragment: AstFragment, derives: &[Mark])
|
|
|
|
|
-> (AstFragment, Vec<Invocation>) {
|
|
|
|
|
let (fragment_with_placeholders, invocations) = {
|
2016-09-07 22:24:01 +00:00
|
|
|
|
let mut collector = InvocationCollector {
|
|
|
|
|
cfg: StripUnconfigured {
|
|
|
|
|
sess: self.cx.parse_sess,
|
|
|
|
|
features: self.cx.ecfg.features,
|
|
|
|
|
},
|
|
|
|
|
cx: self.cx,
|
|
|
|
|
invocations: Vec::new(),
|
2016-09-06 05:42:45 +00:00
|
|
|
|
monotonic: self.monotonic,
|
2016-09-07 22:24:01 +00:00
|
|
|
|
};
|
2018-06-20 02:08:08 +03:00
|
|
|
|
(fragment.fold_with(&mut collector), collector.invocations)
|
2016-09-07 22:24:01 +00:00
|
|
|
|
};
|
2016-09-07 23:21:59 +00:00
|
|
|
|
|
2016-09-19 07:27:20 +00:00
|
|
|
|
if self.monotonic {
|
2016-09-20 06:33:42 +00:00
|
|
|
|
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
|
2016-09-19 07:27:20 +00:00
|
|
|
|
let mark = self.cx.current_expansion.mark;
|
2018-06-20 02:08:08 +03:00
|
|
|
|
self.cx.resolver.visit_ast_fragment_with_placeholders(mark, &fragment_with_placeholders,
|
|
|
|
|
derives);
|
2016-09-20 06:33:42 +00:00
|
|
|
|
self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count;
|
2016-09-19 07:27:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
(fragment_with_placeholders, invocations)
|
2016-05-16 10:09:23 +00:00
|
|
|
|
}
|
2016-06-12 01:50:52 +00:00
|
|
|
|
|
2017-09-12 21:55:41 +02:00
|
|
|
|
fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
|
|
|
|
|
let mut cfg = StripUnconfigured {
|
|
|
|
|
sess: self.cx.parse_sess,
|
|
|
|
|
features: self.cx.ecfg.features,
|
|
|
|
|
};
|
|
|
|
|
// Since the item itself has already been configured by the InvocationCollector,
|
|
|
|
|
// we know that fold result vector will contain exactly one element
|
|
|
|
|
match item {
|
|
|
|
|
Annotatable::Item(item) => {
|
|
|
|
|
Annotatable::Item(cfg.fold_item(item).pop().unwrap())
|
|
|
|
|
}
|
|
|
|
|
Annotatable::TraitItem(item) => {
|
|
|
|
|
Annotatable::TraitItem(item.map(|item| cfg.fold_trait_item(item).pop().unwrap()))
|
|
|
|
|
}
|
|
|
|
|
Annotatable::ImplItem(item) => {
|
|
|
|
|
Annotatable::ImplItem(item.map(|item| cfg.fold_impl_item(item).pop().unwrap()))
|
|
|
|
|
}
|
2018-03-10 18:16:26 -08:00
|
|
|
|
Annotatable::ForeignItem(item) => {
|
|
|
|
|
Annotatable::ForeignItem(
|
|
|
|
|
item.map(|item| cfg.fold_foreign_item(item).pop().unwrap())
|
|
|
|
|
)
|
|
|
|
|
}
|
2018-03-15 23:20:56 -07:00
|
|
|
|
Annotatable::Stmt(stmt) => {
|
|
|
|
|
Annotatable::Stmt(stmt.map(|stmt| cfg.fold_stmt(stmt).pop().unwrap()))
|
|
|
|
|
}
|
|
|
|
|
Annotatable::Expr(expr) => {
|
|
|
|
|
Annotatable::Expr(cfg.fold_expr(expr))
|
|
|
|
|
}
|
2017-09-12 21:55:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) -> Option<AstFragment> {
|
2018-08-04 05:17:51 +03:00
|
|
|
|
if invoc.fragment_kind == AstFragmentKind::ForeignItems &&
|
|
|
|
|
!self.cx.ecfg.macros_in_extern_enabled() {
|
|
|
|
|
if let SyntaxExtension::NonMacroAttr { .. } = *ext {} else {
|
|
|
|
|
emit_feature_err(&self.cx.parse_sess, "macros_in_extern",
|
|
|
|
|
invoc.span(), GateIssue::Language,
|
|
|
|
|
"macro invocations in `extern {}` blocks are experimental");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 04:04:41 +00:00
|
|
|
|
let result = match invoc.kind {
|
2017-12-26 16:47:32 +09:00
|
|
|
|
InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext)?,
|
|
|
|
|
InvocationKind::Attr { .. } => self.expand_attr_invoc(invoc, ext)?,
|
|
|
|
|
InvocationKind::Derive { .. } => self.expand_derive_invoc(invoc, ext)?,
|
2017-03-17 04:04:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit {
|
|
|
|
|
let info = self.cx.current_expansion.mark.expn_info().unwrap();
|
|
|
|
|
let suggested_limit = self.cx.ecfg.recursion_limit * 2;
|
2017-09-02 18:13:25 +02:00
|
|
|
|
let mut err = self.cx.struct_span_err(info.call_site,
|
2017-03-17 04:04:41 +00:00
|
|
|
|
&format!("recursion limit reached while expanding the macro `{}`",
|
2018-06-23 21:41:39 +03:00
|
|
|
|
info.format.name()));
|
2017-03-17 04:04:41 +00:00
|
|
|
|
err.help(&format!(
|
|
|
|
|
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
|
|
|
|
|
suggested_limit));
|
|
|
|
|
err.emit();
|
2017-08-25 20:46:49 +02:00
|
|
|
|
self.cx.trace_macros_diag();
|
2018-01-21 12:47:58 +01:00
|
|
|
|
FatalError.raise();
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
|
2017-12-26 16:47:32 +09:00
|
|
|
|
Some(result)
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 16:47:32 +09:00
|
|
|
|
fn expand_attr_invoc(&mut self,
|
|
|
|
|
invoc: Invocation,
|
2018-02-27 17:11:14 +01:00
|
|
|
|
ext: &SyntaxExtension)
|
2018-06-20 02:08:08 +03:00
|
|
|
|
-> Option<AstFragment> {
|
2016-09-01 07:01:45 +00:00
|
|
|
|
let (attr, item) = match invoc.kind {
|
2017-12-26 16:47:32 +09:00
|
|
|
|
InvocationKind::Attr { attr, item, .. } => (attr?, item),
|
2016-09-01 07:01:45 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-04 00:25:45 +03:00
|
|
|
|
if let NonMacroAttr { mark_used: false } = *ext {} else {
|
2018-08-04 05:17:51 +03:00
|
|
|
|
// Macro attrs are always used when expanded,
|
|
|
|
|
// non-macro attrs are considered used when the field says so.
|
2018-08-04 00:25:45 +03:00
|
|
|
|
attr::mark_used(&attr);
|
|
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
invoc.expansion_data.mark.set_expn_info(ExpnInfo {
|
2016-09-01 07:01:45 +00:00
|
|
|
|
call_site: attr.span,
|
2018-06-23 21:41:39 +03:00
|
|
|
|
def_site: None,
|
2018-07-27 11:11:18 +02:00
|
|
|
|
format: MacroAttribute(Symbol::intern(&attr.path.to_string())),
|
2018-06-23 21:41:39 +03:00
|
|
|
|
allow_internal_unstable: false,
|
|
|
|
|
allow_internal_unsafe: false,
|
2018-06-11 14:21:36 +03:00
|
|
|
|
local_inner_macros: false,
|
2018-06-23 21:41:39 +03:00
|
|
|
|
edition: ext.edition(),
|
2016-09-01 07:01:45 +00:00
|
|
|
|
});
|
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
|
match *ext {
|
2018-08-04 00:25:45 +03:00
|
|
|
|
NonMacroAttr { .. } => {
|
2018-07-23 02:52:51 +03:00
|
|
|
|
attr::mark_known(&attr);
|
|
|
|
|
let item = item.map_attrs(|mut attrs| { attrs.push(attr); attrs });
|
|
|
|
|
Some(invoc.fragment_kind.expect_from_annotatables(iter::once(item)))
|
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
MultiModifier(ref mac) => {
|
2018-03-13 15:12:15 -04:00
|
|
|
|
let meta = attr.parse_meta(self.cx.parse_sess)
|
|
|
|
|
.map_err(|mut e| { e.emit(); }).ok()?;
|
2017-03-03 09:23:59 +00:00
|
|
|
|
let item = mac.expand(self.cx, attr.span, &meta, item);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
Some(invoc.fragment_kind.expect_from_annotatables(item))
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
|
|
|
|
MultiDecorator(ref mac) => {
|
|
|
|
|
let mut items = Vec::new();
|
2018-03-13 15:12:15 -04:00
|
|
|
|
let meta = attr.parse_meta(self.cx.parse_sess)
|
|
|
|
|
.expect("derive meta should already have been parsed");
|
2017-03-03 09:23:59 +00:00
|
|
|
|
mac.expand(self.cx, attr.span, &meta, &item, &mut |item| items.push(item));
|
2016-09-01 07:01:45 +00:00
|
|
|
|
items.push(item);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
Some(invoc.fragment_kind.expect_from_annotatables(items))
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
2018-05-13 03:51:46 +03:00
|
|
|
|
AttrProcMacro(ref mac, ..) => {
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 07:50:39 -07:00
|
|
|
|
self.gate_proc_macro_attr_item(attr.span, &item);
|
2017-03-29 01:55:01 +00:00
|
|
|
|
let item_tok = TokenTree::Token(DUMMY_SP, Token::interpolated(match item {
|
|
|
|
|
Annotatable::Item(item) => token::NtItem(item),
|
2017-12-17 02:21:29 +03:00
|
|
|
|
Annotatable::TraitItem(item) => token::NtTraitItem(item.into_inner()),
|
|
|
|
|
Annotatable::ImplItem(item) => token::NtImplItem(item.into_inner()),
|
2018-03-10 18:16:26 -08:00
|
|
|
|
Annotatable::ForeignItem(item) => token::NtForeignItem(item.into_inner()),
|
2018-03-15 23:20:56 -07:00
|
|
|
|
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
|
|
|
|
|
Annotatable::Expr(expr) => token::NtExpr(expr),
|
2017-03-29 01:55:01 +00:00
|
|
|
|
})).into();
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 07:50:39 -07:00
|
|
|
|
let input = self.extract_proc_macro_attr_input(attr.tokens, attr.span);
|
|
|
|
|
let tok_result = mac.expand(self.cx, attr.span, input, item_tok);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
let res = self.parse_ast_fragment(tok_result, invoc.fragment_kind,
|
|
|
|
|
&attr.path, attr.span);
|
2018-05-09 15:03:02 -07:00
|
|
|
|
self.gate_proc_macro_expansion(attr.span, &res);
|
|
|
|
|
res
|
2016-08-29 16:16:43 +12:00
|
|
|
|
}
|
2017-08-07 10:22:28 +02:00
|
|
|
|
ProcMacroDerive(..) | BuiltinDerive(..) => {
|
2017-03-08 23:13:35 +00:00
|
|
|
|
self.cx.span_err(attr.span, &format!("`{}` is a derive mode", attr.path));
|
2017-08-25 20:46:49 +02:00
|
|
|
|
self.cx.trace_macros_diag();
|
2018-06-20 02:08:08 +03:00
|
|
|
|
invoc.fragment_kind.dummy(attr.span)
|
2016-10-15 20:50:30 +00:00
|
|
|
|
}
|
|
|
|
|
_ => {
|
2017-03-08 23:13:35 +00:00
|
|
|
|
let msg = &format!("macro `{}` may not be used in attributes", attr.path);
|
2017-05-12 20:05:39 +02:00
|
|
|
|
self.cx.span_err(attr.span, msg);
|
2017-08-25 20:46:49 +02:00
|
|
|
|
self.cx.trace_macros_diag();
|
2018-06-20 02:08:08 +03:00
|
|
|
|
invoc.fragment_kind.dummy(attr.span)
|
2016-10-15 20:50:30 +00:00
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 07:50:39 -07:00
|
|
|
|
fn extract_proc_macro_attr_input(&self, tokens: TokenStream, span: Span) -> TokenStream {
|
|
|
|
|
let mut trees = tokens.trees();
|
|
|
|
|
match trees.next() {
|
|
|
|
|
Some(TokenTree::Delimited(_, delim)) => {
|
|
|
|
|
if trees.next().is_none() {
|
|
|
|
|
return delim.tts.into()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some(TokenTree::Token(..)) => {}
|
|
|
|
|
None => return TokenStream::empty(),
|
|
|
|
|
}
|
|
|
|
|
self.cx.span_err(span, "custom attribute invocations must be \
|
|
|
|
|
of the form #[foo] or #[foo(..)], the macro name must only be \
|
|
|
|
|
followed by a delimiter token");
|
|
|
|
|
TokenStream::empty()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
|
|
|
|
|
let (kind, gate) = match *item {
|
|
|
|
|
Annotatable::Item(ref item) => {
|
|
|
|
|
match item.node {
|
|
|
|
|
ItemKind::Mod(_) if self.cx.ecfg.proc_macro_mod() => return,
|
|
|
|
|
ItemKind::Mod(_) => ("modules", "proc_macro_mod"),
|
|
|
|
|
_ => return,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Annotatable::TraitItem(_) => return,
|
|
|
|
|
Annotatable::ImplItem(_) => return,
|
|
|
|
|
Annotatable::ForeignItem(_) => return,
|
|
|
|
|
Annotatable::Stmt(_) |
|
|
|
|
|
Annotatable::Expr(_) if self.cx.ecfg.proc_macro_expr() => return,
|
|
|
|
|
Annotatable::Stmt(_) => ("statements", "proc_macro_expr"),
|
|
|
|
|
Annotatable::Expr(_) => ("expressions", "proc_macro_expr"),
|
|
|
|
|
};
|
|
|
|
|
emit_feature_err(
|
|
|
|
|
self.cx.parse_sess,
|
|
|
|
|
gate,
|
|
|
|
|
span,
|
|
|
|
|
GateIssue::Language,
|
|
|
|
|
&format!("custom attributes cannot be applied to {}", kind),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fn gate_proc_macro_expansion(&self, span: Span, fragment: &Option<AstFragment>) {
|
2018-05-09 15:03:02 -07:00
|
|
|
|
if self.cx.ecfg.proc_macro_gen() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
let fragment = match fragment {
|
|
|
|
|
Some(fragment) => fragment,
|
2018-05-09 15:03:02 -07:00
|
|
|
|
None => return,
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-18 00:52:34 +03:00
|
|
|
|
fragment.visit_with(&mut DisallowMacros {
|
2018-05-09 15:03:02 -07:00
|
|
|
|
span,
|
|
|
|
|
parse_sess: self.cx.parse_sess,
|
|
|
|
|
});
|
|
|
|
|
|
2018-08-18 00:52:34 +03:00
|
|
|
|
struct DisallowMacros<'a> {
|
2018-05-09 15:03:02 -07:00
|
|
|
|
span: Span,
|
|
|
|
|
parse_sess: &'a ParseSess,
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-18 00:52:34 +03:00
|
|
|
|
impl<'ast, 'a> Visitor<'ast> for DisallowMacros<'a> {
|
2018-05-09 15:03:02 -07:00
|
|
|
|
fn visit_item(&mut self, i: &'ast ast::Item) {
|
2018-08-18 00:52:34 +03:00
|
|
|
|
if let ast::ItemKind::MacroDef(_) = i.node {
|
2018-05-09 15:03:02 -07:00
|
|
|
|
emit_feature_err(
|
|
|
|
|
self.parse_sess,
|
|
|
|
|
"proc_macro_gen",
|
|
|
|
|
self.span,
|
|
|
|
|
GateIssue::Language,
|
2018-08-18 00:52:34 +03:00
|
|
|
|
&format!("procedural macros cannot expand to macro definitions"),
|
2018-05-09 15:03:02 -07:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
visit::walk_item(self, i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_mac(&mut self, _mac: &'ast ast::Mac) {
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
/// Expand a macro invocation. Returns the resulting expanded AST fragment.
|
2017-12-26 16:47:32 +09:00
|
|
|
|
fn expand_bang_invoc(&mut self,
|
|
|
|
|
invoc: Invocation,
|
2018-02-27 17:11:14 +01:00
|
|
|
|
ext: &SyntaxExtension)
|
2018-06-20 02:08:08 +03:00
|
|
|
|
-> Option<AstFragment> {
|
|
|
|
|
let (mark, kind) = (invoc.expansion_data.mark, invoc.fragment_kind);
|
2016-12-01 11:54:01 +00:00
|
|
|
|
let (mac, ident, span) = match invoc.kind {
|
|
|
|
|
InvocationKind::Bang { mac, ident, span } => (mac, ident, span),
|
2016-09-01 07:01:45 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2017-02-21 05:05:59 +00:00
|
|
|
|
let path = &mac.node.path;
|
2016-09-01 07:01:45 +00:00
|
|
|
|
|
2017-05-12 20:05:39 +02:00
|
|
|
|
let ident = ident.unwrap_or_else(|| keywords::Invalid.ident());
|
2018-02-24 19:11:06 -08:00
|
|
|
|
let validate_and_set_expn_info = |this: &mut Self, // arg instead of capture
|
|
|
|
|
def_site_span: Option<Span>,
|
2017-08-08 18:21:20 +03:00
|
|
|
|
allow_internal_unstable,
|
2018-02-24 19:11:06 -08:00
|
|
|
|
allow_internal_unsafe,
|
2018-06-11 14:21:36 +03:00
|
|
|
|
local_inner_macros,
|
2018-02-24 19:11:06 -08:00
|
|
|
|
// can't infer this type
|
2018-05-13 03:51:46 +03:00
|
|
|
|
unstable_feature: Option<(Symbol, u32)>,
|
|
|
|
|
edition| {
|
2018-02-24 19:11:06 -08:00
|
|
|
|
|
|
|
|
|
// feature-gate the macro invocation
|
|
|
|
|
if let Some((feature, issue)) = unstable_feature {
|
|
|
|
|
let crate_span = this.cx.current_expansion.crate_span.unwrap();
|
|
|
|
|
// don't stability-check macros in the same crate
|
|
|
|
|
// (the only time this is null is for syntax extensions registered as macros)
|
|
|
|
|
if def_site_span.map_or(false, |def_span| !crate_span.contains(def_span))
|
|
|
|
|
&& !span.allows_unstable() && this.cx.ecfg.features.map_or(true, |feats| {
|
|
|
|
|
// macro features will count as lib features
|
|
|
|
|
!feats.declared_lib_features.iter().any(|&(feat, _)| feat == feature)
|
|
|
|
|
}) {
|
|
|
|
|
let explain = format!("macro {}! is unstable", path);
|
|
|
|
|
emit_feature_err(this.cx.parse_sess, &*feature.as_str(), span,
|
|
|
|
|
GateIssue::Library(Some(issue)), &explain);
|
|
|
|
|
this.cx.trace_macros_diag();
|
|
|
|
|
return Err(kind.dummy(span));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
|
if ident.name != keywords::Invalid.name() {
|
2018-02-24 19:11:06 -08:00
|
|
|
|
let msg = format!("macro {}! expects no ident argument, given '{}'", path, ident);
|
|
|
|
|
this.cx.span_err(path.span, &msg);
|
|
|
|
|
this.cx.trace_macros_diag();
|
|
|
|
|
return Err(kind.dummy(span));
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
mark.set_expn_info(ExpnInfo {
|
|
|
|
|
call_site: span,
|
2018-06-23 21:41:39 +03:00
|
|
|
|
def_site: def_site_span,
|
|
|
|
|
format: macro_bang_format(path),
|
|
|
|
|
allow_internal_unstable,
|
|
|
|
|
allow_internal_unsafe,
|
2018-06-11 14:21:36 +03:00
|
|
|
|
local_inner_macros,
|
2018-06-23 21:41:39 +03:00
|
|
|
|
edition,
|
2017-03-22 08:39:51 +00:00
|
|
|
|
});
|
|
|
|
|
Ok(())
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
|
let opt_expanded = match *ext {
|
2018-06-24 19:54:23 +03:00
|
|
|
|
DeclMacro { ref expander, def_info, edition, .. } => {
|
2018-06-24 19:24:51 +03:00
|
|
|
|
if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
|
2018-06-11 14:21:36 +03:00
|
|
|
|
false, false, false, None,
|
2018-05-13 03:51:46 +03:00
|
|
|
|
edition) {
|
2018-02-24 19:11:06 -08:00
|
|
|
|
dummy_span
|
2017-12-26 16:47:32 +09:00
|
|
|
|
} else {
|
2018-06-24 19:24:51 +03:00
|
|
|
|
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
|
2017-08-08 18:21:20 +03:00
|
|
|
|
NormalTT {
|
|
|
|
|
ref expander,
|
|
|
|
|
def_info,
|
|
|
|
|
allow_internal_unstable,
|
2018-02-24 19:11:06 -08:00
|
|
|
|
allow_internal_unsafe,
|
2018-06-11 14:21:36 +03:00
|
|
|
|
local_inner_macros,
|
2018-02-24 19:11:06 -08:00
|
|
|
|
unstable_feature,
|
2018-05-13 03:51:46 +03:00
|
|
|
|
edition,
|
2017-08-08 18:21:20 +03:00
|
|
|
|
} => {
|
2018-02-24 19:11:06 -08:00
|
|
|
|
if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
|
|
|
|
|
allow_internal_unstable,
|
|
|
|
|
allow_internal_unsafe,
|
2018-06-11 14:21:36 +03:00
|
|
|
|
local_inner_macros,
|
2018-05-13 03:51:46 +03:00
|
|
|
|
unstable_feature,
|
|
|
|
|
edition) {
|
2018-02-24 19:11:06 -08:00
|
|
|
|
dummy_span
|
2017-12-26 16:47:32 +09:00
|
|
|
|
} else {
|
|
|
|
|
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IdentTT(ref expander, tt_span, allow_internal_unstable) => {
|
|
|
|
|
if ident.name == keywords::Invalid.name() {
|
|
|
|
|
self.cx.span_err(path.span,
|
2017-03-08 23:13:35 +00:00
|
|
|
|
&format!("macro {}! expects an ident argument", path));
|
2017-08-25 20:46:49 +02:00
|
|
|
|
self.cx.trace_macros_diag();
|
2017-12-26 16:47:32 +09:00
|
|
|
|
kind.dummy(span)
|
|
|
|
|
} else {
|
|
|
|
|
invoc.expansion_data.mark.set_expn_info(ExpnInfo {
|
|
|
|
|
call_site: span,
|
2018-06-23 21:41:39 +03:00
|
|
|
|
def_site: tt_span,
|
|
|
|
|
format: macro_bang_format(path),
|
|
|
|
|
allow_internal_unstable,
|
|
|
|
|
allow_internal_unsafe: false,
|
2018-06-11 14:21:36 +03:00
|
|
|
|
local_inner_macros: false,
|
2018-06-23 21:41:39 +03:00
|
|
|
|
edition: hygiene::default_edition(),
|
2017-12-26 16:47:32 +09:00
|
|
|
|
});
|
2016-09-01 07:01:45 +00:00
|
|
|
|
|
2017-12-26 16:47:32 +09:00
|
|
|
|
let input: Vec<_> = mac.node.stream().into_trees().collect();
|
|
|
|
|
kind.make_from(expander.expand(self.cx, span, ident, input))
|
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-23 02:52:51 +03:00
|
|
|
|
MultiDecorator(..) | MultiModifier(..) |
|
2018-08-04 00:25:45 +03:00
|
|
|
|
AttrProcMacro(..) | SyntaxExtension::NonMacroAttr { .. } => {
|
2016-09-01 07:01:45 +00:00
|
|
|
|
self.cx.span_err(path.span,
|
2017-03-08 23:13:35 +00:00
|
|
|
|
&format!("`{}` can only be used in attributes", path));
|
2017-08-25 20:46:49 +02:00
|
|
|
|
self.cx.trace_macros_diag();
|
2017-12-26 16:47:32 +09:00
|
|
|
|
kind.dummy(span)
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
2016-08-29 16:16:43 +12:00
|
|
|
|
|
2017-08-07 10:22:28 +02:00
|
|
|
|
ProcMacroDerive(..) | BuiltinDerive(..) => {
|
2017-03-08 23:13:35 +00:00
|
|
|
|
self.cx.span_err(path.span, &format!("`{}` is a derive mode", path));
|
2017-08-25 20:46:49 +02:00
|
|
|
|
self.cx.trace_macros_diag();
|
2017-12-26 16:47:32 +09:00
|
|
|
|
kind.dummy(span)
|
2016-10-15 20:50:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-24 19:24:51 +03:00
|
|
|
|
SyntaxExtension::ProcMacro { ref expander, allow_internal_unstable, edition } => {
|
2016-08-29 16:16:43 +12:00
|
|
|
|
if ident.name != keywords::Invalid.name() {
|
|
|
|
|
let msg =
|
2017-03-08 23:13:35 +00:00
|
|
|
|
format!("macro {}! expects no ident argument, given '{}'", path, ident);
|
2016-09-06 17:57:58 +12:00
|
|
|
|
self.cx.span_err(path.span, &msg);
|
2017-08-25 20:46:49 +02:00
|
|
|
|
self.cx.trace_macros_diag();
|
2017-12-26 16:47:32 +09:00
|
|
|
|
kind.dummy(span)
|
|
|
|
|
} else {
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 07:50:39 -07:00
|
|
|
|
self.gate_proc_macro_expansion_kind(span, kind);
|
2017-12-26 16:47:32 +09:00
|
|
|
|
invoc.expansion_data.mark.set_expn_info(ExpnInfo {
|
|
|
|
|
call_site: span,
|
2018-06-23 21:41:39 +03:00
|
|
|
|
// FIXME procedural macros do not have proper span info
|
|
|
|
|
// yet, when they do, we should use it here.
|
|
|
|
|
def_site: None,
|
|
|
|
|
format: macro_bang_format(path),
|
|
|
|
|
// FIXME probably want to follow macro_rules macros here.
|
|
|
|
|
allow_internal_unstable,
|
|
|
|
|
allow_internal_unsafe: false,
|
2018-06-11 14:21:36 +03:00
|
|
|
|
local_inner_macros: false,
|
2018-06-23 21:41:39 +03:00
|
|
|
|
edition,
|
2017-12-26 16:47:32 +09:00
|
|
|
|
});
|
2016-08-29 16:16:43 +12:00
|
|
|
|
|
2018-06-24 19:24:51 +03:00
|
|
|
|
let tok_result = expander.expand(self.cx, span, mac.node.stream());
|
2018-06-20 02:08:08 +03:00
|
|
|
|
let result = self.parse_ast_fragment(tok_result, kind, path, span);
|
2018-05-09 15:03:02 -07:00
|
|
|
|
self.gate_proc_macro_expansion(span, &result);
|
|
|
|
|
result
|
2017-12-26 16:47:32 +09:00
|
|
|
|
}
|
2016-08-29 16:16:43 +12:00
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-12-26 16:47:32 +09:00
|
|
|
|
if opt_expanded.is_some() {
|
|
|
|
|
opt_expanded
|
|
|
|
|
} else {
|
2016-09-01 07:01:45 +00:00
|
|
|
|
let msg = format!("non-{kind} macro in {kind} position: {name}",
|
2018-03-18 03:53:41 +03:00
|
|
|
|
name = path.segments[0].ident.name, kind = kind.name());
|
2016-09-01 07:01:45 +00:00
|
|
|
|
self.cx.span_err(path.span, &msg);
|
2017-08-25 20:46:49 +02:00
|
|
|
|
self.cx.trace_macros_diag();
|
2017-03-28 05:32:43 +00:00
|
|
|
|
kind.dummy(span)
|
2017-12-26 16:47:32 +09:00
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2016-09-26 04:16:55 +00:00
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fn gate_proc_macro_expansion_kind(&self, span: Span, kind: AstFragmentKind) {
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 07:50:39 -07:00
|
|
|
|
let kind = match kind {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::Expr => "expressions",
|
|
|
|
|
AstFragmentKind::OptExpr => "expressions",
|
|
|
|
|
AstFragmentKind::Pat => "patterns",
|
|
|
|
|
AstFragmentKind::Ty => "types",
|
|
|
|
|
AstFragmentKind::Stmts => "statements",
|
|
|
|
|
AstFragmentKind::Items => return,
|
|
|
|
|
AstFragmentKind::TraitItems => return,
|
|
|
|
|
AstFragmentKind::ImplItems => return,
|
|
|
|
|
AstFragmentKind::ForeignItems => return,
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 07:50:39 -07:00
|
|
|
|
};
|
|
|
|
|
if self.cx.ecfg.proc_macro_non_items() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
emit_feature_err(
|
|
|
|
|
self.cx.parse_sess,
|
|
|
|
|
"proc_macro_non_items",
|
|
|
|
|
span,
|
|
|
|
|
GateIssue::Language,
|
|
|
|
|
&format!("procedural macros cannot be expanded to {}", kind),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
/// Expand a derive invocation. Returns the resulting expanded AST fragment.
|
2017-12-26 16:47:32 +09:00
|
|
|
|
fn expand_derive_invoc(&mut self,
|
|
|
|
|
invoc: Invocation,
|
2018-02-27 17:11:14 +01:00
|
|
|
|
ext: &SyntaxExtension)
|
2018-06-20 02:08:08 +03:00
|
|
|
|
-> Option<AstFragment> {
|
2017-03-08 23:13:35 +00:00
|
|
|
|
let (path, item) = match invoc.kind {
|
|
|
|
|
InvocationKind::Derive { path, item } => (path, item),
|
2017-02-01 21:03:09 +10:30
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2017-12-26 16:47:32 +09:00
|
|
|
|
if !item.derive_allowed() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2017-02-01 21:03:09 +10:30
|
|
|
|
|
2017-03-08 23:13:35 +00:00
|
|
|
|
let pretty_name = Symbol::intern(&format!("derive({})", path));
|
|
|
|
|
let span = path.span;
|
|
|
|
|
let attr = ast::Attribute {
|
2017-08-06 22:54:09 -07:00
|
|
|
|
path, span,
|
|
|
|
|
tokens: TokenStream::empty(),
|
2017-03-08 23:13:35 +00:00
|
|
|
|
// irrelevant:
|
|
|
|
|
id: ast::AttrId(0), style: ast::AttrStyle::Outer, is_sugared_doc: false,
|
|
|
|
|
};
|
2017-02-01 21:03:09 +10:30
|
|
|
|
|
2017-03-17 04:04:41 +00:00
|
|
|
|
let mut expn_info = ExpnInfo {
|
2017-02-02 07:01:15 +00:00
|
|
|
|
call_site: span,
|
2018-06-23 21:41:39 +03:00
|
|
|
|
def_site: None,
|
|
|
|
|
format: MacroAttribute(pretty_name),
|
|
|
|
|
allow_internal_unstable: false,
|
|
|
|
|
allow_internal_unsafe: false,
|
2018-06-11 14:21:36 +03:00
|
|
|
|
local_inner_macros: false,
|
2018-06-23 21:41:39 +03:00
|
|
|
|
edition: ext.edition(),
|
2017-03-17 04:04:41 +00:00
|
|
|
|
};
|
2017-02-01 21:03:09 +10:30
|
|
|
|
|
|
|
|
|
match *ext {
|
2018-05-13 03:51:46 +03:00
|
|
|
|
ProcMacroDerive(ref ext, ..) => {
|
2017-03-17 04:04:41 +00:00
|
|
|
|
invoc.expansion_data.mark.set_expn_info(expn_info);
|
2017-07-31 23:04:34 +03:00
|
|
|
|
let span = span.with_ctxt(self.cx.backtrace());
|
2017-03-08 23:13:35 +00:00
|
|
|
|
let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this
|
2018-04-17 15:33:39 +02:00
|
|
|
|
ident: Path::from_ident(keywords::Invalid.ident()),
|
2017-03-08 23:13:35 +00:00
|
|
|
|
span: DUMMY_SP,
|
|
|
|
|
node: ast::MetaItemKind::Word,
|
|
|
|
|
};
|
2018-05-09 15:03:02 -07:00
|
|
|
|
let items = ext.expand(self.cx, span, &dummy, item);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
Some(invoc.fragment_kind.expect_from_annotatables(items))
|
2017-02-01 21:03:09 +10:30
|
|
|
|
}
|
2017-08-07 10:22:28 +02:00
|
|
|
|
BuiltinDerive(func) => {
|
2018-06-23 21:41:39 +03:00
|
|
|
|
expn_info.allow_internal_unstable = true;
|
2017-03-17 04:04:41 +00:00
|
|
|
|
invoc.expansion_data.mark.set_expn_info(expn_info);
|
2017-07-31 23:04:34 +03:00
|
|
|
|
let span = span.with_ctxt(self.cx.backtrace());
|
2017-02-01 21:03:09 +10:30
|
|
|
|
let mut items = Vec::new();
|
2017-12-26 16:47:32 +09:00
|
|
|
|
func(self.cx, span, &attr.meta()?, &item, &mut |a| items.push(a));
|
2018-06-20 02:08:08 +03:00
|
|
|
|
Some(invoc.fragment_kind.expect_from_annotatables(items))
|
2017-02-01 21:03:09 +10:30
|
|
|
|
}
|
|
|
|
|
_ => {
|
2017-03-08 23:13:35 +00:00
|
|
|
|
let msg = &format!("macro `{}` may not be used for derive attributes", attr.path);
|
2017-05-12 20:05:39 +02:00
|
|
|
|
self.cx.span_err(span, msg);
|
2017-08-25 20:46:49 +02:00
|
|
|
|
self.cx.trace_macros_diag();
|
2018-06-20 02:08:08 +03:00
|
|
|
|
invoc.fragment_kind.dummy(span)
|
2017-02-01 21:03:09 +10:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fn parse_ast_fragment(&mut self,
|
|
|
|
|
toks: TokenStream,
|
|
|
|
|
kind: AstFragmentKind,
|
|
|
|
|
path: &Path,
|
|
|
|
|
span: Span)
|
|
|
|
|
-> Option<AstFragment> {
|
2017-02-18 06:18:29 +00:00
|
|
|
|
let mut parser = self.cx.new_parser_from_tts(&toks.into_trees().collect::<Vec<_>>());
|
2018-06-20 02:08:08 +03:00
|
|
|
|
match parser.parse_ast_fragment(kind, false) {
|
|
|
|
|
Ok(fragment) => {
|
2017-12-26 16:47:32 +09:00
|
|
|
|
parser.ensure_complete_parse(path, kind.name(), span);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
Some(fragment)
|
2017-12-26 16:47:32 +09:00
|
|
|
|
}
|
2016-09-26 04:16:55 +00:00
|
|
|
|
Err(mut err) => {
|
2018-03-15 23:20:56 -07:00
|
|
|
|
err.set_span(span);
|
2016-09-26 04:16:55 +00:00
|
|
|
|
err.emit();
|
2017-09-02 18:13:25 +02:00
|
|
|
|
self.cx.trace_macros_diag();
|
2017-12-26 16:47:32 +09:00
|
|
|
|
kind.dummy(span)
|
2016-09-26 04:16:55 +00:00
|
|
|
|
}
|
2017-12-26 16:47:32 +09:00
|
|
|
|
}
|
2016-09-26 04:16:55 +00:00
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-23 09:32:58 +00:00
|
|
|
|
impl<'a> Parser<'a> {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
pub fn parse_ast_fragment(&mut self, kind: AstFragmentKind, macro_legacy_warnings: bool)
|
|
|
|
|
-> PResult<'a, AstFragment> {
|
2016-09-23 09:32:58 +00:00
|
|
|
|
Ok(match kind {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::Items => {
|
2018-08-05 12:04:56 +02:00
|
|
|
|
let mut items = OneVector::new();
|
2016-09-23 09:32:58 +00:00
|
|
|
|
while let Some(item) = self.parse_item()? {
|
|
|
|
|
items.push(item);
|
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragment::Items(items)
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::TraitItems => {
|
2018-08-05 12:04:56 +02:00
|
|
|
|
let mut items = OneVector::new();
|
2016-09-23 09:32:58 +00:00
|
|
|
|
while self.token != token::Eof {
|
2017-04-13 22:37:05 +03:00
|
|
|
|
items.push(self.parse_trait_item(&mut false)?);
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragment::TraitItems(items)
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::ImplItems => {
|
2018-08-05 12:04:56 +02:00
|
|
|
|
let mut items = OneVector::new();
|
2016-09-23 09:32:58 +00:00
|
|
|
|
while self.token != token::Eof {
|
2017-04-13 22:37:05 +03:00
|
|
|
|
items.push(self.parse_impl_item(&mut false)?);
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragment::ImplItems(items)
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::ForeignItems => {
|
2018-08-05 12:04:56 +02:00
|
|
|
|
let mut items = OneVector::new();
|
2018-03-10 18:16:26 -08:00
|
|
|
|
while self.token != token::Eof {
|
|
|
|
|
if let Some(item) = self.parse_foreign_item()? {
|
|
|
|
|
items.push(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragment::ForeignItems(items)
|
2018-03-10 18:16:26 -08:00
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::Stmts => {
|
2018-08-05 12:04:56 +02:00
|
|
|
|
let mut stmts = OneVector::new();
|
2016-10-17 21:01:36 -07:00
|
|
|
|
while self.token != token::Eof &&
|
|
|
|
|
// won't make progress on a `}`
|
|
|
|
|
self.token != token::CloseDelim(token::Brace) {
|
2016-09-26 04:16:55 +00:00
|
|
|
|
if let Some(stmt) = self.parse_full_stmt(macro_legacy_warnings)? {
|
2016-09-23 09:32:58 +00:00
|
|
|
|
stmts.push(stmt);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragment::Stmts(stmts)
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::Expr => AstFragment::Expr(self.parse_expr()?),
|
|
|
|
|
AstFragmentKind::OptExpr => {
|
2018-03-15 23:20:56 -07:00
|
|
|
|
if self.token != token::Eof {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragment::OptExpr(Some(self.parse_expr()?))
|
2018-03-15 23:20:56 -07:00
|
|
|
|
} else {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragment::OptExpr(None)
|
2018-03-15 23:20:56 -07:00
|
|
|
|
}
|
|
|
|
|
},
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::Ty => AstFragment::Ty(self.parse_ty()?),
|
|
|
|
|
AstFragmentKind::Pat => AstFragment::Pat(self.parse_pat()?),
|
2016-09-23 09:32:58 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
2016-09-26 11:24:10 +00:00
|
|
|
|
|
2017-03-08 23:13:35 +00:00
|
|
|
|
pub fn ensure_complete_parse(&mut self, macro_path: &Path, kind_name: &str, span: Span) {
|
2016-09-26 11:24:10 +00:00
|
|
|
|
if self.token != token::Eof {
|
|
|
|
|
let msg = format!("macro expansion ignores token `{}` and any following",
|
|
|
|
|
self.this_token_to_string());
|
2017-07-31 23:04:34 +03:00
|
|
|
|
// Avoid emitting backtrace info twice.
|
|
|
|
|
let def_site_span = self.span.with_ctxt(SyntaxContext::empty());
|
2017-03-28 05:32:43 +00:00
|
|
|
|
let mut err = self.diagnostic().struct_span_err(def_site_span, &msg);
|
2016-09-26 11:24:10 +00:00
|
|
|
|
let msg = format!("caused by the macro expansion here; the usage \
|
|
|
|
|
of `{}!` is likely invalid in {} context",
|
2017-03-08 23:13:35 +00:00
|
|
|
|
macro_path, kind_name);
|
2016-09-26 11:24:10 +00:00
|
|
|
|
err.span_note(span, &msg).emit();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct InvocationCollector<'a, 'b: 'a> {
|
|
|
|
|
cx: &'a mut ExtCtxt<'b>,
|
2016-09-07 22:24:01 +00:00
|
|
|
|
cfg: StripUnconfigured<'a>,
|
2016-09-02 09:12:47 +00:00
|
|
|
|
invocations: Vec<Invocation>,
|
2016-09-06 05:42:45 +00:00
|
|
|
|
monotonic: bool,
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'b> InvocationCollector<'a, 'b> {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
|
2017-03-22 08:39:51 +00:00
|
|
|
|
let mark = Mark::fresh(self.cx.current_expansion.mark);
|
2016-09-02 09:12:47 +00:00
|
|
|
|
self.invocations.push(Invocation {
|
2017-08-06 22:54:09 -07:00
|
|
|
|
kind,
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fragment_kind,
|
2016-09-26 22:54:36 +00:00
|
|
|
|
expansion_data: ExpansionData {
|
2017-08-06 22:54:09 -07:00
|
|
|
|
mark,
|
2016-09-26 22:54:36 +00:00
|
|
|
|
depth: self.cx.current_expansion.depth + 1,
|
|
|
|
|
..self.cx.current_expansion.clone()
|
|
|
|
|
},
|
2016-09-01 07:01:45 +00:00
|
|
|
|
});
|
2018-06-20 02:08:08 +03:00
|
|
|
|
placeholder(fragment_kind, NodeId::placeholder_from_mark(mark))
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
fn collect_bang(&mut self, mac: ast::Mac, span: Span, kind: AstFragmentKind) -> AstFragment {
|
2016-12-01 11:54:01 +00:00
|
|
|
|
self.collect(kind, InvocationKind::Bang { mac: mac, ident: None, span: span })
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
|
fn collect_attr(&mut self,
|
|
|
|
|
attr: Option<ast::Attribute>,
|
2017-03-08 23:13:35 +00:00
|
|
|
|
traits: Vec<Path>,
|
2017-02-02 07:01:15 +00:00
|
|
|
|
item: Annotatable,
|
2018-06-20 02:08:08 +03:00
|
|
|
|
kind: AstFragmentKind)
|
|
|
|
|
-> AstFragment {
|
2017-09-21 00:20:56 -07:00
|
|
|
|
self.collect(kind, InvocationKind::Attr { attr, traits, item })
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 23:19:21 -07:00
|
|
|
|
/// If `item` is an attr invocation, remove and return the macro attribute and derive traits.
|
2017-03-08 23:13:35 +00:00
|
|
|
|
fn classify_item<T>(&mut self, mut item: T) -> (Option<ast::Attribute>, Vec<Path>, T)
|
2017-02-02 07:01:15 +00:00
|
|
|
|
where T: HasAttrs,
|
|
|
|
|
{
|
|
|
|
|
let (mut attr, mut traits) = (None, Vec::new());
|
2017-02-01 21:03:09 +10:30
|
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
|
item = item.map_attrs(|mut attrs| {
|
2018-04-17 23:19:21 -07:00
|
|
|
|
if let Some(legacy_attr_invoc) = self.cx.resolver.find_legacy_attr_invoc(&mut attrs,
|
|
|
|
|
true) {
|
2017-02-02 07:01:15 +00:00
|
|
|
|
attr = Some(legacy_attr_invoc);
|
|
|
|
|
return attrs;
|
|
|
|
|
}
|
2017-02-01 21:03:09 +10:30
|
|
|
|
|
2018-05-14 03:22:52 +03:00
|
|
|
|
attr = find_attr_invoc(&mut attrs);
|
2017-02-02 07:01:15 +00:00
|
|
|
|
traits = collect_derives(&mut self.cx, &mut attrs);
|
2016-09-02 09:12:47 +00:00
|
|
|
|
attrs
|
|
|
|
|
});
|
2017-02-01 21:03:09 +10:30
|
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
|
(attr, traits, item)
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 23:19:21 -07:00
|
|
|
|
/// Alternative of `classify_item()` that ignores `#[derive]` so invocations fallthrough
|
|
|
|
|
/// to the unused-attributes lint (making it an error on statements and expressions
|
|
|
|
|
/// is a breaking change)
|
|
|
|
|
fn classify_nonitem<T: HasAttrs>(&mut self, mut item: T) -> (Option<ast::Attribute>, T) {
|
|
|
|
|
let mut attr = None;
|
|
|
|
|
|
|
|
|
|
item = item.map_attrs(|mut attrs| {
|
|
|
|
|
if let Some(legacy_attr_invoc) = self.cx.resolver.find_legacy_attr_invoc(&mut attrs,
|
|
|
|
|
false) {
|
|
|
|
|
attr = Some(legacy_attr_invoc);
|
|
|
|
|
return attrs;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 03:22:52 +03:00
|
|
|
|
attr = find_attr_invoc(&mut attrs);
|
2018-04-17 23:19:21 -07:00
|
|
|
|
attrs
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
(attr, item)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 22:24:01 +00:00
|
|
|
|
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
|
|
|
|
|
self.cfg.configure(node)
|
|
|
|
|
}
|
2016-12-01 11:20:04 +00:00
|
|
|
|
|
|
|
|
|
// Detect use of feature-gated or invalid attributes on macro invocations
|
|
|
|
|
// since they will not be detected after macro expansion.
|
|
|
|
|
fn check_attributes(&mut self, attrs: &[ast::Attribute]) {
|
|
|
|
|
let features = self.cx.ecfg.features.unwrap();
|
|
|
|
|
for attr in attrs.iter() {
|
2018-01-30 19:28:55 +09:00
|
|
|
|
self.check_attribute_inner(attr, features);
|
2018-04-17 23:19:21 -07:00
|
|
|
|
|
|
|
|
|
// macros are expanded before any lint passes so this warning has to be hardcoded
|
|
|
|
|
if attr.path == "derive" {
|
|
|
|
|
self.cx.struct_span_warn(attr.span, "`#[derive]` does nothing on macro invocations")
|
|
|
|
|
.note("this may become a hard error in a future release")
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
2016-12-01 11:20:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-21 22:37:00 -05:00
|
|
|
|
|
|
|
|
|
fn check_attribute(&mut self, at: &ast::Attribute) {
|
|
|
|
|
let features = self.cx.ecfg.features.unwrap();
|
2018-01-30 19:28:55 +09:00
|
|
|
|
self.check_attribute_inner(at, features);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_attribute_inner(&mut self, at: &ast::Attribute, features: &Features) {
|
2017-09-21 22:37:00 -05:00
|
|
|
|
feature_gate::check_attribute(at, self.cx.parse_sess, features);
|
|
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 08:44:05 +00:00
|
|
|
|
pub fn find_attr_invoc(attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> {
|
2017-05-12 20:05:39 +02:00
|
|
|
|
attrs.iter()
|
|
|
|
|
.position(|a| !attr::is_known(a) && !is_builtin_attr(a))
|
|
|
|
|
.map(|i| attrs.remove(i))
|
2017-02-02 07:01:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
|
impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
|
2014-09-13 19:06:01 +03:00
|
|
|
|
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2017-12-17 02:21:29 +03:00
|
|
|
|
let mut expr = self.cfg.configure_expr(expr).into_inner();
|
2016-09-07 22:24:01 +00:00
|
|
|
|
expr.node = self.cfg.configure_expr_kind(expr.node);
|
|
|
|
|
|
2018-04-17 23:19:21 -07:00
|
|
|
|
// ignore derives so they remain unused
|
|
|
|
|
let (attr, expr) = self.classify_nonitem(expr);
|
2018-03-15 23:20:56 -07:00
|
|
|
|
|
2018-04-17 23:19:21 -07:00
|
|
|
|
if attr.is_some() {
|
2018-03-15 23:20:56 -07:00
|
|
|
|
// collect the invoc regardless of whether or not attributes are permitted here
|
|
|
|
|
// expansion will eat the attribute so it won't error later
|
|
|
|
|
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
|
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
|
// AstFragmentKind::Expr requires the macro to emit an expression
|
|
|
|
|
return self.collect_attr(attr, vec![], Annotatable::Expr(P(expr)),
|
|
|
|
|
AstFragmentKind::Expr).make_expr();
|
2018-03-15 23:20:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 23:03:52 +00:00
|
|
|
|
if let ast::ExprKind::Mac(mac) = expr.node {
|
2016-12-01 11:54:01 +00:00
|
|
|
|
self.check_attributes(&expr.attrs);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
self.collect_bang(mac, expr.span, AstFragmentKind::Expr).make_expr()
|
2016-08-30 23:03:52 +00:00
|
|
|
|
} else {
|
|
|
|
|
P(noop_fold_expr(expr, self))
|
|
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-16 10:09:23 +00:00
|
|
|
|
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
2017-12-17 02:21:29 +03:00
|
|
|
|
let mut expr = configure!(self, expr).into_inner();
|
2016-09-07 22:24:01 +00:00
|
|
|
|
expr.node = self.cfg.configure_expr_kind(expr.node);
|
|
|
|
|
|
2018-04-17 23:19:21 -07:00
|
|
|
|
// ignore derives so they remain unused
|
|
|
|
|
let (attr, expr) = self.classify_nonitem(expr);
|
2018-03-15 23:20:56 -07:00
|
|
|
|
|
2018-04-17 23:19:21 -07:00
|
|
|
|
if attr.is_some() {
|
2018-03-15 23:20:56 -07:00
|
|
|
|
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
|
|
|
|
|
|
2018-04-17 23:19:21 -07:00
|
|
|
|
return self.collect_attr(attr, vec![], Annotatable::Expr(P(expr)),
|
2018-06-20 02:08:08 +03:00
|
|
|
|
AstFragmentKind::OptExpr)
|
2018-03-15 23:20:56 -07:00
|
|
|
|
.make_opt_expr();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 23:03:52 +00:00
|
|
|
|
if let ast::ExprKind::Mac(mac) = expr.node {
|
2016-12-01 11:54:01 +00:00
|
|
|
|
self.check_attributes(&expr.attrs);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
self.collect_bang(mac, expr.span, AstFragmentKind::OptExpr).make_opt_expr()
|
2016-08-30 23:03:52 +00:00
|
|
|
|
} else {
|
|
|
|
|
Some(P(noop_fold_expr(expr, self)))
|
|
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-13 19:06:01 +03:00
|
|
|
|
fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
|
2017-01-03 19:13:01 -08:00
|
|
|
|
let pat = self.cfg.configure_pat(pat);
|
2016-08-30 23:03:52 +00:00
|
|
|
|
match pat.node {
|
|
|
|
|
PatKind::Mac(_) => {}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
_ => return noop_fold_pat(pat, self),
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
2014-05-19 13:59:35 -07:00
|
|
|
|
|
2016-08-30 23:03:52 +00:00
|
|
|
|
pat.and_then(|pat| match pat.node {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
PatKind::Mac(mac) => self.collect_bang(mac, pat.span, AstFragmentKind::Pat).make_pat(),
|
2016-08-30 23:03:52 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
})
|
2013-08-29 12:10:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-05 12:04:56 +02:00
|
|
|
|
fn fold_stmt(&mut self, stmt: ast::Stmt) -> OneVector<ast::Stmt> {
|
2018-03-15 23:20:56 -07:00
|
|
|
|
let mut stmt = match self.cfg.configure_stmt(stmt) {
|
2016-09-07 22:24:01 +00:00
|
|
|
|
Some(stmt) => stmt,
|
2018-08-05 12:04:56 +02:00
|
|
|
|
None => return OneVector::new(),
|
2016-09-07 22:24:01 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-03-15 23:20:56 -07:00
|
|
|
|
// we'll expand attributes on expressions separately
|
|
|
|
|
if !stmt.is_expr() {
|
2018-04-17 23:19:21 -07:00
|
|
|
|
let (attr, derives, stmt_) = if stmt.is_item() {
|
|
|
|
|
self.classify_item(stmt)
|
|
|
|
|
} else {
|
|
|
|
|
// ignore derives on non-item statements so it falls through
|
|
|
|
|
// to the unused-attributes lint
|
|
|
|
|
let (attr, stmt) = self.classify_nonitem(stmt);
|
|
|
|
|
(attr, vec![], stmt)
|
|
|
|
|
};
|
2018-03-15 23:20:56 -07:00
|
|
|
|
|
|
|
|
|
if attr.is_some() || !derives.is_empty() {
|
|
|
|
|
return self.collect_attr(attr, derives,
|
2018-06-20 02:08:08 +03:00
|
|
|
|
Annotatable::Stmt(P(stmt_)), AstFragmentKind::Stmts)
|
2018-03-15 23:20:56 -07:00
|
|
|
|
.make_stmts();
|
|
|
|
|
}
|
2016-08-30 23:03:52 +00:00
|
|
|
|
|
2018-03-15 23:20:56 -07:00
|
|
|
|
stmt = stmt_;
|
|
|
|
|
}
|
2016-08-30 23:03:52 +00:00
|
|
|
|
|
2018-03-15 23:20:56 -07:00
|
|
|
|
if let StmtKind::Mac(mac) = stmt.node {
|
|
|
|
|
let (mac, style, attrs) = mac.into_inner();
|
|
|
|
|
self.check_attributes(&attrs);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
let mut placeholder = self.collect_bang(mac, stmt.span, AstFragmentKind::Stmts)
|
2018-03-15 23:20:56 -07:00
|
|
|
|
.make_stmts();
|
|
|
|
|
|
|
|
|
|
// If this is a macro invocation with a semicolon, then apply that
|
|
|
|
|
// semicolon to the final statement produced by expansion.
|
|
|
|
|
if style == MacStmtStyle::Semicolon {
|
|
|
|
|
if let Some(stmt) = placeholder.pop() {
|
|
|
|
|
placeholder.push(stmt.add_trailing_semicolon());
|
|
|
|
|
}
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
2018-03-15 23:20:56 -07:00
|
|
|
|
|
|
|
|
|
return placeholder;
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-15 23:20:56 -07:00
|
|
|
|
// The placeholder expander gives ids to statements, so we avoid folding the id here.
|
|
|
|
|
let ast::Stmt { id, node, span } = stmt;
|
|
|
|
|
noop_fold_stmt_kind(node, self).into_iter().map(|node| {
|
|
|
|
|
ast::Stmt { id, node, span }
|
|
|
|
|
}).collect()
|
|
|
|
|
|
2013-08-29 12:10:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
|
fn fold_block(&mut self, block: P<Block>) -> P<Block> {
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let old_directory_ownership = self.cx.current_expansion.directory_ownership;
|
|
|
|
|
self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
|
2016-09-01 06:44:54 +00:00
|
|
|
|
let result = noop_fold_block(block, self);
|
2016-11-05 04:16:26 +00:00
|
|
|
|
self.cx.current_expansion.directory_ownership = old_directory_ownership;
|
2016-03-01 09:28:42 +00:00
|
|
|
|
result
|
2013-08-29 12:10:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-05 12:04:56 +02:00
|
|
|
|
fn fold_item(&mut self, item: P<ast::Item>) -> OneVector<P<ast::Item>> {
|
2016-09-07 22:24:01 +00:00
|
|
|
|
let item = configure!(self, item);
|
|
|
|
|
|
2018-09-02 09:03:24 -07:00
|
|
|
|
let (attr, traits, item) = self.classify_item(item);
|
2017-02-02 07:01:15 +00:00
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
2017-09-12 21:55:41 +02:00
|
|
|
|
let item = Annotatable::Item(item);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
return self.collect_attr(attr, traits, item, AstFragmentKind::Items).make_items();
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
|
ast::ItemKind::Mac(..) => {
|
2016-12-01 11:20:04 +00:00
|
|
|
|
self.check_attributes(&item.attrs);
|
2017-03-05 05:15:58 +00:00
|
|
|
|
item.and_then(|item| match item.node {
|
2016-08-30 23:03:52 +00:00
|
|
|
|
ItemKind::Mac(mac) => {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
self.collect(AstFragmentKind::Items, InvocationKind::Bang {
|
2017-08-06 22:54:09 -07:00
|
|
|
|
mac,
|
2016-08-30 23:03:52 +00:00
|
|
|
|
ident: Some(item.ident),
|
|
|
|
|
span: item.span,
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}).make_items()
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
ast::ItemKind::Mod(ast::Mod { inner, .. }) => {
|
2016-09-06 01:45:23 +00:00
|
|
|
|
if item.ident == keywords::Invalid.ident() {
|
2018-09-02 09:03:24 -07:00
|
|
|
|
return noop_fold_item(item, self);
|
2016-09-06 01:45:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let orig_directory_ownership = self.cx.current_expansion.directory_ownership;
|
2016-09-07 23:21:59 +00:00
|
|
|
|
let mut module = (*self.cx.current_expansion.module).clone();
|
|
|
|
|
module.mod_path.push(item.ident);
|
2016-09-05 04:08:38 +00:00
|
|
|
|
|
|
|
|
|
// Detect if this is an inline module (`mod m { ... }` as opposed to `mod m;`).
|
|
|
|
|
// In the non-inline case, `inner` is never the dummy span (c.f. `parse_item_mod`).
|
|
|
|
|
// Thus, if `inner` is the dummy span, we know the module is inline.
|
2018-06-25 01:00:21 +03:00
|
|
|
|
let inline_module = item.span.contains(inner) || inner.is_dummy();
|
2016-09-05 04:08:38 +00:00
|
|
|
|
|
|
|
|
|
if inline_module {
|
2016-09-27 21:14:45 +00:00
|
|
|
|
if let Some(path) = attr::first_attr_value_str_by_name(&item.attrs, "path") {
|
2017-11-27 18:14:24 -08:00
|
|
|
|
self.cx.current_expansion.directory_ownership =
|
|
|
|
|
DirectoryOwnership::Owned { relative: None };
|
2016-11-16 10:52:37 +00:00
|
|
|
|
module.directory.push(&*path.as_str());
|
2016-09-27 21:14:45 +00:00
|
|
|
|
} else {
|
2018-05-26 15:12:38 +03:00
|
|
|
|
module.directory.push(&*item.ident.as_str());
|
2016-09-27 21:14:45 +00:00
|
|
|
|
}
|
2016-08-30 23:03:52 +00:00
|
|
|
|
} else {
|
2018-08-18 12:14:09 +02:00
|
|
|
|
let path = self.cx.parse_sess.source_map().span_to_unmapped_path(inner);
|
2017-12-14 08:09:19 +01:00
|
|
|
|
let mut path = match path {
|
|
|
|
|
FileName::Real(path) => path,
|
|
|
|
|
other => PathBuf::from(other.to_string()),
|
|
|
|
|
};
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let directory_ownership = match path.file_name().unwrap().to_str() {
|
2017-11-27 18:14:24 -08:00
|
|
|
|
Some("mod.rs") => DirectoryOwnership::Owned { relative: None },
|
|
|
|
|
Some(_) => DirectoryOwnership::Owned {
|
|
|
|
|
relative: Some(item.ident),
|
|
|
|
|
},
|
|
|
|
|
None => DirectoryOwnership::UnownedViaMod(false),
|
2016-11-05 04:16:26 +00:00
|
|
|
|
};
|
|
|
|
|
path.pop();
|
|
|
|
|
module.directory = path;
|
|
|
|
|
self.cx.current_expansion.directory_ownership = directory_ownership;
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
|
let orig_module =
|
|
|
|
|
mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
|
2018-09-02 09:03:24 -07:00
|
|
|
|
let result = noop_fold_item(item, self);
|
2016-09-07 23:21:59 +00:00
|
|
|
|
self.cx.current_expansion.module = orig_module;
|
2016-11-05 04:16:26 +00:00
|
|
|
|
self.cx.current_expansion.directory_ownership = orig_directory_ownership;
|
2017-05-12 20:05:39 +02:00
|
|
|
|
result
|
2016-09-07 23:21:59 +00:00
|
|
|
|
}
|
2018-07-20 18:04:02 -07:00
|
|
|
|
|
2018-09-02 09:03:24 -07:00
|
|
|
|
_ => noop_fold_item(item, self),
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
2014-12-02 10:07:41 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-05 12:04:56 +02:00
|
|
|
|
fn fold_trait_item(&mut self, item: ast::TraitItem) -> OneVector<ast::TraitItem> {
|
2016-09-07 22:24:01 +00:00
|
|
|
|
let item = configure!(self, item);
|
|
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
|
let (attr, traits, item) = self.classify_item(item);
|
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
2017-09-12 21:55:41 +02:00
|
|
|
|
let item = Annotatable::TraitItem(P(item));
|
2018-06-20 02:08:08 +03:00
|
|
|
|
return self.collect_attr(attr, traits, item, AstFragmentKind::TraitItems)
|
2017-02-02 07:01:15 +00:00
|
|
|
|
.make_trait_items()
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
|
ast::TraitItemKind::Macro(mac) => {
|
|
|
|
|
let ast::TraitItem { attrs, span, .. } = item;
|
2016-12-01 11:54:01 +00:00
|
|
|
|
self.check_attributes(&attrs);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
self.collect_bang(mac, span, AstFragmentKind::TraitItems).make_trait_items()
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
_ => fold::noop_fold_trait_item(item, self),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-05 12:04:56 +02:00
|
|
|
|
fn fold_impl_item(&mut self, item: ast::ImplItem) -> OneVector<ast::ImplItem> {
|
2016-09-07 22:24:01 +00:00
|
|
|
|
let item = configure!(self, item);
|
|
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
|
let (attr, traits, item) = self.classify_item(item);
|
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
2017-09-12 21:55:41 +02:00
|
|
|
|
let item = Annotatable::ImplItem(P(item));
|
2018-06-20 02:08:08 +03:00
|
|
|
|
return self.collect_attr(attr, traits, item, AstFragmentKind::ImplItems)
|
2017-02-02 07:01:15 +00:00
|
|
|
|
.make_impl_items();
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
|
ast::ImplItemKind::Macro(mac) => {
|
|
|
|
|
let ast::ImplItem { attrs, span, .. } = item;
|
2016-12-01 11:54:01 +00:00
|
|
|
|
self.check_attributes(&attrs);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
self.collect_bang(mac, span, AstFragmentKind::ImplItems).make_impl_items()
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
_ => fold::noop_fold_impl_item(item, self),
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
2014-07-04 11:24:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-25 21:54:19 -07:00
|
|
|
|
fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
|
2016-08-30 23:03:52 +00:00
|
|
|
|
let ty = match ty.node {
|
2017-12-17 02:21:29 +03:00
|
|
|
|
ast::TyKind::Mac(_) => ty.into_inner(),
|
2016-08-30 23:03:52 +00:00
|
|
|
|
_ => return fold::noop_fold_ty(ty, self),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match ty.node {
|
2018-06-20 02:08:08 +03:00
|
|
|
|
ast::TyKind::Mac(mac) => self.collect_bang(mac, ty.span, AstFragmentKind::Ty).make_ty(),
|
2016-08-30 23:03:52 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
}
|
2015-07-25 21:54:19 -07:00
|
|
|
|
}
|
2016-09-07 22:24:01 +00:00
|
|
|
|
|
|
|
|
|
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
|
|
|
|
|
noop_fold_foreign_mod(self.cfg.configure_foreign_mod(foreign_mod), self)
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-10 18:16:26 -08:00
|
|
|
|
fn fold_foreign_item(&mut self,
|
2018-08-05 12:04:56 +02:00
|
|
|
|
foreign_item: ast::ForeignItem) -> OneVector<ast::ForeignItem> {
|
2018-03-10 18:16:26 -08:00
|
|
|
|
let (attr, traits, foreign_item) = self.classify_item(foreign_item);
|
|
|
|
|
|
2018-08-04 05:17:51 +03:00
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
2018-03-10 18:16:26 -08:00
|
|
|
|
let item = Annotatable::ForeignItem(P(foreign_item));
|
2018-06-20 02:08:08 +03:00
|
|
|
|
return self.collect_attr(attr, traits, item, AstFragmentKind::ForeignItems)
|
2018-03-10 18:16:26 -08:00
|
|
|
|
.make_foreign_items();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let ast::ForeignItemKind::Macro(mac) = foreign_item.node {
|
|
|
|
|
self.check_attributes(&foreign_item.attrs);
|
2018-06-20 02:08:08 +03:00
|
|
|
|
return self.collect_bang(mac, foreign_item.span, AstFragmentKind::ForeignItems)
|
2018-03-10 18:16:26 -08:00
|
|
|
|
.make_foreign_items();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
noop_fold_foreign_item(foreign_item, self)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 22:24:01 +00:00
|
|
|
|
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
|
2017-03-05 05:15:58 +00:00
|
|
|
|
match item {
|
|
|
|
|
ast::ItemKind::MacroDef(..) => item,
|
|
|
|
|
_ => noop_fold_item_kind(self.cfg.configure_item_kind(item), self),
|
|
|
|
|
}
|
2016-09-07 22:24:01 +00:00
|
|
|
|
}
|
2016-09-05 00:10:27 +00:00
|
|
|
|
|
2018-06-02 05:10:29 +08:00
|
|
|
|
fn fold_generic_param(&mut self, param: ast::GenericParam) -> ast::GenericParam {
|
|
|
|
|
self.cfg.disallow_cfg_on_generic_param(¶m);
|
|
|
|
|
noop_fold_generic_param(param, self)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 22:37:00 -05:00
|
|
|
|
fn fold_attribute(&mut self, at: ast::Attribute) -> Option<ast::Attribute> {
|
|
|
|
|
// turn `#[doc(include="filename")]` attributes into `#[doc(include(file="filename",
|
|
|
|
|
// contents="file contents")]` attributes
|
|
|
|
|
if !at.check_name("doc") {
|
|
|
|
|
return noop_fold_attribute(at, self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(list) = at.meta_item_list() {
|
|
|
|
|
if !list.iter().any(|it| it.check_name("include")) {
|
|
|
|
|
return noop_fold_attribute(at, self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut items = vec![];
|
|
|
|
|
|
|
|
|
|
for it in list {
|
|
|
|
|
if !it.check_name("include") {
|
|
|
|
|
items.push(noop_fold_meta_list_item(it, self));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(file) = it.value_str() {
|
|
|
|
|
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
|
|
|
|
|
self.check_attribute(&at);
|
|
|
|
|
if self.cx.parse_sess.span_diagnostic.err_count() > err_count {
|
|
|
|
|
// avoid loading the file if they haven't enabled the feature
|
|
|
|
|
return noop_fold_attribute(at, self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut buf = vec![];
|
|
|
|
|
let filename = self.cx.root_path.join(file.to_string());
|
|
|
|
|
|
|
|
|
|
match File::open(&filename).and_then(|mut f| f.read_to_end(&mut buf)) {
|
|
|
|
|
Ok(..) => {}
|
|
|
|
|
Err(e) => {
|
2017-12-19 15:04:03 -06:00
|
|
|
|
self.cx.span_err(at.span,
|
|
|
|
|
&format!("couldn't read {}: {}",
|
|
|
|
|
filename.display(),
|
|
|
|
|
e));
|
2017-09-21 22:37:00 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match String::from_utf8(buf) {
|
|
|
|
|
Ok(src) => {
|
2018-05-23 16:19:20 +02:00
|
|
|
|
let src_interned = Symbol::intern(&src);
|
|
|
|
|
|
2017-12-19 16:43:32 -06:00
|
|
|
|
// Add this input file to the code map to make it available as
|
|
|
|
|
// dependency information
|
2018-08-18 12:14:09 +02:00
|
|
|
|
self.cx.source_map().new_source_file(filename.into(), src);
|
2017-12-19 16:43:32 -06:00
|
|
|
|
|
2017-09-21 22:37:00 -05:00
|
|
|
|
let include_info = vec![
|
|
|
|
|
dummy_spanned(ast::NestedMetaItemKind::MetaItem(
|
2018-03-24 21:17:27 +03:00
|
|
|
|
attr::mk_name_value_item_str(Ident::from_str("file"),
|
|
|
|
|
dummy_spanned(file)))),
|
2017-09-21 22:37:00 -05:00
|
|
|
|
dummy_spanned(ast::NestedMetaItemKind::MetaItem(
|
2018-03-24 21:17:27 +03:00
|
|
|
|
attr::mk_name_value_item_str(Ident::from_str("contents"),
|
2018-05-23 16:19:20 +02:00
|
|
|
|
dummy_spanned(src_interned)))),
|
2017-09-21 22:37:00 -05:00
|
|
|
|
];
|
|
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
|
let include_ident = Ident::from_str("include");
|
|
|
|
|
let item = attr::mk_list_item(DUMMY_SP, include_ident, include_info);
|
|
|
|
|
items.push(dummy_spanned(ast::NestedMetaItemKind::MetaItem(item)));
|
2017-09-21 22:37:00 -05:00
|
|
|
|
}
|
|
|
|
|
Err(_) => {
|
2017-12-19 15:04:03 -06:00
|
|
|
|
self.cx.span_err(at.span,
|
|
|
|
|
&format!("{} wasn't a utf-8 file",
|
|
|
|
|
filename.display()));
|
2017-09-21 22:37:00 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
items.push(noop_fold_meta_list_item(it, self));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
|
let meta = attr::mk_list_item(DUMMY_SP, Ident::from_str("doc"), items);
|
2017-09-21 22:37:00 -05:00
|
|
|
|
match at.style {
|
|
|
|
|
ast::AttrStyle::Inner =>
|
|
|
|
|
Some(attr::mk_spanned_attr_inner(at.span, at.id, meta)),
|
|
|
|
|
ast::AttrStyle::Outer =>
|
|
|
|
|
Some(attr::mk_spanned_attr_outer(at.span, at.id, meta)),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
noop_fold_attribute(at, self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 00:10:27 +00:00
|
|
|
|
fn new_id(&mut self, id: ast::NodeId) -> ast::NodeId {
|
2016-09-06 05:42:45 +00:00
|
|
|
|
if self.monotonic {
|
|
|
|
|
assert_eq!(id, ast::DUMMY_NODE_ID);
|
|
|
|
|
self.cx.resolver.next_node_id()
|
|
|
|
|
} else {
|
|
|
|
|
id
|
|
|
|
|
}
|
2016-09-05 00:10:27 +00:00
|
|
|
|
}
|
2013-07-16 15:05:50 +10:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 21:30:45 +01:00
|
|
|
|
pub struct ExpansionConfig<'feat> {
|
2014-06-06 13:21:18 -07:00
|
|
|
|
pub crate_name: String,
|
2015-02-15 21:30:45 +01:00
|
|
|
|
pub features: Option<&'feat Features>,
|
2015-01-17 23:33:05 +00:00
|
|
|
|
pub recursion_limit: usize,
|
2015-04-14 15:36:38 +02:00
|
|
|
|
pub trace_mac: bool,
|
2016-06-01 01:27:12 +00:00
|
|
|
|
pub should_test: bool, // If false, strip `#[test]` nodes
|
2016-09-12 09:47:54 +00:00
|
|
|
|
pub single_step: bool,
|
|
|
|
|
pub keep_macs: bool,
|
2014-09-26 17:14:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-06 12:56:28 -08:00
|
|
|
|
macro_rules! feature_tests {
|
|
|
|
|
($( fn $getter:ident = $field:ident, )*) => {
|
|
|
|
|
$(
|
|
|
|
|
pub fn $getter(&self) -> bool {
|
|
|
|
|
match self.features {
|
|
|
|
|
Some(&Features { $field: true, .. }) => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)*
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 21:30:45 +01:00
|
|
|
|
impl<'feat> ExpansionConfig<'feat> {
|
|
|
|
|
pub fn default(crate_name: String) -> ExpansionConfig<'static> {
|
2014-09-26 17:14:23 -07:00
|
|
|
|
ExpansionConfig {
|
2017-08-06 22:54:09 -07:00
|
|
|
|
crate_name,
|
2015-02-15 21:30:45 +01:00
|
|
|
|
features: None,
|
2017-05-01 13:49:15 -04:00
|
|
|
|
recursion_limit: 1024,
|
2015-04-14 15:36:38 +02:00
|
|
|
|
trace_mac: false,
|
2016-06-01 01:27:12 +00:00
|
|
|
|
should_test: false,
|
2016-09-12 09:47:54 +00:00
|
|
|
|
single_step: false,
|
|
|
|
|
keep_macs: false,
|
2014-09-26 17:14:23 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-15 21:30:45 +01:00
|
|
|
|
|
2015-03-06 12:56:28 -08:00
|
|
|
|
feature_tests! {
|
2016-04-04 17:08:41 +02:00
|
|
|
|
fn enable_quotes = quote,
|
|
|
|
|
fn enable_asm = asm,
|
2018-09-02 09:03:24 -07:00
|
|
|
|
fn enable_custom_test_frameworks = custom_test_frameworks,
|
2017-03-15 21:27:40 -05:00
|
|
|
|
fn enable_global_asm = global_asm,
|
2016-04-04 17:08:41 +02:00
|
|
|
|
fn enable_log_syntax = log_syntax,
|
|
|
|
|
fn enable_concat_idents = concat_idents,
|
|
|
|
|
fn enable_trace_macros = trace_macros,
|
2015-03-06 12:56:28 -08:00
|
|
|
|
fn enable_allow_internal_unstable = allow_internal_unstable,
|
2016-04-04 17:08:41 +02:00
|
|
|
|
fn enable_custom_derive = custom_derive,
|
2018-07-21 15:50:46 -07:00
|
|
|
|
fn enable_format_args_nl = format_args_nl,
|
2018-03-10 18:16:26 -08:00
|
|
|
|
fn macros_in_extern_enabled = macros_in_extern,
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 07:50:39 -07:00
|
|
|
|
fn proc_macro_mod = proc_macro_mod,
|
2018-05-09 15:03:02 -07:00
|
|
|
|
fn proc_macro_gen = proc_macro_gen,
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 07:50:39 -07:00
|
|
|
|
fn proc_macro_expr = proc_macro_expr,
|
|
|
|
|
fn proc_macro_non_items = proc_macro_non_items,
|
Add #[allow_internal_unstable] to track stability for macros better.
Unstable items used in a macro expansion will now always trigger
stability warnings, *unless* the unstable items are directly inside a
macro marked with `#[allow_internal_unstable]`. IOW, the compiler warns
unless the span of the unstable item is a subspan of the definition of a
macro marked with that attribute.
E.g.
#[allow_internal_unstable]
macro_rules! foo {
($e: expr) => {{
$e;
unstable(); // no warning
only_called_by_foo!();
}}
}
macro_rules! only_called_by_foo {
() => { unstable() } // warning
}
foo!(unstable()) // warning
The unstable inside `foo` is fine, due to the attribute. But the
`unstable` inside `only_called_by_foo` is not, since that macro doesn't
have the attribute, and the `unstable` passed into `foo` is also not
fine since it isn't contained in the macro itself (that is, even though
it is only used directly in the macro).
In the process this makes the stability tracking much more precise,
e.g. previously `println!("{}", unstable())` got no warning, but now it
does. As such, this is a bug fix that may cause [breaking-change]s.
The attribute is definitely feature gated, since it explicitly allows
side-stepping the feature gating system.
2015-03-01 14:09:28 +11:00
|
|
|
|
}
|
2014-02-28 23:17:38 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 04:04:41 +00:00
|
|
|
|
// A Marker adds the given mark to the syntax context.
|
2017-07-19 21:54:01 -07:00
|
|
|
|
#[derive(Debug)]
|
2017-03-28 05:32:43 +00:00
|
|
|
|
pub struct Marker(pub Mark);
|
2013-07-09 15:56:21 -07:00
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
|
impl Folder for Marker {
|
2017-07-31 23:04:34 +03:00
|
|
|
|
fn new_span(&mut self, span: Span) -> Span {
|
2018-03-18 23:51:53 +03:00
|
|
|
|
span.apply_mark(self.0)
|
2013-07-09 15:56:21 -07:00
|
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
|
|
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
|
|
|
|
noop_fold_mac(mac, self)
|
|
|
|
|
}
|
2013-07-09 15:56:21 -07:00
|
|
|
|
}
|