@@ -58,7 +58,6 @@ use hir_ty::{
58
58
consteval:: {
59
59
eval_const, unknown_const_as_generic, ComputedExpr , ConstEvalCtx , ConstEvalError , ConstExt ,
60
60
} ,
61
- could_unify,
62
61
diagnostics:: BodyValidationDiagnostic ,
63
62
method_resolution:: { self , TyFingerprint } ,
64
63
primitive:: UintTy ,
@@ -85,12 +84,11 @@ use crate::db::{DefDatabase, HirDatabase};
85
84
pub use crate :: {
86
85
attrs:: { HasAttrs , Namespace } ,
87
86
diagnostics:: {
88
- AddReferenceHere , AnyDiagnostic , BreakOutsideOfLoop , InactiveCode , IncorrectCase ,
89
- InvalidDeriveTarget , MacroError , MalformedDerive , MismatchedArgCount , MissingFields ,
90
- MissingMatchArms , MissingOkOrSomeInTailExpr , MissingUnsafe , NoSuchField ,
91
- RemoveThisSemicolon , ReplaceFilterMapNextWithFindMap , UnimplementedBuiltinMacro ,
92
- UnresolvedExternCrate , UnresolvedImport , UnresolvedMacroCall , UnresolvedModule ,
93
- UnresolvedProcMacro ,
87
+ AnyDiagnostic , BreakOutsideOfLoop , InactiveCode , IncorrectCase , InvalidDeriveTarget ,
88
+ MacroError , MalformedDerive , MismatchedArgCount , MissingFields , MissingMatchArms ,
89
+ MissingUnsafe , NoSuchField , ReplaceFilterMapNextWithFindMap , TypeMismatch ,
90
+ UnimplementedBuiltinMacro , UnresolvedExternCrate , UnresolvedImport , UnresolvedMacroCall ,
91
+ UnresolvedModule , UnresolvedProcMacro ,
94
92
} ,
95
93
has_source:: HasSource ,
96
94
semantics:: { PathResolution , Semantics , SemanticsScope , TypeInfo } ,
@@ -1005,6 +1003,24 @@ impl Adt {
1005
1003
Type :: from_def ( db, id. module ( db. upcast ( ) ) . krate ( ) , id)
1006
1004
}
1007
1005
1006
+ /// Turns this ADT into a type with the given type parameters. This isn't
1007
+ /// the greatest API, FIXME find a better one.
1008
+ pub fn ty_with_args ( self , db : & dyn HirDatabase , args : & [ Type ] ) -> Type {
1009
+ let id = AdtId :: from ( self ) ;
1010
+ let mut it = args. iter ( ) . map ( |t| t. ty . clone ( ) ) ;
1011
+ let ty = TyBuilder :: def_ty ( db, id. into ( ) )
1012
+ . fill ( |x| {
1013
+ let r = it. next ( ) . unwrap_or_else ( || TyKind :: Error . intern ( Interner ) ) ;
1014
+ match x {
1015
+ ParamKind :: Type => GenericArgData :: Ty ( r) . intern ( Interner ) ,
1016
+ ParamKind :: Const ( ty) => unknown_const_as_generic ( ty. clone ( ) ) ,
1017
+ }
1018
+ } )
1019
+ . build ( ) ;
1020
+ let krate = id. module ( db. upcast ( ) ) . krate ( ) ;
1021
+ Type :: new ( db, krate, id, ty)
1022
+ }
1023
+
1008
1024
pub fn module ( self , db : & dyn HirDatabase ) -> Module {
1009
1025
match self {
1010
1026
Adt :: Struct ( s) => s. module ( db) ,
@@ -1020,6 +1036,14 @@ impl Adt {
1020
1036
Adt :: Enum ( e) => e. name ( db) ,
1021
1037
}
1022
1038
}
1039
+
1040
+ pub fn as_enum ( & self ) -> Option < Enum > {
1041
+ if let Self :: Enum ( v) = self {
1042
+ Some ( * v)
1043
+ } else {
1044
+ None
1045
+ }
1046
+ }
1023
1047
}
1024
1048
1025
1049
impl HasVisibility for Adt {
@@ -1163,6 +1187,30 @@ impl DefWithBody {
1163
1187
}
1164
1188
}
1165
1189
}
1190
+ for ( expr, mismatch) in infer. expr_type_mismatches ( ) {
1191
+ let expr = match source_map. expr_syntax ( expr) {
1192
+ Ok ( expr) => expr,
1193
+ Err ( SyntheticSyntax ) => continue ,
1194
+ } ;
1195
+ acc. push (
1196
+ TypeMismatch {
1197
+ expr,
1198
+ expected : Type :: new (
1199
+ db,
1200
+ krate,
1201
+ DefWithBodyId :: from ( self ) ,
1202
+ mismatch. expected . clone ( ) ,
1203
+ ) ,
1204
+ actual : Type :: new (
1205
+ db,
1206
+ krate,
1207
+ DefWithBodyId :: from ( self ) ,
1208
+ mismatch. actual . clone ( ) ,
1209
+ ) ,
1210
+ }
1211
+ . into ( ) ,
1212
+ ) ;
1213
+ }
1166
1214
1167
1215
for expr in hir_ty:: diagnostics:: missing_unsafe ( db, self . into ( ) ) {
1168
1216
match source_map. expr_syntax ( expr) {
@@ -1259,25 +1307,6 @@ impl DefWithBody {
1259
1307
Err ( SyntheticSyntax ) => ( ) ,
1260
1308
}
1261
1309
}
1262
- BodyValidationDiagnostic :: RemoveThisSemicolon { expr } => {
1263
- match source_map. expr_syntax ( expr) {
1264
- Ok ( expr) => acc. push ( RemoveThisSemicolon { expr } . into ( ) ) ,
1265
- Err ( SyntheticSyntax ) => ( ) ,
1266
- }
1267
- }
1268
- BodyValidationDiagnostic :: MissingOkOrSomeInTailExpr { expr, required } => {
1269
- match source_map. expr_syntax ( expr) {
1270
- Ok ( expr) => acc. push (
1271
- MissingOkOrSomeInTailExpr {
1272
- expr,
1273
- required,
1274
- expected : self . body_type ( db) ,
1275
- }
1276
- . into ( ) ,
1277
- ) ,
1278
- Err ( SyntheticSyntax ) => ( ) ,
1279
- }
1280
- }
1281
1310
BodyValidationDiagnostic :: MissingMatchArms { match_expr } => {
1282
1311
match source_map. expr_syntax ( match_expr) {
1283
1312
Ok ( source_ptr) => {
@@ -1299,12 +1328,6 @@ impl DefWithBody {
1299
1328
Err ( SyntheticSyntax ) => ( ) ,
1300
1329
}
1301
1330
}
1302
- BodyValidationDiagnostic :: AddReferenceHere { arg_expr, mutability } => {
1303
- match source_map. expr_syntax ( arg_expr) {
1304
- Ok ( expr) => acc. push ( AddReferenceHere { expr, mutability } . into ( ) ) ,
1305
- Err ( SyntheticSyntax ) => ( ) ,
1306
- }
1307
- }
1308
1331
}
1309
1332
}
1310
1333
@@ -2618,6 +2641,17 @@ impl Type {
2618
2641
Type { krate, env : environment, ty }
2619
2642
}
2620
2643
2644
+ pub fn reference ( inner : & Type , m : Mutability ) -> Type {
2645
+ inner. derived (
2646
+ TyKind :: Ref (
2647
+ if m. is_mut ( ) { hir_ty:: Mutability :: Mut } else { hir_ty:: Mutability :: Not } ,
2648
+ hir_ty:: static_lifetime ( ) ,
2649
+ inner. ty . clone ( ) ,
2650
+ )
2651
+ . intern ( Interner ) ,
2652
+ )
2653
+ }
2654
+
2621
2655
fn new ( db : & dyn HirDatabase , krate : CrateId , lexical_env : impl HasResolver , ty : Ty ) -> Type {
2622
2656
let resolver = lexical_env. resolver ( db. upcast ( ) ) ;
2623
2657
let environment = resolver
@@ -2659,6 +2693,12 @@ impl Type {
2659
2693
matches ! ( self . ty. kind( Interner ) , TyKind :: Ref ( ..) )
2660
2694
}
2661
2695
2696
+ pub fn as_reference ( & self ) -> Option < ( Type , Mutability ) > {
2697
+ let ( ty, _lt, m) = self . ty . as_reference ( ) ?;
2698
+ let m = Mutability :: from_mutable ( matches ! ( m, hir_ty:: Mutability :: Mut ) ) ;
2699
+ Some ( ( self . derived ( ty. clone ( ) ) , m) )
2700
+ }
2701
+
2662
2702
pub fn is_slice ( & self ) -> bool {
2663
2703
matches ! ( self . ty. kind( Interner ) , TyKind :: Slice ( ..) )
2664
2704
}
@@ -2900,7 +2940,7 @@ impl Type {
2900
2940
self . autoderef_ ( db) . map ( move |ty| self . derived ( ty) )
2901
2941
}
2902
2942
2903
- pub fn autoderef_ < ' a > ( & ' a self , db : & ' a dyn HirDatabase ) -> impl Iterator < Item = Ty > + ' a {
2943
+ fn autoderef_ < ' a > ( & ' a self , db : & ' a dyn HirDatabase ) -> impl Iterator < Item = Ty > + ' a {
2904
2944
// There should be no inference vars in types passed here
2905
2945
let canonical = hir_ty:: replace_errors_with_variables ( & self . ty ) ;
2906
2946
let environment = self . env . clone ( ) ;
@@ -3238,7 +3278,12 @@ impl Type {
3238
3278
3239
3279
pub fn could_unify_with ( & self , db : & dyn HirDatabase , other : & Type ) -> bool {
3240
3280
let tys = hir_ty:: replace_errors_with_variables ( & ( self . ty . clone ( ) , other. ty . clone ( ) ) ) ;
3241
- could_unify ( db, self . env . clone ( ) , & tys)
3281
+ hir_ty:: could_unify ( db, self . env . clone ( ) , & tys)
3282
+ }
3283
+
3284
+ pub fn could_coerce_to ( & self , db : & dyn HirDatabase , to : & Type ) -> bool {
3285
+ let tys = hir_ty:: replace_errors_with_variables ( & ( self . ty . clone ( ) , to. ty . clone ( ) ) ) ;
3286
+ hir_ty:: could_coerce ( db, self . env . clone ( ) , & tys)
3242
3287
}
3243
3288
}
3244
3289
0 commit comments