Skip to content

Commit 659d4f9

Browse files
committed
Impl fmt::Display for Span
1 parent 87e609a commit 659d4f9

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

crates/hir-def/src/macro_expansion_tests/mbe.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ macro_rules! f {
3535
};
3636
}
3737
38-
struct#FileId(0):1@58..64\2# MyTraitMap2#FileId(0):2@31..42\0# {#FileId(0):1@72..73\2#
39-
map#FileId(0):1@86..89\2#:#FileId(0):1@89..90\2# #FileId(0):1@89..90\2#::#FileId(0):1@91..92\2#std#FileId(0):1@93..96\2#::#FileId(0):1@96..97\2#collections#FileId(0):1@98..109\2#::#FileId(0):1@109..110\2#HashSet#FileId(0):1@111..118\2#<#FileId(0):1@118..119\2#(#FileId(0):1@119..120\2#)#FileId(0):1@120..121\2#>#FileId(0):1@121..122\2#,#FileId(0):1@122..123\2#
40-
}#FileId(0):1@132..133\2#
38+
struct#0:1@58..64#2# MyTraitMap2#0:2@31..42#0# {#0:1@72..73#2#
39+
map#0:1@86..89#2#:#0:1@89..90#2# #0:1@89..90#2#::#0:1@91..92#2#std#0:1@93..96#2#::#0:1@96..97#2#collections#0:1@98..109#2#::#0:1@109..110#2#HashSet#0:1@111..118#2#<#0:1@118..119#2#(#0:1@119..120#2#)#0:1@120..121#2#>#0:1@121..122#2#,#0:1@122..123#2#
40+
}#0:1@132..133#2#
4141
"#]],
4242
);
4343
}
@@ -75,12 +75,12 @@ macro_rules! f {
7575
};
7676
}
7777
78-
fn#FileId(0):2@30..32\0# main#FileId(0):2@33..37\0#(#FileId(0):2@37..38\0#)#FileId(0):2@38..39\0# {#FileId(0):2@40..41\0#
79-
1#FileId(0):2@50..51\0#;#FileId(0):2@51..52\0#
80-
1.0#FileId(0):2@61..64\0#;#FileId(0):2@64..65\0#
81-
(#FileId(0):2@74..75\0#(#FileId(0):2@75..76\0#1#FileId(0):2@76..77\0#,#FileId(0):2@77..78\0# )#FileId(0):2@78..79\0#,#FileId(0):2@79..80\0# )#FileId(0):2@80..81\0#.#FileId(0):2@81..82\0#0#FileId(0):2@82..85\0#.#FileId(0):2@82..85\0#0#FileId(0):2@82..85\0#;#FileId(0):2@85..86\0#
82-
let#FileId(0):2@95..98\0# x#FileId(0):2@99..100\0# =#FileId(0):2@101..102\0# 1#FileId(0):2@103..104\0#;#FileId(0):2@104..105\0#
83-
}#FileId(0):2@110..111\0#
78+
fn#0:2@30..32#0# main#0:2@33..37#0#(#0:2@37..38#0#)#0:2@38..39#0# {#0:2@40..41#0#
79+
1#0:2@50..51#0#;#0:2@51..52#0#
80+
1.0#0:2@61..64#0#;#0:2@64..65#0#
81+
(#0:2@74..75#0#(#0:2@75..76#0#1#0:2@76..77#0#,#0:2@77..78#0# )#0:2@78..79#0#,#0:2@79..80#0# )#0:2@80..81#0#.#0:2@81..82#0#0#0:2@82..85#0#.#0:2@82..85#0#0#0:2@82..85#0#;#0:2@85..86#0#
82+
let#0:2@95..98#0# x#0:2@99..100#0# =#0:2@101..102#0# 1#0:2@103..104#0#;#0:2@104..105#0#
83+
}#0:2@110..111#0#
8484
8585
8686
"#]],
@@ -171,7 +171,7 @@ fn main(foo: ()) {
171171
}
172172
173173
fn main(foo: ()) {
174-
/* error: unresolved macro unresolved */"helloworld!"#FileId(0):3@207..323\6#;
174+
/* error: unresolved macro unresolved */"helloworld!"#0:3@207..323#6#;
175175
}
176176
}
177177
@@ -197,7 +197,7 @@ macro_rules! mk_struct {
197197
#[macro_use]
198198
mod foo;
199199
200-
struct#FileId(1):1@59..65\2# Foo#FileId(0):2@32..35\0#(#FileId(1):1@70..71\2#u32#FileId(0):2@41..44\0#)#FileId(1):1@74..75\2#;#FileId(1):1@75..76\2#
200+
struct#1:1@59..65#2# Foo#0:2@32..35#0#(#1:1@70..71#2#u32#0:2@41..44#0#)#1:1@74..75#2#;#1:1@75..76#2#
201201
"#]],
202202
);
203203
}

crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,8 @@ fn pretty_print_macro_expansion(
291291
let span = map.span_for_range(token.text_range());
292292
format_to!(res, "#");
293293
if show_spans {
294-
format_to!(
295-
res,
296-
"{:?}:{:?}@{:?}",
297-
span.anchor.file_id,
298-
span.anchor.ast_id.into_raw(),
299-
span.range,
300-
);
301-
}
302-
if show_ctxt {
294+
format_to!(res, "{span}",);
295+
} else if show_ctxt {
303296
format_to!(res, "\\{}", span.ctx);
304297
}
305298
format_to!(res, "#");

crates/hir-def/src/macro_expansion_tests/proc_macros.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ fn foo(&self) {
181181
self.0. 1;
182182
}
183183
184-
fn#FileId(0):1@45..47\0# foo#FileId(0):1@48..51\0#(#FileId(0):1@51..52\0#&#FileId(0):1@52..53\0#self#FileId(0):1@53..57\0# )#FileId(0):1@57..58\0# {#FileId(0):1@59..60\0#
185-
self#FileId(0):1@65..69\0# .#FileId(0):1@69..70\0#0#FileId(0):1@70..71\0#.#FileId(0):1@71..72\0#1#FileId(0):1@73..74\0#;#FileId(0):1@74..75\0#
186-
}#FileId(0):1@76..77\0#"#]],
184+
fn#0:1@45..47#0# foo#0:1@48..51#0#(#0:1@51..52#0#&#0:1@52..53#0#self#0:1@53..57#0# )#0:1@57..58#0# {#0:1@59..60#0#
185+
self#0:1@65..69#0# .#0:1@69..70#0#0#0:1@70..71#0#.#0:1@71..72#0#1#0:1@73..74#0#;#0:1@74..75#0#
186+
}#0:1@76..77#0#"#]],
187187
);
188188
}

crates/span/src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FIXME: This should be moved into its own crate to get rid of the dependency inversion, base-db
33
// has business depending on tt, tt should depend on a span crate only (which unforunately will have
44
// to depend on salsa)
5-
use std::fmt;
5+
use std::fmt::{self, Write};
66

77
use salsa::InternId;
88

@@ -37,6 +37,8 @@ pub const FIXUP_ERASED_FILE_AST_ID_MARKER: ErasedFileAstId =
3737
// is required to be stable for the proc-macro-server
3838
la_arena::Idx::from_raw(la_arena::RawIdx::from_u32(!0 - 1));
3939

40+
pub type Span = SpanData<SyntaxContextId>;
41+
4042
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
4143
pub struct SpanData<Ctx> {
4244
/// The text range of this span, relative to the anchor.
@@ -47,6 +49,7 @@ pub struct SpanData<Ctx> {
4749
/// The syntax context of the span.
4850
pub ctx: Ctx,
4951
}
52+
5053
impl Span {
5154
#[deprecated = "dummy spans will panic if surfaced incorrectly, as such they should be replaced appropriately"]
5255
pub const DUMMY: Self = SpanData {
@@ -56,7 +59,17 @@ impl Span {
5659
};
5760
}
5861

59-
pub type Span = SpanData<SyntaxContextId>;
62+
impl fmt::Display for Span {
63+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64+
fmt::Debug::fmt(&self.anchor.file_id.index(), f)?;
65+
f.write_char(':')?;
66+
fmt::Debug::fmt(&self.anchor.ast_id.into_raw(), f)?;
67+
f.write_char('@')?;
68+
fmt::Debug::fmt(&self.range, f)?;
69+
f.write_char('#')?;
70+
self.ctx.fmt(f)
71+
}
72+
}
6073

6174
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6275
pub struct SyntaxContextId(InternId);

0 commit comments

Comments
 (0)