`??` is overloaded on optionality of the second parameter,
prevent ranking the argument candidates for this parameter
if there are candidates that come from failable initializer
overloads because non-optional candidates are always going
to be better and that can skew the selection.
Resolves: rdar://156853018
The problem detection logic currently expects `generic argument #<N>`
location to always be associated with two generic types, but that
is not always the case, this locator element is sometimes used for
i.e. optional object types and pointer `Pointee` type when types
appear in argument positions. This needs to be handled specifically.
Resolves: rdar://82971941
If the argument has an extra `?` or `!`, let's not attempt to force
optional (because it's use is already invalid) and re-introduce
the constraint with unwrapped type instead. This would help to diagnose
the invalid chaining as well as any argument to parameter mismatches.
Resolves: rdar://126080504
If left-hand side of any conversion constraint is `inout` type
and right is a pointer (or optional thereof), delay simplification
until `inout` is at least partially structurally resolved (cannot
be a type variable or dependent member) because eager simplification
won't record all of the possible conversions.
We no longer need to check for a type variable
here, it no longer regresses diagnostics. Also,
while here, let's bump the impact for an
Any/AnyObject missing conformance, as that's
unlikely going to be helpful since they cannot
conform to protocols even if the user wanted them
to.
* Issue-60730: Addressing the situation where we were saying that (any P)? does not conform to P, instead we should offer the Force Optional error messaging
* Issue-60730: Removing assigned of AllowArgumentMismatch fix that would get thrown away if we fall into ForceOptional fix
* Issue-60730: Removing file that was accidentally changed
* Issue-60730: Updating to use simplifyConformsToConstraint - and to clean up code per comments
* Issue-60730: Updating to pass protocoDecl instead of type2 to simplifyConformsToConstraint
* Issue-60730: Updating to pass protocoDecl instead of type2 to simplifyConformsToConstraint
* Issue-60730: Updating to not pass 1 to recordFix, where that is the default value
* Issue-60730: Adding in test to validate that the fix works as expected
* Issue-60730: Cleaning up test based on PR Comments
* Issue-60730: Correcting typo in test
* Issue-60730: Adding additional test cases for layers of optionals and for non-conforming argument
* Issue-60730: Updating test to include fixme(diagnostics) for multilpe layers of force unwrap
* [NFC] Add a getOptionalityDepth method to return the number of optionals a type has
* [NFC] Use getOptionalityDepth to remove existing code
* [AST] Add a new diagnostic note for suggesting optional chaining
* [CSDiagnostics] Tweak MissingOptionalUnwrapFailure to suggest using optional chaining on closure
* [Test] Add a couple of test cases for perform_optional_chain_on_closure
* [CSDiagnostics] Change DeclRefExpr casts to isa checks
* [AST] Update diagnostic phrasing
* [Sema] Use new diagnostic identifier and update comment
* [Test] Add a new test case for function type
* [Test] Update cfuncs_parse.swift tests to check for new diagnostic note
While checking validity of the optional chain, let's not assume
that the contextual type associated with is optional because
enclosing expression could too be invalid.
Resolves: rdar://85166519
When `nil` is passed as an argument to call with multiple overloads
it's possible that this would result in ambiguity where matched
expected argument type doesn't conform to `ExpressibleByNilLiteral`.
To handle situations like this locator for contextual mismatch
has to be adjusted to point to the call where `nil` is used, so
`diagnoseAmbiguityWithFixes` can identify multiple overloads and
produce a correct ambiguity diagnostic.
Resolves: rdar://75514153
Currently `getFunctionArgApplyInfo` expects a locator with `ApplyArgToParam`
element to identify location of the argument. `InOutExpr` could only be used
in argument positions but it doesn't have the same locator format as non-inout
arguments, so `getFunctionArgApplyInfo` needs to do some digging in the AST
to retrieve that information.
Resolves: rdar://75146811
Before considering `nil` to be used without a context, let's
check whether parent expression (semantic significance of
which is not important) has a contextual type associated with it,
otherwise it's possible to misdiagnose cases like:
```swift
func test() -> Int? {
return (nil)
}
```
Generally, casting consistency demands that we be able
to extract anything from an existential that can be put
into that existential. (Which is why the casting spec
requires that casting permit arbitrary injection and
projection of optionals.)
This particular diagnostic prevented optionals from being
projected back out of existentials:
let i: Int?
let a: Any = i // Inject Int? into Any
// Error prevents projecting Int? back out of Any
a as? Int?
This also broke certain uses of Mirror (weak variables get reflected as
optionals stored in Any existentials).
Instead of requiring sub-classes of `ContextualMismatch` to implement
`diagnoseForAmbiguity` let's implement it directly on `ContextualMismatch`
itself and check whether all of the aggregated fixes have same types on
both sides and if so, diagnose as-if it was a single fix.
If return of optional chaining is assigned to an optional type
or discarded there should be no force optional unwrap attempt
on it because type variable associated with context can be
bound to optional of any depth.
Resolves: rdar://problem/57668873
Currently `{inout, array, string}-to-pointer` conversion doesn't
track whether there was a difference in optionality between involved
types which leads to ambiguity when different overload choices
have different optionality requirements.
Let's fix that by increasing a score in cases if pointer type
is itself optional e.g.:
```swift
func foo(_ x: UnsafeMutablePointer<Int>) {}
func foo(_ x: UnsafeMutablePointer<Int>?) {}
foo(&foo) // Should pick the least optional overload choice.
```
Resolves: [SR-8411](https://bugs.swift.org/browse/SR-8411)
Instead of keeping two locators in the fix let's store only the
original locator and simplify it later in process of emitting
a diagnostic. That helps to avoid some duplicate work as well
as makes sure that locators supplied to the diagnostic always
have an anchor.
Resolves: rdar://problem/53344815
If "convertTo" type is an optional let's look through it to see
whether it contains another function type which, if so, would
rule out possibility of missing explicit call.
Resolves: rdar://problem/50438071
Result of force unwrap is always connected to its base
optional type via `OptionalObject` constraint which
preserves l-valueness, so in case where object type got
inferred before optional type (because it got the
type from context e.g. parameter type of a function call),
we need to test type with and without l-value after
delaying bindings for as long as possible.
Resolves: rdar://problem/47967277
Detect and fix situations when (force) unwrap is used on
a non-optional type, this helps to diagnose invalid unwraps
precisely and provide fix-its.
Resolves: [SR-8977](https://bugs.swift.org/browse/SR-8977)
Resolves: rdar://problem/45218255
This builds on initial commit which added `RelabelArguments` fix
to the solver that only supported `missingLabels` at that moment,
but now it supports all three posibilities - missing/extraneous and
incorrect labels.
I think there is something far more narrow we could do here, but I'm
not sure if there is real code that such a hack would benefit from. We
can leave that to consider another day.
Fixes rdar://problem/40819547 (aka https://bugs.swift.org/browse/SR-7884).