Skip to content

Commit

Permalink
Reject trailing tokens after declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaesar committed Dec 1, 2022
1 parent 13bd5ea commit 3eddff6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/parse.rs
Expand Up @@ -60,7 +60,14 @@ use std::iter::Peekable;

pub fn parse_declaration(tokens: TokenStream) -> Result<Declaration, Error> {
let mut tokens = tokens.into_iter().peekable();
parse_declaration_tokens(&mut tokens)
let declaration = parse_declaration_tokens(&mut tokens);
if tokens.peek().is_some() {
panic!(
"cannot parse declaration, unexpected trailing tokens: {}",
tokens.collect::<TokenStream>()
);
}
declaration
}

pub(crate) fn parse_declaration_tokens(
Expand Down
6 changes: 6 additions & 0 deletions src/tests.rs
Expand Up @@ -122,6 +122,12 @@ fn parse_empty_enum() {
assert_debug_snapshot!(enum_type);
}

#[test]
#[should_panic]
fn reject_trailing_junk() {
dbg!(parse_declaration(quote::quote! {struct Good {} bad})).unwrap();
}

// ==========
// VISIBILITY
// ==========
Expand Down

0 comments on commit 3eddff6

Please sign in to comment.