Skip to content

Commit

Permalink
Check valid punctuation character in Punct::new
Browse files Browse the repository at this point in the history
Valid punctuation characters generated by:

    use proc_macro::{Punct, Spacing, TokenStream};
    use std::panic;

    #[proc_macro]
    pub fn punct_chars(_input: TokenStream) -> TokenStream {
        for ch in '\0'..=char::MAX {
            if let Ok(_) = panic::catch_unwind(|| {
                let _ = Punct::new(ch, Spacing::Alone);
            }) {
                eprintln!("{:?}", ch);
            }
        }
        TokenStream::new()
    }
  • Loading branch information
dtolnay committed Oct 7, 2024
1 parent d60aaad commit 98ea261
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,16 @@ impl Punct {
/// The returned `Punct` will have the default span of `Span::call_site()`
/// which can be further configured with the `set_span` method below.
pub fn new(ch: char, spacing: Spacing) -> Self {
Punct {
ch,
spacing,
span: Span::call_site(),
if let '!' | '#' | '$' | '%' | '&' | '\'' | '*' | '+' | ',' | '-' | '.' | '/' | ':' | ';'
| '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' = ch
{
Punct {
ch,
spacing,
span: Span::call_site(),
}
} else {
panic!("unsupported proc macro punctuation character {:?}", ch);
}
}

Expand Down

0 comments on commit 98ea261

Please sign in to comment.