Skip to content

feat: allow reading env variables in const context - #164769

Draft
lambinoo wants to merge 2 commits into
rust-lang:mainfrom
lambinoo:comptime-env-shenanigans
Draft

lambinoo wants to merge 2 commits into
rust-lang:mainfrom
lambinoo:comptime-env-shenanigans

Conversation

@lambinoo

@lambinoo lambinoo commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Tracking issue: #146922

This is a first draft for allowing people to read environment variables dynamically in const evaluation context.
For now, this allows reading a variable as a &[u8] from any const context. Since core::str::from_utf8 is const, it should be easy to wrap that to have an interface that looks a tad like std::env::var* but for comptime.

Haven't really thought about how to organize this yet. But we might want a core::(reflection/comptime)::env or something equivalent to store those functions. Maybe they could also just sit in a core::env::comptime_var but that feels weird

r? @oli-obk

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 22, 2026
@rust-log-analyzer

This comment has been minimized.

@asquared31415

asquared31415 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

How does this differ from the env! macro?

(also your tracking issue link is wrong)

@lambinoo
lambinoo force-pushed the comptime-env-shenanigans branch 2 times, most recently from 80ee038 to 54cd284 Compare October 23, 2026 05:42
@lambinoo

lambinoo commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor Author

How does this differ from the env! macro?

(also your tracking issue link is wrong)

This was something @oli-obk talked about during his talk at Eurorust 2026!

I modified the passing ui test to showcase what you can do with it. It basically allows you to chose dynamically at runtime which variable you are reading.

And fixed the tracking issue. Thanks!

sym::type_id_env_var => {
let arg_place = ecx.deref_pointer(&args[0])?;
let key = ecx.read_str(&arg_place)?;
let value = std::env::var_os(&key);

@tmiasko tmiasko Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading from environment will result in inconsistent evaluation, which is unsound.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The env! and option_env! macros already allow doing the same. Main difference is that this allows you to dynamically choose which variable you would be reading from.

Is there different properties with that change that makes this unsound? Or is it something different you are talking about?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are quite different. In the one case the environment is consulted at macro expansion time, in the other during constant evaluation.

Constant evaluation must be deterministic, including across different compiler sessions with different environment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jup, my hope was that we were already doing sth sane wrt env vars during macro expansion, but we probably are just relying on macro expansion happening regardless of query caching.

So, what we also need to do here is to add a query that is eval_always, which does the env var reading, and then call that query from here. This way we re-read the env every time, and if it changes, invalidate every part of the compiler that depended on the change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The evaluation must be consistent across different crates. It would have to capture a complete environment, and then query would have to identify which crate the environment is for.

The capture of complete environment sounds like a bad idea to me, and as far as I see, this is what this functionality implies.

@rust-log-analyzer

This comment has been minimized.

@lambinoo
lambinoo force-pushed the comptime-env-shenanigans branch from 54cd284 to 2c5bdaa Compare October 23, 2026 06:30
@rust-log-analyzer

This comment has been minimized.

To avoid having change maybe ui tests, rename to const_env.

The compiler tries to find matching `env` modules when you
use one without important it. It then starts listing the one
in type_info too.
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job test-pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

#[rustc_intrinsic]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[rustc_comptime]
pub fn type_id_env_var(_env_name: &str) -> Option<&'static [u8]>;

@oli-obk oli-obk Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is not a TypeId based query, we should give it a different prefix. Or just none, just having it be the env_var intrinsic seems fine

View changes since the review

/// Temporary feature, part of comptime. Not really type info related
pub mod const_env {
/// Returns the raw bytes of an environment variable that is visible to the compiler
/// while compile-time constant evaluation.

@oli-obk oli-obk Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// while compile-time constant evaluation.
/// during constant evaluation.

View changes since the review

@rustbot

rustbot commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

This branch has not been deployed

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants