File tree 8 files changed +66
-0
lines changed
test/run-make/env-dep-info
8 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -157,6 +157,7 @@ macro_rules! define_handles {
157
157
}
158
158
define_handles ! {
159
159
' owned:
160
+ FreeFunctions ,
160
161
TokenStream ,
161
162
TokenStreamBuilder ,
162
163
TokenStreamIter ,
Original file line number Diff line number Diff line change @@ -52,6 +52,10 @@ use std::thread;
52
52
macro_rules! with_api {
53
53
( $S: ident, $self: ident, $m: ident) => {
54
54
$m! {
55
+ FreeFunctions {
56
+ fn drop( $self: $S:: FreeFunctions ) ;
57
+ fn track_env_var( var: & str , value: Option <& str >) ;
58
+ } ,
55
59
TokenStream {
56
60
fn drop( $self: $S:: TokenStream ) ;
57
61
fn clone( $self: & $S:: TokenStream ) -> $S:: TokenStream ;
Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ use super::client::HandleStore;
8
8
/// Declare an associated item of one of the traits below, optionally
9
9
/// adjusting it (i.e., adding bounds to types and default bodies to methods).
10
10
macro_rules! associated_item {
11
+ ( type FreeFunctions ) =>
12
+ ( type FreeFunctions : ' static ; ) ;
11
13
( type TokenStream ) =>
12
14
( type TokenStream : ' static + Clone ; ) ;
13
15
( type TokenStreamBuilder ) =>
Original file line number Diff line number Diff line change 24
24
#![ feature( decl_macro) ]
25
25
#![ feature( extern_types) ]
26
26
#![ feature( in_band_lifetimes) ]
27
+ #![ feature( inner_deref) ]
27
28
#![ feature( negative_impls) ]
28
29
#![ feature( optin_builtin_traits) ]
29
30
#![ feature( restricted_std) ]
@@ -1160,3 +1161,23 @@ impl fmt::Debug for Literal {
1160
1161
self . 0 . fmt ( f)
1161
1162
}
1162
1163
}
1164
+
1165
+ /// Tracked access to environment variables.
1166
+ #[ unstable( feature = "proc_macro_tracked_env" , issue = "74690" ) ]
1167
+ pub mod tracked_env {
1168
+ use std:: env:: { self , VarError } ;
1169
+ use std:: ffi:: OsStr ;
1170
+
1171
+ /// Retrieve an environment variable and add it to build dependency info.
1172
+ /// Build system executing the compiler will know that the variable was accessed during
1173
+ /// compilation, and will be able to rerun the build when the value of that variable changes.
1174
+ /// Besides the dependency tracking this function should be equivalent to `env::var` from the
1175
+ /// standard library, except that the argument must be UTF-8.
1176
+ #[ unstable( feature = "proc_macro_tracked_env" , issue = "74690" ) ]
1177
+ pub fn var < K : AsRef < OsStr > + AsRef < str > > ( key : K ) -> Result < String , VarError > {
1178
+ let key: & str = key. as_ref ( ) ;
1179
+ let value = env:: var ( key) ;
1180
+ crate :: bridge:: client:: FreeFunctions :: track_env_var ( key, value. as_deref ( ) . ok ( ) ) ;
1181
+ value
1182
+ }
1183
+ }
Original file line number Diff line number Diff line change @@ -274,6 +274,8 @@ impl ToInternal<rustc_errors::Level> for Level {
274
274
}
275
275
}
276
276
277
+ pub struct FreeFunctions ;
278
+
277
279
#[ derive( Clone ) ]
278
280
pub struct TokenStreamIter {
279
281
cursor : tokenstream:: Cursor ,
@@ -379,6 +381,7 @@ impl<'a> Rustc<'a> {
379
381
}
380
382
381
383
impl server:: Types for Rustc < ' _ > {
384
+ type FreeFunctions = FreeFunctions ;
382
385
type TokenStream = TokenStream ;
383
386
type TokenStreamBuilder = tokenstream:: TokenStreamBuilder ;
384
387
type TokenStreamIter = TokenStreamIter ;
@@ -392,6 +395,12 @@ impl server::Types for Rustc<'_> {
392
395
type Span = Span ;
393
396
}
394
397
398
+ impl server:: FreeFunctions for Rustc < ' _ > {
399
+ fn track_env_var ( & mut self , var : & str , value : Option < & str > ) {
400
+ self . sess . env_depinfo . borrow_mut ( ) . insert ( ( Symbol :: intern ( var) , value. map ( Symbol :: intern) ) ) ;
401
+ }
402
+ }
403
+
395
404
impl server:: TokenStream for Rustc < ' _ > {
396
405
fn new ( & mut self ) -> Self :: TokenStream {
397
406
TokenStream :: default ( )
Original file line number Diff line number Diff line change 1
1
-include ../../run-make-fulldeps/tools.mk
2
2
3
+ # FIXME(eddyb) provide `HOST_RUSTC` and `TARGET_RUSTC`
4
+ # instead of hardcoding them everywhere they're needed.
5
+ ifeq ($(IS_MUSL_HOST ) ,1)
6
+ ADDITIONAL_ARGS := $(RUSTFLAGS )
7
+ endif
8
+
3
9
all :
4
10
EXISTING_ENV=1 EXISTING_OPT_ENV=1 $(RUSTC ) --emit dep-info main.rs
5
11
$(CGREP ) " # env-dep:EXISTING_ENV=1" < $(TMPDIR ) /main.d
6
12
$(CGREP ) " # env-dep:EXISTING_OPT_ENV=1" < $(TMPDIR ) /main.d
7
13
$(CGREP ) " # env-dep:NONEXISTENT_OPT_ENV" < $(TMPDIR ) /main.d
8
14
$(CGREP ) " # env-dep:ESCAPE\nESCAPE\\ " < $(TMPDIR ) /main.d
15
+ # Proc macro
16
+ $(BARE_RUSTC ) $(ADDITIONAL_ARGS ) --out-dir $(TMPDIR ) macro_def.rs
17
+ EXISTING_PROC_MACRO_ENV=1 $(RUSTC ) --emit dep-info macro_use.rs
18
+ $(CGREP ) " # env-dep:EXISTING_PROC_MACRO_ENV=1" < $(TMPDIR ) /macro_use.d
19
+ $(CGREP ) " # env-dep:NONEXISTENT_PROC_MACEO_ENV" < $(TMPDIR ) /macro_use.d
Original file line number Diff line number Diff line change
1
+ #![ feature( proc_macro_tracked_env) ]
2
+ #![ crate_type = "proc-macro" ]
3
+
4
+ extern crate proc_macro;
5
+ use proc_macro:: * ;
6
+
7
+ #[ proc_macro]
8
+ pub fn access_env_vars ( _: TokenStream ) -> TokenStream {
9
+ let _ = tracked_env:: var ( "EXISTING_PROC_MACRO_ENV" ) ;
10
+ let _ = tracked_env:: var ( "NONEXISTENT_PROC_MACEO_ENV" ) ;
11
+ TokenStream :: new ( )
12
+ }
Original file line number Diff line number Diff line change
1
+ #[ macro_use]
2
+ extern crate macro_def;
3
+
4
+ access_env_vars ! ( ) ;
5
+
6
+ fn main ( ) { }
You can’t perform that action at this time.
0 commit comments