-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Allow access to location information from proc_macro TokenStreams #38546
Comments
My experimental code can be found here: https://github.com/djc/askama |
Is there any update on this? I need this feature, too: I'm writing a macro that has its own "module system" so to speak. Thus I need to load files form disk relative to the file in which the macro is called. @jseyfried @alexcrichton I could try implementing this feature for |
There's some work in #43604 but AFAIK there's no movement towards manufacturing custom spans. I know it's something we'd like to support eventually! |
#43604 is mainly just waiting on review right now. |
This is Span::call_site, right? That's stable: https://doc.rust-lang.org/stable/proc_macro/struct.Span.html#method.call_site |
I have a hard time remembering exactly my intent with this issue from 5 years ago, but I think this would depend on getting |
What would actually be more useful, less work to stabilize and have additional utility, is to basically just be able to resolve a file path the way Then like the new @petrochenkov I feel like it would actually make a lot of sense to just combine these. Maybe something like: impl SourceFile {
/// Load a file relative to this source file and parse it as a `TokenStream`.
///
/// The file path will be included in the resulting dep-info and will trigger recompilation when it changes.
pub fn include(&self, path: impl AsRef<Path>) -> Result<TokenStream, IncludeError> { ... }
/// Load a file relative to this source file as a UTF-8 string.
///
/// The file path will be included in the resulting dep-info and will trigger recompilation when it changes.
pub fn include_str(path: impl AsRef<Path>) -> Result<String, IncludeError> { ... }
} This would actually fix a long-outstanding issue in SQLx (although I think |
Yeah, that would be pretty great to have for my use case. |
I would like to build a template engine that leverages custom derive to attach a template rendering function to a struct at compile time. I was planning on attaching an attribute to the struct to specify the path to the template file, which could then be read and transpiled into Rust. However, it seems it is currently not possible to get at the path to the source file where the
TokenStream
originated. As such, it seems I would have to rely on absolute paths to template files or on the current working directory. Even assuming cargo compiles (including workspaces), this does not seem reliable.The
Item
s that are part of theTokenStream
contain aSpan
, but I don't think theCodeMap
they index into is currently available to a proc_macro. Would that be a viable implementation strategy?The text was updated successfully, but these errors were encountered: