Remove usages of SpanData where Span suffices

This commit is contained in:
Lukas Wirth 2024-03-15 13:02:40 +01:00
parent b59c8c76db
commit 0dd89d7ee7
6 changed files with 34 additions and 31 deletions

View file

@ -3,7 +3,7 @@
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;
use span::{ErasedFileAstId, Span, SpanAnchor, SpanData, FIXUP_ERASED_FILE_AST_ID_MARKER};
use span::{ErasedFileAstId, Span, SpanAnchor, FIXUP_ERASED_FILE_AST_ID_MARKER};
use stdx::never;
use syntax::{
ast::{self, AstNode, HasLoopBody},
@ -57,7 +57,7 @@ pub(crate) fn fixup_syntax(
let dummy_range = FIXUP_DUMMY_RANGE;
let fake_span = |range| {
let span = span_map.span_for_range(range);
SpanData {
Span {
range: dummy_range,
anchor: SpanAnchor { ast_id: FIXUP_DUMMY_AST_ID, ..span.anchor },
ctx: span.ctx,
@ -76,7 +76,7 @@ pub(crate) fn fixup_syntax(
let span = span_map.span_for_range(node_range);
let replacement = Leaf::Ident(Ident {
text: "__ra_fixup".into(),
span: SpanData {
span: Span {
range: TextRange::new(TextSize::new(idx), FIXUP_DUMMY_RANGE_END),
anchor: SpanAnchor { ast_id: FIXUP_DUMMY_AST_ID, ..span.anchor },
ctx: span.ctx,
@ -305,8 +305,8 @@ pub(crate) fn reverse_fixups(tt: &mut Subtree, undo_info: &SyntaxFixupUndoInfo)
tt.delimiter.close.anchor.ast_id == FIXUP_DUMMY_AST_ID
|| tt.delimiter.open.anchor.ast_id == FIXUP_DUMMY_AST_ID
) {
tt.delimiter.close = SpanData::DUMMY;
tt.delimiter.open = SpanData::DUMMY;
tt.delimiter.close = Span::DUMMY;
tt.delimiter.open = Span::DUMMY;
}
reverse_fixups_(tt, undo_info);
}

View file

@ -697,7 +697,7 @@ impl<'db> SemanticsImpl<'db> {
};
// get mapped token in the include! macro file
let span = span::SpanData {
let span = span::Span {
range: token.text_range(),
anchor: span::SpanAnchor { file_id, ast_id: ROOT_ERASED_FILE_AST_ID },
ctx: SyntaxContextId::ROOT,

View file

@ -1,6 +1,7 @@
//! This module add real world mbe example for benchmark tests
use rustc_hash::FxHashMap;
use span::Span;
use syntax::{
ast::{self, HasName},
AstNode, SmolStr,
@ -9,7 +10,7 @@ use test_utils::{bench, bench_fixture, skip_slow_tests};
use crate::{
parser::{MetaVarKind, Op, RepeatKind, Separator},
syntax_node_to_token_tree, DeclarativeMacro, DummyTestSpanData, DummyTestSpanMap, DUMMY,
syntax_node_to_token_tree, DeclarativeMacro, DummyTestSpanMap, DUMMY,
};
#[test]
@ -50,14 +51,14 @@ fn benchmark_expand_macro_rules() {
assert_eq!(hash, 69413);
}
fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro<DummyTestSpanData>> {
fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro<Span>> {
macro_rules_fixtures_tt()
.into_iter()
.map(|(id, tt)| (id, DeclarativeMacro::parse_macro_rules(&tt, true, true)))
.collect()
}
fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree<DummyTestSpanData>> {
fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree<Span>> {
let fixture = bench_fixture::numerous_macro_rules();
let source_file = ast::SourceFile::parse(&fixture).ok().unwrap();
@ -79,8 +80,8 @@ fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree<DummyTestSpanData>
/// Generate random invocation fixtures from rules
fn invocation_fixtures(
rules: &FxHashMap<String, DeclarativeMacro<DummyTestSpanData>>,
) -> Vec<(String, tt::Subtree<DummyTestSpanData>)> {
rules: &FxHashMap<String, DeclarativeMacro<Span>>,
) -> Vec<(String, tt::Subtree<Span>)> {
let mut seed = 123456789;
let mut res = Vec::new();
@ -128,8 +129,8 @@ fn invocation_fixtures(
return res;
fn collect_from_op(
op: &Op<DummyTestSpanData>,
token_trees: &mut Vec<tt::TokenTree<DummyTestSpanData>>,
op: &Op<Span>,
token_trees: &mut Vec<tt::TokenTree<Span>>,
seed: &mut usize,
) {
return match op {
@ -221,19 +222,19 @@ fn invocation_fixtures(
*seed = usize::wrapping_add(usize::wrapping_mul(*seed, a), c);
*seed
}
fn make_ident(ident: &str) -> tt::TokenTree<DummyTestSpanData> {
fn make_ident(ident: &str) -> tt::TokenTree<Span> {
tt::Leaf::Ident(tt::Ident { span: DUMMY, text: SmolStr::new(ident) }).into()
}
fn make_punct(char: char) -> tt::TokenTree<DummyTestSpanData> {
fn make_punct(char: char) -> tt::TokenTree<Span> {
tt::Leaf::Punct(tt::Punct { span: DUMMY, char, spacing: tt::Spacing::Alone }).into()
}
fn make_literal(lit: &str) -> tt::TokenTree<DummyTestSpanData> {
fn make_literal(lit: &str) -> tt::TokenTree<Span> {
tt::Leaf::Literal(tt::Literal { span: DUMMY, text: SmolStr::new(lit) }).into()
}
fn make_subtree(
kind: tt::DelimiterKind,
token_trees: Option<Vec<tt::TokenTree<DummyTestSpanData>>>,
) -> tt::TokenTree<DummyTestSpanData> {
token_trees: Option<Vec<tt::TokenTree<Span>>>,
) -> tt::TokenTree<Span> {
tt::Subtree {
delimiter: tt::Delimiter { open: DUMMY, close: DUMMY, kind },
token_trees: token_trees.map(Vec::into_boxed_slice).unwrap_or_default(),

View file

@ -41,32 +41,30 @@ impl<S: Span, SM: SpanMapper<S>> SpanMapper<S> for &SM {
/// Dummy things for testing where spans don't matter.
pub(crate) mod dummy_test_span_utils {
use span::{Span, SyntaxContextId};
use super::*;
pub type DummyTestSpanData = span::SpanData<DummyTestSyntaxContext>;
pub const DUMMY: DummyTestSpanData = span::SpanData {
pub const DUMMY: Span = Span {
range: TextRange::empty(TextSize::new(0)),
anchor: span::SpanAnchor {
file_id: span::FileId::BOGUS,
ast_id: span::ROOT_ERASED_FILE_AST_ID,
},
ctx: DummyTestSyntaxContext,
ctx: SyntaxContextId::ROOT,
};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct DummyTestSyntaxContext;
pub struct DummyTestSpanMap;
impl SpanMapper<span::SpanData<DummyTestSyntaxContext>> for DummyTestSpanMap {
fn span_for(&self, range: syntax::TextRange) -> span::SpanData<DummyTestSyntaxContext> {
span::SpanData {
impl SpanMapper<Span> for DummyTestSpanMap {
fn span_for(&self, range: syntax::TextRange) -> Span {
Span {
range,
anchor: span::SpanAnchor {
file_id: span::FileId::BOGUS,
ast_id: span::ROOT_ERASED_FILE_AST_ID,
},
ctx: DummyTestSyntaxContext,
ctx: SyntaxContextId::ROOT,
}
}
}

View file

@ -1,4 +1,5 @@
use rustc_hash::FxHashMap;
use span::Span;
use syntax::{ast, AstNode};
use test_utils::extract_annotations;
use tt::{
@ -6,7 +7,7 @@ use tt::{
Leaf, Punct, Spacing,
};
use crate::{syntax_node_to_token_tree, DummyTestSpanData, DummyTestSpanMap, DUMMY};
use crate::{syntax_node_to_token_tree, DummyTestSpanMap, DUMMY};
fn check_punct_spacing(fixture: &str) {
let source_file = ast::SourceFile::parse(fixture).ok().unwrap();
@ -28,7 +29,7 @@ fn check_punct_spacing(fixture: &str) {
while !cursor.eof() {
while let Some(token_tree) = cursor.token_tree() {
if let TokenTreeRef::Leaf(
Leaf::Punct(Punct { spacing, span: DummyTestSpanData { range, .. }, .. }),
Leaf::Punct(Punct { spacing, span: Span { range, .. }, .. }),
_,
) = token_tree
{

View file

@ -44,6 +44,9 @@ pub const FIXUP_ERASED_FILE_AST_ID_MARKER: ErasedFileAstId =
pub type Span = SpanData<SyntaxContextId>;
/// Spans represent a region of code, used by the IDE to be able link macro inputs and outputs
/// together. Positions in spans are relative to some [`SpanAnchor`] to make them more incremental
/// friendly.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct SpanData<Ctx> {
/// The text range of this span, relative to the anchor.
@ -84,7 +87,7 @@ impl<Ctx: Copy> SpanData<Ctx> {
impl Span {
#[deprecated = "dummy spans will panic if surfaced incorrectly, as such they should be replaced appropriately"]
pub const DUMMY: Self = SpanData {
pub const DUMMY: Self = Self {
range: TextRange::empty(TextSize::new(0)),
anchor: SpanAnchor { file_id: FileId::BOGUS, ast_id: ROOT_ERASED_FILE_AST_ID },
ctx: SyntaxContextId::ROOT,