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

Incorrect span columns for < open angle bracket symbol #58958

Closed
chinedufn opened this issue Mar 6, 2019 · 2 comments
Closed

Incorrect span columns for < open angle bracket symbol #58958

chinedufn opened this issue Mar 6, 2019 · 2 comments

Comments

@chinedufn
Copy link
Contributor

chinedufn commented Mar 6, 2019

This might be an issue for other tokens as well, I just ran into it for < open angle bracket.

The spans for the < token have incorrect columns. They appear to be one less than what the real value should be.

I've reproduced the issue in a minimal repository on GitHub.

git clone https://github.com/chinedufn/rustc-span-error
cd rustc-span-error
cargo test # You'll see a failing test with the incorrect span column

I'll paste the code below since it's short:

src/lib.rs

#![feature(proc_macro_hygiene)]

use proc_macro_example::example;

fn foo () {
    example! {
ABC<
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn foo () {
        
    }
}

proc macro

#![feature(proc_macro_span)]

extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;

#[proc_macro]
pub fn example (input: TokenStream) -> TokenStream {
    let mut tokens = input.into_iter();

    // Assert ABC span start and end columns

    let abc = tokens.next().unwrap();
    assert_eq!(&abc.to_string(), "ABC");

    assert_eq!(abc.span().start().column, 0);
    assert_eq!(abc.span().end().column, 2);

    // Assert `<` span start and end columns.
    // This will fail
    let bracket = tokens.next().unwrap();
    assert_eq!(&bracket.to_string(), "<");

    assert_eq!(bracket.span().end().column, 3);

    let empty = quote! {
    };

    empty.into()
}
@dtolnay
Copy link
Member

dtolnay commented Mar 6, 2019

I think the compiler is correct here. In your standalone repo, the assertion that is failing is the following one (not the one you commented):

assert_eq!(abc.span().end().column, 2);

because the end of ABC is in column 3.

 A B C <
↑ ↑ ↑ ↑ ↑
0 1 2 3 4

ABC goes from 0 to 3, < goes from 3 to 4.

In dtolnay/proc-macro2#166 (comment):

 T  a  g     <  /
↑  ↑  ↑  ↑  ↑  ↑  ↑
33 34 35 36 37 38 39

Tag goes from 33 to 36, < goes from 37 to 38.

FYI @alexcrichton

@chinedufn
Copy link
Contributor Author

Ahhh sorry I misunderstood how columns worked. Thanks so much!

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

No branches or pull requests

2 participants