Skip to content

Commit 38e696f

Browse files
authored
Merge pull request rust-lang#18929 from ChayimFriedman2/i-acknowledge-defeat
fix: Fix another bug when reaching macro expansion limit caused a stack overflow
2 parents 97522d1 + 70309b1 commit 38e696f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,30 @@ mod prim_never {}
291291
"#,
292292
);
293293
}
294+
295+
#[test]
296+
fn no_stack_overflow_for_missing_binding() {
297+
check_diagnostics(
298+
r#"
299+
#[macro_export]
300+
macro_rules! boom {
301+
(
302+
$($code:literal),+,
303+
$(param: $param:expr,)?
304+
) => {{
305+
let _ = $crate::boom!(@param $($param)*);
306+
}};
307+
(@param) => { () };
308+
(@param $param:expr) => { $param };
309+
}
310+
311+
fn it_works() {
312+
// NOTE: there is an error, but RA crashes before showing it
313+
boom!("RAND", param: c7.clone());
314+
// ^^^^^ error: expected literal
315+
}
316+
317+
"#,
318+
);
319+
}
294320
}

src/tools/rust-analyzer/crates/mbe/src/expander/transcriber.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ fn expand_repeat(
448448
let mut counter = 0;
449449
let mut err = None;
450450

451+
let initial_restore_point = builder.restore_point();
451452
let mut restore_point = builder.restore_point();
452453
loop {
453454
let ExpandResult { value: (), err: e } =
@@ -465,6 +466,10 @@ fn expand_repeat(
465466

466467
counter += 1;
467468
if counter == limit {
469+
// FIXME: This is a bug here, we get here when we shouldn't, see https://github.com/rust-lang/rust-analyzer/issues/18910.
470+
// If we don't restore we emit a lot of nodes which causes a stack overflow down the road. For now just ignore them,
471+
// there is always an error here anyway.
472+
builder.restore(initial_restore_point);
468473
err = Some(ExpandError::new(ctx.call_site, ExpandErrorKind::LimitExceeded));
469474
break;
470475
}

0 commit comments

Comments
 (0)