Skip to content

Commit caea42f

Browse files
committed
Introduce and use SyntaxContext::outer_expn_info().
It reduces two `hygiene_data` accesses to one on some hot paths.
1 parent 828f6fd commit caea42f

File tree

15 files changed

+39
-30
lines changed

15 files changed

+39
-30
lines changed

src/librustc/lint/internal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
113113
.help("try using `Ty` instead")
114114
.emit();
115115
} else {
116-
if ty.span.ctxt().outer().expn_info().is_some() {
116+
if ty.span.ctxt().outer_expn_info().is_some() {
117117
return;
118118
}
119119
if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {

src/librustc/lint/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ pub fn provide(providers: &mut Providers<'_>) {
880880
/// This is used to test whether a lint should not even begin to figure out whether it should
881881
/// be reported on the current node.
882882
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
883-
let info = match span.ctxt().outer().expn_info() {
883+
let info = match span.ctxt().outer_expn_info() {
884884
Some(info) => info,
885885
// no ExpnInfo means this span doesn't come from a macro
886886
None => return false,
@@ -908,7 +908,7 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
908908

909909
/// Returns whether `span` originates in a derive macro's expansion
910910
pub fn in_derive_expansion(span: Span) -> bool {
911-
let info = match span.ctxt().outer().expn_info() {
911+
let info = match span.ctxt().outer_expn_info() {
912912
Some(info) => info,
913913
// no ExpnInfo means this span doesn't come from a macro
914914
None => return false,

src/librustc/traits/error_reporting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
6565
format: ExpnFormat::CompilerDesugaring(_),
6666
def_site: Some(def_span),
6767
..
68-
}) = span.ctxt().outer().expn_info() {
68+
}) = span.ctxt().outer_expn_info() {
6969
span = def_span;
7070
}
7171

src/librustc_codegen_ssa/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
131131
// at the level above that.
132132
let mut span = source_info.span;
133133
while span.ctxt() != NO_EXPANSION && span.ctxt() != self.mir.span.ctxt() {
134-
if let Some(info) = span.ctxt().outer().expn_info() {
134+
if let Some(info) = span.ctxt().outer_expn_info() {
135135
span = info.call_site;
136136
} else {
137137
break;

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
158158
if fieldpat.node.is_shorthand {
159159
continue;
160160
}
161-
if fieldpat.span.ctxt().outer().expn_info().is_some() {
161+
if fieldpat.span.ctxt().outer_expn_info().is_some() {
162162
// Don't lint if this is a macro expansion: macro authors
163163
// shouldn't have to worry about this kind of style issue
164164
// (Issue #49588)
@@ -1003,7 +1003,7 @@ impl UnreachablePub {
10031003
let mut applicability = Applicability::MachineApplicable;
10041004
match vis.node {
10051005
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(id) => {
1006-
if span.ctxt().outer().expn_info().is_some() {
1006+
if span.ctxt().outer_expn_info().is_some() {
10071007
applicability = Applicability::MaybeIncorrect;
10081008
}
10091009
let def_span = cx.tcx.sess.source_map().def_span(span);

src/librustc_lint/unused.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,8 @@ impl EarlyLintPass for UnusedParens {
391391
// trigger in situations that macro authors shouldn't have to care about, e.g.,
392392
// when a parenthesized token tree matched in one macro expansion is matched as
393393
// an expression in another and used as a fn/method argument (Issue #47775)
394-
if e.span.ctxt().outer().expn_info()
395-
.map_or(false, |info| info.call_site.ctxt().outer()
396-
.expn_info().is_some()) {
394+
if e.span.ctxt().outer_expn_info()
395+
.map_or(false, |info| info.call_site.ctxt().outer_expn_info().is_some()) {
397396
return;
398397
}
399398
let msg = format!("{} argument", call_kind);

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
762762
ItemKind::Use(..) => {
763763
// don't suggest placing a use before the prelude
764764
// import or other generated ones
765-
if item.span.ctxt().outer().expn_info().is_none() {
765+
if item.span.ctxt().outer_expn_info().is_none() {
766766
self.span = Some(item.span.shrink_to_lo());
767767
self.found_use = true;
768768
return;
@@ -772,7 +772,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
772772
ItemKind::ExternCrate(_) => {}
773773
// but place them before the first other item
774774
_ => if self.span.map_or(true, |span| item.span < span ) {
775-
if item.span.ctxt().outer().expn_info().is_none() {
775+
if item.span.ctxt().outer_expn_info().is_none() {
776776
// don't insert between attributes and an item
777777
if item.attrs.is_empty() {
778778
self.span = Some(item.span.shrink_to_lo());

src/librustc_resolve/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl<'a> Resolver<'a> {
413413

414414
// Possibly apply the macro helper hack
415415
if kind == MacroKind::Bang && path.len() == 1 &&
416-
path[0].ident.span.ctxt().outer().expn_info()
416+
path[0].ident.span.ctxt().outer_expn_info()
417417
.map_or(false, |info| info.local_inner_macros) {
418418
let root = Ident::new(kw::DollarCrate, path[0].ident.span);
419419
path.insert(0, Segment::from_ident(root));

src/librustc_typeck/check/demand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
327327

328328
// Check the `expn_info()` to see if this is a macro; if so, it's hard to
329329
// extract the text and make a good suggestion, so don't bother.
330-
let is_macro = sp.ctxt().outer().expn_info().is_some();
330+
let is_macro = sp.ctxt().outer_expn_info().is_some();
331331

332332
match (&expr.node, &expected.sty, &checked_ty.sty) {
333333
(_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.sty, &check.sty) {

src/librustc_typeck/check/method/suggest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ impl<'a, 'tcx, 'gcx> hir::intravisit::Visitor<'tcx> for UsePlacementFinder<'a, '
892892
hir::ItemKind::Use(..) => {
893893
// Don't suggest placing a `use` before the prelude
894894
// import or other generated ones.
895-
if item.span.ctxt().outer().expn_info().is_none() {
895+
if item.span.ctxt().outer_expn_info().is_none() {
896896
self.span = Some(item.span.shrink_to_lo());
897897
self.found_use = true;
898898
return;
@@ -902,7 +902,7 @@ impl<'a, 'tcx, 'gcx> hir::intravisit::Visitor<'tcx> for UsePlacementFinder<'a, '
902902
hir::ItemKind::ExternCrate(_) => {}
903903
// ...but do place them before the first other item.
904904
_ => if self.span.map_or(true, |span| item.span < span ) {
905-
if item.span.ctxt().outer().expn_info().is_none() {
905+
if item.span.ctxt().outer_expn_info().is_none() {
906906
// Don't insert between attributes and an item.
907907
if item.attrs.is_empty() {
908908
self.span = Some(item.span.shrink_to_lo());

src/libsyntax/ext/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ impl<'a> ExtCtxt<'a> {
872872
let mut ctxt = self.backtrace();
873873
let mut last_macro = None;
874874
loop {
875-
if ctxt.outer().expn_info().map_or(None, |info| {
875+
if ctxt.outer_expn_info().map_or(None, |info| {
876876
if info.format.name() == sym::include {
877877
// Stop going up the backtrace once include! is encountered
878878
return None;

src/libsyntax/source_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use errors::SourceMapper;
3030
/// otherwise return the call site span up to the `enclosing_sp` by
3131
/// following the `expn_info` chain.
3232
pub fn original_sp(sp: Span, enclosing_sp: Span) -> Span {
33-
let call_site1 = sp.ctxt().outer().expn_info().map(|ei| ei.call_site);
34-
let call_site2 = enclosing_sp.ctxt().outer().expn_info().map(|ei| ei.call_site);
33+
let call_site1 = sp.ctxt().outer_expn_info().map(|ei| ei.call_site);
34+
let call_site2 = enclosing_sp.ctxt().outer_expn_info().map(|ei| ei.call_site);
3535
match (call_site1, call_site2) {
3636
(None, _) => sp,
3737
(Some(call_site1), Some(call_site2)) if call_site1 == call_site2 => sp,

src/libsyntax_ext/proc_macro_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ impl server::Span for Rustc<'_> {
680680
self.sess.source_map().lookup_char_pos(span.lo()).file
681681
}
682682
fn parent(&mut self, span: Self::Span) -> Option<Self::Span> {
683-
span.ctxt().outer().expn_info().map(|i| i.call_site)
683+
span.ctxt().outer_expn_info().map(|i| i.call_site)
684684
}
685685
fn source(&mut self, span: Self::Span) -> Self::Span {
686686
span.source_callsite()

src/libsyntax_pos/hygiene.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,16 @@ impl SyntaxContext {
517517
HygieneData::with(|data| data.syntax_contexts[self.0 as usize].outer_mark)
518518
}
519519

520+
/// `ctxt.outer_expn_info()` is equivalent to but faster than
521+
/// `ctxt.outer().expn_info()`.
522+
#[inline]
523+
pub fn outer_expn_info(self) -> Option<ExpnInfo> {
524+
HygieneData::with(|data| {
525+
let outer = data.syntax_contexts[self.0 as usize].outer_mark;
526+
data.marks[outer.0 as usize].expn_info.clone()
527+
})
528+
}
529+
520530
pub fn dollar_crate_name(self) -> Symbol {
521531
HygieneData::with(|data| data.syntax_contexts[self.0 as usize].dollar_crate_name)
522532
}

src/libsyntax_pos/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,18 @@ impl Span {
348348
/// Returns the source span -- this is either the supplied span, or the span for
349349
/// the macro callsite that expanded to it.
350350
pub fn source_callsite(self) -> Span {
351-
self.ctxt().outer().expn_info().map(|info| info.call_site.source_callsite()).unwrap_or(self)
351+
self.ctxt().outer_expn_info().map(|info| info.call_site.source_callsite()).unwrap_or(self)
352352
}
353353

354354
/// The `Span` for the tokens in the previous macro expansion from which `self` was generated,
355355
/// if any.
356356
pub fn parent(self) -> Option<Span> {
357-
self.ctxt().outer().expn_info().map(|i| i.call_site)
357+
self.ctxt().outer_expn_info().map(|i| i.call_site)
358358
}
359359

360360
/// Edition of the crate from which this span came.
361361
pub fn edition(self) -> edition::Edition {
362-
self.ctxt().outer().expn_info().map_or_else(|| {
362+
self.ctxt().outer_expn_info().map_or_else(|| {
363363
Edition::from_session()
364364
}, |einfo| einfo.edition)
365365
}
@@ -381,19 +381,19 @@ impl Span {
381381
/// corresponding to the source callsite.
382382
pub fn source_callee(self) -> Option<ExpnInfo> {
383383
fn source_callee(info: ExpnInfo) -> ExpnInfo {
384-
match info.call_site.ctxt().outer().expn_info() {
384+
match info.call_site.ctxt().outer_expn_info() {
385385
Some(info) => source_callee(info),
386386
None => info,
387387
}
388388
}
389-
self.ctxt().outer().expn_info().map(source_callee)
389+
self.ctxt().outer_expn_info().map(source_callee)
390390
}
391391

392392
/// Checks if a span is "internal" to a macro in which `#[unstable]`
393393
/// items can be used (that is, a macro marked with
394394
/// `#[allow_internal_unstable]`).
395395
pub fn allows_unstable(&self, feature: Symbol) -> bool {
396-
match self.ctxt().outer().expn_info() {
396+
match self.ctxt().outer_expn_info() {
397397
Some(info) => info
398398
.allow_internal_unstable
399399
.map_or(false, |features| features.iter().any(|&f|
@@ -405,7 +405,7 @@ impl Span {
405405

406406
/// Checks if this span arises from a compiler desugaring of kind `kind`.
407407
pub fn is_compiler_desugaring(&self, kind: CompilerDesugaringKind) -> bool {
408-
match self.ctxt().outer().expn_info() {
408+
match self.ctxt().outer_expn_info() {
409409
Some(info) => match info.format {
410410
ExpnFormat::CompilerDesugaring(k) => k == kind,
411411
_ => false,
@@ -417,7 +417,7 @@ impl Span {
417417
/// Returns the compiler desugaring that created this span, or `None`
418418
/// if this span is not from a desugaring.
419419
pub fn compiler_desugaring_kind(&self) -> Option<CompilerDesugaringKind> {
420-
match self.ctxt().outer().expn_info() {
420+
match self.ctxt().outer_expn_info() {
421421
Some(info) => match info.format {
422422
ExpnFormat::CompilerDesugaring(k) => Some(k),
423423
_ => None
@@ -430,7 +430,7 @@ impl Span {
430430
/// can be used without triggering the `unsafe_code` lint
431431
// (that is, a macro marked with `#[allow_internal_unsafe]`).
432432
pub fn allows_unsafe(&self) -> bool {
433-
match self.ctxt().outer().expn_info() {
433+
match self.ctxt().outer_expn_info() {
434434
Some(info) => info.allow_internal_unsafe,
435435
None => false,
436436
}
@@ -439,7 +439,7 @@ impl Span {
439439
pub fn macro_backtrace(mut self) -> Vec<MacroBacktrace> {
440440
let mut prev_span = DUMMY_SP;
441441
let mut result = vec![];
442-
while let Some(info) = self.ctxt().outer().expn_info() {
442+
while let Some(info) = self.ctxt().outer_expn_info() {
443443
// Don't print recursive invocations.
444444
if !info.call_site.source_equal(&prev_span) {
445445
let (pre, post) = match info.format {

0 commit comments

Comments
 (0)