rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

`Span` for external data files.

Open crlf0710 opened this issue 5 years ago • 3 comments

When writing proc macro, sometimes it's convenient to parse external data file and generate code accordingly. It would be nice to allow creating Spans corresponding to byte ranges within the data file, and use them in compiler diagnostics information.

One specific example where the external data is a piece of Rust code, is at https://github.com/dtolnay/request-for-implementation/issues/47 .

crlf0710 avatar Feb 25 '20 16:02 crlf0710

I think it would be enabler for compile-to-Rust languages, e.g. support for experimental syntax or custom syntax sugar.

I would also like this for crates like ructe that compile HTML-like templates to Rust code. Currently it's done in build.rs and errors in templates point to temporary files in OUT_DIR. It's not a great experience. If the template compiler could be converted to a proc_macro with support for spans, it'd work beautifully with errors spans showing in HTML!

kornelski avatar May 01 '20 12:05 kornelski

It is absolutely necessary for yarte, which combines its own parser with syn and mixes the errors. Currently I was taking advantage of a proc_macro2 bug in LineColumn that returned bytes. They have solved it and there is no way.

If it can be given more importance, in yarte it is essential for correct error management.

zzau13 avatar May 25 '20 05:05 zzau13

The API for getting the file of a Span was recently stabilised. Unfortunately it doesn't look like there's even an unstable API to set it. It seems like a Span has references back to internal rustc file maps, so it might not be totally trivial. Still I think you could do it and this would be super useful.

Timmmm avatar Jun 09 '25 19:06 Timmmm