From 223cb94bbfe2bacf97dc64e4b84ee29b8bb6017d Mon Sep 17 00:00:00 2001 From: Stuart Pernsteiner Date: Thu, 4 Apr 2024 13:13:51 -0700 Subject: [PATCH] analyze: discard annotations on DUMMY_SP --- c2rust-analyze/src/analyze.rs | 10 ++++++++-- c2rust-analyze/src/annotate.rs | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/c2rust-analyze/src/analyze.rs b/c2rust-analyze/src/analyze.rs index d52d04cef2..691ac5f075 100644 --- a/c2rust-analyze/src/analyze.rs +++ b/c2rust-analyze/src/analyze.rs @@ -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; @@ -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() { diff --git a/c2rust-analyze/src/annotate.rs b/c2rust-analyze/src/annotate.rs index 1c4b525862..aaaa9ec347 100644 --- a/c2rust-analyze/src/annotate.rs +++ b/c2rust-analyze/src/annotate.rs @@ -1,3 +1,4 @@ +use log::warn; use rustc_middle::ty::TyCtxt; use rustc_span::{FileName, Span}; use std::collections::HashMap; @@ -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();