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

Rust Analyzer passes 0-indexed line numbers using proc_macro_span unstable API #19032

Closed
mr-adult opened this issue Jan 25, 2025 · 3 comments · Fixed by #19033
Closed

Rust Analyzer passes 0-indexed line numbers using proc_macro_span unstable API #19032

mr-adult opened this issue Jan 25, 2025 · 3 comments · Fixed by #19033
Labels
C-bug Category: bug

Comments

@mr-adult
Copy link

Rust-analyzer appears to be passing Spans' line numbers (from the proc_macro_span unstable API) as 0-indexed values to my proc-macro. According to the documentation, this line is supposed to always be 1-indexed. The rustc compiler is using 1-indexed lines. The result of this is an LSP error of proc-macro panicked: attempt to subtract with overflow. When compiled and run using rustc, however, the code runs perfectly fine.

Image

rust-analyzer version: 0.3.2273-standalone (248bd51 2025-01-18)
rustc version: 1.84.0-nightly (9b18a122f 2024-10-26)

editor or extension: VSCode. For VSCode users, specify your extension version: 0.3.2273

relevant settings: N/A

repository link (if public, optional): rust-analyzer-bug-report

code snippet to reproduce:

use simpleml_macro::sml;

fn main() {
    let tree = sml!{
        Configuration
            Video
                Resolution 1280 720
                RefreshRate  60
                Fullscreen true
            -
            Audio
                Volume 100
                Music  80
            -
            Player
                Name "Hero 123"
            -
        -
    };
    println!("{tree:?}")
}
@ChayimFriedman2
Copy link
Contributor

You should note that we don't really support the line/column API (and can't, for incrementality reasons). Currently it just returns 0. Returning 1 is trivial, but it won't really mean we support this API.

@mr-adult
Copy link
Author

That's fair. I don't think returning 1 would help my use case, but would probably still be more consistent with the Span.line() documentation. Since the syntax of SimpleML is whitespace-sensitive, using the line/column APIs was the best way I could find to achieve whitespace-sensitive syntax in a rust macro.

Probably the most helpful thing that could be done here would be to monitor the line/column APIs and, if they're called, suppress all diagnostics from the macro. That doesn't seem worth the development effort though, so I'd also be okay with closing the issue. I can let my users know in documentation that rust-analyzer doesn't support this macro particularly well and they will see phantom errors.

@ChayimFriedman2
Copy link
Contributor

ChayimFriedman2 commented Jan 25, 2025

We can return line/column numbers relative to the macro call's beginning (instead of the file's beginning); will that help you? (Anyway that's not what we're doing currently).

Relying on whitespace information is brittle anyway and macros weren't meant for that. If you'll make the whole argument a string it'll be easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants