@@ -69,7 +69,8 @@ use hir_ty::{
69
69
traits:: FnTrait ,
70
70
AliasTy , CallableDefId , CallableSig , Canonical , CanonicalVarKinds , Cast , ClosureId ,
71
71
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 ,
73
74
} ;
74
75
use itertools:: Itertools ;
75
76
use nameres:: diagnostics:: DefDiagnosticKind ;
@@ -91,10 +92,10 @@ pub use crate::{
91
92
IncorrectCase , InvalidDeriveTarget , MacroDefError , MacroError , MacroExpansionParseError ,
92
93
MalformedDerive , MismatchedArgCount , MissingFields , MissingMatchArms , MissingUnsafe ,
93
94
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 ,
98
99
} ,
99
100
has_source:: HasSource ,
100
101
semantics:: { PathResolution , Semantics , SemanticsScope , TypeInfo , VisibleTraits } ,
@@ -1005,6 +1006,10 @@ impl Struct {
1005
1006
Type :: from_def ( db, self . id )
1006
1007
}
1007
1008
1009
+ pub fn constructor_ty ( self , db : & dyn HirDatabase ) -> Type {
1010
+ Type :: from_value_def ( db, self . id )
1011
+ }
1012
+
1008
1013
pub fn repr ( self , db : & dyn HirDatabase ) -> Option < ReprOptions > {
1009
1014
db. struct_data ( self . id ) . repr
1010
1015
}
@@ -1042,6 +1047,10 @@ impl Union {
1042
1047
Type :: from_def ( db, self . id )
1043
1048
}
1044
1049
1050
+ pub fn constructor_ty ( self , db : & dyn HirDatabase ) -> Type {
1051
+ Type :: from_value_def ( db, self . id )
1052
+ }
1053
+
1045
1054
pub fn fields ( self , db : & dyn HirDatabase ) -> Vec < Field > {
1046
1055
db. union_data ( self . id )
1047
1056
. variant_data
@@ -1173,6 +1182,10 @@ impl Variant {
1173
1182
self . parent
1174
1183
}
1175
1184
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
+
1176
1189
pub fn name ( self , db : & dyn HirDatabase ) -> Name {
1177
1190
db. enum_data ( self . parent . id ) . variants [ self . id ] . name . clone ( )
1178
1191
}
@@ -1574,6 +1587,16 @@ impl DefWithBody {
1574
1587
let expr = expr_syntax ( expr) ;
1575
1588
acc. push ( BreakOutsideOfLoop { expr, is_break, bad_value_break } . into ( ) )
1576
1589
}
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
+ }
1577
1600
}
1578
1601
}
1579
1602
for ( pat_or_expr, mismatch) in infer. type_mismatches ( ) {
@@ -1806,6 +1829,10 @@ impl Function {
1806
1829
db. function_data ( self . id ) . name . clone ( )
1807
1830
}
1808
1831
1832
+ pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
1833
+ Type :: from_value_def ( db, self . id )
1834
+ }
1835
+
1809
1836
/// Get this function's return type
1810
1837
pub fn ret_type ( self , db : & dyn HirDatabase ) -> Type {
1811
1838
let resolver = self . id . resolver ( db. upcast ( ) ) ;
@@ -2085,11 +2112,7 @@ impl Const {
2085
2112
}
2086
2113
2087
2114
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 )
2093
2116
}
2094
2117
2095
2118
pub fn render_eval ( self , db : & dyn HirDatabase ) -> Result < String , ConstEvalError > {
@@ -2136,11 +2159,7 @@ impl Static {
2136
2159
}
2137
2160
2138
2161
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 )
2144
2163
}
2145
2164
}
2146
2165
@@ -3409,24 +3428,33 @@ impl Type {
3409
3428
Type { env : environment, ty }
3410
3429
}
3411
3430
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 ( ) ) ,
3425
3439
} ,
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) )
3430
3458
}
3431
3459
3432
3460
pub fn new_slice ( ty : Type ) -> Type {
0 commit comments