Skip to content

Rollup of 10 pull requests #82895

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

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6a679ff
bypass auto_da_alloc for metadata files
the8472 Feb 12, 2021
6e52b23
Fix jemalloc usage on OSX
sfackler Feb 28, 2021
920e2d8
Add natvis for Result, NonNull, CString, CStr, and Cow
rylev Feb 26, 2021
c8a0e8d
Refactor confirm_builtin_call, remove partial if
osa1 Mar 4, 2021
bc18eb4
expand: Remove obsolete `DirectoryOwnership::UnownedViaMod`
petrochenkov Feb 22, 2021
39052c5
expand: Move module file path stack from global session to expansion …
petrochenkov Feb 21, 2021
5bdf81d
expand: Determine module directory path directly instead of relying o…
petrochenkov Feb 22, 2021
3d0b622
expand: Less path cloning during module loading
petrochenkov Feb 22, 2021
46b67aa
expand: Some more consistent naming in module loading
petrochenkov Feb 22, 2021
da3419e
rustc_interface: Hide some hacky details of early linting from expand
petrochenkov Feb 22, 2021
29a9ef2
expand: Align some code with the PR fixing inner attributes on out-of…
petrochenkov Feb 22, 2021
1e1d574
expand: Share some code between inline and out-of-line module treatment
petrochenkov Feb 22, 2021
1fe2eb8
expand: Introduce enum for module loading errors and make module load…
petrochenkov Feb 22, 2021
1b4860a
Update compiler/rustc/src/main.rs
sfackler Mar 5, 2021
6f49aad
Disable destination propagation on all mir-opt-levels
tmiasko Mar 6, 2021
069e612
rustc_ast: Replace `AstLike::finalize_tokens` with a getter `tokens_mut`
petrochenkov Mar 6, 2021
5dad6c2
Implement built-in attribute macro `#[cfg_eval]`
petrochenkov Mar 6, 2021
cf52469
Remove Item::kind, use tagged enum. Rename variants to match
CraftSpider Feb 28, 2021
ca48d15
Add roundtrip testing and bump format version
CraftSpider Mar 4, 2021
83cece4
Move tests to own file
CraftSpider Mar 5, 2021
70c9b37
x.py fmt
CraftSpider Mar 5, 2021
18841ec
Revert fmt version, add rustdoc-json-types to bootstrap tests
CraftSpider Mar 6, 2021
f9019b7
Move full configuration logic from `rustc_expand` to `rustc_builtin_m…
petrochenkov Mar 6, 2021
10ed08f
cfg_eval: Configure everything through mutable visitor methods
petrochenkov Mar 6, 2021
5d27728
rustc_builtin_macros: Share some more logic between `derive` and `cfg…
petrochenkov Mar 6, 2021
6b2eb0e
Edit ructc_ast_lowering docs
pierwill Mar 7, 2021
ab8995b
Generalize Write impl for Vec<u8> to Vec<u8, A>
athre0z Mar 7, 2021
3bcbd70
Rollup merge of #82047 - the8472:fast-rename, r=davidtwco
Dylan-DPC Mar 8, 2021
20bff26
Rollup merge of #82415 - petrochenkov:modin3, r=davidtwco
Dylan-DPC Mar 8, 2021
4073aa9
Rollup merge of #82557 - rylev:natvis-improvements, r=varkor
Dylan-DPC Mar 8, 2021
24c5dd0
Rollup merge of #82613 - CraftSpider:fix-de, r=aDotInTheVoid
Dylan-DPC Mar 8, 2021
9564b97
Rollup merge of #82642 - sfackler:jemalloc-zone, r=pnkfelix
Dylan-DPC Mar 8, 2021
fb90dbf
Rollup merge of #82682 - petrochenkov:cfgeval, r=Aaron1011
Dylan-DPC Mar 8, 2021
be3a58b
Rollup merge of #82684 - tmiasko:dest-prop, r=jonas-schievink
Dylan-DPC Mar 8, 2021
cb41fc6
Rollup merge of #82755 - osa1:confirm_builtin_call_refactor, r=petroc…
Dylan-DPC Mar 8, 2021
e65090e
Rollup merge of #82857 - pierwill:edit-ast-lowering-lib, r=Dylan-DPC
Dylan-DPC Mar 8, 2021
2389278
Rollup merge of #82862 - athre0z:generalize-vec-write-impl, r=TimDiek…
Dylan-DPC Mar 8, 2021
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
Prev Previous commit
Next Next commit
Move full configuration logic from rustc_expand to `rustc_builtin_m…
…acros`

