Skip to content

Commit

Permalink
Fortify dummy span checking
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jun 29, 2018
1 parent 297109e commit 9f92fce
Show file tree
Hide file tree
Showing 21 changed files with 59 additions and 59 deletions.
4 changes: 1 addition & 3 deletions src/libproc_macro/lib.rs
Expand Up @@ -177,8 +177,6 @@ impl iter::FromIterator<TokenStream> for TokenStream {
#[unstable(feature = "proc_macro", issue = "38356")]
pub mod token_stream {
use syntax::tokenstream;
use syntax_pos::DUMMY_SP;

use {TokenTree, TokenStream, Delimiter};

/// An iterator over `TokenStream`'s `TokenTree`s.
Expand Down Expand Up @@ -207,7 +205,7 @@ pub mod token_stream {
// need to flattened during iteration over stream's token trees.
// Eventually this needs to be removed in favor of keeping original token trees
// and not doing the roundtrip through AST.
if tree.span().0 == DUMMY_SP {
if tree.span().0.is_dummy() {
if let TokenTree::Group(ref group) = tree {
if group.delimiter() == Delimiter::None {
self.cursor.insert(group.stream.clone().0);
Expand Down
11 changes: 3 additions & 8 deletions src/librustc/hir/map/definitions.rs
Expand Up @@ -486,12 +486,7 @@ impl Definitions {
#[inline]
pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
if def_id.krate == LOCAL_CRATE {
let span = self.def_index_to_span.get(&def_id.index).cloned().unwrap_or(DUMMY_SP);
if span != DUMMY_SP {
Some(span)
} else {
None
}
self.def_index_to_span.get(&def_id.index).cloned()
} else {
None
}
Expand Down Expand Up @@ -588,8 +583,8 @@ impl Definitions {
self.opaque_expansions_that_defined.insert(index, expansion);
}

// The span is added if it isn't DUMMY_SP
if span != DUMMY_SP {
// The span is added if it isn't dummy
if !span.is_dummy() {
self.def_index_to_span.insert(index, span);
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/stability.rs
Expand Up @@ -20,7 +20,7 @@ use ty::{self, TyCtxt};
use middle::privacy::AccessLevels;
use session::DiagnosticMessageId;
use syntax::symbol::Symbol;
use syntax_pos::{Span, MultiSpan, DUMMY_SP};
use syntax_pos::{Span, MultiSpan};
use syntax::ast;
use syntax::ast::{NodeId, Attribute};
use syntax::feature_gate::{GateIssue, emit_feature_err, find_lang_feature_accepted_version};
Expand Down Expand Up @@ -687,7 +687,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let msp: MultiSpan = span.into();
let cm = &self.sess.parse_sess.codemap();
let span_key = msp.primary_span().and_then(|sp: Span|
if sp != DUMMY_SP {
if !sp.is_dummy() {
let file = cm.lookup_char_pos(sp.lo()).file;
if file.name.is_macros() {
None
Expand Down Expand Up @@ -725,7 +725,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
match item.node {
hir::ItemExternCrate(_) => {
// compiler-generated `extern crate` items have a dummy span.
if item.span == DUMMY_SP { return }
if item.span.is_dummy() { return }

let def_id = self.tcx.hir.local_def_id(item.id);
let cnum = match self.tcx.extern_mod_stmt_cnum(def_id) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/query/plumbing.rs
Expand Up @@ -708,7 +708,7 @@ macro_rules! define_queries {

// FIXME(eddyb) Get more valid Span's on queries.
pub fn default_span(&self, tcx: TyCtxt<'_, $tcx, '_>, span: Span) -> Span {
if span != DUMMY_SP {
if !span.is_dummy() {
return span;
}
// The def_span query is used to calculate default_span,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/debuginfo/metadata.rs
Expand Up @@ -1662,7 +1662,7 @@ pub fn create_global_var_metadata(cx: &CodegenCx,
let var_scope = get_namespace_for_item(cx, def_id);
let span = tcx.def_span(def_id);

let (file_metadata, line_number) = if span != syntax_pos::DUMMY_SP {
let (file_metadata, line_number) = if !span.is_dummy() {
let loc = span_start(cx, span);
(file_metadata(cx, &loc.file.name, LOCAL_CRATE), loc.line as c_uint)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/debuginfo/mod.rs
Expand Up @@ -219,7 +219,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
let span = mir.span;

// This can be the case for functions inlined from another crate
if span == syntax_pos::DUMMY_SP {
if span.is_dummy() {
// FIXME(simulacrum): Probably can't happen; remove.
return FunctionDebugContext::FunctionWithoutDebugInfo;
}
Expand Down
16 changes: 8 additions & 8 deletions src/librustc_errors/emitter.rs
Expand Up @@ -10,7 +10,7 @@

use self::Destination::*;

use syntax_pos::{DUMMY_SP, FileMap, Span, MultiSpan};
use syntax_pos::{FileMap, Span, MultiSpan};

use {Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, CodeMapperDyn, DiagnosticId};
use snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, StyledString, Style};
Expand Down Expand Up @@ -216,7 +216,7 @@ impl EmitterWriter {

if let Some(ref cm) = self.cm {
for span_label in msp.span_labels() {
if span_label.span == DUMMY_SP {
if span_label.span.is_dummy() {
continue;
}

Expand Down Expand Up @@ -730,7 +730,7 @@ impl EmitterWriter {
let mut max = 0;
if let Some(ref cm) = self.cm {
for primary_span in msp.primary_spans() {
if primary_span != &DUMMY_SP {
if !primary_span.is_dummy() {
let hi = cm.lookup_char_pos(primary_span.hi());
if hi.line > max {
max = hi.line;
Expand All @@ -739,7 +739,7 @@ impl EmitterWriter {
}
if !self.short_message {
for span_label in msp.span_labels() {
if span_label.span != DUMMY_SP {
if !span_label.span.is_dummy() {
let hi = cm.lookup_char_pos(span_label.span.hi());
if hi.line > max {
max = hi.line;
Expand Down Expand Up @@ -778,7 +778,7 @@ impl EmitterWriter {

// First, find all the spans in <*macros> and point instead at their use site
for sp in span.primary_spans() {
if *sp == DUMMY_SP {
if sp.is_dummy() {
continue;
}
let call_sp = cm.call_span_if_macro(*sp);
Expand All @@ -790,7 +790,7 @@ impl EmitterWriter {
// Only show macro locations that are local
// and display them like a span_note
if let Some(def_site) = trace.def_site_span {
if def_site == DUMMY_SP {
if def_site.is_dummy() {
continue;
}
if always_backtrace {
Expand Down Expand Up @@ -830,7 +830,7 @@ impl EmitterWriter {
span.push_span_label(label_span, label_text);
}
for sp_label in span.span_labels() {
if sp_label.span == DUMMY_SP {
if sp_label.span.is_dummy() {
continue;
}
if cm.span_to_filename(sp_label.span.clone()).is_macros() &&
Expand Down Expand Up @@ -1003,7 +1003,7 @@ impl EmitterWriter {
// Make sure our primary file comes first
let (primary_lo, cm) = if let (Some(cm), Some(ref primary_span)) =
(self.cm.as_ref(), msp.primary_span().as_ref()) {
if primary_span != &&DUMMY_SP {
if !primary_span.is_dummy() {
(cm.lookup_char_pos(primary_span.lo()), cm)
} else {
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/encoder.rs
Expand Up @@ -41,7 +41,7 @@ use std::u32;
use syntax::ast::{self, CRATE_NODE_ID};
use syntax::attr;
use syntax::symbol::keywords;
use syntax_pos::{self, hygiene, FileName, FileMap, Span, DUMMY_SP};
use syntax_pos::{self, hygiene, FileName, FileMap, Span};

use rustc::hir::{self, PatKind};
use rustc::hir::itemlikevisit::ItemLikeVisitor;
Expand Down Expand Up @@ -147,7 +147,7 @@ impl<'a, 'tcx> SpecializedEncoder<DefIndex> for EncodeContext<'a, 'tcx> {

impl<'a, 'tcx> SpecializedEncoder<Span> for EncodeContext<'a, 'tcx> {
fn specialized_encode(&mut self, span: &Span) -> Result<(), Self::Error> {
if *span == DUMMY_SP {
if span.is_dummy() {
return TAG_INVALID_SPAN.encode(self)
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/nll/type_check/mod.rs
Expand Up @@ -190,7 +190,7 @@ struct TypeVerifier<'a, 'b: 'a, 'gcx: 'b + 'tcx, 'tcx: 'b> {

impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> {
fn visit_span(&mut self, span: &Span) {
if *span != DUMMY_SP {
if !span.is_dummy() {
self.last_span = *span;
}
}
Expand Down Expand Up @@ -1601,7 +1601,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
statement_index: 0,
};
for stmt in &block_data.statements {
if stmt.source_info.span != DUMMY_SP {
if !stmt.source_info.span.is_dummy() {
self.last_span = stmt.source_info.span;
}
self.check_stmt(mir, stmt, location);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/check_unused.rs
Expand Up @@ -86,7 +86,7 @@ impl<'a, 'b> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b> {
// because this means that they were generated in some fashion by the
// compiler and we don't need to consider them.
if let ast::ItemKind::Use(..) = item.node {
if item.vis.node == ast::VisibilityKind::Public || item.span.source_equal(&DUMMY_SP) {
if item.vis.node == ast::VisibilityKind::Public || item.span.is_dummy() {
return;
}
}
Expand Down Expand Up @@ -129,7 +129,7 @@ pub fn check_crate(resolver: &mut Resolver, krate: &ast::Crate) {
match directive.subclass {
_ if directive.used.get() ||
directive.vis.get() == ty::Visibility::Public ||
directive.span.source_equal(&DUMMY_SP) => {}
directive.span.is_dummy() => {}
ImportDirectiveSubclass::ExternCrate(_) => {
resolver.maybe_unused_extern_crates.push((directive.id, directive.span));
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_resolve/lib.rs
Expand Up @@ -2861,7 +2861,7 @@ impl<'a> Resolver<'a> {
.map(|suggestion| import_candidate_to_paths(&suggestion)).collect::<Vec<_>>();
enum_candidates.sort();
for (sp, variant_path, enum_path) in enum_candidates {
if sp == DUMMY_SP {
if sp.is_dummy() {
let msg = format!("there is an enum variant `{}`, \
try using `{}`?",
variant_path,
Expand Down Expand Up @@ -4285,7 +4285,7 @@ impl<'a> Resolver<'a> {
let mut err = struct_span_err!(self.session, span, E0659, "`{}` is ambiguous", name);
err.span_note(b1.span, &msg1);
match b2.def() {
Def::Macro(..) if b2.span == DUMMY_SP =>
Def::Macro(..) if b2.span.is_dummy() =>
err.note(&format!("`{}` is also a builtin macro", name)),
_ => err.span_note(b2.span, &msg2),
};
Expand Down Expand Up @@ -4398,14 +4398,14 @@ impl<'a> Resolver<'a> {
container));

err.span_label(span, format!("`{}` re{} here", name, new_participle));
if old_binding.span != DUMMY_SP {
if !old_binding.span.is_dummy() {
err.span_label(self.session.codemap().def_span(old_binding.span),
format!("previous {} of the {} `{}` here", old_noun, old_kind, name));
}

// See https://github.com/rust-lang/rust/issues/32354
if old_binding.is_import() || new_binding.is_import() {
let binding = if new_binding.is_import() && new_binding.span != DUMMY_SP {
let binding = if new_binding.is_import() && !new_binding.span.is_dummy() {
new_binding
} else {
old_binding
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/lib.rs
Expand Up @@ -1157,7 +1157,7 @@ fn escape(s: String) -> String {
// Helper function to determine if a span came from a
// macro expansion or syntax extension.
fn generated_code(span: Span) -> bool {
span.ctxt() != NO_EXPANSION || span == DUMMY_SP
span.ctxt() != NO_EXPANSION || span.is_dummy()
}

// DefId::index is a newtype and so the JSON serialisation is ugly. Therefore
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check_unused.rs
Expand Up @@ -12,7 +12,7 @@ use lint;
use rustc::ty::TyCtxt;

use syntax::ast;
use syntax_pos::{Span, DUMMY_SP};
use syntax_pos::Span;

use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::hir::itemlikevisit::ItemLikeVisitor;
Expand All @@ -39,7 +39,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {

impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CheckVisitor<'a, 'tcx> {
fn visit_item(&mut self, item: &hir::Item) {
if item.vis == hir::Public || item.span == DUMMY_SP {
if item.vis == hir::Public || item.span.is_dummy() {
return;
}
if let hir::ItemUse(ref path, _) = item.node {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Expand Up @@ -3464,7 +3464,7 @@ impl Span {

impl Clean<Span> for syntax_pos::Span {
fn clean(&self, cx: &DocContext) -> Span {
if *self == DUMMY_SP {
if self.is_dummy() {
return Span::empty();
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/codemap.rs
Expand Up @@ -443,7 +443,7 @@ impl CodeMap {
}

pub fn span_to_string(&self, sp: Span) -> String {
if self.files.borrow().file_maps.is_empty() && sp.source_equal(&DUMMY_SP) {
if self.files.borrow().file_maps.is_empty() && sp.is_dummy() {
return "no-location".to_string();
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/expand.rs
Expand Up @@ -1297,7 +1297,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
// Detect if this is an inline module (`mod m { ... }` as opposed to `mod m;`).
// In the non-inline case, `inner` is never the dummy span (c.f. `parse_item_mod`).
// Thus, if `inner` is the dummy span, we know the module is inline.
let inline_module = item.span.contains(inner) || inner == DUMMY_SP;
let inline_module = item.span.contains(inner) || inner.is_dummy();

if inline_module {
if let Some(path) = attr::first_attr_value_str_by_name(&item.attrs, "path") {
Expand Down
10 changes: 5 additions & 5 deletions src/libsyntax/ext/tt/quoted.rs
Expand Up @@ -14,7 +14,7 @@ use feature_gate::{self, emit_feature_err, Features, GateIssue};
use parse::{token, ParseSess};
use print::pprust;
use symbol::keywords;
use syntax_pos::{BytePos, Span, DUMMY_SP};
use syntax_pos::{BytePos, Span};
use tokenstream;

use std::iter::Peekable;
Expand All @@ -41,8 +41,8 @@ impl Delimited {

/// Return a `self::TokenTree` with a `Span` corresponding to the opening delimiter.
pub fn open_tt(&self, span: Span) -> TokenTree {
let open_span = if span == DUMMY_SP {
DUMMY_SP
let open_span = if span.is_dummy() {
span
} else {
span.with_lo(span.lo() + BytePos(self.delim.len() as u32))
};
Expand All @@ -51,8 +51,8 @@ impl Delimited {

/// Return a `self::TokenTree` with a `Span` corresponding to the closing delimiter.
pub fn close_tt(&self, span: Span) -> TokenTree {
let close_span = if span == DUMMY_SP {
DUMMY_SP
let close_span = if span.is_dummy() {
span
} else {
span.with_lo(span.hi() - BytePos(self.delim.len() as u32))
};
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/parse/mod.rs
Expand Up @@ -13,7 +13,7 @@
use rustc_data_structures::sync::{Lrc, Lock};
use ast::{self, CrateConfig};
use codemap::{CodeMap, FilePathMapping};
use syntax_pos::{self, Span, FileMap, NO_EXPANSION, FileName};
use syntax_pos::{Span, FileMap, FileName};
use errors::{Handler, ColorConfig, DiagnosticBuilder};
use feature_gate::UnstableFeatures;
use parse::parser::Parser;
Expand Down Expand Up @@ -188,8 +188,8 @@ fn filemap_to_parser(sess: & ParseSess, filemap: Lrc<FileMap>) -> Parser {
let end_pos = filemap.end_pos;
let mut parser = stream_to_parser(sess, filemap_to_stream(sess, filemap, None));

if parser.token == token::Eof && parser.span == syntax_pos::DUMMY_SP {
parser.span = Span::new(end_pos, end_pos, NO_EXPANSION);
if parser.token == token::Eof && parser.span.is_dummy() {
parser.span = Span::new(end_pos, end_pos, parser.span.ctxt());
}

parser
Expand Down

0 comments on commit 9f92fce

Please sign in to comment.