-
Notifications
You must be signed in to change notification settings - Fork 795
/
Copy pathConstraintSolver.fsi
353 lines (263 loc) · 12.8 KB
/
ConstraintSolver.fsi
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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
/// Solves constraints using a mutable constraint-solver state
module internal FSharp.Compiler.ConstraintSolver
open FSharp.Compiler.AccessibilityLogic
open FSharp.Compiler.DiagnosticsLogger
open FSharp.Compiler.Import
open FSharp.Compiler.Infos
open FSharp.Compiler.InfoReader
open FSharp.Compiler.MethodCalls
open FSharp.Compiler.Syntax
open FSharp.Compiler.TcGlobals
open FSharp.Compiler.Text
open FSharp.Compiler.TypedTree
open FSharp.Compiler.TypedTreeOps
/// 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 * // methodNames may be different (with operators?), this is refactored from original logic to assemble overload failure message
cx: TraitConstraintInfo option
/// Represents known information prior to checking an expression or pattern, e.g. it's expected type
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 Commit: TType
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 UnresolvedConversionOperator of displayEnv: DisplayEnv * TType * TType * range
exception UnresolvedOverloading of
displayEnv: DisplayEnv *
callerArgs: CallerArgs<Expr> *
failure: OverloadResolutionFailure *
range: range
exception NonRigidTypar of displayEnv: DisplayEnv * string option * range * TType * TType * range
exception ArgDoesNotMatchError of
error: ErrorsFromAddingSubsumptionConstraint *
calledMeth: CalledMeth<Expr> *
calledArg: CalledArg *
callerArg: CallerArg<Expr>
/// A function that denotes captured tcVal, Used in constraint solver and elsewhere to get appropriate expressions for a ValRef.
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: Internal.Utilities.Collections.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: TcGlobals * ImportMap * InfoReader * TcValF -> ConstraintSolverState
/// Add a post-inference check to run at the end of inference
member PushPostInferenceCheck: preDefaults: bool * check: (unit -> unit) -> unit
/// Get the post-inference checks to run near the end of inference, but before defaults are applied
member GetPostInferenceChecksPreDefaults: unit -> seq<unit -> unit>
/// Get the post-inference checks to run at the end of inference
member GetPostInferenceChecksFinal: unit -> seq<unit -> unit>
val BakedInTraitConstraintNames: Set<string>
[<Sealed; NoEquality; NoComparison>]
type Trace
type OptionalTrace =
| NoTrace
| WithTrace of Trace
val SimplifyMeasuresInTypeScheme: TcGlobals -> bool -> Typars -> TType -> TyparConstraint list -> Typars
/// The entry point to resolve the overloading for an entire call
val ResolveOverloadingForCall:
DisplayEnv ->
ConstraintSolverState ->
range ->
methodName: string ->
callerArgs: CallerArgs<Expr> ->
AccessorDomain ->
calledMethGroup: CalledMeth<Expr> list ->
permitOptArgs: bool ->
reqdRetTy: OverallTy ->
CalledMeth<Expr> option * OperationResult<unit>
val UnifyUniqueOverloading:
DisplayEnv ->
ConstraintSolverState ->
range ->
int * int ->
string ->
AccessorDomain ->
CalledMeth<SynExpr> list ->
OverallTy ->
OperationResult<bool>
/// Re-assess the staticness of the type parameters
val UpdateStaticReqOfTypar: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> Typar -> unit
/// Remove the global constraints related to generalized type variables
val EliminateConstraintsForGeneralizedTypars:
DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> Typars -> unit
val CheckDeclaredTypars: DisplayEnv -> ConstraintSolverState -> range -> Typars -> Typars -> unit
val AddCxTypeEqualsType: ContextInfo -> DisplayEnv -> ConstraintSolverState -> range -> TType -> TType -> unit
val AddCxTypeEqualsTypeUndoIfFailed: DisplayEnv -> ConstraintSolverState -> range -> TType -> TType -> bool
val AddCxTypeEqualsTypeUndoIfFailedOrWarnings: DisplayEnv -> ConstraintSolverState -> range -> TType -> TType -> bool
val AddCxTypeEqualsTypeMatchingOnlyUndoIfFailed: DisplayEnv -> ConstraintSolverState -> range -> TType -> TType -> bool
val AddCxTypeMustSubsumeType:
ContextInfo -> DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> TType -> unit
val AddCxTypeMustSubsumeTypeUndoIfFailed: DisplayEnv -> ConstraintSolverState -> range -> TType -> TType -> bool
val AddCxTypeMustSubsumeTypeMatchingOnlyUndoIfFailed:
DisplayEnv -> ConstraintSolverState -> range -> extraRigidTypars: FreeTypars -> TType -> TType -> bool
val AddCxMethodConstraint: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TraitConstraintInfo -> unit
val AddCxTypeDefnNotSupportsNull: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> unit
val AddCxTypeUseSupportsNull: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> unit
val AddCxTypeCanCarryNullnessInfo: DisplayEnv -> ConstraintSolverState -> range -> TType -> Nullness -> unit
val AddCxTypeMustSupportComparison: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> unit
val AddCxTypeMustSupportEquality: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> unit
val AddCxTypeMustSupportDefaultCtor: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> unit
val AddCxTypeIsReferenceType: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> unit
val AddCxTypeIsValueType: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> unit
val AddCxTypeIsUnmanaged: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> unit
val AddCxTypeIsEnum: DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> TType -> unit
val AddCxTypeIsDelegate:
DisplayEnv -> ConstraintSolverState -> range -> OptionalTrace -> TType -> TType -> TType -> unit
val AddCxTyparDefaultsTo: DisplayEnv -> ConstraintSolverState -> range -> ContextInfo -> Typar -> int -> TType -> unit
val SolveTypeAsError: DisplayEnv -> ConstraintSolverState -> range -> TType -> unit
val ApplyTyparDefaultAtPriority: DisplayEnv -> ConstraintSolverState -> priority: int -> Typar -> unit
/// Generate a witness expression if none is otherwise available, e.g. in legacy non-witness-passing code
val CodegenWitnessExprForTraitConstraint:
TcValF -> TcGlobals -> ImportMap -> range -> TraitConstraintInfo -> Expr list -> OperationResult<Expr option>
/// Determine if a codegen witness for a trait will require witness args to be available, e.g. in generic code
val CodegenWitnessExprForTraitConstraintWillRequireWitnessArgs:
TcValF -> TcGlobals -> ImportMap -> range -> TraitConstraintInfo -> OperationResult<bool>
/// Generate the arguments passed when using a generic construct that accepts traits witnesses
val CodegenWitnessesForTyparInst:
TcValF ->
TcGlobals ->
ImportMap ->
range ->
Typars ->
TType list ->
OperationResult<Choice<TraitConstraintInfo, Expr> list>
/// Generate the lambda argument passed for a use of a generic construct that accepts trait witnesses
val CodegenWitnessArgForTraitConstraint:
TcValF ->
TcGlobals ->
ImportMap ->
range ->
TraitConstraintInfo ->
OperationResult<Choice<TraitConstraintInfo, Expr>>
/// For some code like "let f() = ([] = [])", a free choice is made for a type parameter
/// for an interior type variable. This chooses a solution for a type parameter subject
/// to its constraints and applies that solution by using a constraint.
val ChooseTyparSolutionAndSolve: ConstraintSolverState -> DisplayEnv -> Typar -> unit
val IsApplicableMethApprox: TcGlobals -> ImportMap -> range -> MethInfo -> TType -> bool
val CanonicalizePartialInferenceProblem: ConstraintSolverState -> DisplayEnv -> range -> Typars -> unit