-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
span.start() and span.end() is not working in proc_macro context even in nightly version. #402
Comments
I have this problem too. I'm bisecting now. (I'm afraid I have to use nightly rather than stable for other reasons, so the bisection will be in terms of nightlies.)
Version 1.0.56 doesn't build with 1.74.0, but luckily 1.0.67 builds with 1.71.0, and yields the broken results, so I'll stick with that and see where I get. It looks like the change from 1.0.56 to 1.0.57 is where the breakage appears, at least for me! Oh. Sure enough, from 1.0.57 and on to the current main revision, we have in wrapper.rs #[cfg(span_locations)]
pub fn start(&self) -> LineColumn {
match self {
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.start(),
}
} Presumably this is because However, there are separate #[cfg(span_locations)]
pub fn start(&self) -> LineColumn {
match self {
#[cfg(proc_macro_span)]
Span::Compiler(s) => LineColumn {
line: s.line(),
column: s.column(),
},
#[cfg(not(proc_macro_span))]
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.start(),
}
}
#[cfg(span_locations)]
pub fn end(&self) -> LineColumn {
match self {
#[cfg(proc_macro_span)]
Span::Compiler(s) => LineColumn {
line: s.end().line(),
column: s.end().column(),
},
#[cfg(not(proc_macro_span))]
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.end(),
} Now, how to fix this properly? Easiest would be to alter proc_macro2 to include the implementations just above. However then proc_macro2 wouldn't be usable with older versions of the toolchain. Is that a problem (@dtolnay?)? An alternative would be to expose line() and column() as new methods on Spans (with some kind of end().line() and end().column()), following the proc_macro approach. @dtolnay, your thoughts here would be very welcome. |
^ Here's the tiny crate I've been using to explore the problem. Buggy
Correct output:
|
Unfortunately I wonder if this is going to be hard to write a test case for since it involves running in proc_macro context? There are existing test cases that check column/line values, but since they pass, presumably they use the |
(Here's the commit where the code for retrieving the LineColumn from proc_macro was removed: 5f9d3fe) |
Current Solution:To resolve this problem I temporarily used following approach. The problem was the proc_macro2::span.start().column was not working inside proc_macro scope but it was working fine for normal(outside proc_macro scope) use cases. Also I can't use proc_macro::span.unwrap().start().column() outside proc_macro scope. So I conditionally switched between these two cases based on the function input attribute
note: you have to add |
I'd love to see some progress on this one. The use case is emitting some C code out of a "rust function" that contains the C code as a single string literal for the function body. Having access to the precise span of that string literal would allow me to add a Currently, this can only be done by emitting a |
…unc bodies FIX Since dtolnay/proc-macro2#402 got fixed, we can now enjoy accurate error location when compiling the C code.
Hi, @dtolnay Thank for you great work. I am trying to use proc_macro2 to parse the css content in the project I am working on. You can find it here.
The Problem
This is the piece of code I am trying to use in both scenarios.
The text was updated successfully, but these errors were encountered: