Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
dtolnay opened this issue Jan 6, 2018 · 2 comments
Closed

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

dtolnay opened this issue Jan 6, 2018 · 2 comments
Labels
A-decl-macros-1-2 Area: Declarative macros 1.2 A-decl-macros-2-0 Area: Declarative macros 2.0 (#39412) A-diagnostics Area: Messages for errors, warnings, and lints 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-decl-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-decl-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
@dtolnay
Copy link
Member Author

dtolnay commented Nov 17, 2024

Fixed by #102639.

@dtolnay dtolnay closed this as completed Nov 17, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-decl-macros-1-2 Area: Declarative macros 1.2 A-decl-macros-2-0 Area: Declarative macros 2.0 (#39412) A-diagnostics Area: Messages for errors, warnings, and lints 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