Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7b80455

Browse files
committed
Remove Delimiter::DUMMY_INVISIBLE
1 parent 2c6ce48 commit 7b80455

File tree

20 files changed

+170
-106
lines changed

20 files changed

+170
-106
lines changed

crates/cfg/src/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
use arbitrary::{Arbitrary, Unstructured};
22
use expect_test::{expect, Expect};
3-
use mbe::{syntax_node_to_token_tree, DummyTestSpanMap};
3+
use mbe::{syntax_node_to_token_tree, DummyTestSpanMap, DUMMY};
44
use syntax::{ast, AstNode};
55

66
use crate::{CfgAtom, CfgExpr, CfgOptions, DnfExpr};
77

88
fn assert_parse_result(input: &str, expected: CfgExpr) {
99
let source_file = ast::SourceFile::parse(input).ok().unwrap();
1010
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
11-
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap);
11+
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
1212
let cfg = CfgExpr::parse(&tt);
1313
assert_eq!(cfg, expected);
1414
}
1515

1616
fn check_dnf(input: &str, expect: Expect) {
1717
let source_file = ast::SourceFile::parse(input).ok().unwrap();
1818
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
19-
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap);
19+
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
2020
let cfg = CfgExpr::parse(&tt);
2121
let actual = format!("#![cfg({})]", DnfExpr::new(cfg));
2222
expect.assert_eq(&actual);
@@ -25,7 +25,7 @@ fn check_dnf(input: &str, expect: Expect) {
2525
fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
2626
let source_file = ast::SourceFile::parse(input).ok().unwrap();
2727
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
28-
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap);
28+
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
2929
let cfg = CfgExpr::parse(&tt);
3030
let dnf = DnfExpr::new(cfg);
3131
let why_inactive = dnf.why_inactive(opts).unwrap().to_string();
@@ -36,7 +36,7 @@ fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
3636
fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) {
3737
let source_file = ast::SourceFile::parse(input).ok().unwrap();
3838
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
39-
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap);
39+
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
4040
let cfg = CfgExpr::parse(&tt);
4141
let dnf = DnfExpr::new(cfg);
4242
let hints = dnf.compute_enable_hints(opts).map(|diff| diff.to_string()).collect::<Vec<_>>();

crates/hir-def/src/attr/tests.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
//! This module contains tests for doc-expression parsing.
22
//! Currently, it tests `#[doc(hidden)]` and `#[doc(alias)]`.
33
4+
use triomphe::Arc;
5+
46
use base_db::FileId;
5-
use hir_expand::span_map::{RealSpanMap, SpanMapRef};
7+
use hir_expand::span_map::{RealSpanMap, SpanMap};
68
use mbe::syntax_node_to_token_tree;
7-
use syntax::{ast, AstNode};
9+
use syntax::{ast, AstNode, TextRange};
810

911
use crate::attr::{DocAtom, DocExpr};
1012

1113
fn assert_parse_result(input: &str, expected: DocExpr) {
1214
let source_file = ast::SourceFile::parse(input).ok().unwrap();
1315
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
16+
let map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(FileId::from_raw(0))));
1417
let tt = syntax_node_to_token_tree(
1518
tt.syntax(),
16-
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId::from_raw(0))),
19+
map.as_ref(),
20+
map.span_for_range(TextRange::empty(0.into())),
1721
);
1822
let cfg = DocExpr::parse(&tt);
1923
assert_eq!(cfg, expected);

crates/hir-def/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ fn attr_macro_as_call_id(
13501350
let arg = match macro_attr.input.as_deref() {
13511351
Some(AttrInput::TokenTree(tt)) => {
13521352
let mut tt = tt.as_ref().clone();
1353-
tt.delimiter = tt::Delimiter::DUMMY_INVISIBLE;
1353+
tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
13541354
Some(tt)
13551355
}
13561356

crates/hir-expand/src/attrs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,20 @@ impl Attr {
206206
id: AttrId,
207207
) -> Option<Attr> {
208208
let path = Interned::new(ModPath::from_src(db, ast.path()?, span_map)?);
209+
let span = span_map.span_for_range(ast.syntax().text_range());
209210
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
210211
let value = match lit.kind() {
211212
ast::LiteralKind::String(string) => string.value()?.into(),
212213
_ => lit.syntax().first_token()?.text().trim_matches('"').into(),
213214
};
214215
Some(Interned::new(AttrInput::Literal(value)))
215216
} else if let Some(tt) = ast.token_tree() {
216-
let tree = syntax_node_to_token_tree(tt.syntax(), span_map);
217+
let tree = syntax_node_to_token_tree(tt.syntax(), span_map, span);
217218
Some(Interned::new(AttrInput::TokenTree(Box::new(tree))))
218219
} else {
219220
None
220221
};
221-
Some(Attr { id, path, input, span: span_map.span_for_range(ast.syntax().text_range()) })
222+
Some(Attr { id, path, input, span })
222223
}
223224

224225
fn from_tt(db: &dyn ExpandDatabase, tt: &tt::Subtree, id: AttrId) -> Option<Attr> {

crates/hir-expand/src/builtin_attr_macro.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ fn derive_attr_expand(
101101
MacroCallKind::Attr { attr_args: Some(attr_args), .. } if loc.def.is_attribute_derive() => {
102102
attr_args
103103
}
104-
_ => return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan::DUMMY)),
104+
_ => {
105+
return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan {
106+
open: loc.call_site,
107+
close: loc.call_site,
108+
}))
109+
}
105110
};
106111
pseudo_derive_attr_expansion(tt, derives, loc.call_site)
107112
}

