6
6
//! for their size, offset of a field, etc.).
7
7
8
8
use rustc_hir:: { def:: DefKind , def_id:: DefId , ConstContext } ;
9
- use rustc_index:: bit_set:: FiniteBitSet ;
10
9
use rustc_middle:: mir:: {
11
10
self ,
12
11
visit:: { TyContext , Visitor } ,
@@ -17,12 +16,12 @@ use rustc_middle::ty::{
17
16
query:: Providers ,
18
17
subst:: SubstsRef ,
19
18
visit:: { TypeSuperVisitable , TypeVisitable , TypeVisitor } ,
20
- Const , Ty , TyCtxt ,
19
+ Const , Ty , TyCtxt , UnusedGenericParams ,
21
20
} ;
22
21
use rustc_span:: symbol:: sym;
23
22
use std:: ops:: ControlFlow ;
24
23
25
- use crate :: errors:: UnusedGenericParams ;
24
+ use crate :: errors:: UnusedGenericParamsHint ;
26
25
27
26
/// Provide implementations of queries relating to polymorphization analysis.
28
27
pub fn provide ( providers : & mut Providers ) {
@@ -36,31 +35,30 @@ pub fn provide(providers: &mut Providers) {
36
35
fn unused_generic_params < ' tcx > (
37
36
tcx : TyCtxt < ' tcx > ,
38
37
instance : ty:: InstanceDef < ' tcx > ,
39
- ) -> FiniteBitSet < u32 > {
38
+ ) -> UnusedGenericParams {
40
39
if !tcx. sess . opts . unstable_opts . polymorphize {
41
40
// If polymorphization disabled, then all parameters are used.
42
- return FiniteBitSet :: new_empty ( ) ;
41
+ return UnusedGenericParams :: new_all_used ( ) ;
43
42
}
44
43
45
44
let def_id = instance. def_id ( ) ;
46
45
// Exit early if this instance should not be polymorphized.
47
46
if !should_polymorphize ( tcx, def_id, instance) {
48
- return FiniteBitSet :: new_empty ( ) ;
47
+ return UnusedGenericParams :: new_all_used ( ) ;
49
48
}
50
49
51
50
let generics = tcx. generics_of ( def_id) ;
52
51
debug ! ( ?generics) ;
53
52
54
53
// Exit early when there are no parameters to be unused.
55
54
if generics. count ( ) == 0 {
56
- return FiniteBitSet :: new_empty ( ) ;
55
+ return UnusedGenericParams :: new_all_used ( ) ;
57
56
}
58
57
59
58
// Create a bitset with N rightmost ones for each parameter.
60
59
let generics_count: u32 =
61
60
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) ;
64
62
debug ! ( ?unused_parameters, "(start)" ) ;
65
63
66
64
mark_used_by_default_parameters ( tcx, def_id, generics, & mut unused_parameters) ;
@@ -78,7 +76,7 @@ fn unused_generic_params<'tcx>(
78
76
debug ! ( ?unused_parameters, "(end)" ) ;
79
77
80
78
// Emit errors for debugging and testing if enabled.
81
- if !unused_parameters. is_empty ( ) {
79
+ if !unused_parameters. all_used ( ) {
82
80
emit_unused_generic_params_error ( tcx, def_id, generics, & unused_parameters) ;
83
81
}
84
82
@@ -136,13 +134,13 @@ fn mark_used_by_default_parameters<'tcx>(
136
134
tcx : TyCtxt < ' tcx > ,
137
135
def_id : DefId ,
138
136
generics : & ' tcx ty:: Generics ,
139
- unused_parameters : & mut FiniteBitSet < u32 > ,
137
+ unused_parameters : & mut UnusedGenericParams ,
140
138
) {
141
139
match tcx. def_kind ( def_id) {
142
140
DefKind :: Closure | DefKind :: Generator => {
143
141
for param in & generics. params {
144
142
debug ! ( ?param, "(closure/gen)" ) ;
145
- unused_parameters. clear ( param. index ) ;
143
+ unused_parameters. mark_used ( param. index ) ;
146
144
}
147
145
}
148
146
DefKind :: Mod
@@ -178,7 +176,7 @@ fn mark_used_by_default_parameters<'tcx>(
178
176
for param in & generics. params {
179
177
debug ! ( ?param, "(other)" ) ;
180
178
if let ty:: GenericParamDefKind :: Lifetime = param. kind {
181
- unused_parameters. clear ( param. index ) ;
179
+ unused_parameters. mark_used ( param. index ) ;
182
180
}
183
181
}
184
182
}
@@ -196,7 +194,7 @@ fn emit_unused_generic_params_error<'tcx>(
196
194
tcx : TyCtxt < ' tcx > ,
197
195
def_id : DefId ,
198
196
generics : & ' tcx ty:: Generics ,
199
- unused_parameters : & FiniteBitSet < u32 > ,
197
+ unused_parameters : & UnusedGenericParams ,
200
198
) {
201
199
let base_def_id = tcx. typeck_root_def_id ( def_id) ;
202
200
if !tcx. has_attr ( base_def_id, sym:: rustc_polymorphize_error) {
@@ -213,7 +211,7 @@ fn emit_unused_generic_params_error<'tcx>(
213
211
let mut next_generics = Some ( generics) ;
214
212
while let Some ( generics) = next_generics {
215
213
for param in & generics. params {
216
- if unused_parameters. contains ( param. index ) . unwrap_or ( false ) {
214
+ if unused_parameters. is_unused ( param. index ) {
217
215
debug ! ( ?param) ;
218
216
let def_span = tcx. def_span ( param. def_id ) ;
219
217
param_spans. push ( def_span) ;
@@ -224,14 +222,14 @@ fn emit_unused_generic_params_error<'tcx>(
224
222
next_generics = generics. parent . map ( |did| tcx. generics_of ( did) ) ;
225
223
}
226
224
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 } ) ;
228
226
}
229
227
230
228
/// Visitor used to aggregate generic parameter uses.
231
229
struct MarkUsedGenericParams < ' a , ' tcx > {
232
230
tcx : TyCtxt < ' tcx > ,
233
231
def_id : DefId ,
234
- unused_parameters : & ' a mut FiniteBitSet < u32 > ,
232
+ unused_parameters : & ' a mut UnusedGenericParams ,
235
233
}
236
234
237
235
impl < ' a , ' tcx > MarkUsedGenericParams < ' a , ' tcx > {
@@ -244,7 +242,7 @@ impl<'a, 'tcx> MarkUsedGenericParams<'a, 'tcx> {
244
242
debug ! ( ?self . unused_parameters, ?unused) ;
245
243
for ( i, arg) in substs. iter ( ) . enumerate ( ) {
246
244
let i = i. try_into ( ) . unwrap ( ) ;
247
- if ! unused. contains ( i ) . unwrap_or ( false ) {
245
+ if unused. is_used ( i ) {
248
246
arg. visit_with ( self ) ;
249
247
}
250
248
}
@@ -308,7 +306,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
308
306
match c. kind ( ) {
309
307
ty:: ConstKind :: Param ( param) => {
310
308
debug ! ( ?param) ;
311
- self . unused_parameters . clear ( param. index ) ;
309
+ self . unused_parameters . mark_used ( param. index ) ;
312
310
ControlFlow :: CONTINUE
313
311
}
314
312
ty:: ConstKind :: Unevaluated ( ty:: UnevaluatedConst { def, substs } )
@@ -342,55 +340,10 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
342
340
}
343
341
ty:: Param ( param) => {
344
342
debug ! ( ?param) ;
345
- self . unused_parameters . clear ( param. index ) ;
343
+ self . unused_parameters . mark_used ( param. index ) ;
346
344
ControlFlow :: CONTINUE
347
345
}
348
346
_ => ty. super_visit_with ( self ) ,
349
347
}
350
348
}
351
349
}
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