@@ -6,7 +6,8 @@ use crate::ty::TyKind::*;
66use crate :: ty:: { AdtDef , FieldDef , Ty , TyS , VariantDef } ;
77use crate :: ty:: { AdtKind , Visibility } ;
88use crate :: ty:: { DefId , SubstsRef } ;
9- use rustc_data_structures:: stack:: ensure_sufficient_stack;
9+
10+ use std:: sync:: Arc ;
1011
1112mod def_id_forest;
1213
@@ -187,34 +188,47 @@ impl<'tcx> FieldDef {
187188
188189impl < ' tcx > TyS < ' tcx > {
189190 /// Calculates the forest of `DefId`s from which this type is visibly uninhabited.
190- fn uninhabited_from ( & self , tcx : TyCtxt < ' tcx > , param_env : ty:: ParamEnv < ' tcx > ) -> DefIdForest {
191- match * self . kind ( ) {
192- Adt ( def, substs) => {
193- ensure_sufficient_stack ( || def. uninhabited_from ( tcx, substs, param_env) )
194- }
191+ fn uninhabited_from (
192+ & ' tcx self ,
193+ tcx : TyCtxt < ' tcx > ,
194+ param_env : ty:: ParamEnv < ' tcx > ,
195+ ) -> DefIdForest {
196+ tcx. type_uninhabited_from ( param_env. and ( self ) ) . as_ref ( ) . clone ( )
197+ }
198+ }
195199
196- Never => DefIdForest :: full ( tcx) ,
200+ // Query provider for `type_uninhabited_from`.
201+ pub ( crate ) fn type_uninhabited_from < ' tcx > (
202+ tcx : TyCtxt < ' tcx > ,
203+ key : ty:: ParamEnvAnd < ' tcx , Ty < ' tcx > > ,
204+ ) -> Arc < DefIdForest > {
205+ let ty = key. value ;
206+ let param_env = key. param_env ;
207+ let forest = match * ty. kind ( ) {
208+ Adt ( def, substs) => def. uninhabited_from ( tcx, substs, param_env) ,
197209
198- Tuple ( ref tys) => DefIdForest :: union (
199- tcx,
200- tys. iter ( ) . map ( |ty| ty. expect_ty ( ) . uninhabited_from ( tcx, param_env) ) ,
201- ) ,
210+ Never => DefIdForest :: full ( tcx) ,
202211
203- Array ( ty, len) => match len. try_eval_usize ( tcx, param_env) {
204- Some ( 0 ) | None => DefIdForest :: empty ( ) ,
205- // If the array is definitely non-empty, it's uninhabited if
206- // the type of its elements is uninhabited.
207- Some ( 1 ..) => ty. uninhabited_from ( tcx, param_env) ,
208- } ,
212+ Tuple ( ref tys) => DefIdForest :: union (
213+ tcx,
214+ tys. iter ( ) . map ( |ty| ty. expect_ty ( ) . uninhabited_from ( tcx, param_env) ) ,
215+ ) ,
209216
210- // References to uninitialised memory are valid for any type, including
211- // uninhabited types, in unsafe code, so we treat all references as
212- // inhabited.
213- // The precise semantics of inhabitedness with respect to references is currently
214- // undecided.
215- Ref ( .. ) => DefIdForest :: empty ( ) ,
217+ Array ( ty , len ) => match len . try_eval_usize ( tcx , param_env ) {
218+ Some ( 0 ) | None => DefIdForest :: empty ( ) ,
219+ // If the array is definitely non-empty, it's uninhabited if
220+ // the type of its elements is uninhabited.
221+ Some ( 1 .. ) => ty . uninhabited_from ( tcx , param_env ) ,
222+ } ,
216223
217- _ => DefIdForest :: empty ( ) ,
218- }
219- }
224+ // References to uninitialised memory are valid for any type, including
225+ // uninhabited types, in unsafe code, so we treat all references as
226+ // inhabited.
227+ // The precise semantics of inhabitedness with respect to references is currently
228+ // undecided.
229+ Ref ( ..) => DefIdForest :: empty ( ) ,
230+
231+ _ => DefIdForest :: empty ( ) ,
232+ } ;
233+ Arc :: new ( forest)
220234}
0 commit comments