Skip to content

Commit c14cc53

Browse files
authored
Merge pull request #1534 from mpenciak/macro-span-fix
fix: Point `generated_from_span` at the generating macro
2 parents 090bc99 + 08d7c9d commit c14cc53

35 files changed

Lines changed: 1871 additions & 1177 deletions

‎charon/src/ast/meta/spans.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ pub struct SpanData {
8888
// lines, or point into a very large file or a very long line -- are stored in `WIDE_SPANS` and
8989
// referred to by index.
9090
// Some numbers to back this up:
91-
// - For serde 1.0.228, out of 246K spans, 12 didn't fit
92-
// - For regex 1.11.1, out of 136K spans, 25 didn't fit
93-
// - For syn 2.0.104, out of 800K spans, 36 didn't fit
91+
// - For serde 1.0.228, out of 453K spans, 1398 didn't fit
92+
// - For regex 1.11.1, out of 213K spans, 623 didn't fit
93+
// - For syn 2.0.104, out of 8.5M spans, 3094 didn't fit
9494
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
9595
pub struct Span(u64);
9696

@@ -148,7 +148,7 @@ pub struct SerializedSpan {
148148
/// refer to them by index. Entries are deduplicated so equal spans have equal representations.
149149
///
150150
/// This table is global and never shrinks. In practice this is fine; for instance, syn 2.0.104
151-
/// only had distinct 36 spans here.
151+
/// only had 3094 distinct spans here compared to a total of 8.5M spans.
152152
static WIDE_SPANS: LazyLock<Mutex<WideSpans>> = LazyLock::new(Default::default);
153153

154154
#[derive(Default)]

‎charon/src/bin/charon-driver/translate/translate_meta.rs‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ impl<'tcx> TranslateCtx<'tcx> {
177177
source_scopes: &rustc_index::IndexVec<mir::SourceScope, mir::SourceScopeData>,
178178
source_info: &mir::SourceInfo,
179179
) -> Span {
180-
// Translate the span
181-
let data = self.translate_span_data(source_info.span);
182-
183180
// Lookup the top-most inlined parent scope.
184181
let mut parent_span = None;
185182
let mut scope_data = &source_scopes[source_info.scope];
@@ -189,15 +186,34 @@ impl<'tcx> TranslateCtx<'tcx> {
189186
}
190187

191188
if let Some(parent_span) = parent_span {
189+
let data = self.translate_span_data(source_info.span);
192190
let parent_span = self.translate_span_data(parent_span);
193191
Span::new(parent_span, Some(data))
194192
} else {
195-
Span::new(data, None)
193+
self.translate_span(&source_info.span)
196194
}
197195
}
198196

199197
pub(crate) fn translate_span(&mut self, span: &rustc_span::Span) -> Span {
200-
Span::new(self.translate_span_data(*span), None)
198+
// If the span comes from a macro expansion, point to the outermost macro call and record
199+
// the span inside the macro as `generated_from_span`. We skip compiler desugarings
200+
// `ExpnKind::Desugaring` since their spans already point to user code.
201+
let macro_call = span
202+
.macro_backtrace()
203+
.filter(|expn| matches!(expn.kind, rustc_span::ExpnKind::Macro(..)))
204+
.last();
205+
let data = self.translate_span_data(*span);
206+
match macro_call {
207+
Some(expn) => {
208+
// Code that already points to the macro call doesn't need a `generated_from_span`.
209+
if expn.call_site.source_equal(*span) {
210+
Span::new(data, None)
211+
} else {
212+
Span::new(self.translate_span_data(expn.call_site), Some(data))
213+
}
214+
}
215+
None => Span::new(data, None),
216+
}
201217
}
202218

203219
pub(crate) fn def_span(&mut self, def_id: &hax::DefId) -> Span {

‎charon/tests/crate_data.rs‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,82 @@ fn spans() -> anyhow::Result<()> {
283283
Ok(())
284284
}
285285

286+
#[test]
287+
fn macro_spans() -> anyhow::Result<()> {
288+
let crate_data = translate_rust_file("tests/ui/macro-spans.rs")?;
289+
let repr = |span: Span| {
290+
let repr_data = |data: SpanData| {
291+
let file_name = crate_data.files[data.file_id].name.to_string();
292+
let file_name = file_name.rsplit('/').next().unwrap().to_owned();
293+
format!("{file_name}:{}-{}", data.beg, data.end)
294+
};
295+
match span.generated_from_span() {
296+
Some(generated_from) => {
297+
format!(
298+
"{} <- {}",
299+
repr_data(span.data()),
300+
repr_data(generated_from)
301+
)
302+
}
303+
None => repr_data(span.data()),
304+
}
305+
};
306+
307+
let mut lines = vec![];
308+
for item in crate_data
309+
.all_items()
310+
.filter(|item| item.item_meta().is_local)
311+
{
312+
let name = item.item_meta().name.debug_repr(&crate_data);
313+
lines.push(format!("{name}: {}", repr(item.item_meta().span)));
314+
let ItemRef::Fun(fun) = item else { continue };
315+
let mut stmt_spans = vec![];
316+
let body = fun.body.as_structured().unwrap();
317+
body.body
318+
.dyn_visit_in_body(|st: &Statement| stmt_spans.push(st.span));
319+
match name.as_str() {
320+
// Show the statements of a function that uses a macro in its body.
321+
"test_crate::macro_expr" => {
322+
for span in stmt_spans.into_iter().map(repr).dedup() {
323+
lines.push(format!(" {span}"));
324+
}
325+
}
326+
// Desugarings (`for`, `?`) aren't macros: their spans already point to user code.
327+
"test_crate::desugarings" => {
328+
assert!(
329+
stmt_spans
330+
.iter()
331+
.all(|span| span.generated_from_span().is_none())
332+
)
333+
}
334+
_ => {}
335+
}
336+
}
337+
let expected = indoc::indoc!(
338+
"
339+
test_crate::Derived: macro-spans.rs:30:0-30:24
340+
test_crate::<impl Clone for ??>: macro-spans.rs:29:9-29:14
341+
test_crate::local: macro-spans.rs:11:0-11:15 <- macro-spans.rs:6:8-8:9
342+
test_crate::nested: macro-spans.rs:18:0-18:14 <- macro-spans.rs:6:8-8:9
343+
test_crate::from_aux: macro-spans.rs:20:0-20:39 <- macro-spans-aux.rs:5:8-7:9
344+
test_crate::included: macro-spans-included.rs:3:0-5:1
345+
test_crate::from_included_local: macro-spans-included.rs:14:0-14:38 <- macro-spans-included.rs:9:8-11:9
346+
test_crate::from_included: macro-spans.rs:23:0-23:32 <- macro-spans-included.rs:9:8-11:9
347+
test_crate::from_pathmod: macro-spans.rs:27:0-27:39 <- macro-spans-pathmod.rs:9:8-11:9
348+
test_crate::macro_expr: macro-spans.rs:37:0-39:1
349+
macro-spans.rs:38:12-38:13
350+
macro-spans.rs:38:4-38:14 <- macro-spans.rs:34:8-34:12
351+
macro-spans.rs:38:4-38:14 <- macro-spans.rs:34:11-34:12
352+
macro-spans.rs:39:1-39:1
353+
test_crate::desugarings: macro-spans.rs:41:0-47:1
354+
test_crate::pathmod::in_pathmod: macro-spans-pathmod.rs:3:0-5:1
355+
test_crate::pathmod::from_pathmod_local: macro-spans-pathmod.rs:15:0-15:36 <- macro-spans-pathmod.rs:9:8-11:9
356+
test_crate::<impl Clone for ??>::clone: macro-spans.rs:29:9-29:14"
357+
);
358+
assert_eq!(lines.join("\n"), expected.trim_start());
359+
Ok(())
360+
}
361+
286362
#[test]
287363
fn predicate_origins() -> anyhow::Result<()> {
288364
use PredicateOrigin::*;

‎charon/tests/ui/control-flow/issue-1051-missing-loop-jump.out‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ pub trait Step<Self>
121121
pub fn backward_overflowing(_1: i32, _2: usize) -> (i32, bool)
122122
= <opaque>
123123

124-
// Full name: core::iter::range::{impl Step for i32}::forward_overflowing
125-
pub fn forward_overflowing(_1: i32, _2: usize) -> (i32, bool)
126-
= <opaque>
127-
128124
// Full name: core::iter::range::{impl Step for i32}::backward_checked
129125
pub fn backward_checked(_1: i32, _2: usize) -> Option<i32>[{built_in impl Sized for i32}]
130126
= <opaque>
131127

128+
// Full name: core::iter::range::{impl Step for i32}::forward_overflowing
129+
pub fn forward_overflowing(_1: i32, _2: usize) -> (i32, bool)
130+
= <opaque>
131+
132132
// Full name: core::iter::range::{impl Step for i32}::forward_checked
133133
pub fn forward_checked(_1: i32, _2: usize) -> Option<i32>[{built_in impl Sized for i32}]
134134
= <opaque>

‎charon/tests/ui/control-flow/loops.out‎

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,51 @@ pub trait Step<Self>
205205
non-dyn-compatible
206206
}
207207

208-
// Full name: core::iter::range::{impl Step for usize}::backward_overflowing
209-
pub fn impl_Step_for_usize::backward_overflowing(_1: usize, _2: usize) -> (usize, bool)
208+
// Full name: core::iter::range::{impl Step for i32}::backward_overflowing
209+
pub fn impl_Step_for_i32::backward_overflowing(_1: i32, _2: usize) -> (i32, bool)
210210
= <opaque>
211211

212-
// Full name: core::iter::range::{impl Step for usize}::forward_overflowing
213-
pub fn impl_Step_for_usize::forward_overflowing(_1: usize, _2: usize) -> (usize, bool)
212+
// Full name: core::iter::range::{impl Step for i32}::backward_checked
213+
pub fn impl_Step_for_i32::backward_checked(_1: i32, _2: usize) -> Option<i32>[{built_in impl Sized for i32}]
214+
= <opaque>
215+
216+
// Full name: core::iter::range::{impl Step for i32}::forward_overflowing
217+
pub fn impl_Step_for_i32::forward_overflowing(_1: i32, _2: usize) -> (i32, bool)
218+
= <opaque>
219+
220+
// Full name: core::iter::range::{impl Step for i32}::forward_checked
221+
pub fn impl_Step_for_i32::forward_checked(_1: i32, _2: usize) -> Option<i32>[{built_in impl Sized for i32}]
222+
= <opaque>
223+
224+
// Full name: core::iter::range::{impl Step for i32}::steps_between
225+
pub fn impl_Step_for_i32::steps_between<'_0, '_1>(_1: &'_0 i32, _2: &'_1 i32) -> (usize, Option<usize>[{built_in impl Sized for usize}])
226+
= <opaque>
227+
228+
// Full name: core::iter::range::{impl Step for i32}
229+
impl "impl_Step_for_i32" Step for i32 {
230+
proof ImpliedClause0: (i32: Sized) = {built_in impl Sized for i32}
231+
proof ImpliedClause1: (i32: Clone) = impl_Clone_for_i32
232+
proof ImpliedClause2: (i32: PartialOrd<i32>) = impl_PartialOrd_i32_for_i32
233+
fn steps_between<'_0_1, '_1_1> = impl_Step_for_i32::steps_between<'_0_1, '_1_1>
234+
fn forward_checked = impl_Step_for_i32::forward_checked
235+
fn forward_overflowing = impl_Step_for_i32::forward_overflowing
236+
fn backward_checked = impl_Step_for_i32::backward_checked
237+
fn backward_overflowing = impl_Step_for_i32::backward_overflowing
238+
non-dyn-compatible
239+
}
240+
241+
// Full name: core::iter::range::{impl Step for usize}::backward_overflowing
242+
pub fn impl_Step_for_usize::backward_overflowing(_1: usize, _2: usize) -> (usize, bool)
214243
= <opaque>
215244

216245
// Full name: core::iter::range::{impl Step for usize}::backward_checked
217246
pub fn impl_Step_for_usize::backward_checked(_1: usize, _2: usize) -> Option<usize>[{built_in impl Sized for usize}]
218247
= <opaque>
219248

249+
// Full name: core::iter::range::{impl Step for usize}::forward_overflowing
250+
pub fn impl_Step_for_usize::forward_overflowing(_1: usize, _2: usize) -> (usize, bool)
251+
= <opaque>
252+
220253
// Full name: core::iter::range::{impl Step for usize}::forward_checked
221254
pub fn impl_Step_for_usize::forward_checked(_1: usize, _2: usize) -> Option<usize>[{built_in impl Sized for usize}]
222255
= <opaque>
@@ -242,14 +275,14 @@ impl "impl_Step_for_usize" Step for usize {
242275
pub fn impl_Step_for_u32::backward_overflowing(_1: u32, _2: usize) -> (u32, bool)
243276
= <opaque>
244277

245-
// Full name: core::iter::range::{impl Step for u32}::forward_overflowing
246-
pub fn impl_Step_for_u32::forward_overflowing(_1: u32, _2: usize) -> (u32, bool)
247-
= <opaque>
248-
249278
// Full name: core::iter::range::{impl Step for u32}::backward_checked
250279
pub fn impl_Step_for_u32::backward_checked(_1: u32, _2: usize) -> Option<u32>[{built_in impl Sized for u32}]
251280
= <opaque>
252281

282+
// Full name: core::iter::range::{impl Step for u32}::forward_overflowing
283+
pub fn impl_Step_for_u32::forward_overflowing(_1: u32, _2: usize) -> (u32, bool)
284+
= <opaque>
285+
253286
// Full name: core::iter::range::{impl Step for u32}::forward_checked
254287
pub fn impl_Step_for_u32::forward_checked(_1: u32, _2: usize) -> Option<u32>[{built_in impl Sized for u32}]
255288
= <opaque>
@@ -271,39 +304,6 @@ impl "impl_Step_for_u32" Step for u32 {
271304
non-dyn-compatible
272305
}
273306

274-
// Full name: core::iter::range::{impl Step for i32}::backward_overflowing
275-
pub fn impl_Step_for_i32::backward_overflowing(_1: i32, _2: usize) -> (i32, bool)
276-
= <opaque>
277-
278-
// Full name: core::iter::range::{impl Step for i32}::forward_overflowing
279-
pub fn impl_Step_for_i32::forward_overflowing(_1: i32, _2: usize) -> (i32, bool)
280-
= <opaque>
281-
282-
// Full name: core::iter::range::{impl Step for i32}::backward_checked
283-
pub fn impl_Step_for_i32::backward_checked(_1: i32, _2: usize) -> Option<i32>[{built_in impl Sized for i32}]
284-
= <opaque>
285-
286-
// Full name: core::iter::range::{impl Step for i32}::forward_checked
287-
pub fn impl_Step_for_i32::forward_checked(_1: i32, _2: usize) -> Option<i32>[{built_in impl Sized for i32}]
288-
= <opaque>
289-
290-
// Full name: core::iter::range::{impl Step for i32}::steps_between
291-
pub fn impl_Step_for_i32::steps_between<'_0, '_1>(_1: &'_0 i32, _2: &'_1 i32) -> (usize, Option<usize>[{built_in impl Sized for usize}])
292-
= <opaque>
293-
294-
// Full name: core::iter::range::{impl Step for i32}
295-
impl "impl_Step_for_i32" Step for i32 {
296-
proof ImpliedClause0: (i32: Sized) = {built_in impl Sized for i32}
297-
proof ImpliedClause1: (i32: Clone) = impl_Clone_for_i32
298-
proof ImpliedClause2: (i32: PartialOrd<i32>) = impl_PartialOrd_i32_for_i32
299-
fn steps_between<'_0_1, '_1_1> = impl_Step_for_i32::steps_between<'_0_1, '_1_1>
300-
fn forward_checked = impl_Step_for_i32::forward_checked
301-
fn forward_overflowing = impl_Step_for_i32::forward_overflowing
302-
fn backward_checked = impl_Step_for_i32::backward_checked
303-
fn backward_overflowing = impl_Step_for_i32::backward_overflowing
304-
non-dyn-compatible
305-
}
306-
307307
// Full name: core::ops::range::Range
308308
#[lang_item(Range)]
309309
pub struct Range<Idx>

‎charon/tests/ui/control-flow/ullbc-control-flow.out‎

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -149,51 +149,18 @@ pub trait Step<Self>
149149
non-dyn-compatible
150150
}
151151

152-
// Full name: core::iter::range::{impl Step for usize}::backward_overflowing
153-
pub fn impl_Step_for_usize::backward_overflowing(_1: usize, _2: usize) -> (usize, bool)
154-
= <opaque>
155-
156-
// Full name: core::iter::range::{impl Step for usize}::forward_overflowing
157-
pub fn impl_Step_for_usize::forward_overflowing(_1: usize, _2: usize) -> (usize, bool)
158-
= <opaque>
159-
160-
// Full name: core::iter::range::{impl Step for usize}::backward_checked
161-
pub fn impl_Step_for_usize::backward_checked(_1: usize, _2: usize) -> Option<usize>[{built_in impl Sized for usize}]
162-
= <opaque>
163-
164-
// Full name: core::iter::range::{impl Step for usize}::forward_checked
165-
pub fn impl_Step_for_usize::forward_checked(_1: usize, _2: usize) -> Option<usize>[{built_in impl Sized for usize}]
166-
= <opaque>
167-
168-
// Full name: core::iter::range::{impl Step for usize}::steps_between
169-
pub fn impl_Step_for_usize::steps_between<'_0, '_1>(_1: &'_0 usize, _2: &'_1 usize) -> (usize, Option<usize>[{built_in impl Sized for usize}])
170-
= <opaque>
171-
172-
// Full name: core::iter::range::{impl Step for usize}
173-
impl "impl_Step_for_usize" Step for usize {
174-
proof ImpliedClause0: (usize: Sized) = {built_in impl Sized for usize}
175-
proof ImpliedClause1: (usize: Clone) = impl_Clone_for_usize
176-
proof ImpliedClause2: (usize: PartialOrd<usize>) = impl_PartialOrd_usize_for_usize
177-
fn steps_between<'_0_1, '_1_1> = impl_Step_for_usize::steps_between<'_0_1, '_1_1>
178-
fn forward_checked = impl_Step_for_usize::forward_checked
179-
fn forward_overflowing = impl_Step_for_usize::forward_overflowing
180-
fn backward_checked = impl_Step_for_usize::backward_checked
181-
fn backward_overflowing = impl_Step_for_usize::backward_overflowing
182-
non-dyn-compatible
183-
}
184-
185152
// Full name: core::iter::range::{impl Step for i32}::backward_overflowing
186153
pub fn impl_Step_for_i32::backward_overflowing(_1: i32, _2: usize) -> (i32, bool)
187154
= <opaque>
188155

189-
// Full name: core::iter::range::{impl Step for i32}::forward_overflowing
190-
pub fn impl_Step_for_i32::forward_overflowing(_1: i32, _2: usize) -> (i32, bool)
191-
= <opaque>
192-
193156
// Full name: core::iter::range::{impl Step for i32}::backward_checked
194157
pub fn impl_Step_for_i32::backward_checked(_1: i32, _2: usize) -> Option<i32>[{built_in impl Sized for i32}]
195158
= <opaque>
196159

160+
// Full name: core::iter::range::{impl Step for i32}::forward_overflowing
161+
pub fn impl_Step_for_i32::forward_overflowing(_1: i32, _2: usize) -> (i32, bool)
162+
= <opaque>
163+
197164
// Full name: core::iter::range::{impl Step for i32}::forward_checked
198165
pub fn impl_Step_for_i32::forward_checked(_1: i32, _2: usize) -> Option<i32>[{built_in impl Sized for i32}]
199166
= <opaque>
@@ -215,6 +182,39 @@ impl "impl_Step_for_i32" Step for i32 {
215182
non-dyn-compatible
216183
}
217184

185+
// Full name: core::iter::range::{impl Step for usize}::backward_overflowing
186+
pub fn impl_Step_for_usize::backward_overflowing(_1: usize, _2: usize) -> (usize, bool)
187+
= <opaque>
188+
189+
// Full name: core::iter::range::{impl Step for usize}::backward_checked
190+
pub fn impl_Step_for_usize::backward_checked(_1: usize, _2: usize) -> Option<usize>[{built_in impl Sized for usize}]
191+
= <opaque>
192+
193+
// Full name: core::iter::range::{impl Step for usize}::forward_overflowing
194+
pub fn impl_Step_for_usize::forward_overflowing(_1: usize, _2: usize) -> (usize, bool)
195+
= <opaque>
196+
197+
// Full name: core::iter::range::{impl Step for usize}::forward_checked
198+
pub fn impl_Step_for_usize::forward_checked(_1: usize, _2: usize) -> Option<usize>[{built_in impl Sized for usize}]
199+
= <opaque>
200+
201+
// Full name: core::iter::range::{impl Step for usize}::steps_between
202+
pub fn impl_Step_for_usize::steps_between<'_0, '_1>(_1: &'_0 usize, _2: &'_1 usize) -> (usize, Option<usize>[{built_in impl Sized for usize}])
203+
= <opaque>
204+
205+
// Full name: core::iter::range::{impl Step for usize}
206+
impl "impl_Step_for_usize" Step for usize {
207+
proof ImpliedClause0: (usize: Sized) = {built_in impl Sized for usize}
208+
proof ImpliedClause1: (usize: Clone) = impl_Clone_for_usize
209+
proof ImpliedClause2: (usize: PartialOrd<usize>) = impl_PartialOrd_usize_for_usize
210+
fn steps_between<'_0_1, '_1_1> = impl_Step_for_usize::steps_between<'_0_1, '_1_1>
211+
fn forward_checked = impl_Step_for_usize::forward_checked
212+
fn forward_overflowing = impl_Step_for_usize::forward_overflowing
213+
fn backward_checked = impl_Step_for_usize::backward_checked
214+
fn backward_overflowing = impl_Step_for_usize::backward_overflowing
215+
non-dyn-compatible
216+
}
217+
218218
// Full name: core::ops::range::Range
219219
#[lang_item(Range)]
220220
pub struct Range<Idx>

0 commit comments

Comments
 (0)