11//! Check whether a type has (potentially) non-trivial drop glue.
22
33use rustc_data_structures:: fx:: FxHashSet ;
4- use rustc_hir as hir;
54use rustc_hir:: def_id:: DefId ;
6- use rustc_infer:: infer:: TyCtxtInferExt ;
75use rustc_middle:: ty:: subst:: Subst ;
86use rustc_middle:: ty:: util:: { needs_drop_components, AlwaysRequiresDrop } ;
97use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
108use rustc_session:: Limit ;
119use rustc_span:: { sym, DUMMY_SP } ;
12- use rustc_trait_selection:: traits:: { Obligation , ObligationCause , SelectionContext } ;
1310
1411type NeedsDropResult < T > = Result < T , AlwaysRequiresDrop > ;
1512
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+
2117 // If we don't know a type doesn't need drop, for example if it's a type
2218 // parameter without a `Copy` bound, then we conservatively return that it
2319 // 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 ( ) ;
3822
3923 debug ! ( "needs_drop_raw({:?}) = {:?}" , query, res) ;
4024 res
@@ -46,10 +30,9 @@ fn has_significant_drop_raw<'tcx>(
4630) -> bool {
4731 let significant_drop_fields =
4832 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 ( ) ;
5336 debug ! ( "has_significant_drop_raw({:?}) = {:?}" , query, res) ;
5437 res
5538}
@@ -66,7 +49,6 @@ struct NeedsDropTypes<'tcx, F> {
6649 unchecked_tys : Vec < ( Ty < ' tcx > , usize ) > ,
6750 recursion_limit : Limit ,
6851 adt_components : F ,
69- needs_non_const_drop : bool ,
7052}
7153
7254impl < ' tcx , F > NeedsDropTypes < ' tcx , F > {
@@ -75,7 +57,6 @@ impl<'tcx, F> NeedsDropTypes<'tcx, F> {
7557 param_env : ty:: ParamEnv < ' tcx > ,
7658 ty : Ty < ' tcx > ,
7759 adt_components : F ,
78- needs_non_const_drop : bool ,
7960 ) -> Self {
8061 let mut seen_tys = FxHashSet :: default ( ) ;
8162 seen_tys. insert ( ty) ;
@@ -87,7 +68,6 @@ impl<'tcx, F> NeedsDropTypes<'tcx, F> {
8768 unchecked_tys : vec ! [ ( ty, 0 ) ] ,
8869 recursion_limit : tcx. recursion_limit ( ) ,
8970 adt_components,
90- needs_non_const_drop,
9171 }
9272 }
9373}
@@ -170,35 +150,6 @@ where
170150 queue_type ( self , subst_ty) ;
171151 }
172152 }
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- }
202153 ty:: Array ( ..) | ty:: Opaque ( ..) | ty:: Projection ( ..) | ty:: Param ( _) => {
203154 if ty == component {
204155 // Return the type to the caller: they may be able
@@ -228,7 +179,6 @@ fn adt_drop_tys_helper(
228179 tcx : TyCtxt < ' _ > ,
229180 def_id : DefId ,
230181 adt_has_dtor : impl Fn ( & ty:: AdtDef ) -> bool ,
231- needs_non_const_drop : bool ,
232182) -> Result < & ty:: List < Ty < ' _ > > , AlwaysRequiresDrop > {
233183 let adt_components = move |adt_def : & ty:: AdtDef | {
234184 if adt_def. is_manually_drop ( ) {
@@ -247,25 +197,15 @@ fn adt_drop_tys_helper(
247197 let adt_ty = tcx. type_of ( def_id) ;
248198 let param_env = tcx. param_env ( def_id) ;
249199 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 ( ) ;
251201
252202 debug ! ( "adt_drop_tys(`{}`) = `{:?}`" , tcx. def_path_str( def_id) , res) ;
253203 res. map ( |components| tcx. intern_type_list ( & components) )
254204}
255205
256206fn adt_drop_tys ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> Result < & ty:: List < Ty < ' _ > > , AlwaysRequiresDrop > {
257207 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)
269209}
270210
271211fn adt_significant_drop_tys (
@@ -278,16 +218,14 @@ fn adt_significant_drop_tys(
278218 . map ( |dtor| !tcx. has_attr ( dtor. did , sym:: rustc_insignificant_dtor) )
279219 . unwrap_or ( false )
280220 } ;
281- adt_drop_tys_helper ( tcx, def_id, adt_has_dtor, false )
221+ adt_drop_tys_helper ( tcx, def_id, adt_has_dtor)
282222}
283223
284224pub ( crate ) fn provide ( providers : & mut ty:: query:: Providers ) {
285225 * 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,
288227 has_significant_drop_raw,
289228 adt_drop_tys,
290- adt_drop_tys_non_const,
291229 adt_significant_drop_tys,
292230 ..* providers
293231 } ;
0 commit comments