crates/hir-expand/src/builtin_derive_macro.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,23 +246,23 @@ fn parse_adt(
246246
match this {
247247
Some(it) => {
248248
param_type_set.insert(it.as_name());
249-
mbe::syntax_node_to_token_tree(it.syntax(), tm)
249+
mbe::syntax_node_to_token_tree(it.syntax(), tm, call_site)
250250
}
251251
None => {
252252
tt::Subtree::empty(::tt::DelimSpan { open: call_site, close: call_site })
253253
}
254254
}
255255
};
256256
let bounds = match &param {
257-
ast::TypeOrConstParam::Type(it) => {
258-
it.type_bound_list().map(|it| mbe::syntax_node_to_token_tree(it.syntax(), tm))
259-
}
257+
ast::TypeOrConstParam::Type(it) => it
258+
.type_bound_list()
259+
.map(|it| mbe::syntax_node_to_token_tree(it.syntax(), tm, call_site)),
260260
ast::TypeOrConstParam::Const(_) => None,
261261
};
262262
let ty = if let ast::TypeOrConstParam::Const(param) = param {
263263
let ty = param
264264
.ty()
265-
.map(|ty| mbe::syntax_node_to_token_tree(ty.syntax(), tm))
265+
.map(|ty| mbe::syntax_node_to_token_tree(ty.syntax(), tm, call_site))
266266
.unwrap_or_else(|| {
267267
tt::Subtree::empty(::tt::DelimSpan { open: call_site, close: call_site })
268268
});
@@ -298,7 +298,7 @@ fn parse_adt(
298298
let name = p.path()?.qualifier()?.as_single_name_ref()?.as_name();
299299
param_type_set.contains(&name).then_some(p)
300300
})
301-
.map(|it| mbe::syntax_node_to_token_tree(it.syntax(), tm))
301+
.map(|it| mbe::syntax_node_to_token_tree(it.syntax(), tm, call_site))
302302
.collect();
303303
let name_token = name_to_token(tm, name)?;
304304
Ok(BasicAdtInfo { name: name_token, shape, param_types, associated_types })

