Skip to content

Commit 0dd89d7

Browse files
committed
Remove usages of SpanData where Span suffices
1 parent b59c8c7 commit 0dd89d7

File tree

6 files changed

+34
-31
lines changed

6 files changed

+34
-31
lines changed

crates/hir-expand/src/fixup.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use rustc_hash::{FxHashMap, FxHashSet};
55
use smallvec::SmallVec;
6-
use span::{ErasedFileAstId, Span, SpanAnchor, SpanData, FIXUP_ERASED_FILE_AST_ID_MARKER};
6+
use span::{ErasedFileAstId, Span, SpanAnchor, FIXUP_ERASED_FILE_AST_ID_MARKER};
77
use stdx::never;
88
use syntax::{
99
ast::{self, AstNode, HasLoopBody},
@@ -57,7 +57,7 @@ pub(crate) fn fixup_syntax(
5757
let dummy_range = FIXUP_DUMMY_RANGE;
5858
let fake_span = |range| {
5959
let span = span_map.span_for_range(range);
60-
SpanData {
60+
Span {
6161
range: dummy_range,
6262
anchor: SpanAnchor { ast_id: FIXUP_DUMMY_AST_ID, ..span.anchor },
6363
ctx: span.ctx,
@@ -76,7 +76,7 @@ pub(crate) fn fixup_syntax(
7676
let span = span_map.span_for_range(node_range);
7777
let replacement = Leaf::Ident(Ident {
7878
text: "__ra_fixup".into(),
79-
span: SpanData {
79+
span: Span {
8080
range: TextRange::new(TextSize::new(idx), FIXUP_DUMMY_RANGE_END),
8181
anchor: SpanAnchor { ast_id: FIXUP_DUMMY_AST_ID, ..span.anchor },
8282
ctx: span.ctx,
@@ -305,8 +305,8 @@ pub(crate) fn reverse_fixups(tt: &mut Subtree, undo_info: &SyntaxFixupUndoInfo)
305305
tt.delimiter.close.anchor.ast_id == FIXUP_DUMMY_AST_ID
306306
|| tt.delimiter.open.anchor.ast_id == FIXUP_DUMMY_AST_ID
307307
) {
308-
tt.delimiter.close = SpanData::DUMMY;
309-
tt.delimiter.open = SpanData::DUMMY;
308+
tt.delimiter.close = Span::DUMMY;
309+
tt.delimiter.open = Span::DUMMY;
310310
}
311311
reverse_fixups_(tt, undo_info);
312312
}

crates/hir/src/semantics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ impl<'db> SemanticsImpl<'db> {
697697
};
698698

699699
// get mapped token in the include! macro file
700-
let span = span::SpanData {
700+
let span = span::Span {
701701
range: token.text_range(),
702702
anchor: span::SpanAnchor { file_id, ast_id: ROOT_ERASED_FILE_AST_ID },
703703
ctx: SyntaxContextId::ROOT,

crates/mbe/src/benchmark.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! This module add real world mbe example for benchmark tests
22
33
use rustc_hash::FxHashMap;
4+
use span::Span;
45
use syntax::{
56
ast::{self, HasName},
67
AstNode, SmolStr,
@@ -9,7 +10,7 @@ use test_utils::{bench, bench_fixture, skip_slow_tests};
910

1011
use crate::{
1112
parser::{MetaVarKind, Op, RepeatKind, Separator},
12-
syntax_node_to_token_tree, DeclarativeMacro, DummyTestSpanData, DummyTestSpanMap, DUMMY,
13+
syntax_node_to_token_tree, DeclarativeMacro, DummyTestSpanMap, DUMMY,
1314
};
1415

1516
#[test]
@@ -50,14 +51,14 @@ fn benchmark_expand_macro_rules() {
5051
assert_eq!(hash, 69413);
5152
}
5253

53-
fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro<DummyTestSpanData>> {
54+
fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro<Span>> {
5455
macro_rules_fixtures_tt()
5556
.into_iter()
5657
.map(|(id, tt)| (id, DeclarativeMacro::parse_macro_rules(&tt, true, true)))
5758
.collect()
5859
}
5960

60-
fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree<DummyTestSpanData>> {
61+
fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree<Span>> {
6162
let fixture = bench_fixture::numerous_macro_rules();
6263
let source_file = ast::SourceFile::parse(&fixture).ok().unwrap();
6364

@@ -79,8 +80,8 @@ fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree<DummyTestSpanData>
7980

8081
/// Generate random invocation fixtures from rules
8182
fn invocation_fixtures(
82-
rules: &FxHashMap<String, DeclarativeMacro<DummyTestSpanData>>,
83-
) -> Vec<(String, tt::Subtree<DummyTestSpanData>)> {
83+
rules: &FxHashMap<String, DeclarativeMacro<Span>>,
84+
) -> Vec<(String, tt::Subtree<Span>)> {
8485
let mut seed = 123456789;
8586
let mut res = Vec::new();
8687

@@ -128,8 +129,8 @@ fn invocation_fixtures(
128129
return res;
129130

130131
fn collect_from_op(
131-
op: &Op<DummyTestSpanData>,
132-
token_trees: &mut Vec<tt::TokenTree<DummyTestSpanData>>,
132+
op: &Op<Span>,
133+
token_trees: &mut Vec<tt::TokenTree<Span>>,
133134
seed: &mut usize,
134135
) {
135136
return match op {
@@ -221,19 +222,19 @@ fn invocation_fixtures(
221222
*seed = usize::wrapping_add(usize::wrapping_mul(*seed, a), c);
222223
*seed
223224
}
224-
fn make_ident(ident: &str) -> tt::TokenTree<DummyTestSpanData> {
225+
fn make_ident(ident: &str) -> tt::TokenTree<Span> {
225226
tt::Leaf::Ident(tt::Ident { span: DUMMY, text: SmolStr::new(ident) }).into()
226227
}
227-
fn make_punct(char: char) -> tt::TokenTree<DummyTestSpanData> {
228+
fn make_punct(char: char) -> tt::TokenTree<Span> {
228229
tt::Leaf::Punct(tt::Punct { span: DUMMY, char, spacing: tt::Spacing::Alone }).into()
229230
}
230-
fn make_literal(lit: &str) -> tt::TokenTree<DummyTestSpanData> {
231+
fn make_literal(lit: &str) -> tt::TokenTree<Span> {
231232
tt::Leaf::Literal(tt::Literal { span: DUMMY, text: SmolStr::new(lit) }).into()
232233
}
233234
fn make_subtree(
234235
kind: tt::DelimiterKind,
235-
token_trees: Option<Vec<tt::TokenTree<DummyTestSpanData>>>,
236-
) -> tt::TokenTree<DummyTestSpanData> {
236+
token_trees: Option<Vec<tt::TokenTree<Span>>>,
237+
) -> tt::TokenTree<Span> {
237238
tt::Subtree {
238239
delimiter: tt::Delimiter { open: DUMMY, close: DUMMY, kind },
239240
token_trees: token_trees.map(Vec::into_boxed_slice).unwrap_or_default(),

crates/mbe/src/syntax_bridge.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,30 @@ impl<S: Span, SM: SpanMapper<S>> SpanMapper<S> for &SM {
4141
/// Dummy things for testing where spans don't matter.
4242
pub(crate) mod dummy_test_span_utils {
4343

44+
use span::{Span, SyntaxContextId};
45+
4446
use super::*;
4547

46-
pub type DummyTestSpanData = span::SpanData<DummyTestSyntaxContext>;
47-
pub const DUMMY: DummyTestSpanData = span::SpanData {
48+
pub const DUMMY: Span = Span {
4849
range: TextRange::empty(TextSize::new(0)),
4950
anchor: span::SpanAnchor {
5051
file_id: span::FileId::BOGUS,
5152
ast_id: span::ROOT_ERASED_FILE_AST_ID,
5253
},
53-
ctx: DummyTestSyntaxContext,
54+
ctx: SyntaxContextId::ROOT,
5455
};
5556

56-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
57-
pub struct DummyTestSyntaxContext;
58-
5957
pub struct DummyTestSpanMap;
6058

61-
impl SpanMapper<span::SpanData<DummyTestSyntaxContext>> for DummyTestSpanMap {
62-
fn span_for(&self, range: syntax::TextRange) -> span::SpanData<DummyTestSyntaxContext> {
63-
span::SpanData {
59+
impl SpanMapper<Span> for DummyTestSpanMap {
60+
fn span_for(&self, range: syntax::TextRange) -> Span {
61+
Span {
6462
range,
6563
anchor: span::SpanAnchor {
6664
file_id: span::FileId::BOGUS,
6765
ast_id: span::ROOT_ERASED_FILE_AST_ID,
6866
},
69-
ctx: DummyTestSyntaxContext,
67+
ctx: SyntaxContextId::ROOT,
7068
}
7169
}
7270
}

crates/mbe/src/syntax_bridge/tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use rustc_hash::FxHashMap;
2+
use span::Span;
23
use syntax::{ast, AstNode};
34
use test_utils::extract_annotations;
45
use tt::{
56
buffer::{TokenBuffer, TokenTreeRef},
67
Leaf, Punct, Spacing,
78
};
89

9-
use crate::{syntax_node_to_token_tree, DummyTestSpanData, DummyTestSpanMap, DUMMY};
10+
use crate::{syntax_node_to_token_tree, DummyTestSpanMap, DUMMY};
1011

1112
fn check_punct_spacing(fixture: &str) {
1213
let source_file = ast::SourceFile::parse(fixture).ok().unwrap();
@@ -28,7 +29,7 @@ fn check_punct_spacing(fixture: &str) {
2829
while !cursor.eof() {
2930
while let Some(token_tree) = cursor.token_tree() {
3031
if let TokenTreeRef::Leaf(
31-
Leaf::Punct(Punct { spacing, span: DummyTestSpanData { range, .. }, .. }),
32+
Leaf::Punct(Punct { spacing, span: Span { range, .. }, .. }),
3233
_,
3334
) = token_tree
3435
{

crates/span/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ pub const FIXUP_ERASED_FILE_AST_ID_MARKER: ErasedFileAstId =
4444

4545
pub type Span = SpanData<SyntaxContextId>;
4646

47+
/// Spans represent a region of code, used by the IDE to be able link macro inputs and outputs
48+
/// together. Positions in spans are relative to some [`SpanAnchor`] to make them more incremental
49+
/// friendly.
4750
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
4851
pub struct SpanData<Ctx> {
4952
/// The text range of this span, relative to the anchor.
@@ -84,7 +87,7 @@ impl<Ctx: Copy> SpanData<Ctx> {
8487

8588
impl Span {
8689
#[deprecated = "dummy spans will panic if surfaced incorrectly, as such they should be replaced appropriately"]
87-
pub const DUMMY: Self = SpanData {
90+
pub const DUMMY: Self = Self {
8891
range: TextRange::empty(TextSize::new(0)),
8992
anchor: SpanAnchor { file_id: FileId::BOGUS, ast_id: ROOT_ERASED_FILE_AST_ID },
9093
ctx: SyntaxContextId::ROOT,

0 commit comments

Comments
 (0)