Skip to content

Perform cfg attribute processing during macro expansion and fix bugs #33706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
@@ -720,16 +720,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
ret
});

// JBC: make CFG processing part of expansion to avoid this problem:

// strip again, in case expansion added anything with a #[cfg].
krate = sess.track_errors(|| {
let krate = time(time_passes, "configuration 2", || {
syntax::config::strip_unconfigured_items(sess.diagnostic(),
krate,
&mut feature_gated_cfgs)
});

time(time_passes, "gated configuration checking", || {
let features = sess.features.borrow();
feature_gated_cfgs.sort();
25 changes: 5 additions & 20 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ pub use self::UnsafeSource::*;
pub use self::ViewPath_::*;
pub use self::PathParameters::*;

use attr::ThinAttributes;
use attr::{ThinAttributes, HasAttrs};
use codemap::{mk_sp, respan, Span, Spanned, DUMMY_SP, ExpnId};
use abi::Abi;
use errors;
@@ -829,13 +829,7 @@ impl StmtKind {
}

pub fn attrs(&self) -> &[Attribute] {
match *self {
StmtKind::Decl(ref d, _) => d.attrs(),
StmtKind::Expr(ref e, _) |
StmtKind::Semi(ref e, _) => e.attrs(),
StmtKind::Mac(_, _, Some(ref b)) => b,
StmtKind::Mac(_, _, None) => &[],
}
HasAttrs::attrs(self)
}
}

@@ -868,10 +862,7 @@ pub struct Local {

impl Local {
pub fn attrs(&self) -> &[Attribute] {
match self.attrs {
Some(ref b) => b,
None => &[],
}
HasAttrs::attrs(self)
}
}

@@ -887,10 +878,7 @@ pub enum DeclKind {

impl Decl {
pub fn attrs(&self) -> &[Attribute] {
match self.node {
DeclKind::Local(ref l) => l.attrs(),
DeclKind::Item(ref i) => i.attrs(),
}
HasAttrs::attrs(self)
}
}

@@ -935,10 +923,7 @@ pub struct Expr {

impl Expr {
pub fn attrs(&self) -> &[Attribute] {
match self.attrs {
Some(ref b) => b,
None => &[],
}
HasAttrs::attrs(self)
}
}

141 changes: 84 additions & 57 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
@@ -884,82 +884,109 @@ impl AttributesExt for Vec<Attribute> {
}
}

pub trait HasAttrs: Sized {
fn attrs(&self) -> &[ast::Attribute];
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self;
}

/// A cheap way to add Attributes to an AST node.
pub trait WithAttrs {
// FIXME: Could be extended to anything IntoIter<Item=Attribute>
fn with_attrs(self, attrs: ThinAttributes) -> Self;
}

impl WithAttrs for P<Expr> {
impl<T: HasAttrs> WithAttrs for T {
fn with_attrs(self, attrs: ThinAttributes) -> Self {
self.map(|mut e| {
e.attrs.update(|a| a.append(attrs));
e
self.map_attrs(|mut orig_attrs| {
orig_attrs.extend(attrs.into_attr_vec());
orig_attrs
})
}
}

impl WithAttrs for P<Item> {
fn with_attrs(self, attrs: ThinAttributes) -> Self {
self.map(|Item { ident, attrs: mut ats, id, node, vis, span }| {
ats.extend(attrs.into_attr_vec());
Item {
ident: ident,
attrs: ats,
id: id,
node: node,
vis: vis,
span: span,
}
})
impl HasAttrs for Vec<Attribute> {
fn attrs(&self) -> &[Attribute] {
&self
}
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
f(self)
}
}

impl WithAttrs for P<Local> {
fn with_attrs(self, attrs: ThinAttributes) -> Self {
self.map(|Local { pat, ty, init, id, span, attrs: mut ats }| {
ats.update(|a| a.append(attrs));
Local {
pat: pat,
ty: ty,
init: init,
id: id,
span: span,
attrs: ats,
}
})
impl HasAttrs for ThinAttributes {
fn attrs(&self) -> &[Attribute] {
self.as_attr_slice()
}
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
self.map_thin_attrs(f)
}
}

impl WithAttrs for P<Decl> {
fn with_attrs(self, attrs: ThinAttributes) -> Self {
self.map(|Spanned { span, node }| {
Spanned {
span: span,
node: match node {
DeclKind::Local(local) => DeclKind::Local(local.with_attrs(attrs)),
DeclKind::Item(item) => DeclKind::Item(item.with_attrs(attrs)),
}
}
})
impl<T: HasAttrs + 'static> HasAttrs for P<T> {
fn attrs(&self) -> &[Attribute] {
(**self).attrs()
}
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
self.map(|t| t.map_attrs(f))
}
}

impl WithAttrs for P<Stmt> {
fn with_attrs(self, attrs: ThinAttributes) -> Self {
self.map(|Spanned { span, node }| {
Spanned {
span: span,
node: match node {
StmtKind::Decl(decl, id) => StmtKind::Decl(decl.with_attrs(attrs), id),
StmtKind::Expr(expr, id) => StmtKind::Expr(expr.with_attrs(attrs), id),
StmtKind::Semi(expr, id) => StmtKind::Semi(expr.with_attrs(attrs), id),
StmtKind::Mac(mac, style, mut ats) => {
ats.update(|a| a.append(attrs));
StmtKind::Mac(mac, style, ats)
}
},
}
})
impl HasAttrs for DeclKind {
fn attrs(&self) -> &[Attribute] {
match *self {
DeclKind::Local(ref local) => local.attrs(),
DeclKind::Item(ref item) => item.attrs(),
}
}

fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
match self {
DeclKind::Local(local) => DeclKind::Local(local.map_attrs(f)),
DeclKind::Item(item) => DeclKind::Item(item.map_attrs(f)),
}
}
}

impl HasAttrs for StmtKind {
fn attrs(&self) -> &[Attribute] {
match *self {
StmtKind::Decl(ref decl, _) => decl.attrs(),
StmtKind::Expr(ref expr, _) | StmtKind::Semi(ref expr, _) => expr.attrs(),
StmtKind::Mac(_, _, ref attrs) => attrs.attrs(),
}
}

fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
match self {
StmtKind::Decl(decl, id) => StmtKind::Decl(decl.map_attrs(f), id),
StmtKind::Expr(expr, id) => StmtKind::Expr(expr.map_attrs(f), id),
StmtKind::Semi(expr, id) => StmtKind::Semi(expr.map_attrs(f), id),
StmtKind::Mac(mac, style, attrs) =>
StmtKind::Mac(mac, style, attrs.map_attrs(f)),
}
}
}

macro_rules! derive_has_attrs_from_field {
($($ty:path),*) => { derive_has_attrs_from_field!($($ty: .attrs),*); };
($($ty:path : $(.$field:ident)*),*) => { $(
impl HasAttrs for $ty {
fn attrs(&self) -> &[Attribute] {
self $(.$field)* .attrs()
}

fn map_attrs<F>(mut self, f: F) -> Self
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>,
{
self $(.$field)* = self $(.$field)* .map_attrs(f);
self
}
}
)* }
}

derive_has_attrs_from_field! {
Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm
}

derive_has_attrs_from_field! { Decl: .node, Stmt: .node, ast::Variant: .node.attrs }
25 changes: 0 additions & 25 deletions src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
@@ -1258,31 +1258,6 @@ impl CodeMap {
return a;
}

/// Check if the backtrace `subtrace` contains `suptrace` as a prefix.
pub fn more_specific_trace(&self,
mut subtrace: ExpnId,
suptrace: ExpnId)
-> bool {
loop {
if subtrace == suptrace {
return true;
}

let stop = self.with_expn_info(subtrace, |opt_expn_info| {
if let Some(expn_info) = opt_expn_info {
subtrace = expn_info.call_site.expn_id;
false
} else {
true
}
});

if stop {
return false;
}
}
}

pub fn record_expansion(&self, expn_info: ExpnInfo) -> ExpnId {
let mut expansions = self.expansions.borrow_mut();
expansions.push(expn_info);
596 changes: 184 additions & 412 deletions src/libsyntax/config.rs

Large diffs are not rendered by default.

147 changes: 65 additions & 82 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,8 @@ use ext::build::AstBuilder;
use attr;
use attr::{AttrMetaMethods, WithAttrs, ThinAttributesExt};
use codemap;
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
use codemap::{Span, Spanned, ExpnInfo, ExpnId, NameAndSpan, MacroBang, MacroAttribute};
use config::StripUnconfigured;
use ext::base::*;
use feature_gate::{self, Features};
use fold;
@@ -33,7 +34,6 @@ use visit::Visitor;
use std_inject;

use std::collections::HashSet;
use std::env;

// A trait for AST nodes and AST node lists into which macro invocations may expand.
trait MacroGenerable: Sized {
@@ -77,25 +77,35 @@ impl_macro_generable! {
"statement", .make_stmts, lift .fold_stmt, |_span| SmallVector::zero();
}

pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
return e.and_then(|ast::Expr {id, node, span, attrs}| match node {
impl MacroGenerable for Option<P<ast::Expr>> {
fn kind_name() -> &'static str { "expression" }
fn dummy(_span: Span) -> Self { None }
fn make_with<'a>(result: Box<MacResult + 'a>) -> Option<Self> {
result.make_expr().map(Some)
}
fn fold_with<F: Folder>(self, folder: &mut F) -> Self {
self.and_then(|expr| folder.fold_opt_expr(expr))
}
}

pub fn expand_expr(expr: ast::Expr, fld: &mut MacroExpander) -> P<ast::Expr> {
match expr.node {
// expr_mac should really be expr_ext or something; it's the
// entry-point for all syntax extensions.
ast::ExprKind::Mac(mac) => {
expand_mac_invoc(mac, None, attrs.into_attr_vec(), span, fld)
expand_mac_invoc(mac, None, expr.attrs.into_attr_vec(), expr.span, fld)
}

ast::ExprKind::While(cond, body, opt_ident) => {
let cond = fld.fold_expr(cond);
let (body, opt_ident) = expand_loop_block(body, opt_ident, fld);
fld.cx.expr(span, ast::ExprKind::While(cond, body, opt_ident))
.with_attrs(fold_thin_attrs(attrs, fld))
fld.cx.expr(expr.span, ast::ExprKind::While(cond, body, opt_ident))
.with_attrs(fold_thin_attrs(expr.attrs, fld))
}

ast::ExprKind::WhileLet(pat, expr, body, opt_ident) => {
ast::ExprKind::WhileLet(pat, cond, body, opt_ident) => {
let pat = fld.fold_pat(pat);
let expr = fld.fold_expr(expr);
let cond = fld.fold_expr(cond);

// Hygienic renaming of the body.
let ((body, opt_ident), mut rewritten_pats) =
@@ -107,14 +117,14 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
});
assert!(rewritten_pats.len() == 1);

let wl = ast::ExprKind::WhileLet(rewritten_pats.remove(0), expr, body, opt_ident);
fld.cx.expr(span, wl).with_attrs(fold_thin_attrs(attrs, fld))
let wl = ast::ExprKind::WhileLet(rewritten_pats.remove(0), cond, body, opt_ident);
fld.cx.expr(expr.span, wl).with_attrs(fold_thin_attrs(expr.attrs, fld))
}

ast::ExprKind::Loop(loop_block, opt_ident) => {
let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld);
fld.cx.expr(span, ast::ExprKind::Loop(loop_block, opt_ident))
.with_attrs(fold_thin_attrs(attrs, fld))
fld.cx.expr(expr.span, ast::ExprKind::Loop(loop_block, opt_ident))
.with_attrs(fold_thin_attrs(expr.attrs, fld))
}

ast::ExprKind::ForLoop(pat, head, body, opt_ident) => {
@@ -132,7 +142,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {

let head = fld.fold_expr(head);
let fl = ast::ExprKind::ForLoop(rewritten_pats.remove(0), head, body, opt_ident);
fld.cx.expr(span, fl).with_attrs(fold_thin_attrs(attrs, fld))
fld.cx.expr(expr.span, fl).with_attrs(fold_thin_attrs(expr.attrs, fld))
}

ast::ExprKind::IfLet(pat, sub_expr, body, else_opt) => {
@@ -151,7 +161,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
let else_opt = else_opt.map(|else_opt| fld.fold_expr(else_opt));
let sub_expr = fld.fold_expr(sub_expr);
let il = ast::ExprKind::IfLet(rewritten_pats.remove(0), sub_expr, body, else_opt);
fld.cx.expr(span, il).with_attrs(fold_thin_attrs(attrs, fld))
fld.cx.expr(expr.span, il).with_attrs(fold_thin_attrs(expr.attrs, fld))
}

ast::ExprKind::Closure(capture_clause, fn_decl, block, fn_decl_span) => {
@@ -160,22 +170,15 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
let new_node = ast::ExprKind::Closure(capture_clause,
rewritten_fn_decl,
rewritten_block,
fld.new_span(fn_decl_span));
P(ast::Expr{ id:id,
fn_decl_span);
P(ast::Expr{ id: expr.id,
node: new_node,
span: fld.new_span(span),
attrs: fold_thin_attrs(attrs, fld) })
span: expr.span,
attrs: fold_thin_attrs(expr.attrs, fld) })
}

_ => {
P(noop_fold_expr(ast::Expr {
id: id,
node: node,
span: span,
attrs: attrs
}, fld))
}
});
_ => P(noop_fold_expr(expr, fld)),
}
}

/// Expand a macro invocation. Returns the result of expansion.
@@ -322,8 +325,9 @@ fn expand_mac_invoc<T>(mac: ast::Mac, ident: Option<Ident>, attrs: Vec<ast::Attr
return T::dummy(span);
};

let marked = expanded.fold_with(&mut Marker { mark: mark });
let fully_expanded = marked.fold_with(fld);
let marked = expanded.fold_with(&mut Marker { mark: mark, expn_id: Some(fld.cx.backtrace()) });
let configured = marked.fold_with(&mut fld.strip_unconfigured());
let fully_expanded = configured.fold_with(fld);
fld.cx.bt_pop();
fully_expanded
}
@@ -699,12 +703,12 @@ impl<'a> Folder for PatIdentRenamer<'a> {
mtwt::apply_renames(self.renames, ident.ctxt));
let new_node =
PatKind::Ident(binding_mode,
Spanned{span: self.new_span(sp), node: new_ident},
Spanned{span: sp, node: new_ident},
sub.map(|p| self.fold_pat(p)));
ast::Pat {
id: id,
node: new_node,
span: self.new_span(span)
span: span,
}
},
_ => unreachable!()
@@ -774,7 +778,7 @@ fn expand_annotatable(a: Annotatable,
}
_ => unreachable!()
},
span: fld.new_span(ti.span)
span: ti.span,
})
}
_ => fold::noop_fold_trait_item(it.unwrap(), fld)
@@ -914,7 +918,7 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
}
_ => unreachable!()
},
span: fld.new_span(ii.span)
span: ii.span,
}),
ast::ImplItemKind::Macro(mac) => {
expand_mac_invoc(mac, None, ii.attrs, ii.span, fld)
@@ -988,6 +992,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
pub fn new(cx: &'a mut ExtCtxt<'b>) -> MacroExpander<'a, 'b> {
MacroExpander { cx: cx }
}

fn strip_unconfigured(&mut self) -> StripUnconfigured {
StripUnconfigured::new(&self.cx.cfg,
&self.cx.parse_sess.span_diagnostic,
self.cx.feature_gated_cfgs)
}
}

impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
@@ -997,7 +1007,15 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
}

fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
expand_expr(expr, self)
expr.and_then(|expr| expand_expr(expr, self))
}

fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
expr.and_then(|expr| match expr.node {
ast::ExprKind::Mac(mac) =>
expand_mac_invoc(mac, None, expr.attrs.into_attr_vec(), expr.span, self),
_ => Some(expand_expr(expr, self)),
})
}

fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
@@ -1060,10 +1078,6 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
expand_type(ty, self)
}

fn new_span(&mut self, span: Span) -> Span {
new_span(self.cx, span)
}
}

impl<'a, 'b> MacroExpander<'a, 'b> {
@@ -1081,45 +1095,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
}

fn new_span(cx: &ExtCtxt, sp: Span) -> Span {
debug!("new_span(sp={:?})", sp);

if cx.codemap().more_specific_trace(sp.expn_id, cx.backtrace()) {
// If the span we are looking at has a backtrace that has more
// detail than our current backtrace, then we keep that
// backtrace. Honestly, I have no idea if this makes sense,
// because I have no idea why we are stripping the backtrace
// below. But the reason I made this change is because, in
// deriving, we were generating attributes with a specific
// backtrace, which was essential for `#[structural_match]` to
// be properly supported, but these backtraces were being
// stripped and replaced with a null backtrace. Sort of
// unclear why this is the case. --nmatsakis
debug!("new_span: keeping trace from {:?} because it is more specific",
sp.expn_id);
sp
} else {
// This discards information in the case of macro-defining macros.
//
// The comment above was originally added in
// b7ec2488ff2f29681fe28691d20fd2c260a9e454 in Feb 2012. I
// *THINK* the reason we are doing this is because we want to
// replace the backtrace of the macro contents with the
// backtrace that contains the macro use. But it's pretty
// unclear to me. --nmatsakis
let sp1 = Span {
lo: sp.lo,
hi: sp.hi,
expn_id: cx.backtrace(),
};
debug!("new_span({:?}) = {:?}", sp, sp1);
if sp.expn_id.into_u32() == 0 && env::var_os("NDM").is_some() {
panic!("NDM");
}
sp1
}
}

