Skip to content

Implement env!() builtin macro #977

Description

@CohenArthur

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions