internal: more macro tests

This commit is contained in:
Aleksey Kladov 2022-01-02 17:18:21 +03:00
parent 7de4b964a9
commit 7989d567e2
5 changed files with 76 additions and 22 deletions

View File

@ -85,26 +85,6 @@ impl From<Subtree> for TokenTree {
);
}
#[test]
fn expansion_does_not_parse_as_expression() {
check(
r#"
macro_rules! stmts {
() => { let _ = 0; }
}
fn f() { let _ = stmts!(); }
"#,
expect![[r#"
macro_rules! stmts {
() => { let _ = 0; }
}
fn f() { let _ = /* error: could not convert tokens */; }
"#]],
)
}
#[test]
fn wrong_nesting_level() {
check(

View File

@ -2,7 +2,9 @@
//! Rather, token trees are an explicit bridge between the parser and
//! (procedural or declarative) macros.
//!
//! This module tests tt <-> syntax tree conversion specifically
//! This module tests tt <-> syntax tree conversion specifically. In particular,
//! it, among other things, check that we convert `tt` to the right kind of
//! syntax node depending on the macro call-site.
use expect_test::expect;
use crate::macro_expansion_tests::check;
@ -100,3 +102,49 @@ macro_rules! m2 { ($x:ident) => {} }
"#]],
)
}
#[test]
fn expansion_does_not_parse_as_expression() {
cov_mark::check!(expansion_does_not_parse_as_expression);
check(
r#"
macro_rules! stmts {
() => { let _ = 0; }
}
fn f() { let _ = stmts!(); }
"#,
expect![[r#"
macro_rules! stmts {
() => { let _ = 0; }
}
fn f() { let _ = /* error: could not convert tokens */; }
"#]],
)
}
#[test]
fn broken_pat() {
check(
r#"
macro_rules! m1 { () => (Some(x) left overs) }
macro_rules! m2 { () => ($) }
fn main() {
let m1!() = ();
let m2!/*+errors*/() = ();
}
"#,
expect![[r#"
macro_rules! m1 { () => (Some(x) left overs) }
macro_rules! m2 { () => ($) }
fn main() {
let Some(x) = ();
let /* parse error: expected pattern */
$ = ();
}
"#]],
)
}

View File

@ -67,6 +67,7 @@ pub fn token_tree_to_syntax_node(
}
}
if tree_sink.roots.len() != 1 {
cov_mark::hit!(expansion_does_not_parse_as_expression);
return Err(ExpandError::ConversionError);
}
//FIXME: would be cool to report errors

View File

@ -114,7 +114,13 @@ fn parse(entry: TopEntryPoint, text: &str) -> (String, bool) {
errors.push(format!("error {}: {}\n", pos, msg))
}
});
assert_eq!(len, text.len());
assert_eq!(
len,
text.len(),
"didn't parse all text.\nParsed:\n{}\n\nAll:\n{}\n",
&text[..len],
text
);
for (token, msg) in lexed.errors() {
let pos = lexed.text_start(token);

View File

@ -129,6 +129,25 @@ fn macro_items() {
);
}
#[test]
fn macro_pattern() {
check(
TopEntryPoint::Pattern,
"Some(_)",
expect![[r#"
TUPLE_STRUCT_PAT
PATH
PATH_SEGMENT
NAME_REF
IDENT "Some"
L_PAREN "("
WILDCARD_PAT
UNDERSCORE "_"
R_PAREN ")"
"#]],
);
}
#[track_caller]
fn check(entry: TopEntryPoint, input: &str, expect: expect_test::Expect) {
let (parsed, _errors) = super::parse(entry, input);