@@ -3,8 +3,8 @@ use std::cmp;
33use rustc_data_structures:: fx:: FxIndexMap ;
44use rustc_data_structures:: sorted_map:: SortedMap ;
55use 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 } ;
88use rustc_session:: Session ;
99use rustc_session:: lint:: builtin:: { self , FORBIDDEN_LINT_GROUPS } ;
1010use rustc_session:: lint:: { FutureIncompatibilityReason , Level , Lint , LintExpectationId , LintId } ;
@@ -15,7 +15,7 @@ use tracing::instrument;
1515use crate :: ty:: TyCtxt ;
1616
1717/// How a lint level was set.
18- #[ derive( Clone , Copy , PartialEq , Eq , HashStable , Debug ) ]
18+ #[ derive( Clone , Copy , PartialEq , Eq , Encodable , Decodable , HashStable , Debug ) ]
1919pub enum LintLevelSource {
2020 /// Lint is at the default level as declared in rustc.
2121 Default ,
@@ -119,7 +119,7 @@ impl ShallowLintLevelMap {
119119 #[ instrument( level = "trace" , skip( self , tcx) , ret) ]
120120 fn probe_for_lint_level (
121121 & self ,
122- tcx : TyCtxt < ' _ > ,
122+ tcx : Option < TyCtxt < ' _ > > ,
123123 id : LintId ,
124124 start : HirId ,
125125 ) -> ( Option < Level > , LintLevelSource ) {
@@ -132,31 +132,38 @@ impl ShallowLintLevelMap {
132132 let mut owner = start. owner ;
133133 let mut specs = & self . specs ;
134134
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+ }
144146 }
145147 }
146148
147149 ( None , LintLevelSource :: Default )
148150 }
149151
150152 /// 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) ]
152154 pub fn lint_level_id_at_node (
153155 & self ,
154- tcx : TyCtxt < ' _ > ,
156+ tcx : Option < TyCtxt < ' _ > > ,
157+ sess : & Session ,
155158 lint : LintId ,
156159 cur : HirId ,
157160 ) -> ( 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+ ) ;
158165 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| {
160167 self . probe_for_lint_level ( tcx, lint, cur)
161168 } ) ;
162169 ( level, src)
@@ -166,14 +173,19 @@ impl ShallowLintLevelMap {
166173impl TyCtxt < ' _ > {
167174 /// Fetch and return the user-visible lint level for the given lint at the given HirId.
168175 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+ )
170182 }
171183}
172184
173185/// This struct represents a lint expectation and holds all required information
174186/// to emit the `unfulfilled_lint_expectations` lint if it is unfulfilled after
175187/// the `LateLintPass` has completed.
176- #[ derive( Clone , Debug , HashStable ) ]
188+ #[ derive( Clone , Debug , Encodable , Decodable , HashStable ) ]
177189pub struct LintExpectation {
178190 /// The reason for this expectation that can optionally be added as part of
179191 /// the attribute. It will be displayed as part of the lint message.
0 commit comments