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

Wrong span in proc macro for part of joint op #47226

Open
dtolnay opened this issue Jan 6, 2018 · 1 comment
Open

Wrong span in proc macro for part of joint op #47226

dtolnay opened this issue Jan 6, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros-1.2 Area: Declarative macros 1.2 A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Jan 6, 2018

I have the following proc macro called from the following crate. It is intended to display an error pointing to the first > of a >> joint op. Instead the error points to the whole >> pair of tokens.

#![feature(proc_macro)]

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro]
pub fn antoyo(input: TokenStream) -> TokenStream {
    let mut iter = input.into_iter();
    let tt = iter.next().unwrap();
    tt.span.error("first angle bracket").emit();
    assert!(iter.next().is_some());
    assert!(iter.next().is_none());
    TokenStream::empty()
}
#![feature(proc_macro)]

extern crate mac;

mac::antoyo!(>>);

fn main() {}
error: first angle bracket
 --> src/main.rs:5:14
  |
5 | mac::antoyo!(>>);
  |              ^^

This makes it impossible to show correct error messages on something like Option<Option<String>> for example where you want the span to point out the inner Option<String>, as reported in dtolnay/syn#296.

Mentioning @antoyo who noticed this.
Mentioning @jseyfried @abonander who are involved with this code.

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-macros-2.0 Area: Declarative macros 2.0 (#39412) labels Feb 6, 2018
@dtolnay
Copy link
Member Author

dtolnay commented Apr 29, 2018

Repro script:

#!/bin/sh

cargo new --lib repro_macro
cargo new --lib repro

echo >repro_macro/src/lib.rs '
#![feature(proc_macro)]

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro]
pub fn m(input: TokenStream) -> TokenStream {
    let mut iter = input.into_iter();
    let tt = iter.next().unwrap();
    tt.span().error("first angle bracket").emit();
    assert!(iter.next().is_some());
    assert!(iter.next().is_none());
    TokenStream::empty()
}
'

echo >>repro_macro/Cargo.toml '
[lib]
proc-macro = true
'

echo >repro/src/lib.rs '
#![feature(proc_macro)]
extern crate repro_macro;
repro_macro::m!(>>);
fn main() {}
'

echo >>repro/Cargo.toml '
repro_macro = { path = "../repro_macro" }
'

cargo build --manifest-path repro/Cargo.toml
error: first angle bracket
 --> src/lib.rs:6:17
  |
6 | repro_macro::m!(>>);
  |                 ^^

@alexcrichton alexcrichton added A-macros-1.2 Area: Declarative macros 1.2 C-bug Category: This is a bug. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels May 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros-1.2 Area: Declarative macros 1.2 A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants