Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject trailing tokens after declaration #42

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
11 changes: 11 additions & 0 deletions src/tests.rs
Expand Up @@ -122,6 +122,17 @@ fn parse_empty_enum() {
assert_debug_snapshot!(enum_type);
}

#[test]
#[should_panic]
fn reject_trailing_tokens() {
let declaration = parse_declaration_checked(quote::quote! {
struct Good {}
trailing junk
});

println!("This should have panicked: {:#?}", declaration);
}

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