@@ -3,8 +3,8 @@ use std::cmp;
3
3
use rustc_data_structures:: fx:: FxIndexMap ;
4
4
use rustc_data_structures:: sorted_map:: SortedMap ;
5
5
use rustc_errors:: { Diag , MultiSpan } ;
6
- use rustc_hir:: { HirId , ItemLocalId } ;
7
- use rustc_macros:: HashStable ;
6
+ use rustc_hir:: { CRATE_HIR_ID , HirId , ItemLocalId } ;
7
+ use rustc_macros:: { Decodable , Encodable , HashStable } ;
8
8
use rustc_session:: Session ;
9
9
use rustc_session:: lint:: builtin:: { self , FORBIDDEN_LINT_GROUPS } ;
10
10
use rustc_session:: lint:: { FutureIncompatibilityReason , Level , Lint , LintExpectationId , LintId } ;
@@ -15,7 +15,7 @@ use tracing::instrument;
15
15
use crate :: ty:: TyCtxt ;
16
16
17
17
/// How a lint level was set.
18
- #[ derive( Clone , Copy , PartialEq , Eq , HashStable , Debug ) ]
18
+ #[ derive( Clone , Copy , PartialEq , Eq , Encodable , Decodable , HashStable , Debug ) ]
19
19
pub enum LintLevelSource {
20
20
/// Lint is at the default level as declared in rustc.
21
21
Default ,
@@ -119,7 +119,7 @@ impl ShallowLintLevelMap {
119
119
#[ instrument( level = "trace" , skip( self , tcx) , ret) ]
120
120
fn probe_for_lint_level (
121
121
& self ,
122
- tcx : TyCtxt < ' _ > ,
122
+ tcx : Option < TyCtxt < ' _ > > ,
123
123
id : LintId ,
124
124
start : HirId ,
125
125
) -> ( Option < Level > , LintLevelSource ) {
@@ -132,31 +132,38 @@ impl ShallowLintLevelMap {
132
132
let mut owner = start. owner ;
133
133
let mut specs = & self . specs ;
134
134
135
- for parent in tcx. hir ( ) . parent_id_iter ( start) {
136
- if parent. owner != owner {
137
- owner = parent. owner ;
138
- specs = & tcx. shallow_lint_levels_on ( owner) . specs ;
139
- }
140
- if let Some ( map) = specs. get ( & parent. local_id )
141
- && let Some ( & ( level, src) ) = map. get ( & id)
142
- {
143
- return ( Some ( level) , src) ;
135
+ if let Some ( tcx) = tcx {
136
+ for parent in tcx. hir ( ) . parent_id_iter ( start) {
137
+ if parent. owner != owner {
138
+ owner = parent. owner ;
139
+ specs = & tcx. shallow_lint_levels_on ( owner) . specs ;
140
+ }
141
+ if let Some ( map) = specs. get ( & parent. local_id )
142
+ && let Some ( & ( level, src) ) = map. get ( & id)
143
+ {
144
+ return ( Some ( level) , src) ;
145
+ }
144
146
}
145
147
}
146
148
147
149
( None , LintLevelSource :: Default )
148
150
}
149
151
150
152
/// Fetch and return the user-visible lint level for the given lint at the given HirId.
151
- #[ instrument( level = "trace" , skip( self , tcx) , ret) ]
153
+ #[ instrument( level = "trace" , skip( self , tcx, sess ) , ret) ]
152
154
pub fn lint_level_id_at_node (
153
155
& self ,
154
- tcx : TyCtxt < ' _ > ,
156
+ tcx : Option < TyCtxt < ' _ > > ,
157
+ sess : & Session ,
155
158
lint : LintId ,
156
159
cur : HirId ,
157
160
) -> ( Level , LintLevelSource ) {
161
+ assert ! (
162
+ tcx. is_some( ) || cur == CRATE_HIR_ID ,
163
+ "must pass in a tcx to access any level other than the root"
164
+ ) ;
158
165
let ( level, mut src) = self . probe_for_lint_level ( tcx, lint, cur) ;
159
- let level = reveal_actual_level ( level, & mut src, tcx . sess , lint, |lint| {
166
+ let level = reveal_actual_level ( level, & mut src, sess, lint, |lint| {
160
167
self . probe_for_lint_level ( tcx, lint, cur)
161
168
} ) ;
162
169
( level, src)
@@ -166,14 +173,19 @@ impl ShallowLintLevelMap {
166
173
impl TyCtxt < ' _ > {
167
174
/// Fetch and return the user-visible lint level for the given lint at the given HirId.
168
175
pub fn lint_level_at_node ( self , lint : & ' static Lint , id : HirId ) -> ( Level , LintLevelSource ) {
169
- self . shallow_lint_levels_on ( id. owner ) . lint_level_id_at_node ( self , LintId :: of ( lint) , id)
176
+ self . shallow_lint_levels_on ( id. owner ) . lint_level_id_at_node (
177
+ Some ( self ) ,
178
+ self . sess ,
179
+ LintId :: of ( lint) ,
180
+ id,
181
+ )
170
182
}
171
183
}
172
184
173
185
/// This struct represents a lint expectation and holds all required information
174
186
/// to emit the `unfulfilled_lint_expectations` lint if it is unfulfilled after
175
187
/// the `LateLintPass` has completed.
176
- #[ derive( Clone , Debug , HashStable ) ]
188
+ #[ derive( Clone , Debug , Encodable , Decodable , HashStable ) ]
177
189
pub struct LintExpectation {
178
190
/// The reason for this expectation that can optionally be added as part of
179
191
/// the attribute. It will be displayed as part of the lint message.
0 commit comments