Skip to content

Commit f8262c1

Browse files
Merge pull request #21705 from A4-Tacks/cfg-select-fallback
fix: cfg_select supports non token-tree tokens
2 parents 80af448 + 95fcc2d commit f8262c1

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,12 @@ cfg_select! {
568568
_ => { fn true_2() {} }
569569
}
570570
571+
const _: ((),) = cfg_select! { _ => ((), ) };
572+
const _: i32 = cfg_select! { true => 2 + 3, _ => 3 + 4 };
573+
const _: i32 = cfg_select! { false => 2 + 3, _ => 3 + 4 };
574+
const _: bool = cfg_select! { _ => 2 < 3 };
575+
const _: bool = cfg_select! { true => foo::<(), fn() -> Foo<i32, i64>>(1,), _ => false };
576+
571577
cfg_select! {
572578
false => { fn false_3() {} }
573579
}
@@ -589,6 +595,12 @@ fn true_1() {}
589595
590596
fn true_2() {}
591597
598+
const _: ((),) = ((), );
599+
const _: i32 = 2+3;
600+
const _: i32 = 3+4;
601+
const _: bool = 2<3;
602+
const _: bool = foo::<(), fn() -> Foo<i32, i64>>(1, );
603+
592604
/* error: none of the predicates in this `cfg_select` evaluated to true */
593605
594606
/* error: expected `=>` after cfg expression */

src/tools/rust-analyzer/crates/hir-expand/src/builtin/fn_macro.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,16 +381,40 @@ fn cfg_select_expand(
381381
);
382382
}
383383
}
384-
let expand_to_if_active = match iter.next() {
385-
Some(tt::TtElement::Subtree(_, tt)) => tt.remaining(),
386-
_ => {
384+
let expand_to_if_active = match iter.peek() {
385+
Some(tt::TtElement::Subtree(sub, tt)) if sub.delimiter.kind == DelimiterKind::Brace => {
386+
iter.next();
387+
tt.remaining()
388+
}
389+
None | Some(TtElement::Leaf(tt::Leaf::Punct(tt::Punct { char: ',', .. }))) => {
387390
let err_span = iter.peek().map(|it| it.first_span()).unwrap_or(span);
391+
iter.next();
388392
return ExpandResult::new(
389393
tt::TopSubtree::empty(tt::DelimSpan::from_single(span)),
390394
ExpandError::other(err_span, "expected a token tree after `=>`"),
391395
);
392396
}
397+
Some(_) => {
398+
let expr = expect_fragment(
399+
db,
400+
&mut iter,
401+
parser::PrefixEntryPoint::Expr,
402+
tt.top_subtree().delimiter.delim_span(),
403+
);
404+
if let Some(err) = expr.err {
405+
return ExpandResult::new(
406+
tt::TopSubtree::empty(tt::DelimSpan::from_single(span)),
407+
err.into(),
408+
);
409+
}
410+
expr.value
411+
}
393412
};
413+
if let Some(TtElement::Leaf(tt::Leaf::Punct(p))) = iter.peek()
414+
&& p.char == ','
415+
{
416+
iter.next();
417+
}
394418

395419
if expand_to.is_none() && active {
396420
expand_to = Some(expand_to_if_active);

0 commit comments

Comments
 (0)