Skip to content

Commit fbed308

Browse files
committed
Auto merge of #17799 - Veykril:syntax-bridge, r=Veykril
Split out syntax-bridge into a separate crate This functionality is not really tied to mbe macros, so imo it has no place in that crate.
2 parents b4571d7 + d2dd4f6 commit fbed308

File tree

30 files changed

+268
-140
lines changed

30 files changed

+268
-140
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ salsa = { path = "./crates/salsa", version = "0.0.0" }
7777
span = { path = "./crates/span", version = "0.0.0" }
7878
stdx = { path = "./crates/stdx", version = "0.0.0" }
7979
syntax = { path = "./crates/syntax", version = "0.0.0" }
80+
syntax-bridge = { path = "./crates/syntax-bridge", version = "0.0.0" }
8081
text-edit = { path = "./crates/text-edit", version = "0.0.0" }
8182
toolchain = { path = "./crates/toolchain", version = "0.0.0" }
8283
tt = { path = "./crates/tt", version = "0.0.0" }

crates/cfg/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ arbitrary = "1.3.2"
2828
derive_arbitrary = "1.3.2"
2929

3030
# local deps
31-
mbe.workspace = true
31+
syntax-bridge.workspace = true
3232
syntax.workspace = true
3333

3434
[lints]

crates/cfg/src/tests.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use arbitrary::{Arbitrary, Unstructured};
22
use expect_test::{expect, Expect};
33
use intern::Symbol;
4-
use mbe::{syntax_node_to_token_tree, DocCommentDesugarMode, DummyTestSpanMap, DUMMY};
54
use syntax::{ast, AstNode, Edition};
5+
use syntax_bridge::{
6+
dummy_test_span_utils::{DummyTestSpanMap, DUMMY},
7+
syntax_node_to_token_tree, DocCommentDesugarMode,
8+
};
69

710
use crate::{CfgAtom, CfgExpr, CfgOptions, DnfExpr};
811

crates/hir-def/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ expect-test.workspace = true
5252
# local deps
5353
test-utils.workspace = true
5454
test-fixture.workspace = true
55-
55+
syntax-bridge.workspace = true
5656
[features]
5757
in-rust-tree = ["hir-expand/in-rust-tree"]
5858

