@@ -13,8 +13,8 @@ use rustc_rayon_core as rayon_core;
13
13
/// This is the implicit state of rustc. It contains the current
14
14
/// query. It is updated when
15
15
/// executing a new query.
16
- #[ derive( Clone ) ]
17
- pub struct ImplicitCtxt < ' a > {
16
+ #[ derive( Clone , Default ) ]
17
+ struct ImplicitCtxt < ' a > {
18
18
/// The current query job, if any. This is updated by `JobOwner::start` in
19
19
/// `ty::query::plumbing` when executing a query.
20
20
query : Option < QueryJobId > ,
@@ -28,12 +28,6 @@ pub struct ImplicitCtxt<'a> {
28
28
task_deps : Option < & ' a Lock < TaskDeps > > ,
29
29
}
30
30
31
- impl < ' a > ImplicitCtxt < ' a > {
32
- pub fn new ( ) -> Self {
33
- ImplicitCtxt { query : None , diagnostics : None , task_deps : None }
34
- }
35
- }
36
-
37
31
/// Sets Rayon's thread-local variable, which is preserved for Rayon jobs
38
32
/// to `value` during the call to `f`. It is restored to its previous value after.
39
33
/// This is used to set the pointer to the new `ImplicitCtxt`.
@@ -78,7 +72,7 @@ fn get_tlv() -> usize {
78
72
79
73
/// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`.
80
74
#[ inline]
81
- pub fn enter_context < ' a , F , R > ( context : & ImplicitCtxt < ' a > , f : F ) -> R
75
+ fn enter_context < ' a , F , R > ( context : & ImplicitCtxt < ' a > , f : F ) -> R
82
76
where
83
77
F : FnOnce ( & ImplicitCtxt < ' a > ) -> R ,
84
78
{
@@ -91,26 +85,14 @@ fn with_context_opt<F, R>(f: F) -> R
91
85
where
92
86
F : for < ' a > FnOnce ( Option < & ImplicitCtxt < ' a > > ) -> R ,
93
87
{
94
- let context = get_tlv ( ) ;
95
- if context == 0 {
96
- f ( None )
97
- } else {
98
- // We could get a `ImplicitCtxt` pointer from another thread.
99
- // Ensure that `ImplicitCtxt` is `Sync`.
100
- sync:: assert_sync :: < ImplicitCtxt < ' _ > > ( ) ;
101
-
102
- unsafe { f ( Some ( & * ( context as * const ImplicitCtxt < ' _ > ) ) ) }
103
- }
104
- }
88
+ // We could get a `ImplicitCtxt` pointer from another thread.
89
+ // Ensure that `ImplicitCtxt` is `Sync`.
90
+ sync:: assert_sync :: < ImplicitCtxt < ' _ > > ( ) ;
105
91
106
- /// Allows access to the current `ImplicitCtxt`.
107
- /// Panics if there is no `ImplicitCtxt` available.
108
- #[ inline]
109
- fn with_context < F , R > ( f : F ) -> R
110
- where
111
- F : for < ' a > FnOnce ( & ImplicitCtxt < ' a > ) -> R ,
112
- {
113
- with_context_opt ( |opt_context| f ( opt_context. expect ( "no ImplicitCtxt stored in tls" ) ) )
92
+ let context = get_tlv ( ) ;
93
+ let context =
94
+ if context == 0 { None } else { unsafe { Some ( & * ( context as * const ImplicitCtxt < ' _ > ) ) } } ;
95
+ f ( context)
114
96
}
115
97
116
98
/// This is a callback from `rustc_ast` as it cannot access the implicit state
@@ -131,8 +113,8 @@ pub fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
131
113
where
132
114
OP : FnOnce ( ) -> R ,
133
115
{
134
- crate :: tls:: with_context ( |icx| {
135
- let icx = crate :: tls:: ImplicitCtxt { task_deps, ..icx. clone ( ) } ;
116
+ crate :: tls:: with_context_opt ( |icx| {
117
+ let icx = crate :: tls:: ImplicitCtxt { task_deps, ..icx. cloned ( ) . unwrap_or_default ( ) } ;
136
118
137
119
crate :: tls:: enter_context ( & icx, |_| op ( ) )
138
120
} )
@@ -142,16 +124,13 @@ pub fn read_deps<OP>(op: OP)
142
124
where
143
125
OP : for < ' a > FnOnce ( Option < & ' a Lock < TaskDeps > > ) ,
144
126
{
145
- crate :: tls:: with_context_opt ( |icx| {
146
- let icx = if let Some ( icx) = icx { icx } else { return } ;
147
- op ( icx. task_deps )
148
- } )
127
+ crate :: tls:: with_context_opt ( |icx| op ( icx. and_then ( |icx| icx. task_deps ) ) )
149
128
}
150
129
151
130
/// Get the query information from the TLS context.
152
131
#[ inline( always) ]
153
132
pub fn current_query_job ( ) -> Option < QueryJobId > {
154
- with_context ( |icx| icx. query )
133
+ with_context_opt ( |icx| icx? . query )
155
134
}
156
135
157
136
/// Executes a job by changing the `ImplicitCtxt` to point to the
@@ -163,10 +142,11 @@ pub fn start_query<R>(
163
142
diagnostics : Option < & Lock < ThinVec < Diagnostic > > > ,
164
143
compute : impl FnOnce ( ) -> R ,
165
144
) -> R {
166
- with_context ( move |current_icx| {
145
+ with_context_opt ( move |current_icx| {
146
+ let task_deps = current_icx. and_then ( |icx| icx. task_deps ) ;
147
+
167
148
// Update the `ImplicitCtxt` to point to our new query job.
168
- let new_icx =
169
- ImplicitCtxt { query : Some ( token) , diagnostics, task_deps : current_icx. task_deps } ;
149
+ let new_icx = ImplicitCtxt { query : Some ( token) , diagnostics, task_deps } ;
170
150
171
151
// Use the `ImplicitCtxt` while we execute the query.
172
152
enter_context ( & new_icx, |_| rustc_data_structures:: stack:: ensure_sufficient_stack ( compute) )
0 commit comments