This logic is applicable to two specific macros and not to the expansion infrastructure in general.
  • Loading branch information
petrochenkov committed Mar 6, 2021
commit f9019b70865089e09507274075188f50b189a79d
176 changes: 172 additions & 4 deletions compiler/rustc_builtin_macros/src/cfg_eval.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
use crate::util::check_builtin_macro_attribute;

use rustc_ast::mut_visit::{self, MutVisitor};
use rustc_ast::ptr::P;
use rustc_ast::{self as ast, AstLike};
use rustc_data_structures::map_in_place::MapInPlace;
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_expand::config::StripUnconfigured;
use rustc_expand::configure;
use rustc_span::symbol::sym;
use rustc_span::Span;
use smallvec::SmallVec;

pub fn expand(
crate fn expand(
ecx: &mut ExtCtxt<'_>,
_span: Span,
meta_item: &ast::MetaItem,
item: Annotatable,
) -> Vec<Annotatable> {
check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);

let mut visitor =
StripUnconfigured { sess: ecx.sess, features: ecx.ecfg.features, modified: false };
let mut visitor = CfgEval {
cfg: StripUnconfigured { sess: ecx.sess, features: ecx.ecfg.features, modified: false },
};
let mut item = visitor.fully_configure(item);
if visitor.modified {
if visitor.cfg.modified {
// Erase the tokens if cfg-stripping modified the item
// This will cause us to synthesize fake tokens
// when `nt_to_tokenstream` is called on this item.
Expand All @@ -27,3 +33,165 @@ pub fn expand(
}
vec![item]
}

crate struct CfgEval<'a> {
pub cfg: StripUnconfigured<'a>,
}

impl CfgEval<'_> {
fn configure<T: AstLike>(&mut self, node: T) -> Option<T> {
self.cfg.configure(node)
}

fn configure_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
let ast::ForeignMod { unsafety: _, abi: _, items } = foreign_mod;
items.flat_map_in_place(|item| self.configure(item));
}

fn configure_variant_data(&mut self, vdata: &mut ast::VariantData) {
match vdata {
ast::VariantData::Struct(fields, ..) | ast::VariantData::Tuple(fields, _) => {
fields.flat_map_in_place(|field| self.configure(field))
}
ast::VariantData::Unit(_) => {}
}
}

fn configure_item_kind(&mut self, item: &mut ast::ItemKind) {
match item {
ast::ItemKind::Struct(def, _generics) | ast::ItemKind::Union(def, _generics) => {
self.configure_variant_data(def)
}
ast::ItemKind::Enum(ast::EnumDef { variants }, _generics) => {
variants.flat_map_in_place(|variant| self.configure(variant));
for variant in variants {
self.configure_variant_data(&mut variant.data);
}
}
_ => {}
}
}

fn configure_expr_kind(&mut self, expr_kind: &mut ast::ExprKind) {
match expr_kind {
ast::ExprKind::Match(_m, arms) => {
arms.flat_map_in_place(|arm| self.configure(arm));
}
ast::ExprKind::Struct(_path, fields, _base) => {
fields.flat_map_in_place(|field| self.configure(field));
}
_ => {}
}
}

fn configure_pat(&mut self, pat: &mut P<ast::Pat>) {
if let ast::PatKind::Struct(_path, fields, _etc) = &mut pat.kind {
fields.flat_map_in_place(|field| self.configure(field));
}
}

fn configure_fn_decl(&mut self, fn_decl: &mut ast::FnDecl) {
fn_decl.inputs.flat_map_in_place(|arg| self.configure(arg));
}

crate fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
// 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(self.flat_map_item(item).pop().unwrap()),
Annotatable::TraitItem(item) => {
Annotatable::TraitItem(self.flat_map_trait_item(item).pop().unwrap())
}
Annotatable::ImplItem(item) => {
Annotatable::ImplItem(self.flat_map_impl_item(item).pop().unwrap())
}
Annotatable::ForeignItem(item) => {
Annotatable::ForeignItem(self.flat_map_foreign_item(item).pop().unwrap())
}
Annotatable::Stmt(stmt) => {
Annotatable::Stmt(stmt.map(|stmt| self.flat_map_stmt(stmt).pop().unwrap()))
}
Annotatable::Expr(mut expr) => Annotatable::Expr({
self.visit_expr(&mut expr);
expr
}),
Annotatable::Arm(arm) => Annotatable::Arm(self.flat_map_arm(arm).pop().unwrap()),
Annotatable::Field(field) => {
Annotatable::Field(self.flat_map_field(field).pop().unwrap())
}
Annotatable::FieldPat(fp) => {
Annotatable::FieldPat(self.flat_map_field_pattern(fp).pop().unwrap())
}
Annotatable::GenericParam(param) => {
Annotatable::GenericParam(self.flat_map_generic_param(param).pop().unwrap())
}
Annotatable::Param(param) => {
Annotatable::Param(self.flat_map_param(param).pop().unwrap())
}
Annotatable::StructField(sf) => {
Annotatable::StructField(self.flat_map_struct_field(sf).pop().unwrap())
}
Annotatable::Variant(v) => {
Annotatable::Variant(self.flat_map_variant(v).pop().unwrap())
}
}
}
}

impl MutVisitor for CfgEval<'_> {
fn visit_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
self.configure_foreign_mod(foreign_mod);
mut_visit::noop_visit_foreign_mod(foreign_mod, self);
}

fn visit_item_kind(&mut self, item: &mut ast::ItemKind) {
self.configure_item_kind(item);
mut_visit::noop_visit_item_kind(item, self);
}

fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
self.cfg.configure_expr(expr);
self.configure_expr_kind(&mut expr.kind);
mut_visit::noop_visit_expr(expr, self);
}

fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
let mut expr = configure!(self, expr);
self.configure_expr_kind(&mut expr.kind);
mut_visit::noop_visit_expr(&mut expr, self);
Some(expr)
}

fn flat_map_generic_param(
&mut self,
param: ast::GenericParam,
) -> SmallVec<[ast::GenericParam; 1]> {
mut_visit::noop_flat_map_generic_param(configure!(self, param), self)
}

fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
mut_visit::noop_flat_map_stmt(configure!(self, stmt), self)
}

fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
mut_visit::noop_flat_map_item(configure!(self, item), self)
}

fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
}

fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
}

fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
self.configure_pat(pat);
mut_visit::noop_visit_pat(pat, self)
}

fn visit_fn_decl(&mut self, mut fn_decl: &mut P<ast::FnDecl>) {
self.configure_fn_decl(&mut fn_decl);
mut_visit::noop_visit_fn_decl(fn_decl, self);
}
}
9 changes: 6 additions & 3 deletions compiler/rustc_builtin_macros/src/derive.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::cfg_eval::CfgEval;

use rustc_ast::{self as ast, token, AstLike, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
use rustc_errors::{struct_span_err, Applicability};
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
Expand Down Expand Up @@ -52,10 +54,11 @@ impl MultiItemModifier for Expander {
// FIXME: Try to cache intermediate results to avoid collecting same paths multiple times.
match ecx.resolver.resolve_derives(ecx.current_expansion.id, derives, ecx.force_mode) {
Ok(()) => {
let mut visitor =
StripUnconfigured { sess, features: ecx.ecfg.features, modified: false };
let mut visitor = CfgEval {
cfg: StripUnconfigured { sess, features: ecx.ecfg.features, modified: false },
};
let mut item = visitor.fully_configure(item);
if visitor.modified {
if visitor.cfg.modified {
// Erase the tokens if cfg-stripping modified the item
// This will cause us to synthesize fake tokens
// when `nt_to_tokenstream` is called on this item.
Expand Down
Loading