From 0dd89d7ee7800c53dc10cec941c4e740562d54df Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 15 Mar 2024 13:02:40 +0100 Subject: [PATCH] Remove usages of SpanData where Span suffices --- crates/hir-expand/src/fixup.rs | 10 +++++----- crates/hir/src/semantics.rs | 2 +- crates/mbe/src/benchmark.rs | 25 +++++++++++++------------ crates/mbe/src/syntax_bridge.rs | 18 ++++++++---------- crates/mbe/src/syntax_bridge/tests.rs | 5 +++-- crates/span/src/lib.rs | 5 ++++- 6 files changed, 34 insertions(+), 31 deletions(-) diff --git a/crates/hir-expand/src/fixup.rs b/crates/hir-expand/src/fixup.rs index b44feb5ebe..959595afb5 100644 --- a/crates/hir-expand/src/fixup.rs +++ b/crates/hir-expand/src/fixup.rs @@ -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); } diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 02f9d5f31a..9796009cb4 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -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, diff --git a/crates/mbe/src/benchmark.rs b/crates/mbe/src/benchmark.rs index d946ecc1ca..ac7f071178 100644 --- a/crates/mbe/src/benchmark.rs +++ b/crates/mbe/src/benchmark.rs @@ -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> { +fn macro_rules_fixtures() -> FxHashMap> { macro_rules_fixtures_tt() .into_iter() .map(|(id, tt)| (id, DeclarativeMacro::parse_macro_rules(&tt, true, true))) .collect() } -fn macro_rules_fixtures_tt() -> FxHashMap> { +fn macro_rules_fixtures_tt() -> FxHashMap> { 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 /// Generate random invocation fixtures from rules fn invocation_fixtures( - rules: &FxHashMap>, -) -> Vec<(String, tt::Subtree)> { + rules: &FxHashMap>, +) -> Vec<(String, tt::Subtree)> { let mut seed = 123456789; let mut res = Vec::new(); @@ -128,8 +129,8 @@ fn invocation_fixtures( return res; fn collect_from_op( - op: &Op, - token_trees: &mut Vec>, + op: &Op, + token_trees: &mut Vec>, 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 { + fn make_ident(ident: &str) -> tt::TokenTree { tt::Leaf::Ident(tt::Ident { span: DUMMY, text: SmolStr::new(ident) }).into() } - fn make_punct(char: char) -> tt::TokenTree { + fn make_punct(char: char) -> tt::TokenTree { tt::Leaf::Punct(tt::Punct { span: DUMMY, char, spacing: tt::Spacing::Alone }).into() } - fn make_literal(lit: &str) -> tt::TokenTree { + fn make_literal(lit: &str) -> tt::TokenTree { tt::Leaf::Literal(tt::Literal { span: DUMMY, text: SmolStr::new(lit) }).into() } fn make_subtree( kind: tt::DelimiterKind, - token_trees: Option>>, - ) -> tt::TokenTree { + token_trees: Option>>, + ) -> tt::TokenTree { tt::Subtree { delimiter: tt::Delimiter { open: DUMMY, close: DUMMY, kind }, token_trees: token_trees.map(Vec::into_boxed_slice).unwrap_or_default(), diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index cb8f8dff50..57d6082dd7 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -41,32 +41,30 @@ impl> SpanMapper 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; - 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> for DummyTestSpanMap { - fn span_for(&self, range: syntax::TextRange) -> span::SpanData { - span::SpanData { + impl SpanMapper 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, } } } diff --git a/crates/mbe/src/syntax_bridge/tests.rs b/crates/mbe/src/syntax_bridge/tests.rs index 11d1a72879..a261b1d431 100644 --- a/crates/mbe/src/syntax_bridge/tests.rs +++ b/crates/mbe/src/syntax_bridge/tests.rs @@ -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 { diff --git a/crates/span/src/lib.rs b/crates/span/src/lib.rs index a29e5369a6..6b849ce373 100644 --- a/crates/span/src/lib.rs +++ b/crates/span/src/lib.rs @@ -44,6 +44,9 @@ pub const FIXUP_ERASED_FILE_AST_ID_MARKER: ErasedFileAstId = pub type Span = SpanData; +/// 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 { /// The text range of this span, relative to the anchor. @@ -84,7 +87,7 @@ impl SpanData { 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,