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

View file

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

View file

@ -1,6 +1,7 @@
//! This module add real world mbe example for benchmark tests //! This module add real world mbe example for benchmark tests
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use span::Span;
use syntax::{ use syntax::{
ast::{self, HasName}, ast::{self, HasName},
AstNode, SmolStr, AstNode, SmolStr,
@ -9,7 +10,7 @@ use test_utils::{bench, bench_fixture, skip_slow_tests};
use crate::{ use crate::{
parser::{MetaVarKind, Op, RepeatKind, Separator}, parser::{MetaVarKind, Op, RepeatKind, Separator},
syntax_node_to_token_tree, DeclarativeMacro, DummyTestSpanData, DummyTestSpanMap, DUMMY, syntax_node_to_token_tree, DeclarativeMacro, DummyTestSpanMap, DUMMY,
}; };
#[test] #[test]
@ -50,14 +51,14 @@ fn benchmark_expand_macro_rules() {
assert_eq!(hash, 69413); assert_eq!(hash, 69413);
} }
fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro<DummyTestSpanData>> { fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro<Span>> {
macro_rules_fixtures_tt() macro_rules_fixtures_tt()
.into_iter() .into_iter()
.map(|(id, tt)| (id, DeclarativeMacro::parse_macro_rules(&tt, true, true))) .map(|(id, tt)| (id, DeclarativeMacro::parse_macro_rules(&tt, true, true)))
.collect() .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 fixture = bench_fixture::numerous_macro_rules();
let source_file = ast::SourceFile::parse(&fixture).ok().unwrap(); 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 /// Generate random invocation fixtures from rules
fn invocation_fixtures( fn invocation_fixtures(
rules: &FxHashMap<String, DeclarativeMacro<DummyTestSpanData>>, rules: &FxHashMap<String, DeclarativeMacro<Span>>,
) -> Vec<(String, tt::Subtree<DummyTestSpanData>)> { ) -> Vec<(String, tt::Subtree<Span>)> {
let mut seed = 123456789; let mut seed = 123456789;
let mut res = Vec::new(); let mut res = Vec::new();
@ -128,8 +129,8 @@ fn invocation_fixtures(
return res; return res;
fn collect_from_op( fn collect_from_op(
op: &Op<DummyTestSpanData>, op: &Op<Span>,
token_trees: &mut Vec<tt::TokenTree<DummyTestSpanData>>, token_trees: &mut Vec<tt::TokenTree<Span>>,
seed: &mut usize, seed: &mut usize,
) { ) {
return match op { return match op {
@ -221,19 +222,19 @@ fn invocation_fixtures(
*seed = usize::wrapping_add(usize::wrapping_mul(*seed, a), c); *seed = usize::wrapping_add(usize::wrapping_mul(*seed, a), c);
*seed *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() 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() 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() tt::Leaf::Literal(tt::Literal { span: DUMMY, text: SmolStr::new(lit) }).into()
} }
fn make_subtree( fn make_subtree(
kind: tt::DelimiterKind, kind: tt::DelimiterKind,
token_trees: Option<Vec<tt::TokenTree<DummyTestSpanData>>>, token_trees: Option<Vec<tt::TokenTree<Span>>>,
) -> tt::TokenTree<DummyTestSpanData> { ) -> tt::TokenTree<Span> {
tt::Subtree { tt::Subtree {
delimiter: tt::Delimiter { open: DUMMY, close: DUMMY, kind }, delimiter: tt::Delimiter { open: DUMMY, close: DUMMY, kind },
token_trees: token_trees.map(Vec::into_boxed_slice).unwrap_or_default(), 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. /// Dummy things for testing where spans don't matter.
pub(crate) mod dummy_test_span_utils { pub(crate) mod dummy_test_span_utils {
use span::{Span, SyntaxContextId};
use super::*; use super::*;
pub type DummyTestSpanData = span::SpanData<DummyTestSyntaxContext>; pub const DUMMY: Span = Span {
pub const DUMMY: DummyTestSpanData = span::SpanData {
range: TextRange::empty(TextSize::new(0)), range: TextRange::empty(TextSize::new(0)),
anchor: span::SpanAnchor { anchor: span::SpanAnchor {
file_id: span::FileId::BOGUS, file_id: span::FileId::BOGUS,
ast_id: span::ROOT_ERASED_FILE_AST_ID, 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; pub struct DummyTestSpanMap;
impl SpanMapper<span::SpanData<DummyTestSyntaxContext>> for DummyTestSpanMap { impl SpanMapper<Span> for DummyTestSpanMap {
fn span_for(&self, range: syntax::TextRange) -> span::SpanData<DummyTestSyntaxContext> { fn span_for(&self, range: syntax::TextRange) -> Span {
span::SpanData { Span {
range, range,
anchor: span::SpanAnchor { anchor: span::SpanAnchor {
file_id: span::FileId::BOGUS, file_id: span::FileId::BOGUS,
ast_id: span::ROOT_ERASED_FILE_AST_ID, ast_id: span::ROOT_ERASED_FILE_AST_ID,
}, },
ctx: DummyTestSyntaxContext, ctx: SyntaxContextId::ROOT,
} }
} }
} }

View file

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

View file

@ -44,6 +44,9 @@ pub const FIXUP_ERASED_FILE_AST_ID_MARKER: ErasedFileAstId =
pub type Span = SpanData<SyntaxContextId>; 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)] #[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct SpanData<Ctx> { pub struct SpanData<Ctx> {
/// The text range of this span, relative to the anchor. /// The text range of this span, relative to the anchor.
@ -84,7 +87,7 @@ impl<Ctx: Copy> SpanData<Ctx> {
impl Span { impl Span {
#[deprecated = "dummy spans will panic if surfaced incorrectly, as such they should be replaced appropriately"] #[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)), range: TextRange::empty(TextSize::new(0)),
anchor: SpanAnchor { file_id: FileId::BOGUS, ast_id: ROOT_ERASED_FILE_AST_ID }, anchor: SpanAnchor { file_id: FileId::BOGUS, ast_id: ROOT_ERASED_FILE_AST_ID },
ctx: SyntaxContextId::ROOT, ctx: SyntaxContextId::ROOT,