@@ -64,10 +64,10 @@ let compgenId = mkSynId range0 unassignedTyparName
6464let NewCompGenTypar ( kind , rigid , staticReq , dynamicReq , error ) =
6565 NewTypar( kind, rigid, Typar( compgenId, staticReq, true ), error, dynamicReq, [], false , false )
6666
67- let anon_id m = mkSynId m unassignedTyparName
67+ let AnonTyparId m = mkSynId m unassignedTyparName
6868
6969let NewAnonTypar ( kind , m , rigid , var , dyn ) =
70- NewTypar ( kind, rigid, Typar( anon _ id m, var, true ), false , dyn, [], false , false )
70+ NewTypar ( kind, rigid, Typar( AnonTyparId m, var, true ), false , dyn, [], false , false )
7171
7272let NewNamedInferenceMeasureVar ( _m , rigid , var , id ) =
7373 NewTypar( TyparKind.Measure, rigid, Typar( id, var, false ), false , TyparDynamicReq.No, [], false , false )
@@ -104,6 +104,7 @@ let FreshenAndFixupTypars m rigid fctps tinst tpsorig =
104104 tps, renaming, tinst
105105
106106let FreshenTypeInst m tpsorig = FreshenAndFixupTypars m TyparRigidity.Flexible [] [] tpsorig
107+
107108let FreshMethInst m fctps tinst tpsorig = FreshenAndFixupTypars m TyparRigidity.Flexible fctps tinst tpsorig
108109
109110let FreshenTypars m tpsorig =
@@ -126,62 +127,95 @@ let FreshenMethInfo m (minfo: MethInfo) =
126127[<RequireQualifiedAccess>]
127128/// Information about the context of a type equation.
128129type ContextInfo =
130+
129131 /// No context was given.
130132 | NoContext
133+
131134 /// The type equation comes from an IF expression.
132135 | IfExpression of range
136+
133137 /// The type equation comes from an omitted else branch.
134138 | OmittedElseBranch of range
139+
135140 /// The type equation comes from a type check of the result of an else branch.
136141 | ElseBranchResult of range
142+
137143 /// The type equation comes from the verification of record fields.
138144 | RecordFields
145+
139146 /// The type equation comes from the verification of a tuple in record fields.
140147 | TupleInRecordFields
148+
141149 /// The type equation comes from a list or array constructor
142150 | CollectionElement of bool * range
151+
143152 /// The type equation comes from a return in a computation expression.
153+
144154 | ReturnInComputationExpression
155+
145156 /// The type equation comes from a yield in a computation expression.
146157 | YieldInComputationExpression
158+
147159 /// The type equation comes from a runtime type test.
148160 | RuntimeTypeTest of bool
161+
149162 /// The type equation comes from an downcast where a upcast could be used.
150163 | DowncastUsedInsteadOfUpcast of bool
164+
151165 /// The type equation comes from a return type of a pattern match clause (not the first clause).
152166 | FollowingPatternMatchClause of range
167+
153168 /// The type equation comes from a pattern match guard.
154169 | PatternMatchGuard of range
170+
155171 /// The type equation comes from a sequence expression.
156172 | SequenceExpression of TType
157173
158- exception ConstraintSolverTupleDiffLengths of displayEnv : DisplayEnv * TType list * TType list * range * range
159- exception ConstraintSolverInfiniteTypes of displayEnv : DisplayEnv * contextInfo : ContextInfo * TType * TType * range * range
160- exception ConstraintSolverTypesNotInEqualityRelation of displayEnv : DisplayEnv * TType * TType * range * range * ContextInfo
174+ exception ConstraintSolverTupleDiffLengths of displayEnv : DisplayEnv * TType list * TType list * range * range
175+
176+ exception ConstraintSolverInfiniteTypes of displayEnv : DisplayEnv * contextInfo : ContextInfo * TType * TType * range * range
177+
178+ exception ConstraintSolverTypesNotInEqualityRelation of displayEnv : DisplayEnv * TType * TType * range * range * ContextInfo
179+
161180exception ConstraintSolverTypesNotInSubsumptionRelation of displayEnv : DisplayEnv * TType * TType * range * range
162- exception ConstraintSolverMissingConstraint of displayEnv : DisplayEnv * Tast.Typar * Tast.TyparConstraint * range * range
163- exception ConstraintSolverError of string * range * range
164- exception ConstraintSolverRelatedInformation of string option * range * exn
165181
166- exception ErrorFromApplyingDefault of tcGlobals : TcGlobals * displayEnv : DisplayEnv * Tast.Typar * TType * exn * range
167- exception ErrorFromAddingTypeEquation of tcGlobals : TcGlobals * displayEnv : DisplayEnv * TType * TType * exn * range
182+ exception ConstraintSolverMissingConstraint of displayEnv : DisplayEnv * Tast.Typar * Tast.TyparConstraint * range * range
183+
184+ exception ConstraintSolverError of string * range * range
185+
186+ exception ConstraintSolverRelatedInformation of string option * range * exn
187+
188+ exception ErrorFromApplyingDefault of tcGlobals : TcGlobals * displayEnv : DisplayEnv * Tast.Typar * TType * exn * range
189+
190+ exception ErrorFromAddingTypeEquation of tcGlobals : TcGlobals * displayEnv : DisplayEnv * TType * TType * exn * range
191+
168192exception ErrorsFromAddingSubsumptionConstraint of tcGlobals : TcGlobals * displayEnv : DisplayEnv * TType * TType * exn * ContextInfo * range
169- exception ErrorFromAddingConstraint of displayEnv : DisplayEnv * exn * range
170- exception PossibleOverload of displayEnv : DisplayEnv * string * exn * range
171- exception UnresolvedOverloading of displayEnv : DisplayEnv * exn list * string * range
172- exception UnresolvedConversionOperator of displayEnv : DisplayEnv * TType * TType * range
193+
194+ exception ErrorFromAddingConstraint of displayEnv : DisplayEnv * exn * range
195+
196+ exception PossibleOverload of displayEnv : DisplayEnv * string * exn * range
197+
198+ exception UnresolvedOverloading of displayEnv : DisplayEnv * exn list * string * range
199+
200+ exception UnresolvedConversionOperator of displayEnv : DisplayEnv * TType * TType * range
173201
174202let GetPossibleOverloads amap m denv ( calledMethGroup : ( CalledMeth < _ > * exn ) list ) =
175- calledMethGroup |> List.map ( fun ( cmeth , e ) -> PossibleOverload( denv, NicePrint.stringOfMethInfo amap m denv cmeth.Method, e, m))
203+ calledMethGroup |> List.map ( fun ( cmeth , e ) ->
204+ PossibleOverload( denv, NicePrint.stringOfMethInfo amap m denv cmeth.Method, e, m))
176205
177206type TcValF = ( ValRef -> ValUseFlag -> TType list -> range -> Expr * TType)
178207
179208type ConstraintSolverState =
180209 {
181210 g: TcGlobals
211+
182212 amap: Import .ImportMap
213+
183214 InfoReader: InfoReader
215+
216+ /// The function used to freshen values we encounter during trait constraint solving
184217 TcVal: TcValF
218+
185219 /// This table stores all unsolved, ungeneralized trait constraints, indexed by free type variable.
186220 /// That is, there will be one entry in this table for each free type variable in
187221 /// each outstanding, unsolved, ungeneralized trait constraint. Constraints are removed from the table and resolved
@@ -196,20 +230,29 @@ type ConstraintSolverState =
196230 InfoReader = infoReader
197231 TcVal = tcVal }
198232
199-
200233type ConstraintSolverEnv =
201234 {
202235 SolverState: ConstraintSolverState
236+
203237 eContextInfo: ContextInfo
238+
204239 MatchingOnly: bool
240+
205241 m: range
242+
206243 EquivEnv: TypeEquivEnv
244+
207245 DisplayEnv: DisplayEnv
208246 }
247+
209248 member csenv.InfoReader = csenv.SolverState.InfoReader
249+
210250 member csenv.g = csenv.SolverState.g
251+
211252 member csenv.amap = csenv.SolverState.amap
212253
254+ override csenv.ToString () = " <ConstraintSolverEnv> @ " + csenv.m.ToString()
255+
213256let MakeConstraintSolverEnv contextInfo css m denv =
214257 { SolverState = css
215258 m = m
@@ -219,11 +262,6 @@ let MakeConstraintSolverEnv contextInfo css m denv =
219262 EquivEnv = TypeEquivEnv.Empty
220263 DisplayEnv = denv }
221264
222-
223- //-------------------------------------------------------------------------
224- // Occurs check
225- //-------------------------------------------------------------------------
226-
227265/// Check whether a type variable occurs in the r.h.s. of a type, e.g. to catch
228266/// infinite equations such as
229267/// 'a = list<'a>
@@ -287,9 +325,13 @@ let isDecimalTy g ty =
287325 typeEquivAux EraseMeasures g g.decimal_ ty ty
288326
289327let IsNonDecimalNumericOrIntegralEnumType g ty = isIntegerOrIntegerEnumTy g ty || isFpTy g ty
328+
290329let IsNumericOrIntegralEnumType g ty = IsNonDecimalNumericOrIntegralEnumType g ty || isDecimalTy g ty
330+
291331let IsNonDecimalNumericType g ty = isIntegerTy g ty || isFpTy g ty
332+
292333let IsNumericType g ty = IsNonDecimalNumericType g ty || isDecimalTy g ty
334+
293335let IsRelationalType g ty = IsNumericType g ty || isStringTy g ty || isCharTy g ty || isBoolTy g ty
294336
295337// Get measure of type, float<_> or float32<_> or decimal<_> but not float=float<1> or float32=float32<1> or decimal=decimal<1>
@@ -386,8 +428,11 @@ let ShowAccessDomain ad =
386428// Solve
387429
388430exception NonRigidTypar of displayEnv : DisplayEnv * string option * range * TType * TType * range
431+
389432exception LocallyAbortOperationThatFailsToResolveOverload
433+
390434exception LocallyAbortOperationThatLosesAbbrevs
435+
391436let localAbortD = ErrorD LocallyAbortOperationThatLosesAbbrevs
392437
393438/// Return true if we would rather unify this variable v1 := v2 than vice versa
@@ -652,7 +697,6 @@ let NormalizeExponentsInTypeScheme uvars ty =
652697 SubstMeasure v ( Measure.RationalPower ( Measure.Var v', DivRational OneRational expGcd))
653698 v')
654699
655-
656700// We normalize unit-of-measure-polymorphic type schemes. There
657701// are three reasons for doing this:
658702// (1) to present concise and consistent type schemes to the programmer
@@ -732,8 +776,6 @@ let rec SolveTyparEqualsType (csenv: ConstraintSolverEnv) ndeep m2 (trace: Optio
732776 // Record a entry in the undo trace if one is provided
733777 trace.Exec ( fun () -> r.typar_ solution <- Some ty) ( fun () -> r.typar_ solution <- None)
734778
735- (* dprintf "setting typar %d to type %s at %a\n" r.Stamp ((DebugPrint.showType ty)) outputRange m; *)
736-
737779 // Only solve constraints if this is not an error var
738780 if r.IsFromError then () else
739781 // Check to see if this type variable is relevant to any trait constraints.
@@ -745,15 +787,17 @@ let rec SolveTyparEqualsType (csenv: ConstraintSolverEnv) ndeep m2 (trace: Optio
745787
746788 | _ -> failwith " SolveTyparEqualsType"
747789 }
748-
749790
750791/// Apply the constraints on 'typar' to the type 'ty'
751792and solveTypMeetsTyparConstraints ( csenv : ConstraintSolverEnv ) ndeep m2 trace ty ( r : Typar ) = trackErrors {
752793 let g = csenv.g
794+
753795 // Propagate compat flex requirements from 'tp' to 'ty'
754796 do ! SolveTypIsCompatFlex csenv trace r.IsCompatFlex ty
797+
755798 // Propagate dynamic requirements from 'tp' to 'ty'
756799 do ! SolveTypDynamicReq csenv trace r.DynamicReq ty
800+
757801 // Propagate static requirements from 'tp' to 'ty'
758802 do ! SolveTypStaticReq csenv trace r.StaticReq ty
759803
@@ -899,6 +943,7 @@ and SolveTypeSubsumesType (csenv: ConstraintSolverEnv) ndeep m2 (trace: Optional
899943 let amap = csenv.amap
900944 let aenv = csenv.EquivEnv
901945 let denv = csenv.DisplayEnv
946+
902947 match sty1, sty2 with
903948 | TType_ var tp1, _ ->
904949 match aenv.EquivTypars.TryFind tp1 with
@@ -914,15 +959,19 @@ and SolveTypeSubsumesType (csenv: ConstraintSolverEnv) ndeep m2 (trace: Optional
914959 | TType_ tuple ( tupInfo1, l1), TType_ tuple ( tupInfo2, l2) ->
915960 if evalTupInfoIsStruct tupInfo1 <> evalTupInfoIsStruct tupInfo2 then ErrorD ( ConstraintSolverError( FSComp.SR.tcTupleStructMismatch(), csenv.m, m2)) else
916961 SolveTypeEqualsTypeEqns csenv ndeep m2 trace cxsln l1 l2 (* nb. can unify since no variance *)
962+
917963 | TType_ anon ( anonInfo1, l1), TType_ anon ( anonInfo2, l2) ->
918964 SolveAnonInfoEqualsAnonInfo csenv m2 anonInfo1 anonInfo2 ++ ( fun () ->
919965 SolveTypeEqualsTypeEqns csenv ndeep m2 trace cxsln l1 l2) (* nb. can unify since no variance *)
966+
920967 | TType_ fun ( d1, r1), TType_ fun ( d2, r2) -> SolveFunTypeEqn csenv ndeep m2 trace cxsln d1 d2 r1 r2 (* nb. can unify since no variance *)
968+
921969 | TType_ measure ms1, TType_ measure ms2 -> UnifyMeasures csenv trace ms1 ms2
922970
923971 // Enforce the identities float=float<1>, float32=float32<1> and decimal=decimal<1>
924972 | (_, TType_ app ( tc2, [ ms])) when ( tc2.IsMeasureableReprTycon && typeEquiv csenv.g sty1 ( reduceTyconRefMeasureableOrProvided csenv.g tc2 [ ms]))
925973 -> SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln ms ( TType_ measure Measure.One)
974+
926975 | ( TType_ app ( tc2, [ ms]), _) when ( tc2.IsMeasureableReprTycon && typeEquiv csenv.g sty2 ( reduceTyconRefMeasureableOrProvided csenv.g tc2 [ ms]))
927976 -> SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln ms ( TType_ measure Measure.One)
928977
@@ -973,6 +1022,7 @@ and SolveTypeSubsumesType (csenv: ConstraintSolverEnv) ndeep m2 (trace: Optional
9731022 let ty2arg = destArrayTy g ty2
9741023 SolveTypeEqualsTypeKeepAbbrevsWithCxsln csenv ndeep m2 trace cxsln ty1arg ty2arg
9751024 | _ -> error( InternalError( " destArrayTy" , m))
1025+
9761026 | _ ->
9771027 // D<inst> :> Head<_> --> C<inst'> :> Head<_> for the
9781028 // first interface or super-class C supported by D which
@@ -991,7 +1041,6 @@ and SolveTypeSubsumesTypeKeepAbbrevs csenv ndeep m2 trace cxsln ty1 ty2 =
9911041// Solve and record non-equality constraints
9921042//-------------------------------------------------------------------------
9931043
994-
9951044and SolveTyparSubtypeOfType ( csenv : ConstraintSolverEnv ) ndeep m2 trace tp ty1 =
9961045 let g = csenv.g
9971046 if isObjTy g ty1 then CompleteD
@@ -1052,7 +1101,7 @@ and SolveMemberConstraint (csenv: ConstraintSolverEnv) ignoreUnresolvedOverload
10521101 | _ -> do ! ErrorD ( ConstraintSolverError( FSComp.SR.csExpectedArguments(), m, m2))
10531102 // Trait calls are only supported on pseudo type (variables)
10541103 for e in tys do
1055- do ! SolveTypStaticReq csenv trace HeadTypeStaticReq e
1104+ do ! SolveTypStaticReq csenv trace HeadTypeStaticReq e
10561105
10571106 let argtys = if memFlags.IsInstance then List.tail argtys else argtys
10581107
@@ -1108,14 +1157,18 @@ and SolveMemberConstraint (csenv: ConstraintSolverEnv) ignoreUnresolvedOverload
11081157 do ! SolveTypeEqualsTypeKeepAbbrevs csenv ndeep m2 trace argty2 ( mkAppTy tcref [ TType_ measure ms2])
11091158 do ! SolveTypeEqualsTypeKeepAbbrevs csenv ndeep m2 trace rty ( mkAppTy tcref [ TType_ measure ( Measure.Prod( ms1, if nm = " op_Multiply" then ms2 else Measure.Inv ms2))])
11101159 return TTraitBuiltIn
1160+
11111161 | _ ->
1162+
11121163 match GetMeasureOfType g argty2 with
11131164 | Some ( tcref, ms2) ->
11141165 let ms1 = freshMeasure ()
11151166 do ! SolveTypeEqualsTypeKeepAbbrevs csenv ndeep m2 trace argty1 ( mkAppTy tcref [ TType_ measure ms1])
11161167 do ! SolveTypeEqualsTypeKeepAbbrevs csenv ndeep m2 trace rty ( mkAppTy tcref [ TType_ measure ( Measure.Prod( ms1, if nm = " op_Multiply" then ms2 else Measure.Inv ms2))])
11171168 return TTraitBuiltIn
1169+
11181170 | _ ->
1171+
11191172 do ! SolveTypeEqualsTypeKeepAbbrevs csenv ndeep m2 trace argty2 argty1
11201173 do ! SolveTypeEqualsTypeKeepAbbrevs csenv ndeep m2 trace rty argty1
11211174 return TTraitBuiltIn
0 commit comments