1
1
//! Check whether a type has (potentially) non-trivial drop glue.
2
2
3
3
use rustc_data_structures:: fx:: FxHashSet ;
4
- use rustc_hir as hir;
5
4
use rustc_hir:: def_id:: DefId ;
6
- use rustc_infer:: infer:: TyCtxtInferExt ;
7
5
use rustc_middle:: ty:: subst:: Subst ;
8
6
use rustc_middle:: ty:: util:: { needs_drop_components, AlwaysRequiresDrop } ;
9
7
use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
10
8
use rustc_session:: Limit ;
11
9
use rustc_span:: { sym, DUMMY_SP } ;
12
- use rustc_trait_selection:: traits:: { Obligation , ObligationCause , SelectionContext } ;
13
10
14
11
type NeedsDropResult < T > = Result < T , AlwaysRequiresDrop > ;
15
12
16
- fn needs_drop_raw < ' tcx > (
17
- tcx : TyCtxt < ' tcx > ,
18
- query : ty:: ParamEnvAnd < ' tcx , Ty < ' tcx > > ,
19
- needs_non_const_drop : bool ,
20
- ) -> bool {
13
+ fn needs_drop_raw < ' tcx > ( tcx : TyCtxt < ' tcx > , query : ty:: ParamEnvAnd < ' tcx , Ty < ' tcx > > ) -> bool {
14
+ let adt_components =
15
+ move |adt_def : & ty:: AdtDef | tcx. adt_drop_tys ( adt_def. did ) . map ( |tys| tys. iter ( ) ) ;
16
+
21
17
// If we don't know a type doesn't need drop, for example if it's a type
22
18
// parameter without a `Copy` bound, then we conservatively return that it
23
19
// needs drop.
24
- let res = if needs_non_const_drop {
25
- let adt_components = move |adt_def : & ty:: AdtDef | {
26
- tcx. adt_drop_tys_non_const ( adt_def. did ) . map ( |tys| tys. iter ( ) )
27
- } ;
28
- NeedsDropTypes :: new ( tcx, query. param_env , query. value , adt_components, needs_non_const_drop)
29
- . next ( )
30
- . is_some ( )
31
- } else {
32
- let adt_components =
33
- move |adt_def : & ty:: AdtDef | tcx. adt_drop_tys ( adt_def. did ) . map ( |tys| tys. iter ( ) ) ;
34
- NeedsDropTypes :: new ( tcx, query. param_env , query. value , adt_components, needs_non_const_drop)
35
- . next ( )
36
- . is_some ( )
37
- } ;
20
+ let res =
21
+ NeedsDropTypes :: new ( tcx, query. param_env , query. value , adt_components) . next ( ) . is_some ( ) ;
38
22
39
23
debug ! ( "needs_drop_raw({:?}) = {:?}" , query, res) ;
40
24
res
@@ -46,10 +30,9 @@ fn has_significant_drop_raw<'tcx>(
46
30
) -> bool {
47
31
let significant_drop_fields =
48
32
move |adt_def : & ty:: AdtDef | tcx. adt_significant_drop_tys ( adt_def. did ) . map ( |tys| tys. iter ( ) ) ;
49
- let res =
50
- NeedsDropTypes :: new ( tcx, query. param_env , query. value , significant_drop_fields, false )
51
- . next ( )
52
- . is_some ( ) ;
33
+ let res = NeedsDropTypes :: new ( tcx, query. param_env , query. value , significant_drop_fields)
34
+ . next ( )
35
+ . is_some ( ) ;
53
36
debug ! ( "has_significant_drop_raw({:?}) = {:?}" , query, res) ;
54
37
res
55
38
}
@@ -66,7 +49,6 @@ struct NeedsDropTypes<'tcx, F> {
66
49
unchecked_tys : Vec < ( Ty < ' tcx > , usize ) > ,
67
50
recursion_limit : Limit ,
68
51
adt_components : F ,
69
- needs_non_const_drop : bool ,
70
52
}
71
53
72
54
impl < ' tcx , F > NeedsDropTypes < ' tcx , F > {
@@ -75,7 +57,6 @@ impl<'tcx, F> NeedsDropTypes<'tcx, F> {
75
57
param_env : ty:: ParamEnv < ' tcx > ,
76
58
ty : Ty < ' tcx > ,
77
59
adt_components : F ,
78
- needs_non_const_drop : bool ,
79
60
) -> Self {
80
61
let mut seen_tys = FxHashSet :: default ( ) ;
81
62
seen_tys. insert ( ty) ;
@@ -87,7 +68,6 @@ impl<'tcx, F> NeedsDropTypes<'tcx, F> {
87
68
unchecked_tys : vec ! [ ( ty, 0 ) ] ,
88
69
recursion_limit : tcx. recursion_limit ( ) ,
89
70
adt_components,
90
- needs_non_const_drop,
91
71
}
92
72
}
93
73
}
@@ -170,35 +150,6 @@ where
170
150
queue_type ( self , subst_ty) ;
171
151
}
172
152
}
173
- ty:: Param ( _)
174
- if self . needs_non_const_drop && self . tcx . features ( ) . const_trait_impl =>
175
- {
176
- // Check if the param is bounded to have a `~const Drop` impl.
177
- let drop_trait = self . tcx . require_lang_item ( hir:: LangItem :: Drop , None ) ;
178
- let trait_ref = ty:: TraitRef {
179
- def_id : drop_trait,
180
- substs : self . tcx . mk_substs_trait ( component, & [ ] ) ,
181
- } ;
182
-
183
- let obligation = Obligation :: new (
184
- ObligationCause :: dummy ( ) ,
185
- self . param_env ,
186
- ty:: Binder :: dummy ( ty:: TraitPredicate {
187
- trait_ref,
188
- constness : ty:: BoundConstness :: ConstIfConst ,
189
- } ) ,
190
- ) ;
191
-
192
- let implsrc = tcx. infer_ctxt ( ) . enter ( |infcx| {
193
- let mut selcx =
194
- SelectionContext :: with_constness ( & infcx, hir:: Constness :: Const ) ;
195
- selcx. select ( & obligation)
196
- } ) ;
197
-
198
- if let Ok ( Some ( _) ) = implsrc {
199
- return None ;
200
- }
201
- }
202
153
ty:: Array ( ..) | ty:: Opaque ( ..) | ty:: Projection ( ..) | ty:: Param ( _) => {
203
154
if ty == component {
204
155
// Return the type to the caller: they may be able
@@ -228,7 +179,6 @@ fn adt_drop_tys_helper(
228
179
tcx : TyCtxt < ' _ > ,
229
180
def_id : DefId ,
230
181
adt_has_dtor : impl Fn ( & ty:: AdtDef ) -> bool ,
231
- needs_non_const_drop : bool ,
232
182
) -> Result < & ty:: List < Ty < ' _ > > , AlwaysRequiresDrop > {
233
183
let adt_components = move |adt_def : & ty:: AdtDef | {
234
184
if adt_def. is_manually_drop ( ) {
@@ -247,25 +197,15 @@ fn adt_drop_tys_helper(
247
197
let adt_ty = tcx. type_of ( def_id) ;
248
198
let param_env = tcx. param_env ( def_id) ;
249
199
let res: Result < Vec < _ > , _ > =
250
- NeedsDropTypes :: new ( tcx, param_env, adt_ty, adt_components, needs_non_const_drop ) . collect ( ) ;
200
+ NeedsDropTypes :: new ( tcx, param_env, adt_ty, adt_components) . collect ( ) ;
251
201
252
202
debug ! ( "adt_drop_tys(`{}`) = `{:?}`" , tcx. def_path_str( def_id) , res) ;
253
203
res. map ( |components| tcx. intern_type_list ( & components) )
254
204
}
255
205
256
206
fn adt_drop_tys ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> Result < & ty:: List < Ty < ' _ > > , AlwaysRequiresDrop > {
257
207
let adt_has_dtor = |adt_def : & ty:: AdtDef | adt_def. destructor ( tcx) . is_some ( ) ;
258
- adt_drop_tys_helper ( tcx, def_id, adt_has_dtor, false )
259
- }
260
-
261
- fn adt_drop_tys_non_const (
262
- tcx : TyCtxt < ' _ > ,
263
- def_id : DefId ,
264
- ) -> Result < & ty:: List < Ty < ' _ > > , AlwaysRequiresDrop > {
265
- let adt_has_dtor = |adt_def : & ty:: AdtDef | {
266
- adt_def. destructor ( tcx) . map ( |d| d. constness ) == Some ( hir:: Constness :: NotConst )
267
- } ;
268
- adt_drop_tys_helper ( tcx, def_id, adt_has_dtor, true )
208
+ adt_drop_tys_helper ( tcx, def_id, adt_has_dtor)
269
209
}
270
210
271
211
fn adt_significant_drop_tys (
@@ -278,16 +218,14 @@ fn adt_significant_drop_tys(
278
218
. map ( |dtor| !tcx. has_attr ( dtor. did , sym:: rustc_insignificant_dtor) )
279
219
. unwrap_or ( false )
280
220
} ;
281
- adt_drop_tys_helper ( tcx, def_id, adt_has_dtor, false )
221
+ adt_drop_tys_helper ( tcx, def_id, adt_has_dtor)
282
222
}
283
223
284
224
pub ( crate ) fn provide ( providers : & mut ty:: query:: Providers ) {
285
225
* providers = ty:: query:: Providers {
286
- needs_drop_raw : |tcx, query| needs_drop_raw ( tcx, query, false ) ,
287
- needs_non_const_drop_raw : |tcx, query| needs_drop_raw ( tcx, query, true ) ,
226
+ needs_drop_raw,
288
227
has_significant_drop_raw,
289
228
adt_drop_tys,
290
- adt_drop_tys_non_const,
291
229
adt_significant_drop_tys,
292
230
..* providers
293
231
} ;
0 commit comments