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):[email protected]\2# MyTraitMap2#FileId(0):[email protected]\0# {#FileId(0):[email protected]\2#
39-
map#FileId(0):[email protected]\2#:#FileId(0):[email protected]\2# #FileId(0):[email protected]\2#::#FileId(0):[email protected]\2#std#FileId(0):[email protected]\2#::#FileId(0):[email protected]\2#collections#FileId(0):[email protected]\2#::#FileId(0):[email protected]\2#HashSet#FileId(0):[email protected]\2#<#FileId(0):[email protected]\2#(#FileId(0):[email protected]\2#)#FileId(0):[email protected]\2#>#FileId(0):[email protected]\2#,#FileId(0):[email protected]\2#
40-
}#FileId(0):[email protected]\2#
38+
struct#0:[email protected]#2# MyTraitMap2#0:[email protected]#0# {#0:[email protected]#2#
39+
40+
4141
"#]],
4242
);
4343
}
@@ -75,12 +75,12 @@ macro_rules! f {
7575
};
7676
}
7777
78-
fn#FileId(0):[email protected]\0# main#FileId(0):[email protected]\0#(#FileId(0):[email protected]\0#)#FileId(0):[email protected]\0# {#FileId(0):[email protected]\0#
79-
1#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0#
80-
1.0#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0#
81-
(#FileId(0):[email protected]\0#(#FileId(0):[email protected]\0#1#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# )#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# )#FileId(0):[email protected]\0#.#FileId(0):[email protected]\0#0#FileId(0):[email protected]\0#.#FileId(0):[email protected]\0#0#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0#
82-
let#FileId(0):[email protected]\0# x#FileId(0):[email protected]\0# =#FileId(0):[email protected]\0# 1#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0#
83-
}#FileId(0):[email protected]\0#
78+
79+
80+
81+
82+
83+
8484
8585
8686
"#]],
@@ -171,7 +171,7 @@ fn main(foo: ()) {
171171
}
172172
173173
fn main(foo: ()) {
174-
/* error: unresolved macro unresolved */"helloworld!"#FileId(0):[email protected]\6#;
174+
/* error: unresolved macro unresolved */"helloworld!"#0:[email protected]#6#;
175175
}
176176
}
177177
@@ -197,7 +197,7 @@ macro_rules! mk_struct {
197197
#[macro_use]
198198
mod foo;
199199
200-
struct#FileId(1):[email protected]\2# Foo#FileId(0):[email protected]\0#(#FileId(1):[email protected]\2#u32#FileId(0):[email protected]\0#)#FileId(1):[email protected]\2#;#FileId(1):[email protected]\2#
200+
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):[email protected]\0# foo#FileId(0):[email protected]\0#(#FileId(0):[email protected]\0#&#FileId(0):[email protected]\0#self#FileId(0):[email protected]\0# )#FileId(0):[email protected]\0# {#FileId(0):[email protected]\0#
185-
self#FileId(0):[email protected]\0# .#FileId(0):[email protected]\0#0#FileId(0):[email protected]\0#.#FileId(0):[email protected]\0#1#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0#
186-
}#FileId(0):[email protected]\0#"#]],
184+
185+
186+
}#0:[email protected]#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)