pub struct ExpansionConfig<'feat> {
pub crate_name: String,
pub features: Option<&'feat Features>,
@@ -1206,8 +1181,9 @@ pub fn expand_crate(mut cx: ExtCtxt,
// the ones defined here include:
// Marker - add a mark to a context

// A Marker adds the given mark to the syntax context
struct Marker { mark: Mrk }
// A Marker adds the given mark to the syntax context and
// sets spans' `expn_id` to the given expn_id (unless it is `None`).
struct Marker { mark: Mrk, expn_id: Option<ExpnId> }

impl Folder for Marker {
fn fold_ident(&mut self, id: Ident) -> Ident {
@@ -1220,14 +1196,21 @@ impl Folder for Marker {
tts: self.fold_tts(&node.tts),
ctxt: mtwt::apply_mark(self.mark, node.ctxt),
},
span: span,
span: self.new_span(span),
}
}

fn new_span(&mut self, mut span: Span) -> Span {
if let Some(expn_id) = self.expn_id {
span.expn_id = expn_id;
}
span
}
}

// apply a given mark to the given token trees. Used prior to expansion of a macro.
fn mark_tts(tts: &[TokenTree], m: Mrk) -> Vec<TokenTree> {
noop_fold_tts(tts, &mut Marker{mark:m})
noop_fold_tts(tts, &mut Marker{mark:m, expn_id: None})
}

