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

Implement env!() builtin macro #977

Closed
CohenArthur opened this issue Feb 25, 2022 · 5 comments · Fixed by #1113
Closed

Implement env!() builtin macro #977

CohenArthur opened this issue Feb 25, 2022 · 5 comments · Fixed by #1113

Comments

@CohenArthur
Copy link
Member

env!() is part of the list of builtin macros enumerated here

Its description is as follows:

Inspects an environment variable at compile time

The goal is to fetch environment variables passed to gccrs, and expand them into the user's compiled code.

arthur@platypus ~/G/gccrs (add-assert-macro)> cat test.rs
fn main() {
    let s = env!("CUSTOM");

    println!("{}", s);
}
arthur@platypus ~/G/gccrs (add-assert-macro)> rustc test.rs
error: environment variable `CUSTOM` not defined
 --> test.rs:2:13
  |
2 |     let s = env!("CUSTOM");
  |             ^^^^^^^^^^^^^^
  |
  = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

arthur@platypus ~/G/gccrs (add-assert-macro) [1]> CUSTOM='hi!' rustc test.rs
arthur@platypus ~/G/gccrs (add-assert-macro)> ./test
hi!

The macro should not expand to a function call that fetches environment variables during the user's program's execution.

arthur@platypus ~/G/gccrs (add-assert-macro)> ./test
hi!
arthur@platypus ~/G/gccrs (add-assert-macro)> CUSTOM='something else!' ./test
hi!

The original macro contains two match arms, including one with a custom error message:

    macro_rules! env {
        ($name:expr $(,)?) => {{ /* compiler built-in */ }};
        ($name:expr, $error_msg:expr $(,)?) => {{ /* compiler built-in */ }};
    }

You should try and support the first form first, by simply fetching an environment variable and creating an associated string using the make_string function defined in gcc/rust/expand/rust-macro-builtins.cc

The second form could be implemented in a later PR for simplicity

@bjorn3
Copy link

bjorn3 commented Feb 25, 2022

Rustc has a dep file extension showing which env vars are read and if valid UTF-8 what value saod env vars had. This is used by cargo to rebuild if the env vars change.

@CohenArthur
Copy link
Member Author

Oh that's really smart. I think this is still a little far for gccrs but we should keep it in mind. I'm guessing this is what this line is for, then. We'll definitely need to look into it before compiling actual projects

@bjorn3
Copy link

bjorn3 commented Feb 25, 2022

Yes, and this is where it is emitted: https://github.com/rust-lang/rust/blob/45e2c2881d11324d610815bfff097e25c412199e/compiler/rustc_interface/src/passes.rs#L703-L706 And this is the PR originally implementing it: rust-lang/rust#71858

@philberty philberty removed this from To do in Control Flow 3 Macros Mar 21, 2022
@philberty philberty removed this from the Macro Expansion milestone Mar 21, 2022
@omachota
Copy link
Contributor

Hi, I would like to work on this issue.

@CohenArthur
Copy link
Member Author

Hi @omachota, and welcome! I've assigned you the issue. Feel free to ask any questions here

bors bot added a commit that referenced this issue Apr 14, 2022
1113: macros: Add env! macro r=CohenArthur a=omachota

Added the `env!()` macro and relevant test cases

Fixes: #977 

Signed-off-by: Ondřej Machota <ondrejmachota@gmail.com>

Co-authored-by: Ondřej Machota <ondrejmachota@gmail.com>
@bors bors bot closed this as completed in #1113 Apr 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants