Skip to content

Commit 8ec502e

Browse files
committed
syntax: Introduce default/with_unstable constructors for ExpnInfo
1 parent 68e1141 commit 8ec502e

File tree

9 files changed

+59
-80
lines changed

9 files changed

+59
-80
lines changed

src/librustc/hir/lowering.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ use syntax::errors;
6262
use syntax::ext::hygiene::{Mark, SyntaxContext};
6363
use syntax::print::pprust;
6464
use syntax::ptr::P;
65-
use syntax::source_map::{self, respan, CompilerDesugaringKind, Spanned};
65+
use syntax::source_map::{self, respan, ExpnInfo, CompilerDesugaringKind, Spanned};
6666
use syntax::source_map::CompilerDesugaringKind::IfTemporary;
6767
use syntax::std_inject;
6868
use syntax::symbol::{kw, sym, Symbol};
6969
use syntax::tokenstream::{TokenStream, TokenTree};
7070
use syntax::parse::token::{self, Token};
7171
use syntax::visit::{self, Visitor};
72-
use syntax_pos::{DUMMY_SP, edition, Span};
72+
use syntax_pos::{DUMMY_SP, Span};
7373

7474
const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;
7575

@@ -853,14 +853,10 @@ impl<'a> LoweringContext<'a> {
853853
allow_internal_unstable: Option<Lrc<[Symbol]>>,
854854
) -> Span {
855855
let mark = Mark::fresh(Mark::root());
856-
mark.set_expn_info(source_map::ExpnInfo {
857-
call_site: span,
856+
mark.set_expn_info(ExpnInfo {
858857
def_site: Some(span),
859-
format: source_map::CompilerDesugaring(reason),
860858
allow_internal_unstable,
861-
allow_internal_unsafe: false,
862-
local_inner_macros: false,
863-
edition: edition::Edition::from_session(),
859+
..ExpnInfo::default(source_map::CompilerDesugaring(reason), span, self.sess.edition())
864860
});
865861
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
866862
}

src/librustc_allocator/expand.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::{
1919
mut_visit::{self, MutVisitor},
2020
parse::ParseSess,
2121
ptr::P,
22-
symbol::{kw, sym, Symbol}
22+
symbol::{kw, sym}
2323
};
2424
use syntax_pos::Span;
2525

@@ -58,11 +58,10 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
5858
fn flat_map_item(&mut self, item: P<Item>) -> SmallVec<[P<Item>; 1]> {
5959
debug!("in submodule {}", self.in_submod);
6060

61-
let name = if attr::contains_name(&item.attrs, sym::global_allocator) {
62-
"global_allocator"
63-
} else {
61+
if !attr::contains_name(&item.attrs, sym::global_allocator) {
6462
return mut_visit::noop_flat_map_item(item, self);
65-
};
63+
}
64+
6665
match item.node {
6766
ItemKind::Static(..) => {}
6867
_ => {
@@ -87,15 +86,9 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
8786

8887
// Create a fresh Mark for the new macro expansion we are about to do
8988
let mark = Mark::fresh(Mark::root());
90-
mark.set_expn_info(ExpnInfo {
91-
call_site: item.span, // use the call site of the static
92-
def_site: None,
93-
format: MacroAttribute(Symbol::intern(name)),
94-
allow_internal_unstable: Some([sym::rustc_attrs][..].into()),
95-
allow_internal_unsafe: false,
96-
local_inner_macros: false,
97-
edition: self.sess.edition,
98-
});
89+
mark.set_expn_info(ExpnInfo::with_unstable(
90+
MacroAttribute(sym::global_allocator), item.span, self.sess.edition, &[sym::rustc_attrs]
91+
));
9992

10093
// Tie the span to the macro expansion info we just created
10194
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));

src/libsyntax/ext/derive.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,10 @@ pub fn add_derived_markers<T>(cx: &mut ExtCtxt<'_>, span: Span, traits: &[ast::P
6060
}
6161
pretty_name.push(')');
6262

63-
cx.current_expansion.mark.set_expn_info(ExpnInfo {
64-
call_site: span,
65-
def_site: None,
66-
format: ExpnFormat::MacroAttribute(Symbol::intern(&pretty_name)),
67-
allow_internal_unstable: Some([sym::rustc_attrs, sym::structural_match][..].into()),
68-
allow_internal_unsafe: false,
69-
local_inner_macros: false,
70-
edition: cx.parse_sess.edition,
71-
});
63+
cx.current_expansion.mark.set_expn_info(ExpnInfo::with_unstable(
64+
ExpnFormat::MacroAttribute(Symbol::intern(&pretty_name)), span, cx.parse_sess.edition,
65+
&[sym::rustc_attrs, sym::structural_match],
66+
));
7267

7368
let span = span.with_ctxt(cx.backtrace());
7469
item.visit_attrs(|attrs| {

src/libsyntax/std_inject.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@ use syntax_pos::{DUMMY_SP, Span};
1616
/// The expanded code uses the unstable `#[prelude_import]` attribute.
1717
fn ignored_span(sp: Span, edition: Edition) -> Span {
1818
let mark = Mark::fresh(Mark::root());
19-
mark.set_expn_info(ExpnInfo {
20-
call_site: DUMMY_SP,
21-
def_site: None,
22-
format: MacroAttribute(Symbol::intern("std_inject")),
23-
allow_internal_unstable: Some([sym::prelude_import][..].into()),
24-
allow_internal_unsafe: false,
25-
local_inner_macros: false,
26-
edition,
27-
});
19+
mark.set_expn_info(ExpnInfo::with_unstable(
20+
MacroAttribute(Symbol::intern("std_inject")), sp, edition, &[sym::prelude_import]
21+
));
2822
sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
2923
}
3024

src/libsyntax/test.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,10 @@ fn generate_test_harness(sess: &ParseSess,
280280
test_runner
281281
};
282282

283-
mark.set_expn_info(ExpnInfo {
284-
call_site: DUMMY_SP,
285-
def_site: None,
286-
format: MacroAttribute(sym::test_case),
287-
allow_internal_unstable: Some([sym::main, sym::test, sym::rustc_attrs][..].into()),
288-
allow_internal_unsafe: false,
289-
local_inner_macros: false,
290-
edition: sess.edition,
291-
});
283+
mark.set_expn_info(ExpnInfo::with_unstable(
284+
MacroAttribute(sym::test_case), DUMMY_SP, sess.edition,
285+
&[sym::main, sym::test, sym::rustc_attrs],
286+
));
292287

293288
TestHarnessGenerator {
294289
cx,

src/libsyntax_ext/proc_macro_decls.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -347,17 +347,10 @@ fn mk_decls(
347347
custom_macros: &[ProcMacroDef],
348348
) -> P<ast::Item> {
349349
let mark = Mark::fresh(Mark::root());
350-
mark.set_expn_info(ExpnInfo {
351-
call_site: DUMMY_SP,
352-
def_site: None,
353-
format: MacroAttribute(sym::proc_macro),
354-
allow_internal_unstable: Some([
355-
sym::rustc_attrs, Symbol::intern("proc_macro_internals")
356-
][..].into()),
357-
allow_internal_unsafe: false,
358-
local_inner_macros: false,
359-
edition: cx.parse_sess.edition,
360-
});
350+
mark.set_expn_info(ExpnInfo::with_unstable(
351+
MacroAttribute(sym::proc_macro), DUMMY_SP, cx.parse_sess.edition,
352+
&[sym::rustc_attrs, Symbol::intern("proc_macro_internals")],
353+
));
361354
let span = DUMMY_SP.apply_mark(mark);
362355

363356
let hidden = cx.meta_list_item_word(span, sym::hidden);

src/libsyntax_ext/test.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use syntax::attr;
88
use syntax::ast;
99
use syntax::print::pprust;
1010
use syntax::symbol::{Symbol, sym};
11-
use syntax_pos::{DUMMY_SP, Span};
11+
use syntax_pos::Span;
1212
use syntax::source_map::{ExpnInfo, MacroAttribute};
1313
use std::iter;
1414

@@ -62,15 +62,10 @@ pub fn expand_test_or_bench(
6262

6363
let (sp, attr_sp) = {
6464
let mark = Mark::fresh(Mark::root());
65-
mark.set_expn_info(ExpnInfo {
66-
call_site: DUMMY_SP,
67-
def_site: None,
68-
format: MacroAttribute(sym::test),
69-
allow_internal_unstable: Some([sym::rustc_attrs, sym::test][..].into()),
70-
allow_internal_unsafe: false,
71-
local_inner_macros: false,
72-
edition: cx.parse_sess.edition,
73-
});
65+
mark.set_expn_info(ExpnInfo::with_unstable(
66+
MacroAttribute(sym::test), attr_sp, cx.parse_sess.edition,
67+
&[sym::rustc_attrs, sym::test],
68+
));
7469
(item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark)),
7570
attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(mark)))
7671
};

src/libsyntax_ext/test_case.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use syntax::ext::hygiene::{Mark, SyntaxContext};
1515
use syntax::ast;
1616
use syntax::source_map::respan;
1717
use syntax::symbol::sym;
18-
use syntax_pos::{DUMMY_SP, Span};
18+
use syntax_pos::Span;
1919
use syntax::source_map::{ExpnInfo, MacroAttribute};
2020
use syntax::feature_gate;
2121

@@ -37,15 +37,10 @@ pub fn expand(
3737

3838
let sp = {
3939
let mark = Mark::fresh(Mark::root());
40-
mark.set_expn_info(ExpnInfo {
41-
call_site: DUMMY_SP,
42-
def_site: None,
43-
format: MacroAttribute(sym::test_case),
44-
allow_internal_unstable: Some([sym::test, sym::rustc_attrs][..].into()),
45-
allow_internal_unsafe: false,
46-
local_inner_macros: false,
47-
edition: ecx.parse_sess.edition,
48-
});
40+
mark.set_expn_info(ExpnInfo::with_unstable(
41+
MacroAttribute(sym::test_case), attr_sp, ecx.parse_sess.edition,
42+
&[sym::test, sym::rustc_attrs],
43+
));
4944
attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
5045
};
5146

src/libsyntax_pos/hygiene.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,29 @@ pub struct ExpnInfo {
682682
pub edition: Edition,
683683
}
684684

685+
impl ExpnInfo {
686+
/// Constructs an expansion info with default properties.
687+
pub fn default(format: ExpnFormat, call_site: Span, edition: Edition) -> ExpnInfo {
688+
ExpnInfo {
689+
call_site,
690+
def_site: None,
691+
format,
692+
allow_internal_unstable: None,
693+
allow_internal_unsafe: false,
694+
local_inner_macros: false,
695+
edition,
696+
}
697+
}
698+
699+
pub fn with_unstable(format: ExpnFormat, call_site: Span, edition: Edition,
700+
allow_internal_unstable: &[Symbol]) -> ExpnInfo {
701+
ExpnInfo {
702+
allow_internal_unstable: Some(allow_internal_unstable.into()),
703+
..ExpnInfo::default(format, call_site, edition)
704+
}
705+
}
706+
}
707+
685708
/// The source of expansion.
686709
#[derive(Clone, Hash, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
687710
pub enum ExpnFormat {

0 commit comments

Comments
 (0)