|
4 | 4 | //
|
5 | 5 |
|
6 | 6 | use rustc_ast::tokenstream::TokenStream;
|
7 |
| -use rustc_ast::{self as ast, GenericArg}; |
| 7 | +use rustc_ast::{self as ast, AstDeref, GenericArg}; |
8 | 8 | use rustc_expand::base::{self, *};
|
9 | 9 | use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
10 | 10 | use rustc_span::Span;
|
@@ -76,41 +76,46 @@ pub fn expand_env<'cx>(
|
76 | 76 | },
|
77 | 77 | };
|
78 | 78 |
|
79 |
| - let sp = cx.with_def_site_ctxt(sp); |
| 79 | + let span = cx.with_def_site_ctxt(sp); |
80 | 80 | let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern);
|
81 | 81 | cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
|
82 | 82 | let e = match value {
|
83 | 83 | None => {
|
84 |
| - // Use the string literal in the code in the diagnostic to avoid confusing diagnostics, |
85 |
| - // e.g. when the literal contains escape sequences. |
86 | 84 | let ast::ExprKind::Lit(ast::token::Lit {
|
87 | 85 | kind: ast::token::LitKind::Str | ast::token::LitKind::StrRaw(..),
|
88 |
| - symbol: original_var, |
| 86 | + symbol, |
89 | 87 | ..
|
90 | 88 | }) = &var_expr.kind
|
91 | 89 | else {
|
92 | 90 | unreachable!("`expr_to_string` ensures this is a string lit")
|
93 | 91 | };
|
94 |
| - cx.emit_err(errors::EnvNotDefined { |
95 |
| - span: sp, |
96 |
| - msg: custom_msg, |
97 |
| - var: *original_var, |
98 |
| - help: custom_msg.is_none().then(|| help_for_missing_env_var(var.as_str())), |
99 |
| - }); |
| 92 | + |
| 93 | + if let Some(msg_from_user) = custom_msg { |
| 94 | + cx.emit_err(errors::EnvNotDefinedWithUserMessage { span, msg_from_user }); |
| 95 | + } else if is_cargo_env_var(var.as_str()) { |
| 96 | + cx.emit_err(errors::EnvNotDefined::CargoEnvVar { |
| 97 | + span, |
| 98 | + var: *symbol, |
| 99 | + var_expr: var_expr.ast_deref(), |
| 100 | + }); |
| 101 | + } else { |
| 102 | + cx.emit_err(errors::EnvNotDefined::CustomEnvVar { |
| 103 | + span, |
| 104 | + var: *symbol, |
| 105 | + var_expr: var_expr.ast_deref(), |
| 106 | + }); |
| 107 | + } |
| 108 | + |
100 | 109 | return DummyResult::any(sp);
|
101 | 110 | }
|
102 | 111 | Some(value) => cx.expr_str(sp, value),
|
103 | 112 | };
|
104 | 113 | MacEager::expr(e)
|
105 | 114 | }
|
106 | 115 |
|
107 |
| -fn help_for_missing_env_var(var: &str) -> errors::EnvNotDefinedHelp { |
108 |
| - if var.starts_with("CARGO_") |
| 116 | +/// Returns `true` if an environment variable from `env!` is one used by Cargo. |
| 117 | +fn is_cargo_env_var(var: &str) -> bool { |
| 118 | + var.starts_with("CARGO_") |
109 | 119 | || var.starts_with("DEP_")
|
110 | 120 | || matches!(var, "OUT_DIR" | "OPT_LEVEL" | "PROFILE" | "HOST" | "TARGET")
|
111 |
| - { |
112 |
| - errors::EnvNotDefinedHelp::CargoVar |
113 |
| - } else { |
114 |
| - errors::EnvNotDefinedHelp::Other |
115 |
| - } |
116 | 121 | }
|
0 commit comments