(n *InferenceContext, index int)
| 1281 | } |
| 1282 | |
| 1283 | func (c *Checker) getInferredType(n *InferenceContext, index int) *Type { |
| 1284 | inference := n.inferences[index] |
| 1285 | if inference.inferredType == nil { |
| 1286 | if inference.typeParameter == c.errorType { |
| 1287 | return inference.typeParameter |
| 1288 | } |
| 1289 | var inferredType *Type |
| 1290 | var fallbackType *Type |
| 1291 | if n.signature != nil { |
| 1292 | var inferredCovariantType *Type |
| 1293 | if len(inference.candidates) != 0 { |
| 1294 | inferredCovariantType = c.getCovariantInference(inference, n.signature) |
| 1295 | } |
| 1296 | var inferredContravariantType *Type |
| 1297 | if len(inference.contraCandidates) != 0 { |
| 1298 | inferredContravariantType = c.getContravariantInference(inference) |
| 1299 | } |
| 1300 | if inferredCovariantType != nil || inferredContravariantType != nil { |
| 1301 | // If we have both co- and contra-variant inferences, we prefer the co-variant inference if it is not 'never', |
| 1302 | // all co-variant inferences are assignable to it (i.e. it isn't one of a conflicting set of candidates), it is |
| 1303 | // assignable to some contra-variant inference, and no other type parameter is constrained to this type parameter |
| 1304 | // and has inferences that would conflict. Otherwise, we prefer the contra-variant inference. |
| 1305 | // Similarly ignore co-variant `any` inference when both are available as almost everything is assignable to it |
| 1306 | // and it would spoil the overall inference. |
| 1307 | preferCovariantType := inferredCovariantType != nil && (inferredContravariantType == nil || |
| 1308 | inferredCovariantType.flags&(TypeFlagsNever|TypeFlagsAny) == 0 && |
| 1309 | core.Some(inference.contraCandidates, func(t *Type) bool { return c.isTypeAssignableTo(inferredCovariantType, t) }) && |
| 1310 | core.Every(n.inferences, func(other *InferenceInfo) bool { |
| 1311 | return other != inference && c.getConstraintOfTypeParameter(other.typeParameter) != inference.typeParameter || |
| 1312 | core.Every(other.candidates, func(t *Type) bool { return c.isTypeAssignableTo(t, inferredCovariantType) }) |
| 1313 | })) |
| 1314 | if preferCovariantType { |
| 1315 | inferredType = inferredCovariantType |
| 1316 | fallbackType = inferredContravariantType |
| 1317 | } else { |
| 1318 | inferredType = inferredContravariantType |
| 1319 | fallbackType = inferredCovariantType |
| 1320 | } |
| 1321 | } else if n.flags&InferenceFlagsNoDefault != 0 { |
| 1322 | // We use silentNeverType as the wildcard that signals no inferences. |
| 1323 | inferredType = c.silentNeverType |
| 1324 | } else { |
| 1325 | // Infer either the default or the empty object type when no inferences were |
| 1326 | // made. It is important to remember that in this case, inference still |
| 1327 | // succeeds, meaning there is no error for not having inference candidates. An |
| 1328 | // inference error only occurs when there are *conflicting* candidates, i.e. |
| 1329 | // candidates with no common supertype. |
| 1330 | defaultType := c.getDefaultFromTypeParameter(inference.typeParameter) |
| 1331 | if defaultType != nil { |
| 1332 | // Instantiate the default type. Any forward reference to a type |
| 1333 | // parameter should be instantiated to the empty object type. |
| 1334 | inferredType = c.instantiateType(defaultType, mergeTypeMappers(c.newBackreferenceMapper(n, index), n.nonFixingMapper)) |
| 1335 | } |
| 1336 | } |
| 1337 | } else { |
| 1338 | inferredType = c.getTypeFromInference(inference) |
| 1339 | } |
| 1340 | inference.inferredType = inferredType |
no test coverage detected