Skip to content

Commit

Permalink
analyze: discard annotations on DUMMY_SP
Browse files Browse the repository at this point in the history
  • Loading branch information
spernsteiner committed Apr 4, 2024
1 parent bac8baf commit 223cb94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions c2rust-analyze/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use rustc_middle::ty::Ty;
use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::TyKind;
use rustc_middle::ty::WithOptConstParam;
use rustc_span::{Span, Symbol, DUMMY_SP};
use rustc_span::{Span, Symbol};
use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
Expand Down Expand Up @@ -1544,7 +1544,13 @@ fn run(tcx: TyCtxt) {
}

// Emit annotations for fields
let span = tcx.def_ident_span(did).unwrap_or(DUMMY_SP);
let span = match tcx.def_ident_span(did) {
Some(x) => x,
None => {
warn!("field {:?} has no def_ident_span to annotate", did);
continue;
}
};
let mut ptrs = Vec::new();
let ty_str = context::print_ty_with_pointer_labels(field_lty, |ptr| {
if ptr.is_none() {
Expand Down
11 changes: 11 additions & 0 deletions c2rust-analyze/src/annotate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::warn;
use rustc_middle::ty::TyCtxt;
use rustc_span::{FileName, Span};
use std::collections::HashMap;
Expand All @@ -22,6 +23,16 @@ impl<'tcx> AnnotationBuffer<'tcx> {
}

pub fn emit(&mut self, span: Span, msg: impl Display) {
if span.is_dummy() {
// `DUMMY_SP` covers the range `BytePos(0) .. BytePos(0)`. Whichever file happens to
// be added to the `SourceMap` first will be assigned a range starting at `BytePos(0)`,
// so the `SourceFile` lookup below would attach the annotation to that file. Rather
// than letting the annotation be attached to an arbitrary file, we warn and discard
// it.
warn!("discarding annotation on DUMMY_SP: {}", msg);
return;
}

let sm = self.tcx.sess.source_map();

let span = span.source_callsite();
Expand Down

0 comments on commit 223cb94

Please sign in to comment.