]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/env.rs
Allow more deriving on packed structs.
[rust.git] / compiler / rustc_builtin_macros / src / env.rs
1 // The compiler code necessary to support the env! extension. Eventually this
2 // should all get sucked into either the compiler syntax extension plugin
3 // interface.
4 //
5
6 use rustc_ast::tokenstream::TokenStream;
7 use rustc_ast::{self as ast, GenericArg};
8 use rustc_expand::base::{self, *};
9 use rustc_span::symbol::{kw, sym, Ident, Symbol};
10 use rustc_span::Span;
11
12 use std::env;
13
14 pub fn expand_option_env<'cx>(
15     cx: &'cx mut ExtCtxt<'_>,
16     sp: Span,
17     tts: TokenStream,
18 ) -> Box<dyn base::MacResult + 'cx> {
19     let Some(var) = get_single_str_from_tts(cx, sp, tts, "option_env!") else {
20         return DummyResult::any(sp);
21     };
22
23     let sp = cx.with_def_site_ctxt(sp);
24     let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern);
25     cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
26     let e = match value {
27         None => {
28             let lt = cx.lifetime(sp, Ident::new(kw::StaticLifetime, sp));
29             cx.expr_path(cx.path_all(
30                 sp,
31                 true,
32                 cx.std_path(&[sym::option, sym::Option, sym::None]),
33                 vec![GenericArg::Type(cx.ty_ref(
34                     sp,
35                     cx.ty_ident(sp, Ident::new(sym::str, sp)),
36                     Some(lt),
37                     ast::Mutability::Not,
38                 ))],
39             ))
40         }
41         Some(value) => cx.expr_call_global(
42             sp,
43             cx.std_path(&[sym::option, sym::Option, sym::Some]),
44             vec![cx.expr_str(sp, value)],
45         ),
46     };
47     MacEager::expr(e)
48 }
49
50 pub fn expand_env<'cx>(
51     cx: &'cx mut ExtCtxt<'_>,
52     sp: Span,
53     tts: TokenStream,
54 ) -> Box<dyn base::MacResult + 'cx> {
55     let mut exprs = match get_exprs_from_tts(cx, tts) {
56         Some(exprs) if exprs.is_empty() => {
57             cx.span_err(sp, "env! takes 1 or 2 arguments");
58             return DummyResult::any(sp);
59         }
60         None => return DummyResult::any(sp),
61         Some(exprs) => exprs.into_iter(),
62     };
63
64     let Some((var, _style)) = expr_to_string(cx, exprs.next().unwrap(), "expected string literal") else {
65         return DummyResult::any(sp);
66     };
67     let msg = match exprs.next() {
68         None => Symbol::intern(&format!("environment variable `{}` not defined", var)),
69         Some(second) => match expr_to_string(cx, second, "expected string literal") {
70             None => return DummyResult::any(sp),
71             Some((s, _style)) => s,
72         },
73     };
74
75     if exprs.next().is_some() {
76         cx.span_err(sp, "env! takes 1 or 2 arguments");
77         return DummyResult::any(sp);
78     }
79
80     let sp = cx.with_def_site_ctxt(sp);
81     let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern);
82     cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
83     let e = match value {
84         None => {
85             cx.span_err(sp, msg.as_str());
86             return DummyResult::any(sp);
87         }
88         Some(value) => cx.expr_str(sp, value),
89     };
90     MacEager::expr(e)
91 }