Skip to content

Commit 69f6145

Browse files
committed
Change look from a macro to a function.
Using `#[track_caller]` works if the assertion is moved outside of the closure.
1 parent 336e89b commit 69f6145

File tree

1 file changed

+63
-62
lines changed

1 file changed

+63
-62
lines changed

compiler/rustc_parse/src/parser/tests.rs

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,12 +1376,13 @@ fn ttdelim_span() {
13761376
});
13771377
}
13781378

1379-
// Uses a macro rather than a function so that failure messages mention the
1380-
// correct line in the test function.
1381-
macro_rules! look {
1382-
($p:ident, $dist:literal, $kind:expr) => {
1383-
$p.look_ahead($dist, |tok| assert_eq!($kind, tok.kind));
1384-
};
1379+
#[track_caller]
1380+
fn look(p: &Parser<'_>, dist: usize, kind: rustc_ast::token::TokenKind) {
1381+
// Do the `assert_eq` outside the closure so that `track_caller` works.
1382+
// (`#![feature(closure_track_caller)]` + `#[track_caller]` on the closure
1383+
// doesn't give the line number in the test below if the assertion fails.)
1384+
let tok = p.look_ahead(dist, |tok| tok.clone());
1385+
assert_eq!(kind, tok.kind);
13851386
}
13861387

13871388
#[test]
@@ -1397,63 +1398,63 @@ fn look_ahead() {
13971398
let mut p = string_to_parser(&psess, "fn f(x: u32) { x } struct S;".to_string());
13981399

13991400
// Current position is the `fn`.
1400-
look!(p, 0, token::Ident(kw::Fn, raw_no));
1401-
look!(p, 1, token::Ident(sym_f, raw_no));
1402-
look!(p, 2, token::OpenDelim(Delimiter::Parenthesis));
1403-
look!(p, 3, token::Ident(sym_x, raw_no));
1404-
look!(p, 4, token::Colon);
1405-
look!(p, 5, token::Ident(sym::u32, raw_no));
1406-
look!(p, 6, token::CloseDelim(Delimiter::Parenthesis));
1407-
look!(p, 7, token::OpenDelim(Delimiter::Brace));
1408-
look!(p, 8, token::Ident(sym_x, raw_no));
1409-
look!(p, 9, token::CloseDelim(Delimiter::Brace));
1410-
look!(p, 10, token::Ident(kw::Struct, raw_no));
1411-
look!(p, 11, token::Ident(sym_S, raw_no));
1412-
look!(p, 12, token::Semi);
1401+
look(&p, 0, token::Ident(kw::Fn, raw_no));
1402+
look(&p, 1, token::Ident(sym_f, raw_no));
1403+
look(&p, 2, token::OpenDelim(Delimiter::Parenthesis));
1404+
look(&p, 3, token::Ident(sym_x, raw_no));
1405+
look(&p, 4, token::Colon);
1406+
look(&p, 5, token::Ident(sym::u32, raw_no));
1407+
look(&p, 6, token::CloseDelim(Delimiter::Parenthesis));
1408+
look(&p, 7, token::OpenDelim(Delimiter::Brace));
1409+
look(&p, 8, token::Ident(sym_x, raw_no));
1410+
look(&p, 9, token::CloseDelim(Delimiter::Brace));
1411+
look(&p, 10, token::Ident(kw::Struct, raw_no));
1412+
look(&p, 11, token::Ident(sym_S, raw_no));
1413+
look(&p, 12, token::Semi);
14131414
// Any lookahead past the end of the token stream returns `Eof`.
1414-
look!(p, 13, token::Eof);
1415-
look!(p, 14, token::Eof);
1416-
look!(p, 15, token::Eof);
1417-
look!(p, 100, token::Eof);
1415+
look(&p, 13, token::Eof);
1416+
look(&p, 14, token::Eof);
1417+
look(&p, 15, token::Eof);
1418+
look(&p, 100, token::Eof);
14181419

14191420
// Move forward to the first `x`.
14201421
for _ in 0..3 {
14211422
p.bump();
14221423
}
1423-
look!(p, 0, token::Ident(sym_x, raw_no));
1424-
look!(p, 1, token::Colon);
1425-
look!(p, 2, token::Ident(sym::u32, raw_no));
1426-
look!(p, 3, token::CloseDelim(Delimiter::Parenthesis));
1427-
look!(p, 4, token::OpenDelim(Delimiter::Brace));
1428-
look!(p, 5, token::Ident(sym_x, raw_no));
1429-
look!(p, 6, token::CloseDelim(Delimiter::Brace));
1430-
look!(p, 7, token::Ident(kw::Struct, raw_no));
1431-
look!(p, 8, token::Ident(sym_S, raw_no));
1432-
look!(p, 9, token::Semi);
1433-
look!(p, 10, token::Eof);
1434-
look!(p, 11, token::Eof);
1435-
look!(p, 100, token::Eof);
1424+
look(&p, 0, token::Ident(sym_x, raw_no));
1425+
look(&p, 1, token::Colon);
1426+
look(&p, 2, token::Ident(sym::u32, raw_no));
1427+
look(&p, 3, token::CloseDelim(Delimiter::Parenthesis));
1428+
look(&p, 4, token::OpenDelim(Delimiter::Brace));
1429+
look(&p, 5, token::Ident(sym_x, raw_no));
1430+
look(&p, 6, token::CloseDelim(Delimiter::Brace));
1431+
look(&p, 7, token::Ident(kw::Struct, raw_no));
1432+
look(&p, 8, token::Ident(sym_S, raw_no));
1433+
look(&p, 9, token::Semi);
1434+
look(&p, 10, token::Eof);
1435+
look(&p, 11, token::Eof);
1436+
look(&p, 100, token::Eof);
14361437

14371438
// Move forward to the `;`.
14381439
for _ in 0..9 {
14391440
p.bump();
14401441
}
1441-
look!(p, 0, token::Semi);
1442+
look(&p, 0, token::Semi);
14421443
// Any lookahead past the end of the token stream returns `Eof`.
1443-
look!(p, 1, token::Eof);
1444-
look!(p, 100, token::Eof);
1444+
look(&p, 1, token::Eof);
1445+
look(&p, 100, token::Eof);
14451446

14461447
// Move one past the `;`, i.e. past the end of the token stream.
14471448
p.bump();
1448-
look!(p, 0, token::Eof);
1449-
look!(p, 1, token::Eof);
1450-
look!(p, 100, token::Eof);
1449+
look(&p, 0, token::Eof);
1450+
look(&p, 1, token::Eof);
1451+
look(&p, 100, token::Eof);
14511452

14521453
// Bumping after Eof is idempotent.
14531454
p.bump();
1454-
look!(p, 0, token::Eof);
1455-
look!(p, 1, token::Eof);
1456-
look!(p, 100, token::Eof);
1455+
look(&p, 0, token::Eof);
1456+
look(&p, 1, token::Eof);
1457+
look(&p, 100, token::Eof);
14571458
});
14581459
}
14591460

