Skip to content

Commit dfcbe75

Browse files
committed
syntax_pos: Introduce a helper for checking whether a span comes from expansion
1 parent f7af19c commit dfcbe75

File tree

12 files changed

+25
-20
lines changed

12 files changed

+25
-20
lines changed

src/librustc/lint/internal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
108108
.help("try using `Ty` instead")
109109
.emit();
110110
} else {
111-
if ty.span.ctxt().outer_expn_info().is_some() {
111+
if ty.span.from_expansion() {
112112
return;
113113
}
114114
if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {

src/librustc_codegen_ssa/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::base;
88
use crate::debuginfo::{self, VariableAccess, VariableKind, FunctionDebugContext};
99
use crate::traits::*;
1010

11-
use syntax_pos::{DUMMY_SP, NO_EXPANSION, BytePos, Span};
11+
use syntax_pos::{DUMMY_SP, BytePos, Span};
1212
use syntax::symbol::kw;
1313

1414
use std::iter;
@@ -120,7 +120,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
120120
// In order to have a good line stepping behavior in debugger, we overwrite debug
121121
// locations of macro expansions with that of the outermost expansion site
122122
// (unless the crate is being compiled with `-Z debug-macros`).
123-
if source_info.span.ctxt() == NO_EXPANSION ||
123+
if !source_info.span.from_expansion() ||
124124
self.cx.sess().opts.debugging_opts.debug_macros {
125125
let scope = self.scope_metadata_for_loc(source_info.scope, source_info.span.lo());
126126
(scope, source_info.span)

src/librustc_lint/builtin.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use syntax::source_map::Spanned;
4242
use syntax::edition::Edition;
4343
use syntax::feature_gate::{self, AttributeGate, AttributeType};
4444
use syntax::feature_gate::{Stability, deprecated_attributes};
45-
use syntax_pos::{BytePos, Span, SyntaxContext};
45+
use syntax_pos::{BytePos, Span};
4646
use syntax::symbol::{Symbol, kw, sym};
4747
use syntax::errors::{Applicability, DiagnosticBuilder};
4848
use syntax::print::pprust::expr_to_string;
@@ -78,7 +78,7 @@ impl EarlyLintPass for WhileTrue {
7878
if let ast::ExprKind::While(cond, ..) = &e.node {
7979
if let ast::ExprKind::Lit(ref lit) = pierce_parens(cond).node {
8080
if let ast::LitKind::Bool(true) = lit.node {
81-
if lit.span.ctxt() == SyntaxContext::empty() {
81+
if !lit.span.from_expansion() {
8282
let msg = "denote infinite loops with `loop { ... }`";
8383
let condition_span = cx.sess.source_map().def_span(e.span);
8484
cx.struct_span_lint(WHILE_TRUE, condition_span, msg)
@@ -167,7 +167,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
167167
if fieldpat.is_shorthand {
168168
continue;
169169
}
170-
if fieldpat.span.ctxt().outer_expn_info().is_some() {
170+
if fieldpat.span.from_expansion() {
171171
// Don't lint if this is a macro expansion: macro authors
172172
// shouldn't have to worry about this kind of style issue
173173
// (Issue #49588)
@@ -1012,7 +1012,7 @@ impl UnreachablePub {
10121012
let mut applicability = Applicability::MachineApplicable;
10131013
match vis.node {
10141014
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(id) => {
1015-
if span.ctxt().outer_expn_info().is_some() {
1015+
if span.from_expansion() {
10161016
applicability = Applicability::MaybeIncorrect;
10171017
}
10181018
let def_span = cx.tcx.sess.source_map().def_span(span);

src/librustc_lint/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl EarlyLintPass for UnusedParens {
518518
// when a parenthesized token tree matched in one macro expansion is matched as
519519
// an expression in another and used as a fn/method argument (Issue #47775)
520520
if e.span.ctxt().outer_expn_info()
521-
.map_or(false, |info| info.call_site.ctxt().outer_expn_info().is_some()) {
521+
.map_or(false, |info| info.call_site.from_expansion()) {
522522
return;
523523
}
524524
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
@@ -274,7 +274,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
274274
ItemKind::Use(..) => {
275275
// don't suggest placing a use before the prelude
276276
// import or other generated ones
277-
if item.span.ctxt().outer_expn_info().is_none() {
277+
if !item.span.from_expansion() {
278278
self.span = Some(item.span.shrink_to_lo());
279279
self.found_use = true;
280280
return;
@@ -284,7 +284,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
284284
ItemKind::ExternCrate(_) => {}
285285
// but place them before the first other item
286286
_ => if self.span.map_or(true, |span| item.span < span ) {
287-
if item.span.ctxt().outer_expn_info().is_none() {
287+
if !item.span.from_expansion() {
288288
// don't insert between attributes and an item
289289
if item.attrs.is_empty() {
290290
self.span = Some(item.span.shrink_to_lo());

src/librustc_save_analysis/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ fn escape(s: String) -> String {
11561156
// Helper function to determine if a span came from a
11571157
// macro expansion or syntax extension.
11581158
fn generated_code(span: Span) -> bool {
1159-
span.ctxt() != NO_EXPANSION || span.is_dummy()
1159+
span.from_expansion() || span.is_dummy()
11601160
}
11611161

11621162
// DefId::index is a newtype and so the JSON serialisation is ugly. Therefore

src/librustc_typeck/check/demand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
347347
sp,
348348
);
349349

350-
// Check the `expn_info()` to see if this is a macro; if so, it's hard to
351-
// extract the text and make a good suggestion, so don't bother.
352-
let is_macro = sp.ctxt().outer_expn_info().is_some();
350+
// If the span is from a macro, then it's hard to extract the text
351+
// and make a good suggestion, so don't bother.
352+
let is_macro = sp.from_expansion();
353353

354354
match (&expr.node, &expected.sty, &checked_ty.sty) {
355355
(_, &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
@@ -985,7 +985,7 @@ impl hir::intravisit::Visitor<'tcx> for UsePlacementFinder<'tcx> {
985985
hir::ItemKind::Use(..) => {
986986
// Don't suggest placing a `use` before the prelude
987987
// import or other generated ones.
988-
if item.span.ctxt().outer_expn_info().is_none() {
988+
if !item.span.from_expansion() {
989989
self.span = Some(item.span.shrink_to_lo());
990990
self.found_use = true;
991991
return;
@@ -995,7 +995,7 @@ impl hir::intravisit::Visitor<'tcx> for UsePlacementFinder<'tcx> {
995995
hir::ItemKind::ExternCrate(_) => {}
996996
// ...but do place them before the first other item.
997997
_ => if self.span.map_or(true, |span| item.span < span ) {
998-
if item.span.ctxt().outer_expn_info().is_none() {
998+
if !item.span.from_expansion() {
999999
// Don't insert between attributes and an item.
10001000
if item.attrs.is_empty() {
10011001
self.span = Some(item.span.shrink_to_lo());

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ impl<'a> Parser<'a> {
11011101

11021102
crate fn process_potential_macro_variable(&mut self) {
11031103
self.token = match self.token.kind {
1104-
token::Dollar if self.token.span.ctxt() != SyntaxContext::empty() &&
1104+
token::Dollar if self.token.span.from_expansion() &&
11051105
self.look_ahead(1, |t| t.is_ident()) => {
11061106
self.bump();
11071107
let name = match self.token.kind {

src/libsyntax_pos/hygiene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl HygieneData {
255255
}
256256

257257
fn walk_chain(&self, mut span: Span, to: SyntaxContext) -> Span {
258-
while span.ctxt() != crate::NO_EXPANSION && span.ctxt() != to {
258+
while span.from_expansion() && span.ctxt() != to {
259259
if let Some(info) = self.expn_info(self.outer_expn(span.ctxt())) {
260260
span = info.call_site;
261261
} else {

src/libsyntax_pos/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ impl Span {
288288
span.lo.0 == 0 && span.hi.0 == 0
289289
}
290290

291+
/// Returns `true` if this span comes from a macro or desugaring.
292+
#[inline]
293+
pub fn from_expansion(self) -> bool {
294+
self.ctxt() != SyntaxContext::empty()
295+
}
296+
291297
/// Returns a new span representing an empty span at the beginning of this span
292298
#[inline]
293299
pub fn shrink_to_lo(self) -> Span {

src/libsyntax_pos/symbol.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::fmt;
1414
use std::hash::{Hash, Hasher};
1515
use std::str;
1616

17-
use crate::hygiene::SyntaxContext;
1817
use crate::{Span, DUMMY_SP, GLOBALS};
1918

2019
#[cfg(test)]
@@ -851,7 +850,7 @@ impl fmt::Display for Ident {
851850

852851
impl Encodable for Ident {
853852
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
854-
if self.span.ctxt().modern() == SyntaxContext::empty() {
853+
if !self.span.modern().from_expansion() {
855854
s.emit_str(&self.as_str())
856855
} else { // FIXME(jseyfried): intercrate hygiene
857856
let mut string = "#".to_owned();

0 commit comments

Comments
 (0)