@@ -69,7 +69,8 @@ use hir_ty::{
6969 traits:: FnTrait ,
7070 AliasTy , CallableDefId , CallableSig , Canonical , CanonicalVarKinds , Cast , ClosureId ,
7171 GenericArgData , Interner , ParamKind , QuantifiedWhereClause , Scalar , Substitution ,
72- TraitEnvironment , TraitRefExt , Ty , TyBuilder , TyDefId , TyExt , TyKind , WhereClause ,
72+ TraitEnvironment , TraitRefExt , Ty , TyBuilder , TyDefId , TyExt , TyKind , ValueTyDefId ,
73+ WhereClause ,
7374} ;
7475use itertools:: Itertools ;
7576use nameres:: diagnostics:: DefDiagnosticKind ;
@@ -91,10 +92,10 @@ pub use crate::{
9192 IncorrectCase , InvalidDeriveTarget , MacroDefError , MacroError , MacroExpansionParseError ,
9293 MalformedDerive , MismatchedArgCount , MissingFields , MissingMatchArms , MissingUnsafe ,
9394 MovedOutOfRef , NeedMut , NoSuchField , PrivateAssocItem , PrivateField ,
94- ReplaceFilterMapNextWithFindMap , TypeMismatch , UndeclaredLabel , UnimplementedBuiltinMacro ,
95- UnreachableLabel , UnresolvedExternCrate , UnresolvedField , UnresolvedImport ,
96- UnresolvedMacroCall , UnresolvedMethodCall , UnresolvedModule , UnresolvedProcMacro ,
97- UnusedMut ,
95+ ReplaceFilterMapNextWithFindMap , TypeMismatch , TypedHole , UndeclaredLabel ,
96+ UnimplementedBuiltinMacro , UnreachableLabel , UnresolvedExternCrate , UnresolvedField ,
97+ UnresolvedImport , UnresolvedMacroCall , UnresolvedMethodCall , UnresolvedModule ,
98+ UnresolvedProcMacro , UnusedMut ,
9899 } ,
99100 has_source:: HasSource ,
100101 semantics:: { PathResolution , Semantics , SemanticsScope , TypeInfo , VisibleTraits } ,
@@ -1005,6 +1006,10 @@ impl Struct {
10051006 Type :: from_def ( db, self . id )
10061007 }
10071008
1009+ pub fn constructor_ty ( self , db : & dyn HirDatabase ) -> Type {
1010+ Type :: from_value_def ( db, self . id )
1011+ }
1012+
10081013 pub fn repr ( self , db : & dyn HirDatabase ) -> Option < ReprOptions > {
10091014 db. struct_data ( self . id ) . repr
10101015 }
@@ -1042,6 +1047,10 @@ impl Union {
10421047 Type :: from_def ( db, self . id )
10431048 }
10441049
1050+ pub fn constructor_ty ( self , db : & dyn HirDatabase ) -> Type {
1051+ Type :: from_value_def ( db, self . id )
1052+ }
1053+
10451054 pub fn fields ( self , db : & dyn HirDatabase ) -> Vec < Field > {
10461055 db. union_data ( self . id )
10471056 . variant_data
@@ -1173,6 +1182,10 @@ impl Variant {
11731182 self . parent
11741183 }
11751184
1185+ pub fn constructor_ty ( self , db : & dyn HirDatabase ) -> Type {
1186+ Type :: from_value_def ( db, EnumVariantId { parent : self . parent . id , local_id : self . id } )
1187+ }
1188+
11761189 pub fn name ( self , db : & dyn HirDatabase ) -> Name {
11771190 db. enum_data ( self . parent . id ) . variants [ self . id ] . name . clone ( )
11781191 }
@@ -1574,6 +1587,16 @@ impl DefWithBody {
15741587 let expr = expr_syntax ( expr) ;
15751588 acc. push ( BreakOutsideOfLoop { expr, is_break, bad_value_break } . into ( ) )
15761589 }
1590+ hir_ty:: InferenceDiagnostic :: TypedHole { expr, expected } => {
1591+ let expr = expr_syntax ( * expr) ;
1592+ acc. push (
1593+ TypedHole {
1594+ expr,
1595+ expected : Type :: new ( db, DefWithBodyId :: from ( self ) , expected. clone ( ) ) ,
1596+ }
1597+ . into ( ) ,
1598+ )
1599+ }
15771600 }
15781601 }
15791602 for ( pat_or_expr, mismatch) in infer. type_mismatches ( ) {
@@ -1806,6 +1829,10 @@ impl Function {
18061829 db. function_data ( self . id ) . name . clone ( )
18071830 }
18081831
1832+ pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
1833+ Type :: from_value_def ( db, self . id )
1834+ }
1835+
18091836 /// Get this function's return type
18101837 pub fn ret_type ( self , db : & dyn HirDatabase ) -> Type {
18111838 let resolver = self . id . resolver ( db. upcast ( ) ) ;
@@ -2085,11 +2112,7 @@ impl Const {
20852112 }
20862113
20872114 pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
2088- let data = db. const_data ( self . id ) ;
2089- let resolver = self . id . resolver ( db. upcast ( ) ) ;
2090- let ctx = hir_ty:: TyLoweringContext :: new ( db, & resolver) ;
2091- let ty = ctx. lower_ty ( & data. type_ref ) ;
2092- Type :: new_with_resolver_inner ( db, & resolver, ty)
2115+ Type :: from_value_def ( db, self . id )
20932116 }
20942117
20952118 pub fn render_eval ( self , db : & dyn HirDatabase ) -> Result < String , ConstEvalError > {
@@ -2136,11 +2159,7 @@ impl Static {
21362159 }
21372160
21382161 pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
2139- let data = db. static_data ( self . id ) ;
2140- let resolver = self . id . resolver ( db. upcast ( ) ) ;
2141- let ctx = hir_ty:: TyLoweringContext :: new ( db, & resolver) ;
2142- let ty = ctx. lower_ty ( & data. type_ref ) ;
2143- Type :: new_with_resolver_inner ( db, & resolver, ty)
2162+ Type :: from_value_def ( db, self . id )
21442163 }
21452164}
21462165
@@ -3409,24 +3428,33 @@ impl Type {
34093428 Type { env : environment, ty }
34103429 }
34113430
3412- fn from_def ( db : & dyn HirDatabase , def : impl HasResolver + Into < TyDefId > ) -> Type {
3413- let ty_def = def. into ( ) ;
3414- let parent_subst = match ty_def {
3415- TyDefId :: TypeAliasId ( id) => match id. lookup ( db. upcast ( ) ) . container {
3416- ItemContainerId :: TraitId ( id) => {
3417- let subst = TyBuilder :: subst_for_def ( db, id, None ) . fill_with_unknown ( ) . build ( ) ;
3418- Some ( subst)
3419- }
3420- ItemContainerId :: ImplId ( id) => {
3421- let subst = TyBuilder :: subst_for_def ( db, id, None ) . fill_with_unknown ( ) . build ( ) ;
3422- Some ( subst)
3423- }
3424- _ => None ,
3431+ fn from_def ( db : & dyn HirDatabase , def : impl Into < TyDefId > + HasResolver ) -> Type {
3432+ let ty = db. ty ( def. into ( ) ) ;
3433+ let substs = TyBuilder :: unknown_subst (
3434+ db,
3435+ match def. into ( ) {
3436+ TyDefId :: AdtId ( it) => GenericDefId :: AdtId ( it) ,
3437+ TyDefId :: TypeAliasId ( it) => GenericDefId :: TypeAliasId ( it) ,
3438+ TyDefId :: BuiltinType ( _) => return Type :: new ( db, def, ty. skip_binders ( ) . clone ( ) ) ,
34253439 } ,
3426- _ => None ,
3427- } ;
3428- let ty = TyBuilder :: def_ty ( db, ty_def, parent_subst) . fill_with_unknown ( ) . build ( ) ;
3429- Type :: new ( db, def, ty)
3440+ ) ;
3441+ Type :: new ( db, def, ty. substitute ( Interner , & substs) )
3442+ }
3443+
3444+ fn from_value_def ( db : & dyn HirDatabase , def : impl Into < ValueTyDefId > + HasResolver ) -> Type {
3445+ let ty = db. value_ty ( def. into ( ) ) ;
3446+ let substs = TyBuilder :: unknown_subst (
3447+ db,
3448+ match def. into ( ) {
3449+ ValueTyDefId :: ConstId ( it) => GenericDefId :: ConstId ( it) ,
3450+ ValueTyDefId :: FunctionId ( it) => GenericDefId :: FunctionId ( it) ,
3451+ ValueTyDefId :: StructId ( it) => GenericDefId :: AdtId ( AdtId :: StructId ( it) ) ,
3452+ ValueTyDefId :: UnionId ( it) => GenericDefId :: AdtId ( AdtId :: UnionId ( it) ) ,
3453+ ValueTyDefId :: EnumVariantId ( it) => GenericDefId :: EnumVariantId ( it) ,
3454+ ValueTyDefId :: StaticId ( _) => return Type :: new ( db, def, ty. skip_binders ( ) . clone ( ) ) ,
3455+ } ,
3456+ ) ;
3457+ Type :: new ( db, def, ty. substitute ( Interner , & substs) )
34303458 }
34313459
34323460 pub fn new_slice ( ty : Type ) -> Type {
0 commit comments