crates/hir-expand/src/builtin_fn_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn assert_expand(
201201
tt: &tt::Subtree,
202202
span: Span,
203203
) -> ExpandResult<tt::Subtree> {
204-
let args = parse_exprs_with_sep(tt, ',');
204+
let args = parse_exprs_with_sep(tt, ',', span);
205205
let dollar_crate = tt::Ident { text: SmolStr::new_inline("$crate"), span };
206206
let expanded = match &*args {
207207
[cond, panic_args @ ..] => {

crates/hir-expand/src/db.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl DeclarativeMacroExpander {
7272
});
7373
match self.mac.err() {
7474
Some(e) => ExpandResult::new(
75-
tt::Subtree::empty(tt::DelimSpan::DUMMY),
75+
tt::Subtree::empty(tt::DelimSpan { open: loc.call_site, close: loc.call_site }),
7676
ExpandError::other(format!("invalid macro definition: {e}")),
7777
),
7878
None => self
@@ -108,7 +108,7 @@ impl DeclarativeMacroExpander {
108108
});
109109
match self.mac.err() {
110110
Some(e) => ExpandResult::new(
111-
tt::Subtree::empty(tt::DelimSpan::DUMMY),
111+
tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
112112
ExpandError::other(format!("invalid macro definition: {e}")),
113113
),
114114
None => self.mac.expand(&tt, |_| (), new_meta_vars, call_site).map_err(Into::into),
@@ -244,12 +244,13 @@ pub fn expand_speculative(
244244

245245
// Build the subtree and token mapping for the speculative args
246246
let (mut tt, undo_info) = match loc.kind {
247-
MacroCallKind::FnLike { .. } => {
248-
(mbe::syntax_node_to_token_tree(speculative_args, span_map), SyntaxFixupUndoInfo::NONE)
249-
}
247+
MacroCallKind::FnLike { .. } => (
248+
mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site),
249+
SyntaxFixupUndoInfo::NONE,
250+
),
250251
MacroCallKind::Derive { .. } | MacroCallKind::Attr { .. } => {
251252
let censor = censor_for_macro_input(&loc, speculative_args);
252-
let mut fixups = fixup::fixup_syntax(span_map, speculative_args);
253+
let mut fixups = fixup::fixup_syntax(span_map, speculative_args, loc.call_site);
253254
fixups.append.retain(|it, _| match it {
254255
syntax::NodeOrToken::Node(it) => !censor.contains(it),
255256
syntax::NodeOrToken::Token(_) => true,
@@ -261,6 +262,7 @@ pub fn expand_speculative(
261262
span_map,
262263
fixups.append,
263264
fixups.remove,
265+
loc.call_site,
264266
),
265267
fixups.undo_info,
266268
)
@@ -282,8 +284,9 @@ pub fn expand_speculative(
282284
}?;
283285
match attr.token_tree() {
284286
Some(token_tree) => {
285-
let mut tree = syntax_node_to_token_tree(token_tree.syntax(), span_map);
286-
tree.delimiter = tt::Delimiter::DUMMY_INVISIBLE;
287+
let mut tree =
288+
syntax_node_to_token_tree(token_tree.syntax(), span_map, loc.call_site);
289+
tree.delimiter = tt::Delimiter::invisible_spanned(loc.call_site);
287290

288291
Some(tree)
289292
}
@@ -297,7 +300,7 @@ pub fn expand_speculative(
297300
// Otherwise the expand query will fetch the non speculative attribute args and pass those instead.
298301
let mut speculative_expansion = match loc.def.kind {
299302
MacroDefKind::ProcMacro(expander, ..) => {
300-
tt.delimiter = tt::Delimiter::DUMMY_INVISIBLE;
303+
tt.delimiter = tt::Delimiter::invisible_spanned(loc.call_site);
301304
let call_site = loc.span(db);
302305
expander.expand(
303306
db,
@@ -473,12 +476,13 @@ fn macro_arg(
473476
MacroCallKind::Attr { ast_id, .. } => ast_id.to_ptr(db).to_node(&root).syntax().clone(),
474477
};
475478
let (mut tt, undo_info) = match loc.kind {
476-
MacroCallKind::FnLike { .. } => {
477-
(mbe::syntax_node_to_token_tree(&syntax, map.as_ref()), SyntaxFixupUndoInfo::NONE)
478-
}
479+
MacroCallKind::FnLike { .. } => (
480+
mbe::syntax_node_to_token_tree(&syntax, map.as_ref(), loc.call_site),
481+
SyntaxFixupUndoInfo::NONE,
482+
),
479483
MacroCallKind::Derive { .. } | MacroCallKind::Attr { .. } => {
480484
let censor = censor_for_macro_input(&loc, &syntax);
481-
let mut fixups = fixup::fixup_syntax(map.as_ref(), &syntax);
485+
let mut fixups = fixup::fixup_syntax(map.as_ref(), &syntax, loc.call_site);
482486
fixups.append.retain(|it, _| match it {
483487
syntax::NodeOrToken::Node(it) => !censor.contains(it),
484488
syntax::NodeOrToken::Token(_) => true,
@@ -490,6 +494,7 @@ fn macro_arg(
490494
map.as_ref(),
491495
fixups.append.clone(),
492496
fixups.remove.clone(),
497+
loc.call_site,
493498
);
494499
reverse_fixups(&mut tt, &fixups.undo_info);
495500
}
@@ -499,6 +504,7 @@ fn macro_arg(
499504
map,
500505
fixups.append,
501506
fixups.remove,
507+
loc.call_site,
502508
),
503509
fixups.undo_info,
504510
)
@@ -507,7 +513,7 @@ fn macro_arg(
507513

508514
if loc.def.is_proc_macro() {
509515
// proc macros expect their inputs without parentheses, MBEs expect it with them included
510-
tt.delimiter = tt::Delimiter::DUMMY_INVISIBLE;
516+
tt.delimiter.kind = tt::DelimiterKind::Invisible;
511517
}
512518

513519
if matches!(loc.def.kind, MacroDefKind::BuiltInEager(..)) {
@@ -611,7 +617,11 @@ fn decl_macro_expander(
611617
ast::Macro::MacroRules(macro_rules) => (
612618
match macro_rules.token_tree() {
613619
Some(arg) => {
614-
let tt = mbe::syntax_node_to_token_tree(arg.syntax(), map.as_ref());
620+
let tt = mbe::syntax_node_to_token_tree(
621+
arg.syntax(),
622+
map.as_ref(),
623+
map.span_for_range(macro_rules.macro_rules_token().unwrap().text_range()),
624+
);
615625
let mac = mbe::DeclarativeMacro::parse_macro_rules(&tt, is_2021, new_meta_vars);
616626
mac
617627
}
@@ -625,7 +635,11 @@ fn decl_macro_expander(
625635
ast::Macro::MacroDef(macro_def) => (
626636
match macro_def.body() {
627637
Some(arg) => {
628-
let tt = mbe::syntax_node_to_token_tree(arg.syntax(), map.as_ref());
638+
let tt = mbe::syntax_node_to_token_tree(
639+
arg.syntax(),
640+
map.as_ref(),
641+
map.span_for_range(macro_def.macro_token().unwrap().text_range()),
642+
);
629643
let mac = mbe::DeclarativeMacro::parse_macro2(&tt, is_2021, new_meta_vars);
630644
mac
631645
}
@@ -677,7 +691,7 @@ fn macro_expand(
677691
let Some((macro_arg, undo_info)) = value else {
678692
return ExpandResult {
679693
value: Arc::new(tt::Subtree {
680-
delimiter: tt::Delimiter::DUMMY_INVISIBLE,
694+
delimiter: tt::Delimiter::invisible_spanned(loc.call_site),
681695
token_trees: Vec::new(),
682696
}),
683697
// FIXME: We should make sure to enforce an invariant that invalid macro
@@ -736,7 +750,7 @@ fn macro_expand(
736750
// Skip checking token tree limit for include! macro call
737751
if !loc.def.is_include() {
738752
// Set a hard limit for the expanded tt
739-
if let Err(value) = check_tt_count(&tt) {
753+
if let Err(value) = check_tt_count(&tt, loc.call_site) {
740754
return value;
741755
}
742756
}
@@ -749,7 +763,7 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
749763
let Some((macro_arg, undo_info)) = db.macro_arg(id).value else {
750764
return ExpandResult {
751765
value: Arc::new(tt::Subtree {
752-
delimiter: tt::Delimiter::DUMMY_INVISIBLE,
766+
delimiter: tt::Delimiter::invisible_spanned(loc.call_site),
753767
token_trees: Vec::new(),
754768
}),
755769
// FIXME: We should make sure to enforce an invariant that invalid macro
@@ -783,7 +797,7 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
783797
);
784798

785799
// Set a hard limit for the expanded tt
786-
if let Err(value) = check_tt_count(&tt) {
800+
if let Err(value) = check_tt_count(&tt, loc.call_site) {
787801
return value;
788802
}
789803

@@ -806,12 +820,12 @@ fn token_tree_to_syntax_node(
806820
mbe::token_tree_to_syntax_node(tt, entry_point)
807821
}
808822

809-
fn check_tt_count(tt: &tt::Subtree) -> Result<(), ExpandResult<Arc<tt::Subtree>>> {
823+
fn check_tt_count(tt: &tt::Subtree, call_site: Span) -> Result<(), ExpandResult<Arc<tt::Subtree>>> {
810824
let count = tt.count();
811825
if TOKEN_LIMIT.check(count).is_err() {
812826
Err(ExpandResult {
813827
value: Arc::new(tt::Subtree {
814-
delimiter: tt::Delimiter::DUMMY_INVISIBLE,
828+
delimiter: tt::Delimiter::invisible_spanned(call_site),
815829
token_trees: vec![],
816830
}),
817831
err: Some(ExpandError::other(format!(

crates/hir-expand/src/eager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ pub fn expand_eager_macro_input(
8282
return ExpandResult { value: None, err };
8383
};
8484

85-
let mut subtree = mbe::syntax_node_to_token_tree(&expanded_eager_input, arg_map);
85+
let mut subtree = mbe::syntax_node_to_token_tree(&expanded_eager_input, arg_map, call_site);
8686

87-
subtree.delimiter = crate::tt::Delimiter::DUMMY_INVISIBLE;
87+
subtree.delimiter.kind = crate::tt::DelimiterKind::Invisible;
8888

8989
let loc = MacroCallLoc {
9090
def,

0 commit comments

Comments
 (0)