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

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

Merged
merged 1 commit into from
Jun 20, 2024

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Jun 20, 2024

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]);
  |                 ^

@dtolnay dtolnay merged commit 47b6aa0 into master Jun 20, 2024
29 checks passed
@dtolnay dtolnay deleted the endspan branch June 20, 2024 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant