-
Notifications
You must be signed in to change notification settings - Fork 789
/
ConstraintSolver.fs
4202 lines (3664 loc) · 217 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 parameters
//
// 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.
//
// The two main principles are:
// 1. Ensure any solution that is found is sound (no logic is skipped),
// 2. Because of method overloading and SRTP constraints and other constructs, processing of
// constraints is algorithmic and must proceed in a definite, fixed order.
// Once we start doing resolutions in a particular order we must keep doing them
// in the same order.
//
// There is little use of back-tracking/undo or "retry" in the constraint solver, except in the
// limited case ofs of SRTP solving and method overloading, and some other adhoc limited cases
// like checking for "printf" format strings. As a result there are cases involving
// method overloading and SRTP that the solver "can't solve". This is intentional and by-design.
//-------------------------------------------------------------------------
module internal FSharp.Compiler.ConstraintSolver
open Internal.Utilities.Collections
open Internal.Utilities.Library
open Internal.Utilities.Library.Extras
open Internal.Utilities.Rational
open FSharp.Compiler
open FSharp.Compiler.AbstractIL
open FSharp.Compiler.AccessibilityLogic
open FSharp.Compiler.AttributeChecking
open FSharp.Compiler.DiagnosticsLogger
open FSharp.Compiler.Features
open FSharp.Compiler.Import
open FSharp.Compiler.InfoReader
open FSharp.Compiler.Infos
open FSharp.Compiler.MethodCalls
open FSharp.Compiler.NameResolution
open FSharp.Compiler.Syntax
open FSharp.Compiler.Syntax.PrettyNaming
open FSharp.Compiler.SyntaxTreeOps
open FSharp.Compiler.TcGlobals
open FSharp.Compiler.Text
open FSharp.Compiler.TypedTree
open FSharp.Compiler.TypedTreeBasics
open FSharp.Compiler.TypedTreeOps
open FSharp.Compiler.TypeHierarchy
open FSharp.Compiler.TypeRelations
#if !NO_TYPEPROVIDERS
open FSharp.Compiler.TypeProviders
#endif
//-------------------------------------------------------------------------
// 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 Range.range0 unassignedTyparName
let NewCompGenTypar (kind, rigid, staticReq, dynamicReq, error) =
Construct.NewTypar(kind, rigid, SynTypar(compgenId, staticReq, true), error, dynamicReq, [], false, false)
let AnonTyparId m = mkSynId m unassignedTyparName
let NewAnonTypar (kind, m, rigid, var, dyn) =
Construct.NewTypar (kind, rigid, SynTypar(AnonTyparId m, var, true), false, dyn, [], false, false)
let NewNamedInferenceMeasureVar (_m, rigid, var, id) =
Construct.NewTypar(TyparKind.Measure, rigid, SynTypar(id, var, false), false, TyparDynamicReq.No, [], false, false)
let NewInferenceMeasurePar () =
NewCompGenTypar (TyparKind.Measure, TyparRigidity.Flexible, TyparStaticReq.None, TyparDynamicReq.No, false)
let NewErrorTypar () =
NewCompGenTypar (TyparKind.Type, TyparRigidity.Flexible, TyparStaticReq.None, TyparDynamicReq.No, true)
let NewErrorType () =
mkTyparTy (NewErrorTypar ())
let FreshenTypar (g: TcGlobals) rigid (tp: Typar) =
let clearStaticReq = g.langVersion.SupportsFeature LanguageFeature.InterfacesWithAbstractStaticMembers
let staticReq = if clearStaticReq then TyparStaticReq.None else tp.StaticReq
let dynamicReq = if rigid = TyparRigidity.Rigid then TyparDynamicReq.Yes else TyparDynamicReq.No
NewCompGenTypar (tp.Kind, rigid, staticReq, dynamicReq, false)
// 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 g m rigid fctps tinst tpsorig =
let tps = tpsorig |> List.map (FreshenTypar g rigid)
let renaming, tinst = FixupNewTypars m fctps tinst tpsorig tps
tps, renaming, tinst
let FreshenTypeInst g m tpsorig =
FreshenAndFixupTypars g m TyparRigidity.Flexible [] [] tpsorig
let FreshMethInst g m fctps tinst tpsorig =
FreshenAndFixupTypars g m TyparRigidity.Flexible fctps tinst tpsorig
let FreshenMethInfo m (minfo: MethInfo) =
let _, _, tpTys = FreshMethInst minfo.TcGlobals m (minfo.GetFormalTyparsOfDeclaringType m) minfo.DeclaringTypeInst minfo.FormalMethodTypars
tpTys
//-------------------------------------------------------------------------
// Unification of types: solve/record equality constraints
// Subsumption of types: solve/record subtyping constraints
//-------------------------------------------------------------------------
/// Information about the context of a type equation.
[<RequireQualifiedAccess>]
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
/// Captures relevant information for a particular failed overload resolution.
type OverloadInformation =
{
methodSlot: CalledMeth<Expr>
infoReader : InfoReader
error: exn
}
/// Cases for overload resolution failure that exists in the implementation of the compiler.
type OverloadResolutionFailure =
| NoOverloadsFound of
methodName: string *
candidates: OverloadInformation list *
cx: TraitConstraintInfo option
| PossibleCandidates of
methodName: string *
candidates: OverloadInformation list *
cx: TraitConstraintInfo option
type OverallTy =
/// Each branch of the expression must have the type indicated
| MustEqual of TType
/// Each branch of the expression must convert to the type indicated
| MustConvertTo of isMethodArg: bool * ty: TType
/// Represents a point where no subsumption/widening is possible
member x.Commit =
match x with
| MustEqual ty -> ty
| MustConvertTo (_, ty) -> ty
exception ConstraintSolverTupleDiffLengths of displayEnv: DisplayEnv * contextInfo: ContextInfo * TType list * TType list * range * range
exception ConstraintSolverInfiniteTypes of displayEnv: DisplayEnv * contextInfo: ContextInfo * TType * TType * range * range
exception ConstraintSolverTypesNotInEqualityRelation of displayEnv: DisplayEnv * TType * TType * range * range * ContextInfo
exception ConstraintSolverTypesNotInSubsumptionRelation of displayEnv: DisplayEnv * argTy: TType * paramTy: TType * callRange: range * parameterRange: range
exception ConstraintSolverMissingConstraint of displayEnv: DisplayEnv * Typar * TyparConstraint * range * range
exception ConstraintSolverNullnessWarningEquivWithTypes of DisplayEnv * TType * TType * NullnessInfo * NullnessInfo * range * range
exception ConstraintSolverNullnessWarningWithTypes of DisplayEnv * TType * TType * NullnessInfo * NullnessInfo * range * range
exception ConstraintSolverNullnessWarningWithType of DisplayEnv * TType * NullnessInfo * range * range
exception ConstraintSolverNullnessWarning of string * range * range
exception ConstraintSolverError of string * range * range
exception ErrorFromApplyingDefault of tcGlobals: TcGlobals * displayEnv: DisplayEnv * Typar * TType * error: exn * range: range
exception ErrorFromAddingTypeEquation of tcGlobals: TcGlobals * displayEnv: DisplayEnv * expectedTy: TType * actualTy: TType * error: exn * range: range
exception ErrorsFromAddingSubsumptionConstraint of tcGlobals: TcGlobals * displayEnv: DisplayEnv * expectedTy: TType * actualTy: TType * error: exn * ctxtInfo: ContextInfo * parameterRange: range
exception ErrorFromAddingConstraint of displayEnv: DisplayEnv * error: exn * range: range
exception UnresolvedOverloading of displayEnv: DisplayEnv * callerArgs: CallerArgs<Expr> * failure: OverloadResolutionFailure * range: range
exception UnresolvedConversionOperator of displayEnv: DisplayEnv * TType * TType * range
type TcValF = ValRef -> ValUseFlag -> TType list -> range -> Expr * TType
type ConstraintSolverState =
{
g: TcGlobals
amap: ImportMap
InfoReader: InfoReader
/// The function used to freshen values we encounter during trait constraint solving
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>
/// Checks to run after all inference is complete, but before defaults are applied and internal unknowns solved
PostInferenceChecksPreDefaults: ResizeArray<unit -> unit>
/// Checks to run after all inference is complete.
PostInferenceChecksFinal: ResizeArray<unit -> unit>
WarnWhenUsingWithoutNullOnAWithNullTarget: string option
}
static member New(g, amap, infoReader, tcVal) =
{ g = g
amap = amap
ExtraCxs = HashMultiMap(10, HashIdentity.Structural)
InfoReader = infoReader
TcVal = tcVal
PostInferenceChecksPreDefaults = ResizeArray()
PostInferenceChecksFinal = ResizeArray()
WarnWhenUsingWithoutNullOnAWithNullTarget = None }
member this.PushPostInferenceCheck (preDefaults, check) =
if preDefaults then
this.PostInferenceChecksPreDefaults.Add check
else
this.PostInferenceChecksFinal.Add check
member this.PopPostInferenceCheck (preDefaults) =
if preDefaults then
this.PostInferenceChecksPreDefaults.RemoveAt(this.PostInferenceChecksPreDefaults.Count-1)
else
this.PostInferenceChecksFinal.RemoveAt(this.PostInferenceChecksPreDefaults.Count-1)
member this.GetPostInferenceChecksPreDefaults() =
this.PostInferenceChecksPreDefaults.ToArray() :> seq<_>
member this.GetPostInferenceChecksFinal() =
this.PostInferenceChecksFinal.ToArray() :> seq<_>
type ConstraintSolverEnv =
{
SolverState: ConstraintSolverState
eContextInfo: ContextInfo
// Is this speculative, with a trace allowing undo, and trial method overload resolution
IsSpeculativeForMethodOverloading: bool
// Can this ignore the 'must support null' constraint, e.g. in a mutable assignment scenario
IsSupportsNullFlex: bool
/// Indicates that when unifying ty1 = ty2, only type variables in ty1 may be solved. Constraints
/// can't be added to type variables in ty2
MatchingOnly: bool
/// Indicates that special errors on unresolved SRTP constraint overloads may be generated. When
/// these are caught they result in postponed constraints.
ErrorOnFailedMemberConstraintResolution: bool
/// During MatchingOnly constraint solving, marks additional type variables as
/// rigid, preventing constraints flowing to those type variables.
ExtraRigidTypars: Zset<Typar>
m: range
EquivEnv: TypeEquivEnv
DisplayEnv: DisplayEnv
}
member csenv.InfoReader = csenv.SolverState.InfoReader
member csenv.g = csenv.SolverState.g
member csenv.amap = csenv.SolverState.amap
override csenv.ToString() = "<ConstraintSolverEnv> @ " + csenv.m.ToString()
let MakeConstraintSolverEnv contextInfo css m denv =
{ SolverState = css
m = m
eContextInfo = contextInfo
MatchingOnly = false
ErrorOnFailedMemberConstraintResolution = false
EquivEnv = TypeEquivEnv.Empty
DisplayEnv = denv
IsSpeculativeForMethodOverloading = false
IsSupportsNullFlex = false
ExtraRigidTypars = emptyFreeTypars
}
/// Check whether a type variable occurs in the r.h.s. of a type, e.g. to catch
/// infinite equations such as
/// 'a = 'a list
let rec occursCheck g un ty =
match stripTyEqns g ty with
| TType_ucase(_, l)
| TType_app (_, l, _)
| TType_anon(_, l)
| TType_tuple (_, l) -> List.exists (occursCheck g un) l
| TType_fun (domainTy, rangeTy, _) -> occursCheck g un domainTy || occursCheck g un rangeTy
| TType_var (r, _) -> typarEq un r
| TType_forall (_, tau) -> occursCheck g un tau
| _ -> false
//-------------------------------------------------------------------------
// Predicates on types
//-------------------------------------------------------------------------
/// Some additional solutions are forced prior to generalization (permitWeakResolution=true). These are, roughly speaking, rules
/// for binary-operand constraints arising from constructs such as "1.0 + x" where "x" is an unknown type. The constraint here
/// involves two type parameters - one for the left, and one for the right. The left is already known to be Double.
/// In this situation (and in the absence of other evidence prior to generalization), constraint solving forces an assumption that
/// the right is also Double - this is "weak" because there is only weak evidence for it.
///
/// permitWeakResolution also applies to resolutions of multi-type-variable constraints via method overloads. Method overloading gets applied even if
/// only one of the two type variables is known.
///
/// During code gen we run with permitWeakResolution on, but we only apply it where one of the argument types for the built-in constraint resolution is
/// a variable type.
type PermitWeakResolution =
| Yes
| No
member x.Permit = match x with Yes -> true | No -> false
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 rec IsIntegerOrIntegerEnumTy g ty =
isSignedIntegerTy g ty ||
isUnsignedIntegerTy g ty ||
(isEnumTy g ty && IsIntegerOrIntegerEnumTy g (underlyingTypeOfEnumTy 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
let IsNonDecimalNumericOrIntegralEnumType g ty = IsIntegerOrIntegerEnumTy g ty || isFpTy g ty
let IsNumericOrIntegralEnumType g ty = IsNonDecimalNumericOrIntegralEnumType g ty || isDecimalTy g ty
let IsRelationalType g ty = isNumericType g ty || isStringTy g ty || isCharTy g ty || isBoolTy g ty
let IsCharOrStringType g ty = isCharTy g ty || isStringTy g ty
/// Checks the argument type for a built-in solution to an op_Addition, op_Subtraction or op_Modulus constraint.
let IsAddSubModType nm g ty = IsNumericOrIntegralEnumType g ty || (nm = "op_Addition" && IsCharOrStringType g ty) || (nm = "op_Subtraction" && isCharTy g ty)
/// Checks the argument type for a built-in solution to a bitwise operator constraint
let IsBitwiseOpType g ty = IsIntegerOrIntegerEnumTy g ty || (isEnumTy g ty)
/// Check the other type in a built-in solution for a binary operator.
/// For weak resolution, require a relevant primitive on one side.
/// For strong resolution, a variable type is permitted.
let IsBinaryOpOtherArgType g permitWeakResolution ty =
match permitWeakResolution with
| PermitWeakResolution.No ->
not (isTyparTy g ty)
| PermitWeakResolution.Yes -> true
/// Checks the argument type for a built-in solution to a get_Sign constraint.
let IsSignType g ty =
isSignedIntegerTy g ty || isFpTy g ty || isDecimalTy g ty
type TraitConstraintSolution =
| TTraitUnsolved
| TTraitBuiltIn
| TTraitSolved of minfo: MethInfo * minst: TypeInst * staticTyOpt: TType option
| TTraitSolvedRecdProp of fieldInfo: RecdFieldInfo * isSetProp: bool
| TTraitSolvedAnonRecdProp of anonRecdTypeInfo: AnonRecdTypeInfo * typeInst: TypeInst * index: int
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, res) -> Some (calledMeth, warns, trace, res))
let ShowAccessDomain ad =
match ad with
| AccessibleFromEverywhere -> "public"
| AccessibleFrom _ -> "accessible"
| AccessibleFromSomeFSharpCode -> "public, protected or internal"
| AccessibleFromSomewhere -> ""
//-------------------------------------------------------------------------
// Solve
exception NonRigidTypar of displayEnv: DisplayEnv * string option * range * TType * TType * range
/// Signal that there is still an unresolved overload in the constraint problem. The
/// unresolved overload constraint remains in the constraint state, and we skip any
/// further processing related to whichever overall adjustment to constraint solver state
/// is being processed.
///
// NOTE: The addition of this abort+skip appears to be a mistake which has crept into F# type inference,
// and its status is currently under review. See https://github.com/dotnet/fsharp/pull/8294 and others.
//
// Here is the history:
// 1. The local abort was added as part of an attempted performance optimization https://github.com/dotnet/fsharp/pull/1650
// This change was released in the VS2017 GA release.
//
// 2. However, it also impacts the logic of type inference, by skipping checking.
// Because of this an attempt was made to revert it in https://github.com/dotnet/fsharp/pull/4173.
//
// Unfortunately, existing code had begun to depend on the new behaviours enabled by the
// change, and the revert was abandoned before release in https://github.com/dotnet/fsharp/pull/4348
//
// Comments on soundness:
// The use of the abort is normally sound because the SRTP constraint
// will be subject to further processing at a later point.
//
// However, it seems likely that the abort may result in other processing associated
// with an overall constraint being skipped (e.g. the processing related to subsequent elements
// of a tuple constraint).
exception AbortForFailedMemberConstraintResolution
/// This is used internally in method overload resolution
let IgnoreFailedMemberConstraintResolution f1 f2 =
TryD
f1
(function
| AbortForFailedMemberConstraintResolution -> CompleteD
| exn -> f2 exn)
/// This is used at (nearly all) entry points into the constraint solver to make sure that the
/// AbortForFailedMemberConstraintResolution error result is caught, the constraint recorded
/// as a post-inference check and processing continues.
///
/// Due to the legacy of the change https://github.com/dotnet/fsharp/pull/1650, some constraint
/// applications must be allowed to "succeed" with partial processing of the unification being
/// left in place, and no error being raised. This happens in cases where SRTP overload
/// resolution has failed. SRTP resolution is delayed and presumably resolved by later type information.
///
/// Quite a lot of code related to tasks has come to rely on this feature.
///
/// To ensure soundness, we double-check the constraint at the end of inference
/// with 'ErrorOnFailedMemberConstraintResolution' set to false.
let PostponeOnFailedMemberConstraintResolution (csenv: ConstraintSolverEnv) (trace: OptionalTrace) f1 f2 =
TryD
(fun () ->
let csenv = { csenv with ErrorOnFailedMemberConstraintResolution = true }
f1 csenv)
(function
| AbortForFailedMemberConstraintResolution ->
// Postponed checking of constraints for failed SRTP resolutions is supported from F# 6.0 onwards
// and is required for the "tasks" (aka ResumableStateMachines) feature.
//
// See https://github.com/dotnet/fsharp/issues/12188
if csenv.g.langVersion.SupportsFeature LanguageFeature.ResumableStateMachines then
trace.Exec
(fun () ->
csenv.SolverState.PushPostInferenceCheck (preDefaults=true, check = fun () ->
let csenv = { csenv with ErrorOnFailedMemberConstraintResolution = false }
f1 csenv |> RaiseOperationResult))
(fun () ->
csenv.SolverState.PopPostInferenceCheck (preDefaults=true))
CompleteD
| exn -> f2 exn)
/// used to provide detail about non matched argument in overload resolution error message
exception ArgDoesNotMatchError of error: ErrorsFromAddingSubsumptionConstraint * calledMeth: CalledMeth<Expr> * calledArg: CalledArg * callerArg: CallerArg<Expr>
/// Represents a very local condition where we prefer to report errors before stripping type abbreviations.
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
let g = csenv.g
// Prior to feature InterfacesWithAbstractStaticMembers the StaticReq must match the
// declared StaticReq. With feature InterfacesWithAbstractStaticMembers it is inferred
// from the finalized constraints on the type variable.
if not (g.langVersion.SupportsFeature LanguageFeature.InterfacesWithAbstractStaticMembers) && 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
| TyparStaticReq.None -> CompleteD
| TyparStaticReq.HeadType ->
// requires that a type constructor be known at compile time
match stripTyparEqns ty with
| TType_measure ms ->
let vs = ListMeasureVarOccsWithNonZeroExponents ms
trackErrors {
for tpr, _ in vs do
do! SolveTypStaticReqTypar csenv trace req tpr
}
| _ ->
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 =
trackErrors {
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
do! SolveTypStaticReq csenv trace v.StaticReq (TType_measure ms)
SubstMeasure v ms
return! 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'
do! SolveTypStaticReq csenv trace v.StaticReq (TType_measure ms)
SubstMeasure v ms
if v.Rigidity = TyparRigidity.Anon && measureEquiv csenv.g ms Measure.One then
return! WarnD(Error(FSComp.SR.csCodeLessGeneric(), v.Range))
else
()
}
let IsRigid (csenv: ConstraintSolverEnv) (tp: Typar) =
tp.Rigidity = TyparRigidity.Rigid
|| csenv.ExtraRigidTypars.Contains tp
/// 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, _) -> IsRigid csenv v)
// 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.Const 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 nonZeroCon = ListMeasureConOccsWithNonZeroExponents g false ms
let nonZeroVar = ListMeasureVarOccsWithNonZeroExponents ms
let newms =
ProdMeasures [
for (c, e') in nonZeroCon do
Measure.RationalPower (Measure.Const c, NegRational (DivRational e' e))
for (v', e') in nonZeroVar do
if typarEq v v' then
newvarExpr
else
Measure.RationalPower (Measure.Var v', NegRational (DivRational e' e))
]
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_anon (_,l)
| TType_tuple (_, l) -> SimplifyMeasuresInTypes g param l
| TType_fun (domainTy, rangeTy, _) ->
if resultFirst then
SimplifyMeasuresInTypes g param [rangeTy;domainTy]
else
SimplifyMeasuresInTypes g param [domainTy;rangeTy]
| 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_anon (_,l)
| TType_tuple (_, l) -> GetMeasureVarGcdInTypes v l
| TType_fun (domainTy, rangeTy, _) -> GcdRational (GetMeasureVarGcdInType v domainTy) (GetMeasureVarGcdInType v rangeTy)
| 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.Rigidity <> TyparRigidity.Rigid && v.Kind = TyparKind.Measure)
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 SolveTyparEqualsTypePart1 (csenv: ConstraintSolverEnv) m2 (trace: OptionalTrace) ty1 r ty =
trackErrors {
// The types may still be equivalent due to abbreviations, which we are trying not to eliminate
if typeEquiv csenv.g ty1 ty then () else
// The famous 'occursCheck' check to catch "infinite types" like 'a = list<'a> - see also https://github.com/dotnet/fsharp/issues/1170
if occursCheck csenv.g r ty then return! ErrorD (ConstraintSolverInfiniteTypes(csenv.DisplayEnv, csenv.eContextInfo, ty1, ty, csenv.m, m2)) else
// Note: warn _and_ continue!
do! CheckWarnIfRigid csenv ty1 r ty
// 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
//let ty1AllowsNull = r.Constraints |> List.exists (function | TyparConstraint.SupportsNull _ -> true | _ -> false )
//let tyAllowsNull() = TypeNullIsExtraValueNew csenv.g m2 ty
//if ty1AllowsNull && not (tyAllowsNull()) then
// trace.Exec (fun () -> r.typar_solution <- Some (ty |> replaceNullnessOfTy csenv.g.knownWithNull)) (fun () -> r.typar_solution <- None)
//else
// trace.Exec (fun () -> r.typar_solution <- Some ty) (fun () -> r.typar_solution <- None)
trace.Exec (fun () -> r.typar_solution <- Some ty) (fun () -> r.typar_solution <- None)
}
and SolveTyparEqualsTypePart2 (csenv: ConstraintSolverEnv) ndeep m2 (trace: OptionalTrace) (r: Typar) ty =
trackErrors {
// Only solve constraints if this is not an error var
if r.IsFromError then () 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
do! RepeatWhileD ndeep (fun ndeep -> SolveRelevantMemberConstraintsForTypar csenv ndeep PermitWeakResolution.No trace r)
// Re-solve the other constraints associated with this type variable
return! SolveTypMeetsTyparConstraints csenv ndeep m2 trace ty r
}
/// Apply the constraints on 'typar' to the type 'ty'
and SolveTypMeetsTyparConstraints (csenv: ConstraintSolverEnv) ndeep m2 trace ty (r: Typar) =
trackErrors {
let g = csenv.g
// Propagate compat flex requirements from 'tp' to 'ty'
do! SolveTypIsCompatFlex csenv trace r.IsCompatFlex ty
// Propagate dynamic requirements from 'tp' to 'ty'
do! SolveTypDynamicReq csenv trace r.DynamicReq ty
// Propagate static requirements from 'tp' to 'ty'
do! SolveTypStaticReq csenv trace r.StaticReq ty
if not (g.langVersion.SupportsFeature LanguageFeature.InterfacesWithAbstractStaticMembers) then
// Propagate static requirements from 'tp' to 'ty'