Skip to content

Commit 5a292c9

Browse files
committed
Allow macro expansions into RestPat in tuple args work as ellipsis like plain RestPat
1 parent a3d6efc commit 5a292c9

File tree

2 files changed

+108
-5
lines changed

2 files changed

+108
-5
lines changed

src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::mem;
55

66
use base_db::CrateId;
7+
use either::Either;
78
use hir_expand::{
89
name::{name, AsName, Name},
910
ExpandError, InFile,
@@ -1432,14 +1433,12 @@ impl ExprCollector<'_> {
14321433
has_leading_comma: bool,
14331434
binding_list: &mut BindingList,
14341435
) -> (Box<[PatId]>, Option<usize>) {
1436+
let args: Vec<_> = args.map(|p| self.collect_pat_possibly_rest(p, binding_list)).collect();
14351437
// Find the location of the `..`, if there is one. Note that we do not
14361438
// consider the possibility of there being multiple `..` here.
1437-
let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::RestPat(_)));
1439+
let ellipsis = args.iter().position(|p| p.is_right());
14381440
// We want to skip the `..` pattern here, since we account for it above.
1439-
let mut args: Vec<_> = args
1440-
.filter(|p| !matches!(p, ast::Pat::RestPat(_)))
1441-
.map(|p| self.collect_pat(p, binding_list))
1442-
.collect();
1441+
let mut args: Vec<_> = args.into_iter().filter_map(Either::left).collect();
14431442
// if there is a leading comma, the user is most likely to type out a leading pattern
14441443
// so we insert a missing pattern at the beginning for IDE features
14451444
if has_leading_comma {
@@ -1449,6 +1448,41 @@ impl ExprCollector<'_> {
14491448
(args.into_boxed_slice(), ellipsis)
14501449
}
14511450

1451+
// `collect_pat` rejects `ast::Pat::RestPat`, but it should be handled in some cases that
1452+
// it is the macro expansion result of an arg sub-pattern in a slice or tuple pattern.
1453+
fn collect_pat_possibly_rest(
1454+
&mut self,
1455+
pat: ast::Pat,
1456+
binding_list: &mut BindingList,
1457+
) -> Either<PatId, ()> {
1458+
match &pat {
1459+
ast::Pat::RestPat(_) => Either::Right(()),
1460+
ast::Pat::MacroPat(mac) => match mac.macro_call() {
1461+
Some(call) => {
1462+
let macro_ptr = AstPtr::new(&call);
1463+
let src = self.expander.in_file(AstPtr::new(&pat));
1464+
let pat =
1465+
self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| {
1466+
if let Some(expanded_pat) = expanded_pat {
1467+
this.collect_pat_possibly_rest(expanded_pat, binding_list)
1468+
} else {
1469+
Either::Left(this.missing_pat())
1470+
}
1471+
});
1472+
if let Some(pat) = pat.left() {
1473+
self.source_map.pat_map.insert(src, pat);
1474+
}
1475+
pat
1476+
}
1477+
None => {
1478+
let ptr = AstPtr::new(&pat);
1479+
Either::Left(self.alloc_pat(Pat::Missing, ptr))
1480+
}
1481+
},
1482+
_ => Either::Left(self.collect_pat(pat, binding_list)),
1483+
}
1484+
}
1485+
14521486
// endregion: patterns
14531487

14541488
/// Returns `None` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `Some(())` when

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,75 @@ impl Foo {
256256
);
257257
}
258258

259+
#[test]
260+
fn rest_pat_in_macro_expansion() {
261+
check_diagnostics(
262+
r#"
263+
// issue #17292
264+
#![allow(dead_code)]
265+
266+
macro_rules! replace_with_2_dots {
267+
( $( $input:tt )* ) => {
268+
..
269+
};
270+
}
271+
272+
macro_rules! enum_str {
273+
(
274+
$(
275+
$variant:ident (
276+
$( $tfield:ty ),*
277+
)
278+
)
279+
,
280+
*
281+
) => {
282+
enum Foo {
283+
$(
284+
$variant ( $( $tfield ),* ),
285+
)*
286+
}
287+
288+
impl Foo {
289+
fn variant_name_as_str(&self) -> &str {
290+
match self {
291+
$(
292+
Self::$variant ( replace_with_2_dots!( $( $tfield ),* ) )
293+
=> "",
294+
)*
295+
}
296+
}
297+
}
298+
};
299+
}
300+
301+
enum_str! {
302+
TupleVariant1(i32),
303+
TupleVariant2(),
304+
TupleVariant3(i8,u8,i128)
305+
}
306+
"#,
307+
);
308+
309+
check_diagnostics(
310+
r#"
311+
#![allow(dead_code)]
312+
macro_rules! two_dots1 {
313+
() => { .. };
314+
}
315+
316+
macro_rules! two_dots2 {
317+
() => { two_dots1!() };
318+
}
319+
320+
fn test() {
321+
let (_, _, two_dots1!()) = ((), 42);
322+
let (_, two_dots2!(), _) = (1, true, 2, false, (), (), 3);
323+
}
324+
"#,
325+
);
326+
}
327+
259328
#[test]
260329
fn varargs() {
261330
check_diagnostics(

0 commit comments

Comments
 (0)