syntax: Introduce default/with_unstable constructors for ExpnInfo

This commit is contained in:
Vadim Petrochenkov 2019-06-17 22:18:56 +03:00
parent 68e1141c45
commit 8ec502eecd
9 changed files with 59 additions and 80 deletions

View File

@ -62,14 +62,14 @@ use syntax::errors;
use syntax::ext::hygiene::{Mark, SyntaxContext};
use syntax::print::pprust;
use syntax::ptr::P;
use syntax::source_map::{self, respan, CompilerDesugaringKind, Spanned};
use syntax::source_map::{self, respan, ExpnInfo, CompilerDesugaringKind, Spanned};
use syntax::source_map::CompilerDesugaringKind::IfTemporary;
use syntax::std_inject;
use syntax::symbol::{kw, sym, Symbol};
use syntax::tokenstream::{TokenStream, TokenTree};
use syntax::parse::token::{self, Token};
use syntax::visit::{self, Visitor};
use syntax_pos::{DUMMY_SP, edition, Span};
use syntax_pos::{DUMMY_SP, Span};
const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;
@ -853,14 +853,10 @@ impl<'a> LoweringContext<'a> {
allow_internal_unstable: Option<Lrc<[Symbol]>>,
) -> Span {
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(source_map::ExpnInfo {
call_site: span,
mark.set_expn_info(ExpnInfo {
def_site: Some(span),
format: source_map::CompilerDesugaring(reason),
allow_internal_unstable,
allow_internal_unsafe: false,
local_inner_macros: false,
edition: edition::Edition::from_session(),
..ExpnInfo::default(source_map::CompilerDesugaring(reason), span, self.sess.edition())
});
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
}

View File

@ -19,7 +19,7 @@ use syntax::{
mut_visit::{self, MutVisitor},
parse::ParseSess,
ptr::P,
symbol::{kw, sym, Symbol}
symbol::{kw, sym}
};
use syntax_pos::Span;
@ -58,11 +58,10 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
fn flat_map_item(&mut self, item: P<Item>) -> SmallVec<[P<Item>; 1]> {
debug!("in submodule {}", self.in_submod);
let name = if attr::contains_name(&item.attrs, sym::global_allocator) {
"global_allocator"
} else {
if !attr::contains_name(&item.attrs, sym::global_allocator) {
return mut_visit::noop_flat_map_item(item, self);
};
}
match item.node {
ItemKind::Static(..) => {}
_ => {
@ -87,15 +86,9 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
// Create a fresh Mark for the new macro expansion we are about to do
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(ExpnInfo {
call_site: item.span, // use the call site of the static
def_site: None,
format: MacroAttribute(Symbol::intern(name)),
allow_internal_unstable: Some([sym::rustc_attrs][..].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: self.sess.edition,
});
mark.set_expn_info(ExpnInfo::with_unstable(
MacroAttribute(sym::global_allocator), item.span, self.sess.edition, &[sym::rustc_attrs]
));
// Tie the span to the macro expansion info we just created
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));

View File

@ -60,15 +60,10 @@ pub fn add_derived_markers<T>(cx: &mut ExtCtxt<'_>, span: Span, traits: &[ast::P
}
pretty_name.push(')');
cx.current_expansion.mark.set_expn_info(ExpnInfo {
call_site: span,
def_site: None,
format: ExpnFormat::MacroAttribute(Symbol::intern(&pretty_name)),
allow_internal_unstable: Some([sym::rustc_attrs, sym::structural_match][..].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: cx.parse_sess.edition,
});
cx.current_expansion.mark.set_expn_info(ExpnInfo::with_unstable(
ExpnFormat::MacroAttribute(Symbol::intern(&pretty_name)), span, cx.parse_sess.edition,
&[sym::rustc_attrs, sym::structural_match],
));
let span = span.with_ctxt(cx.backtrace());
item.visit_attrs(|attrs| {

View File

@ -16,15 +16,9 @@ use syntax_pos::{DUMMY_SP, Span};
/// The expanded code uses the unstable `#[prelude_import]` attribute.
fn ignored_span(sp: Span, edition: Edition) -> Span {
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(ExpnInfo {
call_site: DUMMY_SP,
def_site: None,
format: MacroAttribute(Symbol::intern("std_inject")),
allow_internal_unstable: Some([sym::prelude_import][..].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition,
});
mark.set_expn_info(ExpnInfo::with_unstable(
MacroAttribute(Symbol::intern("std_inject")), sp, edition, &[sym::prelude_import]
));
sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
}

View File

@ -280,15 +280,10 @@ fn generate_test_harness(sess: &ParseSess,
test_runner
};
mark.set_expn_info(ExpnInfo {
call_site: DUMMY_SP,
def_site: None,
format: MacroAttribute(sym::test_case),
allow_internal_unstable: Some([sym::main, sym::test, sym::rustc_attrs][..].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: sess.edition,
});
mark.set_expn_info(ExpnInfo::with_unstable(
MacroAttribute(sym::test_case), DUMMY_SP, sess.edition,
&[sym::main, sym::test, sym::rustc_attrs],
));
TestHarnessGenerator {
cx,

View File

@ -347,17 +347,10 @@ fn mk_decls(
custom_macros: &[ProcMacroDef],
) -> P<ast::Item> {
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(ExpnInfo {
call_site: DUMMY_SP,
def_site: None,
format: MacroAttribute(sym::proc_macro),
allow_internal_unstable: Some([
sym::rustc_attrs, Symbol::intern("proc_macro_internals")
][..].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: cx.parse_sess.edition,
});
mark.set_expn_info(ExpnInfo::with_unstable(
MacroAttribute(sym::proc_macro), DUMMY_SP, cx.parse_sess.edition,
&[sym::rustc_attrs, Symbol::intern("proc_macro_internals")],
));
let span = DUMMY_SP.apply_mark(mark);
let hidden = cx.meta_list_item_word(span, sym::hidden);

View File

@ -8,7 +8,7 @@ use syntax::attr;
use syntax::ast;
use syntax::print::pprust;
use syntax::symbol::{Symbol, sym};
use syntax_pos::{DUMMY_SP, Span};
use syntax_pos::Span;
use syntax::source_map::{ExpnInfo, MacroAttribute};
use std::iter;
@ -62,15 +62,10 @@ pub fn expand_test_or_bench(
let (sp, attr_sp) = {
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(ExpnInfo {
call_site: DUMMY_SP,
def_site: None,
format: MacroAttribute(sym::test),
allow_internal_unstable: Some([sym::rustc_attrs, sym::test][..].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: cx.parse_sess.edition,
});
mark.set_expn_info(ExpnInfo::with_unstable(
MacroAttribute(sym::test), attr_sp, cx.parse_sess.edition,
&[sym::rustc_attrs, sym::test],
));
(item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark)),
attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(mark)))
};

View File

@ -15,7 +15,7 @@ use syntax::ext::hygiene::{Mark, SyntaxContext};
use syntax::ast;
use syntax::source_map::respan;
use syntax::symbol::sym;
use syntax_pos::{DUMMY_SP, Span};
use syntax_pos::Span;
use syntax::source_map::{ExpnInfo, MacroAttribute};
use syntax::feature_gate;
@ -37,15 +37,10 @@ pub fn expand(
let sp = {
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(ExpnInfo {
call_site: DUMMY_SP,
def_site: None,
format: MacroAttribute(sym::test_case),
allow_internal_unstable: Some([sym::test, sym::rustc_attrs][..].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: ecx.parse_sess.edition,
});
mark.set_expn_info(ExpnInfo::with_unstable(
MacroAttribute(sym::test_case), attr_sp, ecx.parse_sess.edition,
&[sym::test, sym::rustc_attrs],
));
attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
};

View File

@ -682,6 +682,29 @@ pub struct ExpnInfo {
pub edition: Edition,
}
impl ExpnInfo {
/// Constructs an expansion info with default properties.
pub fn default(format: ExpnFormat, call_site: Span, edition: Edition) -> ExpnInfo {
ExpnInfo {
call_site,
def_site: None,
format,
allow_internal_unstable: None,
allow_internal_unsafe: false,
local_inner_macros: false,
edition,
}
}
pub fn with_unstable(format: ExpnFormat, call_site: Span, edition: Edition,
allow_internal_unstable: &[Symbol]) -> ExpnInfo {
ExpnInfo {
allow_internal_unstable: Some(allow_internal_unstable.into()),
..ExpnInfo::default(format, call_site, edition)
}
}
}
/// The source of expansion.
#[derive(Clone, Hash, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
pub enum ExpnFormat {