crates/hir-def/src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,9 @@ mod tests {
657657
use triomphe::Arc;
658658

659659
use hir_expand::span_map::{RealSpanMap, SpanMap};
660-
use mbe::{syntax_node_to_token_tree, DocCommentDesugarMode};
661660
use span::FileId;
662661
use syntax::{ast, AstNode, TextRange};
662+
use syntax_bridge::{syntax_node_to_token_tree, DocCommentDesugarMode};
663663

664664
use crate::attr::{DocAtom, DocExpr};
665665

crates/hir-def/src/macro_expansion_tests/mbe.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,6 @@ macro_rules! m {
12011201

12021202
#[test]
12031203
fn test_meta_doc_comments() {
1204-
cov_mark::check!(test_meta_doc_comments);
12051204
check(
12061205
r#"
12071206
macro_rules! m {

crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
317317
_: Span,
318318
_: Span,
319319
) -> Result<Subtree, ProcMacroExpansionError> {
320-
let (parse, _) = ::mbe::token_tree_to_syntax_node(
320+
let (parse, _) = syntax_bridge::token_tree_to_syntax_node(
321321
subtree,
322-
::mbe::TopEntryPoint::MacroItems,
322+
syntax_bridge::TopEntryPoint::MacroItems,
323323
span::Edition::CURRENT,
324324
);
325325
if parse.errors().is_empty() {

crates/hir-expand/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mbe.workspace = true
3333
limit.workspace = true
3434
span.workspace = true
3535
parser.workspace = true
36+
syntax-bridge.workspace = true
3637

3738
[dev-dependencies]
3839
expect-test = "1.4.0"

crates/hir-expand/src/attrs.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ use cfg::CfgExpr;
66
use either::Either;
77
use intern::{sym, Interned, Symbol};
88

9-
use mbe::{
10-
desugar_doc_comment_text, syntax_node_to_token_tree, DelimiterKind, DocCommentDesugarMode,
11-
Punct,
12-
};
9+
use mbe::{DelimiterKind, Punct};
1310
use smallvec::{smallvec, SmallVec};
1411
use span::{Span, SyntaxContextId};
1512
use syntax::unescape;
1613
use syntax::{ast, match_ast, AstNode, AstToken, SyntaxNode};
14+
use syntax_bridge::{desugar_doc_comment_text, syntax_node_to_token_tree, DocCommentDesugarMode};
1715
use triomphe::ThinArc;
1816

1917
use crate::name::Name;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
33
use intern::sym;
44
use itertools::izip;
5-
use mbe::DocCommentDesugarMode;
65
use rustc_hash::FxHashSet;
76
use span::{MacroCallId, Span};
87
use stdx::never;
8+
use syntax_bridge::DocCommentDesugarMode;
99
use tracing::debug;
1010

1111
use crate::{
@@ -209,9 +209,9 @@ struct BasicAdtInfo {
209209
}
210210

211211
fn parse_adt(tt: &tt::Subtree, call_site: Span) -> Result<BasicAdtInfo, ExpandError> {
212-
let (parsed, tm) = &mbe::token_tree_to_syntax_node(
212+
let (parsed, tm) = &syntax_bridge::token_tree_to_syntax_node(
213213
tt,
214-
mbe::TopEntryPoint::MacroItems,
214+
syntax_bridge::TopEntryPoint::MacroItems,
215215
parser::Edition::CURRENT_FIXME,
216216
);
217217
let macro_items = ast::MacroItems::cast(parsed.syntax_node())
@@ -268,7 +268,7 @@ fn parse_adt(tt: &tt::Subtree, call_site: Span) -> Result<BasicAdtInfo, ExpandEr
268268
match this {
269269
Some(it) => {
270270
param_type_set.insert(it.as_name());
271-
mbe::syntax_node_to_token_tree(
271+
syntax_bridge::syntax_node_to_token_tree(
272272
it.syntax(),
273273
tm,
274274
call_site,
@@ -282,7 +282,7 @@ fn parse_adt(tt: &tt::Subtree, call_site: Span) -> Result<BasicAdtInfo, ExpandEr
282282
};
283283
let bounds = match &param {
284284
ast::TypeOrConstParam::Type(it) => it.type_bound_list().map(|it| {
285-
mbe::syntax_node_to_token_tree(
285+
syntax_bridge::syntax_node_to_token_tree(
286286
it.syntax(),
287287
tm,
288288
call_site,
@@ -295,7 +295,7 @@ fn parse_adt(tt: &tt::Subtree, call_site: Span) -> Result<BasicAdtInfo, ExpandEr
295295
let ty = param
296296
.ty()
297297
.map(|ty| {
298-
mbe::syntax_node_to_token_tree(
298+
syntax_bridge::syntax_node_to_token_tree(
299299
ty.syntax(),
300300
tm,
301301
call_site,
@@ -316,7 +316,7 @@ fn parse_adt(tt: &tt::Subtree, call_site: Span) -> Result<BasicAdtInfo, ExpandEr
316316
let where_clause = if let Some(w) = where_clause {
317317
w.predicates()
318318
.map(|it| {
319-
mbe::syntax_node_to_token_tree(
319+
syntax_bridge::syntax_node_to_token_tree(
320320
it.syntax(),
321321
tm,
322322
call_site,
@@ -353,7 +353,7 @@ fn parse_adt(tt: &tt::Subtree, call_site: Span) -> Result<BasicAdtInfo, ExpandEr
353353
param_type_set.contains(&name).then_some(p)
354354
})
355355
.map(|it| {
356-
mbe::syntax_node_to_token_tree(
356+
syntax_bridge::syntax_node_to_token_tree(
357357
it.syntax(),
358358
tm,
359359
call_site,

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use base_db::AnchoredPath;
44
use cfg::CfgExpr;
55
use either::Either;
66
use intern::{sym, Symbol};
7-
use mbe::{parse_exprs_with_sep, parse_to_token_tree, DelimiterKind};
7+
use mbe::{expect_fragment, DelimiterKind};
88
use span::{Edition, EditionedFileId, Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
99
use stdx::format_to;
1010
use syntax::{
1111
format_smolstr,
1212
unescape::{unescape_byte, unescape_char, unescape_unicode, Mode},
1313
};
14+
use syntax_bridge::parse_to_token_tree;
1415

1516
use crate::{
1617
builtin::quote::{dollar_crate, quote},
@@ -228,20 +229,22 @@ fn assert_expand(
228229
span: Span,
229230
) -> ExpandResult<tt::Subtree> {
230231
let call_site_span = span_with_call_site_ctxt(db, span, id);
231-
let args = parse_exprs_with_sep(tt, ',', call_site_span, Edition::CURRENT_FIXME);
232+
233+
let mut iter = ::tt::iter::TtIter::new(tt);
234+
235+
let cond = expect_fragment(
236+
&mut iter,
237+
parser::PrefixEntryPoint::Expr,
238+
db.crate_graph()[id.lookup(db).krate].edition,
239+
tt::DelimSpan { open: tt.delimiter.open, close: tt.delimiter.close },
240+
);
241+
_ = iter.expect_char(',');
242+
let rest = iter.as_slice();
243+
232244
let dollar_crate = dollar_crate(span);
233-
let expanded = match &*args {
234-
[cond, panic_args @ ..] => {
235-
let comma = tt::Subtree {
236-
delimiter: tt::Delimiter::invisible_spanned(call_site_span),
237-
token_trees: Box::new([tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct {
238-
char: ',',
239-
spacing: tt::Spacing::Alone,
240-
span: call_site_span,
241-
}))]),
242-
};
243-
let cond = cond.clone();
244-
let panic_args = itertools::Itertools::intersperse(panic_args.iter().cloned(), comma);
245+
let expanded = match cond.value {
246+
Some(cond) => {
247+
let panic_args = rest.iter().cloned();
245248
let mac = if use_panic_2021(db, span) {
246249
quote! {call_site_span => #dollar_crate::panic::panic_2021!(##panic_args) }
247250
} else {
@@ -253,10 +256,13 @@ fn assert_expand(
253256
}
254257
}}
255258
}
256-
[] => quote! {call_site_span =>{}},
259+
None => quote! {call_site_span =>{}},
257260
};
258261

259-
ExpandResult::ok(expanded)
262+
match cond.err {
263+
Some(err) => ExpandResult::new(expanded, err.into()),
264+
None => ExpandResult::ok(expanded),
265+
}
260266
}
261267

262268
fn file_expand(

crates/hir-expand/src/db.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
use base_db::{salsa, CrateId, SourceDatabase};
44
use either::Either;
55
use limit::Limit;
6-
use mbe::{syntax_node_to_token_tree, DocCommentDesugarMode, MatchedArmIndex};
6+
use mbe::MatchedArmIndex;
77
use rustc_hash::FxHashSet;
88
use span::{AstIdMap, EditionedFileId, Span, SyntaxContextData, SyntaxContextId};
99
use syntax::{ast, AstNode, Parse, SyntaxElement, SyntaxError, SyntaxNode, SyntaxToken, T};
10+
use syntax_bridge::{syntax_node_to_token_tree, DocCommentDesugarMode};
1011
use triomphe::Arc;
1112

1213
use crate::{
@@ -165,7 +166,7 @@ pub fn expand_speculative(
165166
// Build the subtree and token mapping for the speculative args
166167
let (mut tt, undo_info) = match loc.kind {
167168
MacroCallKind::FnLike { .. } => (
168-
mbe::syntax_node_to_token_tree(
169+
syntax_bridge::syntax_node_to_token_tree(
169170
speculative_args,
170171
span_map,
171172
span,
@@ -178,7 +179,7 @@ pub fn expand_speculative(
178179
SyntaxFixupUndoInfo::NONE,
179180
),
180181
MacroCallKind::Attr { .. } if loc.def.is_attribute_derive() => (
181-
mbe::syntax_node_to_token_tree(
182+
syntax_bridge::syntax_node_to_token_tree(
182183
speculative_args,
183184
span_map,
184185
span,
@@ -213,7 +214,7 @@ pub fn expand_speculative(
213214
fixups.remove.extend(censor_cfg);
214215

215216
(
216-
mbe::syntax_node_to_token_tree_modified(
217+
syntax_bridge::syntax_node_to_token_tree_modified(
217218
speculative_args,
218219
span_map,
219220
fixups.append,
@@ -459,7 +460,7 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
459460
return dummy_tt(kind);
460461
}
461462

462-
let mut tt = mbe::syntax_node_to_token_tree(
463+
let mut tt = syntax_bridge::syntax_node_to_token_tree(
463464
tt.syntax(),
464465
map.as_ref(),
465466
span,
@@ -515,7 +516,7 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
515516
fixups.remove.extend(censor_cfg);
516517

517518
(
518-
mbe::syntax_node_to_token_tree_modified(
519+
syntax_bridge::syntax_node_to_token_tree_modified(
519520
syntax,
520521
map,
521522
fixups.append,
@@ -720,13 +721,13 @@ fn token_tree_to_syntax_node(
720721
edition: parser::Edition,
721722
) -> (Parse<SyntaxNode>, ExpansionSpanMap) {
722723
let entry_point = match expand_to {
723-
ExpandTo::Statements => mbe::TopEntryPoint::MacroStmts,
724-
ExpandTo::Items => mbe::TopEntryPoint::MacroItems,
725-
ExpandTo::Pattern => mbe::TopEntryPoint::Pattern,
726-
ExpandTo::Type => mbe::TopEntryPoint::Type,
727-
ExpandTo::Expr => mbe::TopEntryPoint::Expr,
724+
ExpandTo::Statements => syntax_bridge::TopEntryPoint::MacroStmts,
725+
ExpandTo::Items => syntax_bridge::TopEntryPoint::MacroItems,
726+
ExpandTo::Pattern => syntax_bridge::TopEntryPoint::Pattern,
727+
ExpandTo::Type => syntax_bridge::TopEntryPoint::Type,
728+
ExpandTo::Expr => syntax_bridge::TopEntryPoint::Expr,
728729
};
729-
mbe::token_tree_to_syntax_node(tt, entry_point, edition)
730+
syntax_bridge::token_tree_to_syntax_node(tt, entry_point, edition)
730731
}
731732

732733
fn check_tt_count(tt: &tt::Subtree) -> Result<(), ExpandResult<()>> {

0 commit comments

Comments
 (0)