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

proc_macro: Add API for loading external files into the SourceMap #92565

Closed
camelid opened this issue Jan 4, 2022 · 6 comments
Closed

proc_macro: Add API for loading external files into the SourceMap #92565

camelid opened this issue Jan 4, 2022 · 6 comments
Labels
A-proc-macros Area: Procedural macros C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@camelid
Copy link
Member

camelid commented Jan 4, 2022

Summary

With this feature, proc macros (like askama::Template) could parse external files and then emit diagnostics with appropriate spans.

Proposed API

Other APIs are possible, but I was thinking of something like this:

impl SourceFile {
    pub fn span(&self) -> Span { ... }
    pub fn contents(&self) -> &str { ... }
    pub fn load(path: &Path) -> Self { ... }
}

I'm not super familiar with the proc_macro API, so feel free to suggest better API designs.

See also

See this discussion on Zulip and this discussion on GitHub for why I think this feature would be useful.

@camelid camelid added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. A-proc-macros Area: Procedural macros labels Jan 4, 2022
@camelid
Copy link
Member Author

camelid commented Jan 4, 2022

cc @djc

@camelid
Copy link
Member Author

camelid commented Jan 4, 2022

Another possible API for this is a function that could be called by a proc macro and would load a file and return a piece of AST representing that file as a string literal. In other words, this would be like include_str! that would be expanded immediately. I feel like I've seen this discussed before, but I can't remember where...

@petrochenkov
Copy link
Contributor

petrochenkov commented Jan 5, 2022

cc #73921 #55904 rust-lang/rfcs#3200

@camelid
Copy link
Member Author

camelid commented Jan 5, 2022

Ah, that's the RFC I was thinking of!

@AlbertMarashi
Copy link

AlbertMarashi commented Feb 7, 2022

+1 on this

@Dylan-DPC
Copy link
Member

Closing this as it is tracked in #99515

@Dylan-DPC Dylan-DPC closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macros Area: Procedural macros C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants