66//! for their size, offset of a field, etc.).
77
88use rustc_hir:: { def:: DefKind , def_id:: DefId , ConstContext } ;
9- use rustc_index:: bit_set:: FiniteBitSet ;
109use rustc_middle:: mir:: {
1110 self ,
1211 visit:: { TyContext , Visitor } ,
@@ -17,12 +16,12 @@ use rustc_middle::ty::{
1716 query:: Providers ,
1817 subst:: SubstsRef ,
1918 visit:: { TypeSuperVisitable , TypeVisitable , TypeVisitor } ,
20- Const , Ty , TyCtxt ,
19+ Const , Ty , TyCtxt , UnusedGenericParams ,
2120} ;
2221use rustc_span:: symbol:: sym;
2322use std:: ops:: ControlFlow ;
2423
25- use crate :: errors:: UnusedGenericParams ;
24+ use crate :: errors:: UnusedGenericParamsHint ;
2625
2726/// Provide implementations of queries relating to polymorphization analysis.
2827pub fn provide ( providers : & mut Providers ) {
@@ -36,31 +35,30 @@ pub fn provide(providers: &mut Providers) {
3635fn unused_generic_params < ' tcx > (
3736 tcx : TyCtxt < ' tcx > ,
3837 instance : ty:: InstanceDef < ' tcx > ,
39- ) -> FiniteBitSet < u32 > {
38+ ) -> UnusedGenericParams {
4039 if !tcx. sess . opts . unstable_opts . polymorphize {
4140 // If polymorphization disabled, then all parameters are used.
42- return FiniteBitSet :: new_empty ( ) ;
41+ return UnusedGenericParams :: new_all_used ( ) ;
4342 }
4443
4544 let def_id = instance. def_id ( ) ;
4645 // Exit early if this instance should not be polymorphized.
4746 if !should_polymorphize ( tcx, def_id, instance) {
48- return FiniteBitSet :: new_empty ( ) ;
47+ return UnusedGenericParams :: new_all_used ( ) ;
4948 }
5049
5150 let generics = tcx. generics_of ( def_id) ;
5251 debug ! ( ?generics) ;
5352
5453 // Exit early when there are no parameters to be unused.
5554 if generics. count ( ) == 0 {
56- return FiniteBitSet :: new_empty ( ) ;
55+ return UnusedGenericParams :: new_all_used ( ) ;
5756 }
5857
5958 // Create a bitset with N rightmost ones for each parameter.
6059 let generics_count: u32 =
6160 generics. count ( ) . try_into ( ) . expect ( "more generic parameters than can fit into a `u32`" ) ;
62- let mut unused_parameters = FiniteBitSet :: < u32 > :: new_empty ( ) ;
63- unused_parameters. set_range ( 0 ..generics_count) ;
61+ let mut unused_parameters = UnusedGenericParams :: new_all_unused ( generics_count) ;
6462 debug ! ( ?unused_parameters, "(start)" ) ;
6563
6664 mark_used_by_default_parameters ( tcx, def_id, generics, & mut unused_parameters) ;
@@ -78,7 +76,7 @@ fn unused_generic_params<'tcx>(
7876 debug ! ( ?unused_parameters, "(end)" ) ;
7977
8078 // Emit errors for debugging and testing if enabled.
81- if !unused_parameters. is_empty ( ) {
79+ if !unused_parameters. all_used ( ) {
8280 emit_unused_generic_params_error ( tcx, def_id, generics, & unused_parameters) ;
8381 }
8482
@@ -136,13 +134,13 @@ fn mark_used_by_default_parameters<'tcx>(
136134 tcx : TyCtxt < ' tcx > ,
137135 def_id : DefId ,
138136 generics : & ' tcx ty:: Generics ,
139- unused_parameters : & mut FiniteBitSet < u32 > ,
137+ unused_parameters : & mut UnusedGenericParams ,
140138) {
141139 match tcx. def_kind ( def_id) {
142140 DefKind :: Closure | DefKind :: Generator => {
143141 for param in & generics. params {
144142 debug ! ( ?param, "(closure/gen)" ) ;
145- unused_parameters. clear ( param. index ) ;
143+ unused_parameters. mark_used ( param. index ) ;
146144 }
147145 }
148146 DefKind :: Mod
@@ -178,7 +176,7 @@ fn mark_used_by_default_parameters<'tcx>(
178176 for param in & generics. params {
179177 debug ! ( ?param, "(other)" ) ;
180178 if let ty:: GenericParamDefKind :: Lifetime = param. kind {
181- unused_parameters. clear ( param. index ) ;
179+ unused_parameters. mark_used ( param. index ) ;
182180 }
183181 }
184182 }
@@ -196,7 +194,7 @@ fn emit_unused_generic_params_error<'tcx>(
196194 tcx : TyCtxt < ' tcx > ,
197195 def_id : DefId ,
198196 generics : & ' tcx ty:: Generics ,
199- unused_parameters : & FiniteBitSet < u32 > ,
197+ unused_parameters : & UnusedGenericParams ,
200198) {
201199 let base_def_id = tcx. typeck_root_def_id ( def_id) ;
202200 if !tcx. has_attr ( base_def_id, sym:: rustc_polymorphize_error) {
@@ -213,7 +211,7 @@ fn emit_unused_generic_params_error<'tcx>(
213211 let mut next_generics = Some ( generics) ;
214212 while let Some ( generics) = next_generics {
215213 for param in & generics. params {
216- if unused_parameters. contains ( param. index ) . unwrap_or ( false ) {
214+ if unused_parameters. is_unused ( param. index ) {
217215 debug ! ( ?param) ;
218216 let def_span = tcx. def_span ( param. def_id ) ;
219217 param_spans. push ( def_span) ;
@@ -224,14 +222,14 @@ fn emit_unused_generic_params_error<'tcx>(
224222 next_generics = generics. parent . map ( |did| tcx. generics_of ( did) ) ;
225223 }
226224
227- tcx. sess . emit_err ( UnusedGenericParams { span : fn_span, param_spans, param_names } ) ;
225+ tcx. sess . emit_err ( UnusedGenericParamsHint { span : fn_span, param_spans, param_names } ) ;
228226}
229227
230228/// Visitor used to aggregate generic parameter uses.
231229struct MarkUsedGenericParams < ' a , ' tcx > {
232230 tcx : TyCtxt < ' tcx > ,
233231 def_id : DefId ,
234- unused_parameters : & ' a mut FiniteBitSet < u32 > ,
232+ unused_parameters : & ' a mut UnusedGenericParams ,
235233}
236234
237235impl < ' a , ' tcx > MarkUsedGenericParams < ' a , ' tcx > {
@@ -244,7 +242,7 @@ impl<'a, 'tcx> MarkUsedGenericParams<'a, 'tcx> {
244242 debug ! ( ?self . unused_parameters, ?unused) ;
245243 for ( i, arg) in substs. iter ( ) . enumerate ( ) {
246244 let i = i. try_into ( ) . unwrap ( ) ;
247- if ! unused. contains ( i ) . unwrap_or ( false ) {
245+ if unused. is_used ( i ) {
248246 arg. visit_with ( self ) ;
249247 }
250248 }
@@ -308,7 +306,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
308306 match c. kind ( ) {
309307 ty:: ConstKind :: Param ( param) => {
310308 debug ! ( ?param) ;
311- self . unused_parameters . clear ( param. index ) ;
309+ self . unused_parameters . mark_used ( param. index ) ;
312310 ControlFlow :: CONTINUE
313311 }
314312 ty:: ConstKind :: Unevaluated ( ty:: UnevaluatedConst { def, substs } )
@@ -342,55 +340,10 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
342340 }
343341 ty:: Param ( param) => {
344342 debug ! ( ?param) ;
345- self . unused_parameters . clear ( param. index ) ;
343+ self . unused_parameters . mark_used ( param. index ) ;
346344 ControlFlow :: CONTINUE
347345 }
348346 _ => ty. super_visit_with ( self ) ,
349347 }
350348 }
351349}
352-
353- /// Visitor used to check if a generic parameter is used.
354- struct HasUsedGenericParams < ' a > {
355- unused_parameters : & ' a FiniteBitSet < u32 > ,
356- }
357-
358- impl < ' a , ' tcx > TypeVisitor < ' tcx > for HasUsedGenericParams < ' a > {
359- type BreakTy = ( ) ;
360-
361- #[ instrument( level = "debug" , skip( self ) ) ]
362- fn visit_const ( & mut self , c : Const < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
363- if !c. has_non_region_param ( ) {
364- return ControlFlow :: CONTINUE ;
365- }
366-
367- match c. kind ( ) {
368- ty:: ConstKind :: Param ( param) => {
369- if self . unused_parameters . contains ( param. index ) . unwrap_or ( false ) {
370- ControlFlow :: CONTINUE
371- } else {
372- ControlFlow :: BREAK
373- }
374- }
375- _ => c. super_visit_with ( self ) ,
376- }
377- }
378-
379- #[ instrument( level = "debug" , skip( self ) ) ]
380- fn visit_ty ( & mut self , ty : Ty < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
381- if !ty. has_non_region_param ( ) {
382- return ControlFlow :: CONTINUE ;
383- }
384-
385- match ty. kind ( ) {
386- ty:: Param ( param) => {
387- if self . unused_parameters . contains ( param. index ) . unwrap_or ( false ) {
388- ControlFlow :: CONTINUE
389- } else {
390- ControlFlow :: BREAK
391- }
392- }
393- _ => ty. super_visit_with ( self ) ,
394- }
395- }
396- }
0 commit comments