-
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
proc_macro: Add API for tracked access to environment variables #74653
Conversation
@petrochenkov: no appropriate reviewer found, use r? to override |
❤️ Thank you for following through on this. cc @dtolnay to get libs sign-off on the unstable API. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems fine. Please file a tracking issue.
Some notes to address here or otherwise add to the tracking issue to address before stabilizing:
-
Ideally the signature would match the function in std::env but I see that putting OsStr in depinfo is possibly hard. Don't filepaths go into depinfo though? Those are OS strings so what do we do there, and would it be possible to do that for here as well?
-
If there is a possibility we want to expose some other things from std::env, maybe
proc_macro::tracked_env::var
would be more appropriate to keep those together.
They do, and they use lossy There's some discussion about non-UTF-8 cases in environment variables in #71858, looks like the general stance is that they don't matter.
I thought about the signature a bit, assuming we are keeping UTF-8, the alternatives to pub fn tracked_env_var(var: impl AsRef<OsStr> + AsRef<str> + Copy) -> Result<String, VarError> {
let value = env::var(var); // Need `Copy` to avoid consuming `var` here
bridge::client::FreeFunctions::track_env_var(var, value.as_deref().ok());
value
} or // Without `Copy`
pub fn tracked_env_var(var: impl AsRef<OsStr> + AsRef<str>) -> Result<String, VarError> {
let var = &var.to_string(); // Have to convert into an owned string
let value = env::var(var);
bridge::client::FreeFunctions::track_env_var(var, value.as_deref().ok());
value
} The original |
Yeah, sounds reasonable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would feel more comfortable with var: impl AsRef<OsStr> + AsRef<str>
. That leaves the possibility to remove AsRef<str> bound backward compatibly to match the std::env::var signature.
Updated.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
@bors r+ |
📌 Commit 62c9fa9 has been approved by |
☀️ Test successful - checks-actions, checks-azure |
📣 Toolstate changed by #74653! Tested on commit 1841fb9. 💔 rls on windows: test-pass → build-fail (cc @Xanewok). |
Tested on commit rust-lang/rust@1841fb9. Direct link to PR: <rust-lang/rust#74653> 💔 rls on windows: test-pass → build-fail (cc @Xanewok). 💔 rls on linux: test-pass → build-fail (cc @Xanewok). 💔 rustfmt on windows: test-pass → build-fail (cc @topecongiro @calebcartwright). 💔 rustfmt on linux: test-pass → build-fail (cc @topecongiro @calebcartwright).
5651: Add track_env_var to the proc macro server r=kjeremy a=lnicola See rust-lang/rust#74653. Fixes #6054. Fixes #5640, maybe. Should be merged when 1.47 is released. Proc macros still don't work for me, but it no longer crashes. Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
Continuation of #71858.
proc_macro::tracked_env::var
is similar to regularenv::var
called from a proc macro, except that it also adds the accessed variable to depinfo.