/// Check that there are no macro invocations left in the AST:
17 changes: 10 additions & 7 deletions src/libsyntax/test.rs
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ pub fn modify_for_testing(sess: &ParseSess,
if should_test {
generate_test_harness(sess, reexport_test_harness_main, krate, cfg, span_diagnostic)
} else {
strip_test_functions(span_diagnostic, krate)
strip_test_functions(krate)
}
}

@@ -312,14 +312,17 @@ fn generate_test_harness(sess: &ParseSess,
return res;
}

fn strip_test_functions(diagnostic: &errors::Handler, krate: ast::Crate)
-> ast::Crate {
fn strip_test_functions(krate: ast::Crate) -> ast::Crate {
// When not compiling with --test we should not compile the
// #[test] functions
config::strip_items(diagnostic, krate, |attrs| {
!attr::contains_name(&attrs[..], "test") &&
!attr::contains_name(&attrs[..], "bench")
})
struct StripTests;
impl config::CfgFolder for StripTests {
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
!attr::contains_name(attrs, "test") && !attr::contains_name(attrs, "bench")
}
}

StripTests.fold_crate(krate)
}

/// Craft a span that will be ignored by the stability lint's
8 changes: 7 additions & 1 deletion src/test/compile-fail/expanded-cfg.rs
Original file line number Diff line number Diff line change
@@ -8,15 +8,21 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(rustc_attrs)]
#![feature(custom_attribute, rustc_attrs)]

macro_rules! mac {
{} => {
#[cfg(attr)]
mod m {
#[lang_item]
fn f() {}

#[cfg_attr(target_thread_local, custom)]
fn g() {}
}

#[cfg(attr)]
unconfigured_invocation!();
}
}