11//! `TyBuilder`, a helper for building instances of `Ty` and related types.
22
33use chalk_ir:: {
4- AdtId , DebruijnIndex , Scalar ,
5- cast:: { Cast , CastTo , Caster } ,
4+ DebruijnIndex , Scalar ,
5+ cast:: { Cast , Caster } ,
66} ;
7- use hir_def:: { GenericDefId , GenericParamId , TraitId , TypeAliasId , builtin_type:: BuiltinType } ;
7+ use hir_def:: { GenericDefId , GenericParamId , TraitId , builtin_type:: BuiltinType } ;
88use smallvec:: SmallVec ;
99
1010use crate :: {
11- BoundVar , CallableSig , GenericArg , GenericArgData , Interner , ProjectionTy , Substitution ,
12- TraitRef , Ty , TyDefId , TyExt , TyKind ,
11+ BoundVar , GenericArg , GenericArgData , Interner , Substitution , TraitRef , Ty , TyKind ,
1312 consteval:: unknown_const_as_generic,
1413 db:: HirDatabase ,
1514 error_lifetime,
@@ -19,18 +18,18 @@ use crate::{
1918 DbInterner , EarlyBinder ,
2019 mapping:: { ChalkToNextSolver , NextSolverToChalk } ,
2120 } ,
22- primitive, to_assoc_type_id , to_chalk_trait_id,
21+ primitive, to_chalk_trait_id,
2322} ;
2423
2524#[ derive( Debug , Clone , PartialEq , Eq ) ]
26- pub enum ParamKind {
25+ pub ( crate ) enum ParamKind {
2726 Type ,
2827 Lifetime ,
2928 Const ( Ty ) ,
3029}
3130
3231/// This is a builder for `Ty` or anything that needs a `Substitution`.
33- pub struct TyBuilder < D > {
32+ pub ( crate ) struct TyBuilder < D > {
3433 /// The `data` field is used to keep track of what we're building (e.g. an
3534 /// ADT, a `TraitRef`, ...).
3635 data : D ,
@@ -60,10 +59,6 @@ impl<D> TyBuilder<D> {
6059 Self { data, vec : SmallVec :: with_capacity ( param_kinds. len ( ) ) , param_kinds, parent_subst }
6160 }
6261
63- fn new_empty ( data : D ) -> Self {
64- TyBuilder :: new ( data, SmallVec :: new ( ) , None )
65- }
66-
6762 fn build_internal ( self ) -> ( D , Substitution ) {
6863 assert_eq ! (
6964 self . vec. len( ) ,
@@ -83,35 +78,15 @@ impl<D> TyBuilder<D> {
8378 ( self . data , subst)
8479 }
8580
86- pub fn build_into_subst ( self ) -> Substitution {
87- self . build_internal ( ) . 1
88- }
89-
90- pub fn push ( mut self , arg : impl CastTo < GenericArg > ) -> Self {
91- assert ! ( self . remaining( ) > 0 ) ;
92- let arg = arg. cast ( Interner ) ;
93- let expected_kind = & self . param_kinds [ self . vec . len ( ) ] ;
94-
95- let arg_kind = match arg. data ( Interner ) {
96- GenericArgData :: Ty ( _) => ParamKind :: Type ,
97- GenericArgData :: Lifetime ( _) => panic ! ( "Got lifetime in TyBuilder::push" ) ,
98- GenericArgData :: Const ( c) => {
99- let c = c. data ( Interner ) ;
100- ParamKind :: Const ( c. ty . clone ( ) )
101- }
102- } ;
103- assert_eq ! ( * expected_kind, arg_kind) ;
104-
105- self . vec . push ( arg) ;
106-
107- self
108- }
109-
110- pub fn remaining ( & self ) -> usize {
81+ pub ( crate ) fn remaining ( & self ) -> usize {
11182 self . param_kinds . len ( ) - self . vec . len ( )
11283 }
11384
114- pub fn fill_with_bound_vars ( self , debruijn : DebruijnIndex , starting_from : usize ) -> Self {
85+ pub ( crate ) fn fill_with_bound_vars (
86+ self ,
87+ debruijn : DebruijnIndex ,
88+ starting_from : usize ,
89+ ) -> Self {
11590 // self.fill is inlined to make borrow checker happy
11691 let mut this = self ;
11792 let other = & this. param_kinds [ this. vec . len ( ) ..] ;
@@ -129,22 +104,6 @@ impl<D> TyBuilder<D> {
129104 this
130105 }
131106
132- pub fn fill_with_unknown ( self ) -> Self {
133- let interner = DbInterner :: conjure ( ) ;
134- // self.fill is inlined to make borrow checker happy
135- let mut this = self ;
136- let filler = this. param_kinds [ this. vec . len ( ) ..] . iter ( ) . map ( |x| match x {
137- ParamKind :: Type => TyKind :: Error . intern ( Interner ) . cast ( Interner ) ,
138- ParamKind :: Const ( ty) => {
139- unknown_const_as_generic ( ty. to_nextsolver ( interner) ) . to_chalk ( interner)
140- }
141- ParamKind :: Lifetime => error_lifetime ( ) . cast ( Interner ) ,
142- } ) ;
143- this. vec . extend ( filler. casted ( Interner ) ) ;
144- assert_eq ! ( this. remaining( ) , 0 ) ;
145- this
146- }
147-
148107 #[ tracing:: instrument( skip_all) ]
149108 pub ( crate ) fn fill_with_inference_vars ( self , table : & mut InferenceTable < ' _ > ) -> Self {
150109 self . fill ( |x| {
@@ -157,7 +116,7 @@ impl<D> TyBuilder<D> {
157116 } )
158117 }
159118
160- pub fn fill ( mut self , filler : impl FnMut ( & ParamKind ) -> GenericArg ) -> Self {
119+ pub ( crate ) fn fill ( mut self , filler : impl FnMut ( & ParamKind ) -> GenericArg ) -> Self {
161120 self . vec . extend ( self . param_kinds [ self . vec . len ( ) ..] . iter ( ) . map ( filler) ) ;
162121 assert_eq ! ( self . remaining( ) , 0 ) ;
163122 self
@@ -174,28 +133,11 @@ impl<D> TyBuilder<D> {
174133}
175134
176135impl TyBuilder < ( ) > {
177- pub fn unit ( ) -> Ty {
178- TyKind :: Tuple ( 0 , Substitution :: empty ( Interner ) ) . intern ( Interner )
179- }
180-
181- // FIXME: rustc's ty is dependent on the adt type, maybe we need to do that as well
182- pub fn discr_ty ( ) -> Ty {
183- TyKind :: Scalar ( chalk_ir:: Scalar :: Int ( chalk_ir:: IntTy :: I128 ) ) . intern ( Interner )
184- }
185-
186- pub fn bool ( ) -> Ty {
187- TyKind :: Scalar ( chalk_ir:: Scalar :: Bool ) . intern ( Interner )
188- }
189-
190- pub fn usize ( ) -> Ty {
136+ pub ( crate ) fn usize ( ) -> Ty {
191137 TyKind :: Scalar ( chalk_ir:: Scalar :: Uint ( chalk_ir:: UintTy :: Usize ) ) . intern ( Interner )
192138 }
193139
194- pub fn fn_ptr ( sig : CallableSig ) -> Ty {
195- TyKind :: Function ( sig. to_fn_ptr ( ) ) . intern ( Interner )
196- }
197-
198- pub fn builtin ( builtin : BuiltinType ) -> Ty {
140+ pub ( crate ) fn builtin ( builtin : BuiltinType ) -> Ty {
199141 match builtin {
200142 BuiltinType :: Char => TyKind :: Scalar ( Scalar :: Char ) . intern ( Interner ) ,
201143 BuiltinType :: Bool => TyKind :: Scalar ( Scalar :: Bool ) . intern ( Interner ) ,
@@ -212,16 +154,10 @@ impl TyBuilder<()> {
212154 }
213155 }
214156
215- pub fn slice ( argument : Ty ) -> Ty {
216- TyKind :: Slice ( argument) . intern ( Interner )
217- }
218-
219- pub fn placeholder_subst ( db : & dyn HirDatabase , def : impl Into < GenericDefId > ) -> Substitution {
220- let params = generics ( db, def. into ( ) ) ;
221- params. placeholder_subst ( db)
222- }
223-
224- pub fn unknown_subst ( db : & dyn HirDatabase , def : impl Into < GenericDefId > ) -> Substitution {
157+ pub ( crate ) fn unknown_subst (
158+ db : & dyn HirDatabase ,
159+ def : impl Into < GenericDefId > ,
160+ ) -> Substitution {
225161 let interner = DbInterner :: conjure ( ) ;
226162 let params = generics ( db, def. into ( ) ) ;
227163 Substitution :: from_iter (
@@ -239,7 +175,7 @@ impl TyBuilder<()> {
239175 }
240176
241177 #[ tracing:: instrument( skip_all) ]
242- pub fn subst_for_def (
178+ pub ( crate ) fn subst_for_def (
243179 db : & dyn HirDatabase ,
244180 def : impl Into < GenericDefId > ,
245181 parent_subst : Option < Substitution > ,
@@ -257,139 +193,33 @@ impl TyBuilder<()> {
257193 TyBuilder :: new ( ( ) , params, parent_subst)
258194 }
259195
260- pub fn build ( self ) -> Substitution {
196+ pub ( crate ) fn build ( self ) -> Substitution {
261197 let ( ( ) , subst) = self . build_internal ( ) ;
262198 subst
263199 }
264200}
265201
266- impl TyBuilder < hir_def:: AdtId > {
267- pub fn adt ( db : & dyn HirDatabase , def : hir_def:: AdtId ) -> TyBuilder < hir_def:: AdtId > {
268- TyBuilder :: subst_for_def ( db, def, None ) . with_data ( def)
269- }
270-
271- pub fn fill_with_defaults (
272- mut self ,
273- db : & dyn HirDatabase ,
274- mut fallback : impl FnMut ( ) -> Ty ,
275- ) -> Self {
276- let interner = DbInterner :: conjure ( ) ;
277- // Note that we're building ADT, so we never have parent generic parameters.
278- let defaults = db. generic_defaults ( self . data . into ( ) ) ;
279-
280- if let Some ( defaults) = defaults. get ( self . vec . len ( ) ..) {
281- for default_ty in defaults {
282- // NOTE(skip_binders): we only check if the arg type is error type.
283- if let Some ( x) = default_ty. skip_binders ( ) . ty ( Interner )
284- && x. is_unknown ( )
285- {
286- self . vec . push ( fallback ( ) . cast ( Interner ) ) ;
287- continue ;
288- }
289- // Each default can only depend on the previous parameters.
290- self . vec . push ( default_ty. clone ( ) . substitute ( Interner , & * self . vec ) . cast ( Interner ) ) ;
291- }
292- }
293-
294- // The defaults may be missing if no param has default, so fill that.
295- let filler = self . param_kinds [ self . vec . len ( ) ..] . iter ( ) . map ( |x| match x {
296- ParamKind :: Type => fallback ( ) . cast ( Interner ) ,
297- ParamKind :: Const ( ty) => {
298- unknown_const_as_generic ( ty. to_nextsolver ( interner) ) . to_chalk ( interner)
299- }
300- ParamKind :: Lifetime => error_lifetime ( ) . cast ( Interner ) ,
301- } ) ;
302- self . vec . extend ( filler. casted ( Interner ) ) ;
303-
304- self
305- }
306-
307- pub fn build ( self ) -> Ty {
308- let ( adt, subst) = self . build_internal ( ) ;
309- TyKind :: Adt ( AdtId ( adt) , subst) . intern ( Interner )
310- }
311- }
312-
313- pub struct Tuple ( usize ) ;
314- impl TyBuilder < Tuple > {
315- pub fn tuple ( size : usize ) -> TyBuilder < Tuple > {
316- TyBuilder :: new ( Tuple ( size) , std:: iter:: repeat_n ( ParamKind :: Type , size) . collect ( ) , None )
317- }
318-
319- pub fn build ( self ) -> Ty {
320- let ( Tuple ( size) , subst) = self . build_internal ( ) ;
321- TyKind :: Tuple ( size, subst) . intern ( Interner )
322- }
323-
324- pub fn tuple_with < I > ( elements : I ) -> Ty
325- where
326- I : IntoIterator < Item = Ty > ,
327- <I as IntoIterator >:: IntoIter : ExactSizeIterator ,
328- {
329- let elements = elements. into_iter ( ) ;
330- let len = elements. len ( ) ;
331- let mut b =
332- TyBuilder :: new ( Tuple ( len) , std:: iter:: repeat_n ( ParamKind :: Type , len) . collect ( ) , None ) ;
333- for e in elements {
334- b = b. push ( e) ;
335- }
336- b. build ( )
337- }
338- }
339-
340202impl TyBuilder < TraitId > {
341- pub fn trait_ref ( db : & dyn HirDatabase , def : TraitId ) -> TyBuilder < TraitId > {
203+ pub ( crate ) fn trait_ref ( db : & dyn HirDatabase , def : TraitId ) -> TyBuilder < TraitId > {
342204 TyBuilder :: subst_for_def ( db, def, None ) . with_data ( def)
343205 }
344206
345- pub fn build ( self ) -> TraitRef {
207+ pub ( crate ) fn build ( self ) -> TraitRef {
346208 let ( trait_id, substitution) = self . build_internal ( ) ;
347209 TraitRef { trait_id : to_chalk_trait_id ( trait_id) , substitution }
348210 }
349211}
350212
351- impl TyBuilder < TypeAliasId > {
352- pub fn assoc_type_projection (
353- db : & dyn HirDatabase ,
354- def : TypeAliasId ,
355- parent_subst : Option < Substitution > ,
356- ) -> TyBuilder < TypeAliasId > {
357- TyBuilder :: subst_for_def ( db, def, parent_subst) . with_data ( def)
358- }
359-
360- pub fn build ( self ) -> ProjectionTy {
361- let ( type_alias, substitution) = self . build_internal ( ) ;
362- ProjectionTy { associated_ty_id : to_assoc_type_id ( type_alias) , substitution }
363- }
364- }
365-
366213impl < ' db , T : rustc_type_ir:: TypeFoldable < DbInterner < ' db > > > TyBuilder < EarlyBinder < ' db , T > > {
367- pub fn build ( self , interner : DbInterner < ' db > ) -> T {
214+ pub ( crate ) fn build ( self , interner : DbInterner < ' db > ) -> T {
368215 let ( b, subst) = self . build_internal ( ) ;
369216 let args: crate :: next_solver:: GenericArgs < ' db > = subst. to_nextsolver ( interner) ;
370217 b. instantiate ( interner, args)
371218 }
372219}
373220
374221impl < ' db > TyBuilder < EarlyBinder < ' db , crate :: next_solver:: Ty < ' db > > > {
375- pub fn def_ty (
376- db : & ' db dyn HirDatabase ,
377- def : TyDefId ,
378- parent_subst : Option < Substitution > ,
379- ) -> TyBuilder < EarlyBinder < ' db , crate :: next_solver:: Ty < ' db > > > {
380- let poly_ty = db. ty ( def) ;
381- let id: GenericDefId = match def {
382- TyDefId :: BuiltinType ( _) => {
383- assert ! ( parent_subst. is_none( ) ) ;
384- return TyBuilder :: new_empty ( poly_ty) ;
385- }
386- TyDefId :: AdtId ( id) => id. into ( ) ,
387- TyDefId :: TypeAliasId ( id) => id. into ( ) ,
388- } ;
389- TyBuilder :: subst_for_def ( db, id, parent_subst) . with_data ( poly_ty)
390- }
391-
392- pub fn impl_self_ty (
222+ pub ( crate ) fn impl_self_ty (
393223 db : & ' db dyn HirDatabase ,
394224 def : hir_def:: ImplId ,
395225 ) -> TyBuilder < EarlyBinder < ' db , crate :: next_solver:: Ty < ' db > > > {
0 commit comments