dtolnay / syn

Parser for Rust source code
Apache License 2.0
2.78k stars 306 forks source link

Improve span returned by Cursor::span at end of a Group's contents #1680

Closed dtolnay closed 1 month ago

dtolnay commented 1 month ago

Example:

// src/main.rs

repro::repro!([1]);

fn main() {}
// src/lib.rs

use proc_macro::TokenStream;
use syn::parse::{Error, ParseStream, Result};
use syn::{bracketed, parse_macro_input};

fn parse(input: ParseStream) -> Result<()> {
    let bracketed;
    bracketed!(bracketed in input);
    bracketed.step(|cursor| {
        let mut cursor = *cursor;
        while let Some((_literal, rest)) = cursor.literal() {
            cursor = rest;
        }
        Err(Error::new(cursor.span(), "literal parser is insatiable"))
    })
}

#[proc_macro]
pub fn repro(input: TokenStream) -> TokenStream {
    parse_macro_input!(input with parse);
    TokenStream::new()
}

Diagnostic before this PR: no indication of where in the input we were parsing.

error: literal parser is insatiable
 --> src/main.rs:3:1
  |
3 | repro::repro!([1]);
  | ^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the macro `repro::repro` (in Nightly builds, run with -Z macro-backtrace for more info)

Diagnostic after:

error: literal parser is insatiable
 --> src/main.rs:3:17
  |
3 | repro::repro!([1]);
  |                 ^