Unnamed repository; edit this file 'description' to name the repository.
Impl fmt::Display for Span
Lukas Wirth 2024-01-16
parent 87e609a · commit 659d4f9
-rw-r--r--crates/hir-def/src/macro_expansion_tests/mbe.rs22
-rw-r--r--crates/hir-def/src/macro_expansion_tests/mod.rs11
-rw-r--r--crates/hir-def/src/macro_expansion_tests/proc_macros.rs6
-rw-r--r--crates/span/src/lib.rs17
4 files changed, 31 insertions, 25 deletions
diff --git a/crates/hir-def/src/macro_expansion_tests/mbe.rs b/crates/hir-def/src/macro_expansion_tests/mbe.rs
index f2046bfbce..6d365bd93c 100644
--- a/crates/hir-def/src/macro_expansion_tests/mbe.rs
+++ b/crates/hir-def/src/macro_expansion_tests/mbe.rs
@@ -35,9 +35,9 @@ macro_rules! f {
};
}
-struct#FileId(0):[email protected]\2# MyTraitMap2#FileId(0):[email protected]\0# {#FileId(0):[email protected]\2#
- 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#
-}#FileId(0):[email protected]\2#
+struct#0:[email protected]#2# MyTraitMap2#0:[email protected]#0# {#0:[email protected]#2#
"#]],
);
}
@@ -75,12 +75,12 @@ macro_rules! f {
};
}
-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#
- 1#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0#
- 1.0#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0#
- (#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#
- 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#
-}#FileId(0):[email protected]\0#
"#]],
@@ -171,7 +171,7 @@ fn main(foo: ()) {
}
fn main(foo: ()) {
- /* error: unresolved macro unresolved */"helloworld!"#FileId(0):[email protected]\6#;
+ /* error: unresolved macro unresolved */"helloworld!"#0:[email protected]#6#;
}
}
@@ -197,7 +197,7 @@ macro_rules! mk_struct {
#[macro_use]
mod foo;
-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#
"#]],
);
}
diff --git a/crates/hir-def/src/macro_expansion_tests/mod.rs b/crates/hir-def/src/macro_expansion_tests/mod.rs
index ee80636123..550ce35f12 100644
--- a/crates/hir-def/src/macro_expansion_tests/mod.rs
+++ b/crates/hir-def/src/macro_expansion_tests/mod.rs
@@ -291,15 +291,8 @@ fn pretty_print_macro_expansion(
let span = map.span_for_range(token.text_range());
format_to!(res, "#");
if show_spans {
- format_to!(
- res,
- "{:?}:{:?}@{:?}",
- span.anchor.file_id,
- span.anchor.ast_id.into_raw(),
- span.range,
- );
- }
- if show_ctxt {
+ format_to!(res, "{span}",);
+ } else if show_ctxt {
format_to!(res, "\\{}", span.ctx);
}
format_to!(res, "#");
diff --git a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
index 060b8aa8c1..a4864c74d7 100644
--- a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
+++ b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
@@ -181,8 +181,8 @@ fn foo(&self) {
self.0. 1;
}
-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#
- 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#
-}#FileId(0):[email protected]\0#"#]],
+}#0:[email protected]#0#"#]],
);
}
diff --git a/crates/span/src/lib.rs b/crates/span/src/lib.rs
index 7617acde64..54fc0225ae 100644
--- a/crates/span/src/lib.rs
+++ b/crates/span/src/lib.rs
@@ -2,7 +2,7 @@
// FIXME: This should be moved into its own crate to get rid of the dependency inversion, base-db
// has business depending on tt, tt should depend on a span crate only (which unforunately will have
// to depend on salsa)
-use std::fmt;
+use std::fmt::{self, Write};
use salsa::InternId;
@@ -37,6 +37,8 @@ pub const FIXUP_ERASED_FILE_AST_ID_MARKER: ErasedFileAstId =
// is required to be stable for the proc-macro-server
la_arena::Idx::from_raw(la_arena::RawIdx::from_u32(!0 - 1));
+pub type Span = SpanData<SyntaxContextId>;
+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct SpanData<Ctx> {
/// The text range of this span, relative to the anchor.
@@ -47,6 +49,7 @@ pub struct SpanData<Ctx> {
/// The syntax context of the span.
pub ctx: Ctx,
}
+
impl Span {
#[deprecated = "dummy spans will panic if surfaced incorrectly, as such they should be replaced appropriately"]
pub const DUMMY: Self = SpanData {
@@ -56,7 +59,17 @@ impl Span {
};
}
-pub type Span = SpanData<SyntaxContextId>;
+impl fmt::Display for Span {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&self.anchor.file_id.index(), f)?;
+ f.write_char(':')?;
+ fmt::Debug::fmt(&self.anchor.ast_id.into_raw(), f)?;
+ f.write_char('@')?;
+ fmt::Debug::fmt(&self.range, f)?;
+ f.write_char('#')?;
+ self.ctx.fmt(f)
+ }
+}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SyntaxContextId(InternId);