rust/compiler/rustc_builtin_macros/src/env.rs

94 lines
3.0 KiB
Rust
Raw Normal View History

2016-06-06 09:52:48 -05:00
// The compiler code necessary to support the env! extension. Eventually this
// should all get sucked into either the compiler syntax extension plugin
// interface.
//
use rustc_ast::tokenstream::TokenStream;
2020-04-27 12:56:11 -05:00
use rustc_ast::{self as ast, GenericArg};
use rustc_expand::base::{self, *};
2020-04-19 06:00:18 -05:00
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::Span;
std: Add a new `env` module This is an implementation of [RFC 578][rfc] which adds a new `std::env` module to replace most of the functionality in the current `std::os` module. More details can be found in the RFC itself, but as a summary the following methods have all been deprecated: [rfc]: https://github.com/rust-lang/rfcs/pull/578 * `os::args_as_bytes` => `env::args` * `os::args` => `env::args` * `os::consts` => `env::consts` * `os::dll_filename` => no replacement, use `env::consts` directly * `os::page_size` => `env::page_size` * `os::make_absolute` => use `env::current_dir` + `join` instead * `os::getcwd` => `env::current_dir` * `os::change_dir` => `env::set_current_dir` * `os::homedir` => `env::home_dir` * `os::tmpdir` => `env::temp_dir` * `os::join_paths` => `env::join_paths` * `os::split_paths` => `env::split_paths` * `os::self_exe_name` => `env::current_exe` * `os::self_exe_path` => use `env::current_exe` + `pop` * `os::set_exit_status` => `env::set_exit_status` * `os::get_exit_status` => `env::get_exit_status` * `os::env` => `env::vars` * `os::env_as_bytes` => `env::vars` * `os::getenv` => `env::var` or `env::var_string` * `os::getenv_as_bytes` => `env::var` * `os::setenv` => `env::set_var` * `os::unsetenv` => `env::remove_var` Many function signatures have also been tweaked for various purposes, but the main changes were: * `Vec`-returning APIs now all return iterators instead * All APIs are now centered around `OsString` instead of `Vec<u8>` or `String`. There is currently on convenience API, `env::var_string`, which can be used to get the value of an environment variable as a unicode `String`. All old APIs are `#[deprecated]` in-place and will remain for some time to allow for migrations. The semantics of the APIs have been tweaked slightly with regard to dealing with invalid unicode (panic instead of replacement). The new `std::env` module is all contained within the `env` feature, so crates must add the following to access the new APIs: #![feature(env)] [breaking-change]
2015-01-27 14:20:58 -06:00
use std::env;
2019-12-22 16:42:04 -06:00
pub fn expand_option_env<'cx>(
cx: &'cx mut ExtCtxt<'_>,
sp: Span,
tts: TokenStream,
) -> Box<dyn base::MacResult + 'cx> {
let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") {
None => return DummyResult::any(sp),
2016-06-06 09:52:48 -05:00
Some(v) => v,
};
2019-09-14 15:17:11 -05:00
let sp = cx.with_def_site_ctxt(sp);
let value = env::var(&var.as_str()).ok().as_deref().map(Symbol::intern);
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((Symbol::intern(&var), value));
let e = match value {
None => {
2019-09-14 15:16:51 -05:00
let lt = cx.lifetime(sp, Ident::new(kw::StaticLifetime, sp));
2019-12-22 16:42:04 -06:00
cx.expr_path(cx.path_all(
sp,
true,
cx.std_path(&[sym::option, sym::Option, sym::None]),
vec![GenericArg::Type(cx.ty_rptr(
sp,
cx.ty_ident(sp, Ident::new(sym::str, sp)),
Some(lt),
ast::Mutability::Not,
))],
))
2016-06-06 09:52:48 -05:00
}
Some(value) => cx.expr_call_global(
2019-12-22 16:42:04 -06:00
sp,
cx.std_path(&[sym::option, sym::Option, sym::Some]),
vec![cx.expr_str(sp, value)],
2019-12-22 16:42:04 -06:00
),
};
MacEager::expr(e)
}
2019-12-22 16:42:04 -06:00
pub fn expand_env<'cx>(
cx: &'cx mut ExtCtxt<'_>,
sp: Span,
tts: TokenStream,
) -> Box<dyn base::MacResult + 'cx> {
2014-09-13 11:06:01 -05:00
let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
Some(ref exprs) if exprs.is_empty() => {
cx.span_err(sp, "env! takes 1 or 2 arguments");
return DummyResult::any(sp);
}
None => return DummyResult::any(sp),
2016-06-06 09:52:48 -05:00
Some(exprs) => exprs.into_iter(),
};
2016-06-06 09:52:48 -05:00
let var = match expr_to_string(cx, exprs.next().unwrap(), "expected string literal") {
None => return DummyResult::any(sp),
2016-06-06 09:52:48 -05:00
Some((v, _style)) => v,
};
2014-09-13 11:06:01 -05:00
let msg = match exprs.next() {
None => Symbol::intern(&format!("environment variable `{}` not defined", var)),
2019-12-22 16:42:04 -06:00
Some(second) => match expr_to_string(cx, second, "expected string literal") {
None => return DummyResult::any(sp),
Some((s, _style)) => s,
},
2014-09-13 11:06:01 -05:00
};
if exprs.next().is_some() {
cx.span_err(sp, "env! takes 1 or 2 arguments");
return DummyResult::any(sp);
2014-09-13 11:06:01 -05:00
}
2020-05-26 18:45:30 -05:00
let sp = cx.with_def_site_ctxt(sp);
let value = env::var(&*var.as_str()).ok().as_deref().map(Symbol::intern);
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
let e = match value {
None => {
cx.span_err(sp, &msg.as_str());
return DummyResult::any(sp);
}
Some(value) => cx.expr_str(sp, value),
};
MacEager::expr(e)
}