Skip to content

Commit 1372157

Browse files
authored
Merge pull request #21349 from ChayimFriedman2/no-generic-span
internal: Make token trees no longer generic over the span
2 parents 546d1ff + a366a36 commit 1372157

32 files changed

Lines changed: 419 additions & 485 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cfg/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ tracing.workspace = true
1919
# locals deps
2020
tt = { workspace = true, optional = true }
2121
syntax = { workspace = true, optional = true }
22+
span = { path = "../span", version = "0.0", optional = true }
2223
intern.workspace = true
2324

2425
[dev-dependencies]
@@ -35,6 +36,8 @@ cfg = { path = ".", default-features = false, features = ["tt"] }
3536

3637
[features]
3738
default = []
39+
syntax = ["dep:syntax", "dep:span"]
40+
tt = ["dep:tt"]
3841
in-rust-tree = []
3942

4043
[lints]

crates/cfg/src/cfg_expr.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ impl CfgExpr {
9696
// FIXME: Parsing from `tt` is only used in a handful of places, reconsider
9797
// if we should switch them to AST.
9898
#[cfg(feature = "tt")]
99-
pub fn parse<S: Copy>(tt: &tt::TopSubtree<S>) -> CfgExpr {
99+
pub fn parse(tt: &tt::TopSubtree) -> CfgExpr {
100100
next_cfg_expr(&mut tt.iter()).unwrap_or(CfgExpr::Invalid)
101101
}
102102

103103
#[cfg(feature = "tt")]
104-
pub fn parse_from_iter<S: Copy>(tt: &mut tt::iter::TtIter<'_, S>) -> CfgExpr {
104+
pub fn parse_from_iter(tt: &mut tt::iter::TtIter<'_>) -> CfgExpr {
105105
next_cfg_expr(tt).unwrap_or(CfgExpr::Invalid)
106106
}
107107

@@ -149,7 +149,15 @@ fn next_cfg_expr_from_ast(
149149
if let Some(NodeOrToken::Token(literal)) = it.peek()
150150
&& matches!(literal.kind(), SyntaxKind::STRING)
151151
{
152-
let literal = tt::token_to_literal(literal.text(), ()).symbol;
152+
let dummy_span = span::Span {
153+
range: span::TextRange::empty(span::TextSize::new(0)),
154+
anchor: span::SpanAnchor {
155+
file_id: span::EditionedFileId::from_raw(0),
156+
ast_id: span::FIXUP_ERASED_FILE_AST_ID_MARKER,
157+
},
158+
ctx: span::SyntaxContext::root(span::Edition::Edition2015),
159+
};
160+
let literal = tt::token_to_literal(literal.text(), dummy_span).symbol;
153161
it.next();
154162
CfgAtom::KeyValue { key: name, value: literal.clone() }.into()
155163
} else {
@@ -179,7 +187,7 @@ fn next_cfg_expr_from_ast(
179187
}
180188

181189
#[cfg(feature = "tt")]
182-
fn next_cfg_expr<S: Copy>(it: &mut tt::iter::TtIter<'_, S>) -> Option<CfgExpr> {
190+
fn next_cfg_expr(it: &mut tt::iter::TtIter<'_>) -> Option<CfgExpr> {
183191
use intern::sym;
184192
use tt::iter::TtElement;
185193

crates/hir-def/src/item_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn lower_extra_crate_attrs<'a>(
103103
struct FakeSpanMap {
104104
file_id: span::EditionedFileId,
105105
}
106-
impl syntax_bridge::SpanMapper<Span> for FakeSpanMap {
106+
impl syntax_bridge::SpanMapper for FakeSpanMap {
107107
fn span_for(&self, range: TextRange) -> Span {
108108
Span {
109109
range,

crates/hir-def/src/item_tree/attrs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use hir_expand::{
1818
name::Name,
1919
};
2020
use intern::{Interned, Symbol, sym};
21-
use span::Span;
2221
use syntax::{AstNode, T, ast};
2322
use syntax_bridge::DocCommentDesugarMode;
2423
use tt::token_to_literal;
@@ -49,7 +48,7 @@ impl AttrsOrCfg {
4948
span_map: S,
5049
) -> AttrsOrCfg
5150
where
52-
S: syntax_bridge::SpanMapper<Span> + Copy,
51+
S: syntax_bridge::SpanMapper + Copy,
5352
{
5453
let mut attrs = Vec::new();
5554
let result =

crates/hir-expand/src/builtin/derive_macro.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use intern::sym;
55
use itertools::{Itertools, izip};
66
use parser::SyntaxKind;
77
use rustc_hash::FxHashSet;
8-
use span::{Edition, Span, SyntaxContext};
8+
use span::{Edition, Span};
99
use stdx::never;
1010
use syntax_bridge::DocCommentDesugarMode;
1111
use tracing::debug;
@@ -238,7 +238,7 @@ fn parse_adt(
238238

239239
fn parse_adt_from_syntax(
240240
adt: &ast::Adt,
241-
tm: &span::SpanMap<SyntaxContext>,
241+
tm: &span::SpanMap,
242242
call_site: Span,
243243
) -> Result<BasicAdtInfo, ExpandError> {
244244
let (name, generic_param_list, where_clause, shape) = match &adt {
@@ -390,7 +390,7 @@ fn to_adt_syntax(
390390
db: &dyn ExpandDatabase,
391391
tt: &tt::TopSubtree,
392392
call_site: Span,
393-
) -> Result<(ast::Adt, span::SpanMap<SyntaxContext>), ExpandError> {
393+
) -> Result<(ast::Adt, span::SpanMap), ExpandError> {
394394
let (parsed, tm) = crate::db::token_tree_to_syntax_node(db, tt, crate::ExpandTo::Items);
395395
let macro_items = ast::MacroItems::cast(parsed.syntax_node())
396396
.ok_or_else(|| ExpandError::other(call_site, "invalid item definition"))?;

crates/hir-expand/src/builtin/quote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tt::IdentIsRaw;
88

99
use crate::{name::Name, tt::TopSubtreeBuilder};
1010

11-
pub(crate) fn dollar_crate(span: Span) -> tt::Ident<Span> {
11+
pub(crate) fn dollar_crate(span: Span) -> tt::Ident {
1212
tt::Ident { sym: sym::dollar_crate, span, is_raw: tt::IdentIsRaw::No }
1313
}
1414

crates/hir-expand/src/lib.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,7 @@ pub use crate::{
6666
pub use base_db::EditionedFileId;
6767
pub use mbe::{DeclarativeMacro, MacroCallStyle, MacroCallStyles, ValueResult};
6868

69-
pub mod tt {
70-
pub use span::Span;
71-
pub use tt::{DelimiterKind, IdentIsRaw, LitKind, Spacing, token_to_literal};
72-
73-
pub type Delimiter = ::tt::Delimiter<Span>;
74-
pub type DelimSpan = ::tt::DelimSpan<Span>;
75-
pub type Subtree = ::tt::Subtree<Span>;
76-
pub type Leaf = ::tt::Leaf<Span>;
77-
pub type Literal = ::tt::Literal<Span>;
78-
pub type Punct = ::tt::Punct<Span>;
79-
pub type Ident = ::tt::Ident<Span>;
80-
pub type TokenTree = ::tt::TokenTree<Span>;
81-
pub type TopSubtree = ::tt::TopSubtree<Span>;
82-
pub type TopSubtreeBuilder = ::tt::TopSubtreeBuilder<Span>;
83-
pub type TokenTreesView<'a> = ::tt::TokenTreesView<'a, Span>;
84-
pub type SubtreeView<'a> = ::tt::SubtreeView<'a, Span>;
85-
pub type TtElement<'a> = ::tt::iter::TtElement<'a, Span>;
86-
pub type TtIter<'a> = ::tt::iter::TtIter<'a, Span>;
87-
}
69+
pub use tt;
8870

8971
#[macro_export]
9072
macro_rules! impl_intern_lookup {

crates/hir-expand/src/name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl AsName for ast::NameOrNameRef {
258258
}
259259
}
260260

261-
impl<Span> AsName for tt::Ident<Span> {
261+
impl AsName for tt::Ident {
262262
fn as_name(&self) -> Name {
263263
Name::new_root(self.sym.as_str())
264264
}

crates/hir-expand/src/span_map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Span maps for real files and macro expansions.
22
3-
use span::{Span, SyntaxContext};
3+
use span::Span;
44
use syntax::{AstNode, TextRange, ast};
55
use triomphe::Arc;
66

77
pub use span::RealSpanMap;
88

99
use crate::{HirFileId, MacroCallId, db::ExpandDatabase};
1010

11-
pub type ExpansionSpanMap = span::SpanMap<SyntaxContext>;
11+
pub type ExpansionSpanMap = span::SpanMap;
1212

1313
/// Spanmap for a macro file or a real file
1414
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -27,13 +27,13 @@ pub enum SpanMapRef<'a> {
2727
RealSpanMap(&'a RealSpanMap),
2828
}
2929

30-
impl syntax_bridge::SpanMapper<Span> for SpanMap {
30+
impl syntax_bridge::SpanMapper for SpanMap {
3131
fn span_for(&self, range: TextRange) -> Span {
3232
self.span_for_range(range)
3333
}
3434
}
3535

36-
impl syntax_bridge::SpanMapper<Span> for SpanMapRef<'_> {
36+
impl syntax_bridge::SpanMapper for SpanMapRef<'_> {
3737
fn span_for(&self, range: TextRange) -> Span {
3838
self.span_for_range(range)
3939
}

0 commit comments

Comments
 (0)