@@ -11,18 +11,19 @@ use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpa
11
11
use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
12
12
use rustc_span:: Span ;
13
13
use std:: env;
14
+ use std:: env:: VarError ;
14
15
use thin_vec:: thin_vec;
15
16
16
17
use crate :: errors;
17
18
18
- fn lookup_env < ' cx > ( cx : & ' cx ExtCtxt < ' _ > , var : Symbol ) -> Option < Symbol > {
19
+ fn lookup_env < ' cx > ( cx : & ' cx ExtCtxt < ' _ > , var : Symbol ) -> Result < Symbol , VarError > {
19
20
let var = var. as_str ( ) ;
20
21
if let Some ( value) = cx. sess . opts . logical_env . get ( var) {
21
- return Some ( Symbol :: intern ( value) ) ;
22
+ return Ok ( Symbol :: intern ( value) ) ;
22
23
}
23
24
// If the environment variable was not defined with the `--env-set` option, we try to retrieve it
24
25
// from rustc's environment.
25
- env:: var ( var) . ok ( ) . as_deref ( ) . map ( Symbol :: intern )
26
+ Ok ( Symbol :: intern ( & env:: var ( var) ? ) )
26
27
}
27
28
28
29
pub fn expand_option_env < ' cx > (
@@ -39,7 +40,7 @@ pub fn expand_option_env<'cx>(
39
40
} ;
40
41
41
42
let sp = cx. with_def_site_ctxt ( sp) ;
42
- let value = lookup_env ( cx, var) ;
43
+ let value = lookup_env ( cx, var) . ok ( ) ;
43
44
cx. sess . psess . env_depinfo . borrow_mut ( ) . insert ( ( var, value) ) ;
44
45
let e = match value {
45
46
None => {
@@ -108,35 +109,43 @@ pub fn expand_env<'cx>(
108
109
109
110
let span = cx. with_def_site_ctxt ( sp) ;
110
111
let value = lookup_env ( cx, var) ;
111
- cx. sess . psess . env_depinfo . borrow_mut ( ) . insert ( ( var, value) ) ;
112
+ cx. sess . psess . env_depinfo . borrow_mut ( ) . insert ( ( var, value. as_ref ( ) . ok ( ) . copied ( ) ) ) ;
112
113
let e = match value {
113
- None => {
114
+ Err ( err ) => {
114
115
let ExprKind :: Lit ( token:: Lit {
115
116
kind : LitKind :: Str | LitKind :: StrRaw ( ..) , symbol, ..
116
117
} ) = & var_expr. kind
117
118
else {
118
119
unreachable ! ( "`expr_to_string` ensures this is a string lit" )
119
120
} ;
120
121
121
- let guar = if let Some ( msg_from_user) = custom_msg {
122
- cx. dcx ( ) . emit_err ( errors:: EnvNotDefinedWithUserMessage { span, msg_from_user } )
123
- } else if is_cargo_env_var ( var. as_str ( ) ) {
124
- cx. dcx ( ) . emit_err ( errors:: EnvNotDefined :: CargoEnvVar {
125
- span,
126
- var : * symbol,
127
- var_expr : var_expr. ast_deref ( ) ,
128
- } )
129
- } else {
130
- cx. dcx ( ) . emit_err ( errors:: EnvNotDefined :: CustomEnvVar {
131
- span,
132
- var : * symbol,
133
- var_expr : var_expr. ast_deref ( ) ,
134
- } )
122
+ let guar = match err {
123
+ VarError :: NotPresent => {
124
+ if let Some ( msg_from_user) = custom_msg {
125
+ cx. dcx ( )
126
+ . emit_err ( errors:: EnvNotDefinedWithUserMessage { span, msg_from_user } )
127
+ } else if is_cargo_env_var ( var. as_str ( ) ) {
128
+ cx. dcx ( ) . emit_err ( errors:: EnvNotDefined :: CargoEnvVar {
129
+ span,
130
+ var : * symbol,
131
+ var_expr : var_expr. ast_deref ( ) ,
132
+ } )
133
+ } else {
134
+ cx. dcx ( ) . emit_err ( errors:: EnvNotDefined :: CustomEnvVar {
135
+ span,
136
+ var : * symbol,
137
+ var_expr : var_expr. ast_deref ( ) ,
138
+ } )
139
+ }
140
+ }
141
+ VarError :: NotUnicode ( _) => {
142
+ cx. dcx ( ) . emit_err ( errors:: EnvNotUnicode { span, var : * symbol } )
143
+ }
135
144
} ;
136
145
137
146
return ExpandResult :: Ready ( DummyResult :: any ( sp, guar) ) ;
138
147
}
139
- Some ( value) => cx. expr_str ( span, value) ,
148
+ Ok ( value) => cx. expr_str ( span, value) ,
140
149
} ;
141
150
ExpandResult :: Ready ( MacEager :: expr ( e) )
142
151
}
0 commit comments