@@ -1476,24 +1477,24 @@ fn look_ahead_non_outermost_stream() {
14761477
for _ in 0..3 {
14771478
p.bump();
14781479
}
1479-
look!(p, 0, token::Ident(kw::Fn, raw_no));
1480-
look!(p, 1, token::Ident(sym_f, raw_no));
1481-
look!(p, 2, token::OpenDelim(Delimiter::Parenthesis));
1482-
look!(p, 3, token::Ident(sym_x, raw_no));
1483-
look!(p, 4, token::Colon);
1484-
look!(p, 5, token::Ident(sym::u32, raw_no));
1485-
look!(p, 6, token::CloseDelim(Delimiter::Parenthesis));
1486-
look!(p, 7, token::OpenDelim(Delimiter::Brace));
1487-
look!(p, 8, token::Ident(sym_x, raw_no));
1488-
look!(p, 9, token::CloseDelim(Delimiter::Brace));
1489-
look!(p, 10, token::Ident(kw::Struct, raw_no));
1490-
look!(p, 11, token::Ident(sym_S, raw_no));
1491-
look!(p, 12, token::Semi);
1492-
look!(p, 13, token::CloseDelim(Delimiter::Brace));
1480+
look(&p, 0, token::Ident(kw::Fn, raw_no));
1481+
look(&p, 1, token::Ident(sym_f, raw_no));
1482+
look(&p, 2, token::OpenDelim(Delimiter::Parenthesis));
1483+
look(&p, 3, token::Ident(sym_x, raw_no));
1484+
look(&p, 4, token::Colon);
1485+
look(&p, 5, token::Ident(sym::u32, raw_no));
1486+
look(&p, 6, token::CloseDelim(Delimiter::Parenthesis));
1487+
look(&p, 7, token::OpenDelim(Delimiter::Brace));
1488+
look(&p, 8, token::Ident(sym_x, raw_no));
1489+
look(&p, 9, token::CloseDelim(Delimiter::Brace));
1490+
look(&p, 10, token::Ident(kw::Struct, raw_no));
1491+
look(&p, 11, token::Ident(sym_S, raw_no));
1492+
look(&p, 12, token::Semi);
1493+
look(&p, 13, token::CloseDelim(Delimiter::Brace));
14931494
// Any lookahead past the end of the token stream returns `Eof`.
1494-
look!(p, 14, token::Eof);
1495-
look!(p, 15, token::Eof);
1496-
look!(p, 100, token::Eof);
1495+
look(&p, 14, token::Eof);
1496+
look(&p, 15, token::Eof);
1497+
look(&p, 100, token::Eof);
14971498
});
14981499
}
14991500

0 commit comments

Comments
 (0)