Rust equivalent of #line directive?

Does Rust have any equivalent of C's #line directive?

Some of my source files are generated or otherwise run through some preprocessing steps before rustc sees them, and it would be nice for the error messages to report the position from the actual source file instead of the generated one.

https://doc.rust-lang.org/std/macro.line.html

@naim, line!() is not #line. They do completely different things.

1 Like

Ah, I see,
I found a topic in the internals forum, it doesn't look like this is implemented.
unmerged RFC about #line.

1 Like

Procedural macros allow setting the span of emitted code to "any" desired span, and that will be used for generated errors.

This is currently limited to any span used as input to the procedural macro, but I expect that it will grow the ability to specify more arbitrary spans in the future.

If you can afford doing code generation purely via a proc macro on rustc-tokenizable input to that proc macro, then you can already get pretty impressive results with the stable API.

5 Likes

Unfortunately, my use case is literate programming via noweb, so I’m mixing Latex and Rust code all in one source file. I also am in the habit of mixing languages generally, so my generated code is as likely to come from come from a Python, Bash or M4 script as Rust.

I suppose that I might be able to hack it to wrap the entire outputted rust file into a single, big, proc-macro call that recognizes and strips some markers. As long as I’m careful to not break chunks in the middle of a rust token it should work.

If you control the processing, why not just introduce blank lines (or comments) to fill up all the non-rust portions of the file? That seems considerably easier than messing with a proc-macro.

1 Like

The order of rust code in the noweb source isn’t the same as the order in the rust code. I do things like put the declaration and all the implementations of a single trait method together in the document, but they get scattered to the inside of various impls in the Rust code.

Looking at the Span docs, there’s not much available in stable. The only spans available are for the entire call and the input tokens. I’d have to blank-out the non-rust parts of the source file and do the rearranging inside the proc macro to make this work.

That makes sense. Does rust not let you split an implementation of a trait into several impl blocks? Or the declaration of a trait, for that matter? I don't see any reason syntactically why that shouldn't be allowed, but I've never tried it...

It’s not always just high-level items that are being composed. Perhaps an example will shed some light on the sort of things I’m doing. Here’s the source code to a game jam entry I made last year: https://github.com/e2-71828/ld45/blob/master/doc.pdf

In chunk 10b, I set up the conversion code between a C tagged enum and a Rust enum. The actual match arms are spread out over the next few pages, grouped with the definitions of the corresponding Rust enum variants and the C structure definition for the variant-specific related data.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.