-
Notifications
You must be signed in to change notification settings - Fork 804
/
Copy pathConstraintSolver.fs
2778 lines (2401 loc) · 145 KB
/
ConstraintSolver.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
//-------------------------------------------------------------------------
// Incremental type inference constraint solving.
//
// Primary constraints are:
// - type equations ty1 = ty2
// - subtype inequations ty1 :> ty2
// - trait constraints tyname: (static member op_Addition: 'a * 'b -> 'c)
//
// Plus some other constraints inherited from .NET generics.
//
// The constraints are immediately processed into a normal form, in particular
// - type equations on inference parameters: 'tp = ty
// - type inequations on inference parameters: 'tp :> ty
// - other constraints on inference paramaters
//
// The state of the inference engine is kept in imperative mutations to inference
// type variables.
//
// The use of the normal form allows the state of the inference engine to
// be queried for type-directed name resolution, type-directed overload
// resolution and when generating warning messages.
//
// The inference engine can be used in 'undo' mode to implement
// can-unify predicates used in method overload resolution and trait constraint
// satisfaction.
//
//-------------------------------------------------------------------------
module internal Microsoft.FSharp.Compiler.ConstraintSolver
open Internal.Utilities.Collections
open Microsoft.FSharp.Compiler
open Microsoft.FSharp.Compiler.AbstractIL
open Microsoft.FSharp.Compiler.AbstractIL.Internal.Library
open Microsoft.FSharp.Compiler.Ast
open Microsoft.FSharp.Compiler.ErrorLogger
open Microsoft.FSharp.Compiler.Infos
open Microsoft.FSharp.Compiler.AccessibilityLogic
open Microsoft.FSharp.Compiler.AttributeChecking
open Microsoft.FSharp.Compiler.Lib
open Microsoft.FSharp.Compiler.MethodCalls
open Microsoft.FSharp.Compiler.PrettyNaming
open Microsoft.FSharp.Compiler.Range
open Microsoft.FSharp.Compiler.Rational
open Microsoft.FSharp.Compiler.InfoReader
open Microsoft.FSharp.Compiler.Tast
open Microsoft.FSharp.Compiler.Tastops
open Microsoft.FSharp.Compiler.TcGlobals
open Microsoft.FSharp.Compiler.TypeRelations
//-------------------------------------------------------------------------
// Generate type variables and record them in within the scope of the
// compilation environment, which currently corresponds to the scope
// of the constraint resolution carried out by type checking.
//-------------------------------------------------------------------------
let compgenId = mkSynId range0 unassignedTyparName
let NewCompGenTypar (kind, rigid, staticReq, dynamicReq, error) =
NewTypar(kind, rigid, Typar(compgenId, staticReq, true), error, dynamicReq, [], false, false)
let anon_id m = mkSynId m unassignedTyparName
let NewAnonTypar (kind, m, rigid, var, dyn) =
NewTypar (kind, rigid, Typar(anon_id m, var, true), false, dyn, [], false, false)
let NewNamedInferenceMeasureVar (_m, rigid, var, id) =
NewTypar(TyparKind.Measure, rigid, Typar(id, var, false), false, TyparDynamicReq.No, [], false, false)
let NewInferenceMeasurePar () = NewCompGenTypar (TyparKind.Measure, TyparRigidity.Flexible, NoStaticReq, TyparDynamicReq.No, false)
let NewErrorTypar () = NewCompGenTypar (TyparKind.Type, TyparRigidity.Flexible, NoStaticReq, TyparDynamicReq.No, true)
let NewErrorMeasureVar () = NewCompGenTypar (TyparKind.Measure, TyparRigidity.Flexible, NoStaticReq, TyparDynamicReq.No, true)
let NewInferenceType () = mkTyparTy (NewTypar (TyparKind.Type, TyparRigidity.Flexible, Typar(compgenId, NoStaticReq, true), false, TyparDynamicReq.No, [], false, false))
let NewErrorType () = mkTyparTy (NewErrorTypar ())
let NewErrorMeasure () = Measure.Var (NewErrorMeasureVar ())
let NewByRefKindInferenceType (g: TcGlobals) m =
let tp = NewTypar (TyparKind.Type, TyparRigidity.Flexible, Typar(compgenId, HeadTypeStaticReq, true), false, TyparDynamicReq.No, [], false, false)
if g.byrefkind_InOut_tcr.CanDeref then
tp.SetConstraints [TyparConstraint.DefaultsTo(10, TType_app(g.byrefkind_InOut_tcr, []), m)]
mkTyparTy tp
let NewInferenceTypes l = l |> List.map (fun _ -> NewInferenceType ())
// QUERY: should 'rigid' ever really be 'true'? We set this when we know
// we are going to have to generalize a typar, e.g. when implementing a
// abstract generic method slot. But we later check the generalization
// condition anyway, so we could get away with a non-rigid typar. This
// would sort of be cleaner, though give errors later.
let FreshenAndFixupTypars m rigid fctps tinst tpsorig =
let copy_tyvar (tp:Typar) = NewCompGenTypar (tp.Kind, rigid, tp.StaticReq, (if rigid=TyparRigidity.Rigid then TyparDynamicReq.Yes else TyparDynamicReq.No), false)
let tps = tpsorig |> List.map copy_tyvar
let renaming, tinst = FixupNewTypars m fctps tinst tpsorig tps
tps, renaming, tinst
let FreshenTypeInst m tpsorig = FreshenAndFixupTypars m TyparRigidity.Flexible [] [] tpsorig
let FreshMethInst m fctps tinst tpsorig = FreshenAndFixupTypars m TyparRigidity.Flexible fctps tinst tpsorig
let FreshenTypars m tpsorig =
match tpsorig with
| [] -> []
| _ ->
let _, _, tptys = FreshenTypeInst m tpsorig
tptys
let FreshenMethInfo m (minfo:MethInfo) =
let _, _, tptys = FreshMethInst m (minfo.GetFormalTyparsOfDeclaringType m) minfo.DeclaringTypeInst minfo.FormalMethodTypars
tptys
//-------------------------------------------------------------------------
// Unification of types: solve/record equality constraints
// Subsumption of types: solve/record subtyping constraints
//-------------------------------------------------------------------------
[<RequireQualifiedAccess>]
/// Information about the context of a type equation.
type ContextInfo =
/// No context was given.
| NoContext
/// The type equation comes from an IF expression.
| IfExpression of range
/// The type equation comes from an omitted else branch.
| OmittedElseBranch of range
/// The type equation comes from a type check of the result of an else branch.
| ElseBranchResult of range
/// The type equation comes from the verification of record fields.
| RecordFields
/// The type equation comes from the verification of a tuple in record fields.
| TupleInRecordFields
/// The type equation comes from a list or array constructor
| CollectionElement of bool * range
/// The type equation comes from a return in a computation expression.
| ReturnInComputationExpression
/// The type equation comes from a yield in a computation expression.
| YieldInComputationExpression
/// The type equation comes from a runtime type test.
| RuntimeTypeTest of bool
/// The type equation comes from an downcast where a upcast could be used.
| DowncastUsedInsteadOfUpcast of bool
/// The type equation comes from a return type of a pattern match clause (not the first clause).
| FollowingPatternMatchClause of range
/// The type equation comes from a pattern match guard.
| PatternMatchGuard of range
/// The type equation comes from a sequence expression.
| SequenceExpression of TType
exception ConstraintSolverTupleDiffLengths of DisplayEnv * TType list * TType list * range * range
exception ConstraintSolverInfiniteTypes of ContextInfo * DisplayEnv * TType * TType * range * range
exception ConstraintSolverTypesNotInEqualityRelation of DisplayEnv * TType * TType * range * range * ContextInfo
exception ConstraintSolverTypesNotInSubsumptionRelation of DisplayEnv * TType * TType * range * range
exception ConstraintSolverMissingConstraint of DisplayEnv * Tast.Typar * Tast.TyparConstraint * range * range
exception ConstraintSolverError of string * range * range
exception ConstraintSolverRelatedInformation of string option * range * exn
exception ErrorFromApplyingDefault of TcGlobals * DisplayEnv * Tast.Typar * TType * exn * range
exception ErrorFromAddingTypeEquation of TcGlobals * DisplayEnv * TType * TType * exn * range
exception ErrorsFromAddingSubsumptionConstraint of TcGlobals * DisplayEnv * TType * TType * exn * ContextInfo * range
exception ErrorFromAddingConstraint of DisplayEnv * exn * range
exception PossibleOverload of DisplayEnv * string * exn * range
exception UnresolvedOverloading of DisplayEnv * exn list * string * range
exception UnresolvedConversionOperator of DisplayEnv * TType * TType * range
let GetPossibleOverloads amap m denv (calledMethGroup: (CalledMeth<_> * exn) list) =
calledMethGroup |> List.map (fun (cmeth, e) -> PossibleOverload(denv, NicePrint.stringOfMethInfo amap m denv cmeth.Method, e, m))
type TcValF = (ValRef -> ValUseFlag -> TType list -> range -> Expr * TType)
type ConstraintSolverState =
{
g: TcGlobals
amap: Import.ImportMap
InfoReader: InfoReader
TcVal: TcValF
/// This table stores all unsolved, ungeneralized trait constraints, indexed by free type variable.
/// That is, there will be one entry in this table for each free type variable in
/// each outstanding, unsolved, ungeneralized trait constraint. Constraints are removed from the table and resolved
/// each time a solution to an index variable is found.
mutable ExtraCxs: HashMultiMap<Stamp, (TraitConstraintInfo * range)>
}
static member New(g, amap, infoReader, tcVal) =
{ g = g
amap = amap
ExtraCxs = HashMultiMap(10, HashIdentity.Structural)
InfoReader = infoReader
TcVal = tcVal }
type ConstraintSolverEnv =
{
SolverState: ConstraintSolverState
eContextInfo: ContextInfo
MatchingOnly: bool
m: range
EquivEnv: TypeEquivEnv
DisplayEnv: DisplayEnv
}
member csenv.InfoReader = csenv.SolverState.InfoReader
member csenv.g = csenv.SolverState.g
member csenv.amap = csenv.SolverState.amap
let MakeConstraintSolverEnv contextInfo css m denv =
{ SolverState = css
m = m
eContextInfo = contextInfo
// Indicates that when unifiying ty1 = ty2, only type variables in ty1 may be solved
MatchingOnly = false
EquivEnv = TypeEquivEnv.Empty
DisplayEnv = denv }
//-------------------------------------------------------------------------
// Occurs check
//-------------------------------------------------------------------------
/// Check whether a type variable occurs in the r.h.s. of a type, e.g. to catch
/// infinite equations such as
/// 'a = list<'a>
let rec occursCheck g un ty =
match stripTyEqns g ty with
| TType_ucase(_, l)
| TType_app (_, l)
| TType_tuple (_, l) -> List.exists (occursCheck g un) l
| TType_fun (d, r) -> occursCheck g un d || occursCheck g un r
| TType_var r -> typarEq un r
| TType_forall (_, tau) -> occursCheck g un tau
| _ -> false
//-------------------------------------------------------------------------
// Predicates on types
//-------------------------------------------------------------------------
let rec isNativeIntegerTy g ty =
typeEquivAux EraseMeasures g g.nativeint_ty ty ||
typeEquivAux EraseMeasures g g.unativeint_ty ty ||
(isEnumTy g ty && isNativeIntegerTy g (underlyingTypeOfEnumTy g ty))
let isSignedIntegerTy g ty =
typeEquivAux EraseMeasures g g.sbyte_ty ty ||
typeEquivAux EraseMeasures g g.int16_ty ty ||
typeEquivAux EraseMeasures g g.int32_ty ty ||
typeEquivAux EraseMeasures g g.nativeint_ty ty ||
typeEquivAux EraseMeasures g g.int64_ty ty
let isUnsignedIntegerTy g ty =
typeEquivAux EraseMeasures g g.byte_ty ty ||
typeEquivAux EraseMeasures g g.uint16_ty ty ||
typeEquivAux EraseMeasures g g.uint32_ty ty ||
typeEquivAux EraseMeasures g g.unativeint_ty ty ||
typeEquivAux EraseMeasures g g.uint64_ty ty
let rec isIntegerOrIntegerEnumTy g ty =
isSignedIntegerTy g ty ||
isUnsignedIntegerTy g ty ||
(isEnumTy g ty && isIntegerOrIntegerEnumTy g (underlyingTypeOfEnumTy g ty))
let isIntegerTy g ty =
isSignedIntegerTy g ty ||
isUnsignedIntegerTy g ty
let isStringTy g ty = typeEquiv g g.string_ty ty
let isCharTy g ty = typeEquiv g g.char_ty ty
let isBoolTy g ty = typeEquiv g g.bool_ty ty
/// float or float32 or float<_> or float32<_>
let isFpTy g ty =
typeEquivAux EraseMeasures g g.float_ty ty ||
typeEquivAux EraseMeasures g g.float32_ty ty
/// decimal or decimal<_>
let isDecimalTy g ty =
typeEquivAux EraseMeasures g g.decimal_ty ty
let IsNonDecimalNumericOrIntegralEnumType g ty = isIntegerOrIntegerEnumTy g ty || isFpTy g ty
let IsNumericOrIntegralEnumType g ty = IsNonDecimalNumericOrIntegralEnumType g ty || isDecimalTy g ty
let IsNonDecimalNumericType g ty = isIntegerTy g ty || isFpTy g ty
let IsNumericType g ty = IsNonDecimalNumericType g ty || isDecimalTy g ty
let IsRelationalType g ty = IsNumericType g ty || isStringTy g ty || isCharTy g ty || isBoolTy g ty
// Get measure of type, float<_> or float32<_> or decimal<_> but not float=float<1> or float32=float32<1> or decimal=decimal<1>
let GetMeasureOfType g ty =
match ty with
| AppTy g (tcref, [tyarg]) ->
match stripTyEqns g tyarg with
| TType_measure ms when not (measureEquiv g ms Measure.One) -> Some (tcref, ms)
| _ -> None
| _ -> None
type TraitConstraintSolution =
| TTraitUnsolved
| TTraitBuiltIn
| TTraitSolved of MethInfo * TypeInst
| TTraitSolvedRecdProp of RecdFieldInfo * bool
let BakedInTraitConstraintNames =
[ "op_Division" ; "op_Multiply"; "op_Addition"
"op_Equality" ; "op_Inequality"; "op_GreaterThan" ; "op_LessThan"; "op_LessThanOrEqual"; "op_GreaterThanOrEqual"
"op_Subtraction"; "op_Modulus";
"get_Zero"; "get_One";
"DivideByInt";"get_Item"; "set_Item";
"op_BitwiseAnd"; "op_BitwiseOr"; "op_ExclusiveOr"; "op_LeftShift";
"op_RightShift"; "op_UnaryPlus"; "op_UnaryNegation"; "get_Sign"; "op_LogicalNot"
"op_OnesComplement"; "Abs"; "Sqrt"; "Sin"; "Cos"; "Tan";
"Sinh"; "Cosh"; "Tanh"; "Atan"; "Acos"; "Asin"; "Exp"; "Ceiling"; "Floor"; "Round"; "Log10"; "Log"; "Sqrt";
"Truncate"; "op_Explicit";
"Pow"; "Atan2" ]
|> set
//-------------------------------------------------------------------------
// Run the constraint solver with undo (used during method overload resolution)
type Trace =
{ mutable actions: ((unit -> unit) * (unit -> unit)) list }
static member New () = { actions = [] }
member t.Undo () = List.iter (fun (_, a) -> a ()) t.actions
member t.Push f undo = t.actions <- (f, undo) :: t.actions
type OptionalTrace =
| NoTrace
| WithTrace of Trace
member x.HasTrace = match x with NoTrace -> false | WithTrace _ -> true
member t.Exec f undo =
match t with
| WithTrace trace -> trace.Push f undo; f()
| NoTrace -> f()
member t.AddFromReplay source =
source.actions |> List.rev |>
match t with
| WithTrace trace -> List.iter (fun (action, undo) -> trace.Push action undo; action())
| NoTrace -> List.iter (fun (action, _ ) -> action())
member t.CollectThenUndoOrCommit predicate f =
let newTrace = Trace.New()
let res = f newTrace
match predicate res, t with
| false, _ -> newTrace.Undo()
| true , WithTrace t -> t.actions <- newTrace.actions @ t.actions
| true , NoTrace -> ()
res
let CollectThenUndo f =
let trace = Trace.New()
let res = f trace
trace.Undo()
res
let FilterEachThenUndo f meths =
meths
|> List.choose (fun calledMeth ->
let trace = Trace.New()
let res = f trace calledMeth
trace.Undo()
match CheckNoErrorsAndGetWarnings res with
| None -> None
| Some warns -> Some (calledMeth, warns, trace))
let ShowAccessDomain ad =
match ad with
| AccessibleFromEverywhere -> "public"
| AccessibleFrom(_, _) -> "accessible"
| AccessibleFromSomeFSharpCode -> "public, protected or internal"
| AccessibleFromSomewhere -> ""
//-------------------------------------------------------------------------
// Solve
exception NonRigidTypar of DisplayEnv * string option * range * TType * TType * range
exception LocallyAbortOperationThatFailsToResolveOverload
exception LocallyAbortOperationThatLosesAbbrevs
let localAbortD = ErrorD LocallyAbortOperationThatLosesAbbrevs
/// Return true if we would rather unify this variable v1 := v2 than vice versa
let PreferUnifyTypar (v1:Typar) (v2:Typar) =
match v1.Rigidity, v2.Rigidity with
// Rigid > all
| TyparRigidity.Rigid, _ -> false
// Prefer to unify away WillBeRigid in favour of Rigid
| TyparRigidity.WillBeRigid, TyparRigidity.Rigid -> true
| TyparRigidity.WillBeRigid, TyparRigidity.WillBeRigid -> true
| TyparRigidity.WillBeRigid, TyparRigidity.WarnIfNotRigid -> false
| TyparRigidity.WillBeRigid, TyparRigidity.Anon -> false
| TyparRigidity.WillBeRigid, TyparRigidity.Flexible -> false
// Prefer to unify away WarnIfNotRigid in favour of Rigid
| TyparRigidity.WarnIfNotRigid, TyparRigidity.Rigid -> true
| TyparRigidity.WarnIfNotRigid, TyparRigidity.WillBeRigid -> true
| TyparRigidity.WarnIfNotRigid, TyparRigidity.WarnIfNotRigid -> true
| TyparRigidity.WarnIfNotRigid, TyparRigidity.Anon -> false
| TyparRigidity.WarnIfNotRigid, TyparRigidity.Flexible -> false
// Prefer to unify away anonymous variables in favour of Rigid, WarnIfNotRigid
| TyparRigidity.Anon, TyparRigidity.Rigid -> true
| TyparRigidity.Anon, TyparRigidity.WillBeRigid -> true
| TyparRigidity.Anon, TyparRigidity.WarnIfNotRigid -> true
| TyparRigidity.Anon, TyparRigidity.Anon -> true
| TyparRigidity.Anon, TyparRigidity.Flexible -> false
// Prefer to unify away Flexible in favour of Rigid, WarnIfNotRigid or Anon
| TyparRigidity.Flexible, TyparRigidity.Rigid -> true
| TyparRigidity.Flexible, TyparRigidity.WillBeRigid -> true
| TyparRigidity.Flexible, TyparRigidity.WarnIfNotRigid -> true
| TyparRigidity.Flexible, TyparRigidity.Anon -> true
| TyparRigidity.Flexible, TyparRigidity.Flexible ->
// Prefer to unify away compiler generated type vars
match v1.IsCompilerGenerated, v2.IsCompilerGenerated with
| true, false -> true
| false, true -> false
| _ ->
// Prefer to unify away non-error vars - gives better error recovery since we keep
// error vars lying around, and can avoid giving errors about illegal polymorphism
// if they occur
match v1.IsFromError, v2.IsFromError with
| true, false -> false
| _ -> true
/// Reorder a list of (variable, exponent) pairs so that a variable that is Preferred
/// is at the head of the list, if possible
let FindPreferredTypar vs =
let rec find vs =
match vs with
| [] -> vs
| (v:Typar, e)::vs ->
match find vs with
| [] -> [(v, e)]
| (v', e')::vs' ->
if PreferUnifyTypar v v'
then (v, e) :: vs
else (v', e') :: (v, e) :: vs'
find vs
let SubstMeasure (r:Typar) ms =
if r.Rigidity = TyparRigidity.Rigid then error(InternalError("SubstMeasure: rigid", r.Range));
if r.Kind = TyparKind.Type then error(InternalError("SubstMeasure: kind=type", r.Range));
match r.typar_solution with
| None -> r.typar_solution <- Some (TType_measure ms)
| Some _ -> error(InternalError("already solved", r.Range));
let rec TransactStaticReq (csenv:ConstraintSolverEnv) (trace:OptionalTrace) (tpr:Typar) req =
let m = csenv.m
if tpr.Rigidity.ErrorIfUnified && tpr.StaticReq <> req then
ErrorD(ConstraintSolverError(FSComp.SR.csTypeCannotBeResolvedAtCompileTime(tpr.Name), m, m))
else
let orig = tpr.StaticReq
trace.Exec (fun () -> tpr.SetStaticReq req) (fun () -> tpr.SetStaticReq orig)
CompleteD
and SolveTypStaticReqTypar (csenv:ConstraintSolverEnv) trace req (tpr:Typar) =
let orig = tpr.StaticReq
let req2 = JoinTyparStaticReq req orig
if orig <> req2 then TransactStaticReq csenv trace tpr req2 else CompleteD
and SolveTypStaticReq (csenv:ConstraintSolverEnv) trace req ty =
match req with
| NoStaticReq -> CompleteD
| HeadTypeStaticReq ->
// requires that a type constructor be known at compile time
match stripTyparEqns ty with
| TType_measure ms ->
let vs = ListMeasureVarOccsWithNonZeroExponents ms
IterateD (fun ((tpr:Typar), _) -> SolveTypStaticReqTypar csenv trace req tpr) vs
| _ ->
match tryAnyParTy csenv.g ty with
| ValueSome tpr -> SolveTypStaticReqTypar csenv trace req tpr
| ValueNone -> CompleteD
let TransactDynamicReq (trace:OptionalTrace) (tpr:Typar) req =
let orig = tpr.DynamicReq
trace.Exec (fun () -> tpr.SetDynamicReq req) (fun () -> tpr.SetDynamicReq orig)
CompleteD
let SolveTypDynamicReq (csenv:ConstraintSolverEnv) trace req ty =
match req with
| TyparDynamicReq.No -> CompleteD
| TyparDynamicReq.Yes ->
match tryAnyParTy csenv.g ty with
| ValueSome tpr when tpr.DynamicReq <> TyparDynamicReq.Yes ->
TransactDynamicReq trace tpr TyparDynamicReq.Yes
| _ -> CompleteD
let TransactIsCompatFlex (trace:OptionalTrace) (tpr:Typar) req =
let orig = tpr.IsCompatFlex
trace.Exec (fun () -> tpr.SetIsCompatFlex req) (fun () -> tpr.SetIsCompatFlex orig)
CompleteD
let SolveTypIsCompatFlex (csenv:ConstraintSolverEnv) trace req ty =
if req then
match tryAnyParTy csenv.g ty with
| ValueSome tpr when not tpr.IsCompatFlex -> TransactIsCompatFlex trace tpr req
| _ -> CompleteD
else
CompleteD
let SubstMeasureWarnIfRigid (csenv:ConstraintSolverEnv) trace (v:Typar) ms =
if v.Rigidity.WarnIfUnified && not (isAnyParTy csenv.g (TType_measure ms)) then
// NOTE: we grab the name eagerly to make sure the type variable prints as a type variable
let tpnmOpt = if v.IsCompilerGenerated then None else Some v.Name
SolveTypStaticReq csenv trace v.StaticReq (TType_measure ms) ++ (fun () ->
SubstMeasure v ms
WarnD(NonRigidTypar(csenv.DisplayEnv, tpnmOpt, v.Range, TType_measure (Measure.Var v), TType_measure ms, csenv.m)))
else
// Propagate static requirements from 'tp' to 'ty'
SolveTypStaticReq csenv trace v.StaticReq (TType_measure ms) ++ (fun () ->
SubstMeasure v ms
if v.Rigidity = TyparRigidity.Anon && measureEquiv csenv.g ms Measure.One then
WarnD(Error(FSComp.SR.csCodeLessGeneric(), v.Range))
else CompleteD)
/// Imperatively unify the unit-of-measure expression ms against 1.
/// There are three cases
/// - ms is (equivalent to) 1
/// - ms contains no non-rigid unit variables, and so cannot be unified with 1
/// - ms has the form v^e * ms' for some non-rigid variable v, non-zero exponent e, and measure expression ms'
/// the most general unifier is then simply v := ms' ^ -(1/e)
let UnifyMeasureWithOne (csenv:ConstraintSolverEnv) trace ms =
// Gather the rigid and non-rigid unit variables in this measure expression together with their exponents
let rigidVars, nonRigidVars =
ListMeasureVarOccsWithNonZeroExponents ms
|> List.partition (fun (v, _) -> v.Rigidity = TyparRigidity.Rigid)
// If there is at least one non-rigid variable v with exponent e, then we can unify
match FindPreferredTypar nonRigidVars with
| (v, e)::vs ->
let unexpandedCons = ListMeasureConOccsWithNonZeroExponents csenv.g false ms
let newms = ProdMeasures (List.map (fun (c, e') -> Measure.RationalPower (Measure.Con c, NegRational (DivRational e' e))) unexpandedCons
@ List.map (fun (v, e') -> Measure.RationalPower (Measure.Var v, NegRational (DivRational e' e))) (vs @ rigidVars))
SubstMeasureWarnIfRigid csenv trace v newms
// Otherwise we require ms to be 1
| [] -> if measureEquiv csenv.g ms Measure.One then CompleteD else localAbortD
/// Imperatively unify unit-of-measure expression ms1 against ms2
let UnifyMeasures (csenv:ConstraintSolverEnv) trace ms1 ms2 =
UnifyMeasureWithOne csenv trace (Measure.Prod(ms1, Measure.Inv ms2))
/// Simplify a unit-of-measure expression ms that forms part of a type scheme.
/// We make substitutions for vars, which are the (remaining) bound variables
/// in the scheme that we wish to simplify.
let SimplifyMeasure g vars ms =
let rec simp vars =
match FindPreferredTypar (List.filter (fun (_, e) -> SignRational e<>0) (List.map (fun v -> (v, MeasureVarExponent v ms)) vars)) with
| [] ->
(vars, None)
| (v, e)::vs ->
let newvar = if v.IsCompilerGenerated then NewAnonTypar (TyparKind.Measure, v.Range, TyparRigidity.Flexible, v.StaticReq, v.DynamicReq)
else NewNamedInferenceMeasureVar (v.Range, TyparRigidity.Flexible, v.StaticReq, v.Id)
let remainingvars = ListSet.remove typarEq v vars
let newvarExpr = if SignRational e < 0 then Measure.Inv (Measure.Var newvar) else Measure.Var newvar
let newms = (ProdMeasures (List.map (fun (c, e') -> Measure.RationalPower (Measure.Con c, NegRational (DivRational e' e))) (ListMeasureConOccsWithNonZeroExponents g false ms)
@ List.map (fun (v', e') -> if typarEq v v' then newvarExpr else Measure.RationalPower (Measure.Var v', NegRational (DivRational e' e))) (ListMeasureVarOccsWithNonZeroExponents ms)));
SubstMeasure v newms
match vs with
| [] -> (remainingvars, Some newvar)
| _ -> simp (newvar::remainingvars)
simp vars
// Normalize a type ty that forms part of a unit-of-measure-polymorphic type scheme.
// Generalizable are the unit-of-measure variables that remain to be simplified. Generalized
// is a list of unit-of-measure variables that have already been generalized.
let rec SimplifyMeasuresInType g resultFirst ((generalizable, generalized) as param) ty =
match stripTyparEqns ty with
| TType_ucase(_, l)
| TType_app (_, l)
| TType_tuple (_, l) -> SimplifyMeasuresInTypes g param l
| TType_fun (d, r) -> if resultFirst then SimplifyMeasuresInTypes g param [r;d] else SimplifyMeasuresInTypes g param [d;r]
| TType_var _ -> param
| TType_forall (_, tau) -> SimplifyMeasuresInType g resultFirst param tau
| TType_measure unt ->
let generalizable', newlygeneralized = SimplifyMeasure g generalizable unt
match newlygeneralized with
| None -> (generalizable', generalized)
| Some v -> (generalizable', v::generalized)
and SimplifyMeasuresInTypes g param tys =
match tys with
| [] -> param
| ty::tys ->
let param' = SimplifyMeasuresInType g false param ty
SimplifyMeasuresInTypes g param' tys
let SimplifyMeasuresInConstraint g param c =
match c with
| TyparConstraint.DefaultsTo (_, ty, _)
| TyparConstraint.CoercesTo(ty, _) -> SimplifyMeasuresInType g false param ty
| TyparConstraint.SimpleChoice (tys, _) -> SimplifyMeasuresInTypes g param tys
| TyparConstraint.IsDelegate (ty1, ty2, _) -> SimplifyMeasuresInTypes g param [ty1;ty2]
| _ -> param
let rec SimplifyMeasuresInConstraints g param cs =
match cs with
| [] -> param
| c::cs ->
let param' = SimplifyMeasuresInConstraint g param c
SimplifyMeasuresInConstraints g param' cs
let rec GetMeasureVarGcdInType v ty =
match stripTyparEqns ty with
| TType_ucase(_, l)
| TType_app (_, l)
| TType_tuple (_, l) -> GetMeasureVarGcdInTypes v l
| TType_fun (d, r) -> GcdRational (GetMeasureVarGcdInType v d) (GetMeasureVarGcdInType v r)
| TType_var _ -> ZeroRational
| TType_forall (_, tau) -> GetMeasureVarGcdInType v tau
| TType_measure unt -> MeasureVarExponent v unt
and GetMeasureVarGcdInTypes v tys =
match tys with
| [] -> ZeroRational
| ty::tys -> GcdRational (GetMeasureVarGcdInType v ty) (GetMeasureVarGcdInTypes v tys)
// Normalize the exponents on generalizable variables in a type
// by dividing them by their "rational gcd". For example, the type
// float<'u^(2/3)> -> float<'u^(4/3)> would be normalized to produce
// float<'u> -> float<'u^2> by dividing the exponents by 2/3.
let NormalizeExponentsInTypeScheme uvars ty =
uvars |> List.map (fun v ->
let expGcd = AbsRational (GetMeasureVarGcdInType v ty)
if expGcd = OneRational || expGcd = ZeroRational then
v
else
let v' = NewAnonTypar (TyparKind.Measure, v.Range, TyparRigidity.Flexible, v.StaticReq, v.DynamicReq)
SubstMeasure v (Measure.RationalPower (Measure.Var v', DivRational OneRational expGcd))
v')
// We normalize unit-of-measure-polymorphic type schemes. There
// are three reasons for doing this:
// (1) to present concise and consistent type schemes to the programmer
// (2) so that we can compute equivalence of type schemes in signature matching
// (3) in order to produce a list of type parameters ordered as they appear in the (normalized) scheme.
//
// Representing the normal form as a matrix, with a row for each variable or base unit,
// and a column for each unit-of-measure expression in the "skeleton" of the type.
// Entries for generalizable variables are integers; other rows may contain non-integer exponents.
//
// ( 0...0 a1 as1 b1 bs1 c1 cs1 ...)
// ( 0...0 0 0...0 b2 bs2 c2 cs2 ...)
// ( 0...0 0 0...0 0 0...0 c3 cs3 ...)
//...
// ( 0...0 0 0...0 0 0...0 0 0...0 ...)
//
// The normal form is unique; what's more, it can be used to force a variable ordering
// because the first occurrence of a variable in a type is in a unit-of-measure expression with no
// other "new" variables (a1, b2, c3, above).
//
// The corner entries a1, b2, c3 are all positive. Entries lying above them (b1, c1, c2, etc) are
// non-negative and smaller than the corresponding corner entry. Entries as1, bs1, bs2, etc are arbitrary.
//
// Essentially this is the *reduced row echelon* matrix from linear algebra, with adjustment to ensure that
// exponents are integers where possible (in the reduced row echelon form, a1, b2, etc. would be 1, possibly
// forcing other entries to be non-integers).
let SimplifyMeasuresInTypeScheme g resultFirst (generalizable:Typar list) ty constraints =
// Only bother if we're generalizing over at least one unit-of-measure variable
let uvars, vars =
generalizable
|> List.partition (fun v -> v.Kind = TyparKind.Measure && v.Rigidity <> TyparRigidity.Rigid)
match uvars with
| [] -> generalizable
| _::_ ->
let (_, generalized) = SimplifyMeasuresInType g resultFirst (SimplifyMeasuresInConstraints g (uvars, []) constraints) ty
let generalized' = NormalizeExponentsInTypeScheme generalized ty
vars @ List.rev generalized'
let freshMeasure () = Measure.Var (NewInferenceMeasurePar ())
let CheckWarnIfRigid (csenv:ConstraintSolverEnv) ty1 (r:Typar) ty =
let g = csenv.g
let denv = csenv.DisplayEnv
if not r.Rigidity.WarnIfUnified then CompleteD else
let needsWarning =
match tryAnyParTy g ty with
| ValueNone -> true
| ValueSome tp2 ->
not tp2.IsCompilerGenerated &&
(r.IsCompilerGenerated ||
// exclude this warning for two identically named user-specified type parameters, e.g. from different mutually recursive functions or types
r.DisplayName <> tp2.DisplayName)
if needsWarning then
// NOTE: we grab the name eagerly to make sure the type variable prints as a type variable
let tpnmOpt = if r.IsCompilerGenerated then None else Some r.Name
WarnD(NonRigidTypar(denv, tpnmOpt, r.Range, ty1, ty, csenv.m))
else
CompleteD
/// Add the constraint "ty1 = ty" to the constraint problem, where ty1 is a type variable.
/// Propagate all effects of adding this constraint, e.g. to solve other variables
let rec SolveTyparEqualsType (csenv:ConstraintSolverEnv) ndeep m2 (trace:OptionalTrace) ty1 ty =
let m = csenv.m
DepthCheck ndeep m ++ (fun () ->
match ty1 with
| TType_var r | TType_measure (Measure.Var r) ->
// The types may still be equivalent due to abbreviations, which we are trying not to eliminate
if typeEquiv csenv.g ty1 ty then CompleteD else
// The famous 'occursCheck' check to catch "infinite types" like 'a = list<'a> - see also https://github.com/Microsoft/visualfsharp/issues/1170
if occursCheck csenv.g r ty then ErrorD (ConstraintSolverInfiniteTypes(csenv.eContextInfo, csenv.DisplayEnv, ty1, ty, m, m2)) else
// Note: warn _and_ continue!
CheckWarnIfRigid csenv ty1 r ty ++ (fun () ->
// Record the solution before we solve the constraints, since
// We may need to make use of the equation when solving the constraints.
// Record a entry in the undo trace if one is provided
trace.Exec (fun () -> r.typar_solution <- Some ty) (fun () -> r.typar_solution <- None)
(* dprintf "setting typar %d to type %s at %a\n" r.Stamp ((DebugPrint.showType ty)) outputRange m; *)
// Only solve constraints if this is not an error var
if r.IsFromError then CompleteD else
// Check to see if this type variable is relevant to any trait constraints.
// If so, re-solve the relevant constraints.
(if csenv.SolverState.ExtraCxs.ContainsKey r.Stamp then
RepeatWhileD ndeep (fun ndeep -> SolveRelevantMemberConstraintsForTypar csenv ndeep false trace r)
else
CompleteD) ++ (fun _ ->
// Re-solve the other constraints associated with this type variable
solveTypMeetsTyparConstraints csenv ndeep m2 trace ty r))
| _ -> failwith "SolveTyparEqualsType")
/// Apply the constraints on 'typar' to the type 'ty'
and solveTypMeetsTyparConstraints (csenv:ConstraintSolverEnv) ndeep m2 trace ty (r: Typar) =
let g = csenv.g
// Propagate compat flex requirements from 'tp' to 'ty'
SolveTypIsCompatFlex csenv trace r.IsCompatFlex ty ++ (fun () ->
// Propagate dynamic requirements from 'tp' to 'ty'
SolveTypDynamicReq csenv trace r.DynamicReq ty ++ (fun () ->
// Propagate static requirements from 'tp' to 'ty'
SolveTypStaticReq csenv trace r.StaticReq ty ++ (fun () ->
// Solve constraints on 'tp' w.r.t. 'ty'
r.Constraints |> IterateD (function
| TyparConstraint.DefaultsTo (priority, dty, m) ->
if typeEquiv g ty dty then
CompleteD
else
match tryDestTyparTy g ty with
| ValueNone -> CompleteD
| ValueSome destTypar ->
AddConstraint csenv ndeep m2 trace destTypar (TyparConstraint.DefaultsTo(priority, dty, m))
| TyparConstraint.SupportsNull m2 -> SolveTypeSupportsNull csenv ndeep m2 trace ty
| TyparConstraint.IsEnum(underlying, m2) -> SolveTypeIsEnum csenv ndeep m2 trace ty underlying
| TyparConstraint.SupportsComparison(m2) -> SolveTypeSupportsComparison csenv ndeep m2 trace ty
| TyparConstraint.SupportsEquality(m2) -> SolveTypeSupportsEquality csenv ndeep m2 trace ty
| TyparConstraint.IsDelegate(aty, bty, m2) -> SolveTypeIsDelegate csenv ndeep m2 trace ty aty bty
| TyparConstraint.IsNonNullableStruct m2 -> SolveTypeIsNonNullableValueType csenv ndeep m2 trace ty
| TyparConstraint.IsUnmanaged m2 -> SolveTypeIsUnmanaged csenv ndeep m2 trace ty
| TyparConstraint.IsReferenceType m2 -> SolveTypeIsReferenceType csenv ndeep m2 trace ty
| TyparConstraint.RequiresDefaultConstructor m2 -> SolveTypeRequiresDefaultConstructor csenv ndeep m2 trace ty
| TyparConstraint.SimpleChoice(tys, m2) -> SolveTypeChoice csenv ndeep m2 trace ty tys
| TyparConstraint.CoercesTo(ty2, m2) -> SolveTypeSubsumesTypeKeepAbbrevs csenv ndeep m2 trace None ty2 ty
| TyparConstraint.MayResolveMember(traitInfo, m2) ->
SolveMemberConstraint csenv false false ndeep m2 trace traitInfo ++ (fun _ -> CompleteD)
))))
/// Add the constraint "ty1 = ty2" to the constraint problem.
/// Propagate all effects of adding this constraint, e.g. to solve type variables
and SolveTypeEqualsType (csenv:ConstraintSolverEnv) ndeep m2 (trace: OptionalTrace) (cxsln:(TraitConstraintInfo * TraitConstraintSln) option) ty1 ty2 =
let ndeep = ndeep + 1
let aenv = csenv.EquivEnv
let g = csenv.g
match cxsln with
| Some (traitInfo, traitSln) when traitInfo.Solution.IsNone ->
// If this is an overload resolution at this point it's safe to assume the candidate member being evaluated solves this member constraint.
TransactMemberConstraintSolution traitInfo trace traitSln
| _ -> ()
if ty1 === ty2 then CompleteD else
let canShortcut = not trace.HasTrace
let sty1 = stripTyEqnsA csenv.g canShortcut ty1
let sty2 = stripTyEqnsA csenv.g canShortcut ty2
match sty1, sty2 with
// type vars inside forall-types may be alpha-equivalent
| TType_var tp1, TType_var tp2 when typarEq tp1 tp2 || (match aenv.EquivTypars.TryFind tp1 with | Some v when typeEquiv g v ty2 -> true | _ -> false) -> CompleteD
| TType_var tp1, TType_var tp2 when PreferUnifyTypar tp1 tp2 -> SolveTyparEqualsType csenv ndeep m2 trace sty1 ty2
| TType_var tp1, TType_var tp2 when not csenv.MatchingOnly && PreferUnifyTypar tp2 tp1 -> SolveTyparEqualsType csenv ndeep m2 trace sty2 ty1
| TType_var r, _ when (r.Rigidity <> TyparRigidity.Rigid) -> SolveTyparEqualsType csenv ndeep m2 trace sty1 ty2
| _, TType_var r when (r.Rigidity <> TyparRigidity.Rigid) && not csenv.MatchingOnly -> SolveTyparEqualsType csenv ndeep m2 trace sty2 ty1
// Catch float<_>=float<1>, float32<_>=float32<1> and decimal<_>=decimal<1>
| (_, TType_app (tc2, [ms])) when (tc2.IsMeasureableReprTycon && typeEquiv csenv.g sty1 (reduceTyconRefMeasureableOrProvided csenv.g tc2 [ms]))
-> SolveTypeEqualsType csenv ndeep m2 trace None ms (TType_measure Measure.One)
| (TType_app (tc2, [ms]), _) when (tc2.IsMeasureableReprTycon && typeEquiv csenv.g sty2 (reduceTyconRefMeasureableOrProvided csenv.g tc2 [ms]))
-> SolveTypeEqualsType csenv ndeep m2 trace None ms (TType_measure Measure.One)
| TType_app (tc1, l1) , TType_app (tc2, l2) when tyconRefEq g tc1 tc2 -> SolveTypeEqualsTypeEqns csenv ndeep m2 trace None l1 l2
| TType_app (_, _) , TType_app (_, _) -> localAbortD
| TType_tuple (tupInfo1, l1) , TType_tuple (tupInfo2, l2) ->
if evalTupInfoIsStruct tupInfo1 <> evalTupInfoIsStruct tupInfo2 then ErrorD (ConstraintSolverError(FSComp.SR.tcTupleStructMismatch(), csenv.m, m2)) else
SolveTypeEqualsTypeEqns csenv ndeep m2 trace None l1 l2
| TType_fun (d1, r1) , TType_fun (d2, r2) -> SolveFunTypeEqn csenv ndeep m2 trace None d1 d2 r1 r2
| TType_measure ms1 , TType_measure ms2 -> UnifyMeasures csenv trace ms1 ms2
| TType_forall(tps1, rty1), TType_forall(tps2, rty2) ->
if tps1.Length <> tps2.Length then localAbortD else
let aenv = aenv.BindEquivTypars tps1 tps2
let csenv = {csenv with EquivEnv = aenv }
if not (typarsAEquiv g aenv tps1 tps2) then localAbortD else
SolveTypeEqualsTypeKeepAbbrevs csenv ndeep m2 trace rty1 rty2
| TType_ucase (uc1, l1) , TType_ucase (uc2, l2) when g.unionCaseRefEq uc1 uc2 -> SolveTypeEqualsTypeEqns csenv ndeep m2 trace None l1 l2
| _ -> localAbortD
and SolveTypeEqualsTypeKeepAbbrevs csenv ndeep m2 trace ty1 ty2 = SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace None ty1 ty2
and private SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln ty1 ty2 =
// Back out of expansions of type abbreviations to give improved error messages.
// Note: any "normalization" of equations on type variables must respect the trace parameter
TryD (fun () -> SolveTypeEqualsType csenv ndeep m2 trace cxsln ty1 ty2)
(function LocallyAbortOperationThatLosesAbbrevs -> ErrorD(ConstraintSolverTypesNotInEqualityRelation(csenv.DisplayEnv, ty1, ty2, csenv.m, m2, csenv.eContextInfo))
| err -> ErrorD err)
and SolveTypeEqualsTypeEqns csenv ndeep m2 trace cxsln origl1 origl2 =
match origl1, origl2 with
| [], [] -> CompleteD
| _ ->
// We unwind Iterate2D by hand here for performance reasons.
let rec loop l1 l2 =
match l1, l2 with
| [], [] -> CompleteD
| h1::t1, h2::t2 ->
SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln h1 h2 ++ (fun () -> loop t1 t2)
| _ ->
ErrorD(ConstraintSolverTupleDiffLengths(csenv.DisplayEnv, origl1, origl2, csenv.m, m2))
loop origl1 origl2
and SolveFunTypeEqn csenv ndeep m2 trace cxsln d1 d2 r1 r2 =
SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln d1 d2 ++ (fun () ->
SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln r1 r2)
// ty1: expected
// ty2: actual
//
// "ty2 casts to ty1"
// "a value of type ty2 can be used where a value of type ty1 is expected"
and SolveTypeSubsumesType (csenv:ConstraintSolverEnv) ndeep m2 (trace: OptionalTrace) cxsln ty1 ty2 =
// 'a :> obj ---> <solved>
let ndeep = ndeep + 1
let g = csenv.g
if isObjTy g ty1 then CompleteD else
let canShortcut = not trace.HasTrace
let sty1 = stripTyEqnsA csenv.g canShortcut ty1
let sty2 = stripTyEqnsA csenv.g canShortcut ty2
let amap = csenv.amap
let aenv = csenv.EquivEnv
let denv = csenv.DisplayEnv
match sty1, sty2 with
| TType_var tp1, _ ->
match aenv.EquivTypars.TryFind tp1 with
| Some v -> SolveTypeSubsumesType csenv ndeep m2 trace cxsln v ty2
| _ ->
match sty2 with
| TType_var r2 when typarEq tp1 r2 -> CompleteD
| TType_var r when not csenv.MatchingOnly -> SolveTyparSubtypeOfType csenv ndeep m2 trace r ty1
| _ -> SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln ty1 ty2
| _, TType_var r when not csenv.MatchingOnly -> SolveTyparSubtypeOfType csenv ndeep m2 trace r ty1
| TType_tuple (tupInfo1, l1) , TType_tuple (tupInfo2, l2) ->
if evalTupInfoIsStruct tupInfo1 <> evalTupInfoIsStruct tupInfo2 then ErrorD (ConstraintSolverError(FSComp.SR.tcTupleStructMismatch(), csenv.m, m2)) else
SolveTypeEqualsTypeEqns csenv ndeep m2 trace cxsln l1 l2 (* nb. can unify since no variance *)
| TType_fun (d1, r1) , TType_fun (d2, r2) -> SolveFunTypeEqn csenv ndeep m2 trace cxsln d1 d2 r1 r2 (* nb. can unify since no variance *)
| TType_measure ms1, TType_measure ms2 -> UnifyMeasures csenv trace ms1 ms2
// Enforce the identities float=float<1>, float32=float32<1> and decimal=decimal<1>
| (_, TType_app (tc2, [ms])) when (tc2.IsMeasureableReprTycon && typeEquiv csenv.g sty1 (reduceTyconRefMeasureableOrProvided csenv.g tc2 [ms]))
-> SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln ms (TType_measure Measure.One)
| (TType_app (tc2, [ms]), _) when (tc2.IsMeasureableReprTycon && typeEquiv csenv.g sty2 (reduceTyconRefMeasureableOrProvided csenv.g tc2 [ms]))
-> SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln ms (TType_measure Measure.One)
// Special subsumption rule for byref tags
| TType_app (tc1, l1) , TType_app (tc2, l2) when tyconRefEq g tc1 tc2 && g.byref2_tcr.CanDeref && tyconRefEq g g.byref2_tcr tc1 ->
match l1, l2 with
| [ h1; tag1 ], [ h2; tag2 ] ->
SolveTypeEqualsType csenv ndeep m2 trace None h1 h2 ++ (fun () ->
match stripTyEqnsA csenv.g canShortcut tag1, stripTyEqnsA csenv.g canShortcut tag2 with
| TType_app(tagc1, []), TType_app(tagc2, [])
when (tyconRefEq g tagc2 g.byrefkind_InOut_tcr &&
(tyconRefEq g tagc1 g.byrefkind_In_tcr || tyconRefEq g tagc1 g.byrefkind_Out_tcr) ) -> CompleteD
| _ -> SolveTypeEqualsType csenv ndeep m2 trace cxsln tag1 tag2)
| _ -> SolveTypeEqualsTypeEqns csenv ndeep m2 trace cxsln l1 l2
| TType_app (tc1, l1) , TType_app (tc2, l2) when tyconRefEq g tc1 tc2 ->
SolveTypeEqualsTypeEqns csenv ndeep m2 trace cxsln l1 l2
| TType_ucase (uc1, l1) , TType_ucase (uc2, l2) when g.unionCaseRefEq uc1 uc2 ->
SolveTypeEqualsTypeEqns csenv ndeep m2 trace cxsln l1 l2
| _ ->
// By now we know the type is not a variable type
// C :> obj ---> <solved>
if isObjTy g ty1 then CompleteD else
let m = csenv.m
// 'a[] :> IList<'b> ---> 'a = 'b
// 'a[] :> ICollection<'b> ---> 'a = 'b
// 'a[] :> IEnumerable<'b> ---> 'a = 'b
// 'a[] :> IReadOnlyList<'b> ---> 'a = 'b
// 'a[] :> IReadOnlyCollection<'b> ---> 'a = 'b
// Note we don't support co-variance on array types nor
// the special .NET conversions for these types
match ty1 with
| AppTy g (tcr1, tinst) when
isArray1DTy g ty2 &&
(tyconRefEq g tcr1 g.tcref_System_Collections_Generic_IList ||
tyconRefEq g tcr1 g.tcref_System_Collections_Generic_ICollection ||
tyconRefEq g tcr1 g.tcref_System_Collections_Generic_IReadOnlyList ||
tyconRefEq g tcr1 g.tcref_System_Collections_Generic_IReadOnlyCollection ||
tyconRefEq g tcr1 g.tcref_System_Collections_Generic_IEnumerable) ->
match tinst with
| [ty1arg] ->
let ty2arg = destArrayTy g ty2
SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln ty1arg ty2arg
| _ -> error(InternalError("destArrayTy", m))
| _ ->
// D<inst> :> Head<_> --> C<inst'> :> Head<_> for the
// first interface or super-class C supported by D which
// may feasibly convert to Head.
match FindUniqueFeasibleSupertype g amap m ty1 ty2 with
| None -> ErrorD(ConstraintSolverTypesNotInSubsumptionRelation(denv, ty1, ty2, m, m2))
| Some t -> SolveTypeSubsumesType csenv ndeep m2 trace cxsln ty1 t
and SolveTypeSubsumesTypeKeepAbbrevs csenv ndeep m2 trace cxsln ty1 ty2 =
let denv = csenv.DisplayEnv
TryD (fun () -> SolveTypeSubsumesType csenv ndeep m2 trace cxsln ty1 ty2)
(function LocallyAbortOperationThatLosesAbbrevs -> ErrorD(ConstraintSolverTypesNotInSubsumptionRelation(denv, ty1, ty2, csenv.m, m2))
| err -> ErrorD err)
//-------------------------------------------------------------------------
// Solve and record non-equality constraints
//-------------------------------------------------------------------------
and SolveTyparSubtypeOfType (csenv:ConstraintSolverEnv) ndeep m2 trace tp ty1 =
let g = csenv.g
if isObjTy g ty1 then CompleteD
elif typeEquiv g ty1 (mkTyparTy tp) then CompleteD
elif isSealedTy g ty1 then
SolveTypeEqualsTypeKeepAbbrevs csenv ndeep m2 trace (mkTyparTy tp) ty1
else
AddConstraint csenv ndeep m2 trace tp (TyparConstraint.CoercesTo(ty1, csenv.m))
and DepthCheck ndeep m =
if ndeep > 300 then error(Error(FSComp.SR.csTypeInferenceMaxDepth(), m)) else CompleteD
// If this is a type that's parameterized on a unit-of-measure (expected to be numeric), unify its measure with 1
and SolveDimensionlessNumericType (csenv:ConstraintSolverEnv) ndeep m2 trace ty =
match GetMeasureOfType csenv.g ty with
| Some (tcref, _) ->
SolveTypeEqualsTypeKeepAbbrevs csenv ndeep m2 trace ty (mkAppTy tcref [TType_measure Measure.One])
| None ->
CompleteD
/// We do a bunch of fakery to pretend that primitive types have certain members.
/// We pretend int and other types support a number of operators. In the actual IL for mscorlib they
/// don't, however the type-directed static optimization rules in the library code that makes use of this
/// will deal with the problem.
and SolveMemberConstraint (csenv:ConstraintSolverEnv) ignoreUnresolvedOverload permitWeakResolution ndeep m2 trace (TTrait(tys, nm, memFlags, argtys, rty, sln)): OperationResult<bool> =
// Do not re-solve if already solved
if sln.Value.IsSome then ResultD true else
let g = csenv.g
let m = csenv.m
let amap = csenv.amap
let aenv = csenv.EquivEnv
let denv = csenv.DisplayEnv
let ndeep = ndeep + 1
DepthCheck ndeep m ++ (fun () ->