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

Feature request: get access to the exact text source (&str) behind a Span #110

Closed
ogoffart opened this issue Aug 9, 2018 · 7 comments
Closed

Comments

@ogoffart
Copy link
Contributor

ogoffart commented Aug 9, 2018

In order to be able to upgrade the syn dependency of the rust-cpp crate ( mystor/rust-cpp#41 ), one would need to be able to extract the raw source code for a given TokenTree (or a Span)

One could do that, I guess, by using TokenTree::span, and then getting the start/end and source_file of the span. But these function are "semver exempt". Even if they were not, that would mean we would need to recompute the offset in the file from line/column, which would be inefficient.

So what would be nice would be an start_offset and end_offset within Span.
Or better, a function that would return a String or a &str with the exact spelling.

TokenStream::to_string() does not work, as it adds spaces between token (that makes the resulting string invalid C++ as the two languages don't have the same grammar).
This is useful for crates, like rust-cpp, that allow to embed arbitrary languages within a rust macro (as long as the [], () and {} are balanced))

@alexcrichton
Copy link
Contributor

Seems like a plausible feature to me! To implement in this library though it needs to be implemented in upstream rustc first

@roblabla
Copy link

I am also interested in this. Is there an upstream ticket in rustc?

@alexcrichton
Copy link
Contributor

I think rust-lang/rust#54725 is the closest

bors added a commit to rust-lang/rust that referenced this issue Mar 27, 2019
Introduce proc_macro::Span::source_text

A function to extract the actual source behind a Span.

Background: I would like to use `syn` in a `build.rs` script to parse the rust code, and extract part of the source code. However, `syn` only gives access to proc_macro2::Span, and i would like to get the source code behind that.
I opened an issue on proc_macro2 bug tracker for this feature dtolnay/proc-macro2#110  and @alexcrichton said the feature should first go upstream in proc_macro.  So there it is!

Since most of the Span API is unstable anyway, this is guarded by the same `proc_macro_span` feature as everything else.
@idubrov
Copy link

idubrov commented Jul 25, 2019

Access to offset would also help my case where I use proc_macro2+syn completely outside of the compiler and I want to be able to get. Currently it seems I'll have to recover them from line+number (which is a shame since internally these are offsets in "fallback" implementation).

I'm probably using the tool the wrong way, though, not sure how useful my use-case is 😅

@ogoffart
Copy link
Contributor Author

Yes, my use case is also unrelated to proc_macro: In the cpp_build crate. The use case is to extract the cpp! macro from the build.rs script.

Anyway, even upstream now has a way to get the source text: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.source_text

@rust-shell-script
Copy link

It would be really useful for creating DSL inside rust language itself. I saw people use all kinds of tricks to get the source code (proc-macro-faithful-display, manipulating stringify!, heuristic string with line/column clue ...), I also created a hack macro to achieve the same goal.

I hope it can come along with a simple API, so I can make the below code work without much hack any more:

if run_cmd! {
    ls / | wc -w
    echo "bad cmd"
    ls -l /nofile
    date
}.is_err() {
    warn!("Run group command failed");
}

@dtolnay
Copy link
Owner

dtolnay commented Dec 10, 2021

Closing in favor of consolidating the discussion to rust-lang/rust#54725.

@dtolnay dtolnay closed this as completed Dec 10, 2021
Repository owner locked and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants