Commit Graph

95 Commits

Author SHA1 Message Date
Pavel Yaskevich
b1d9ae01ed [CSSimplify] Delay inout type to pointer conversion until inout is sufficiently resolved
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.
2024-09-12 13:46:02 -07:00
Pavel Yaskevich
7594e43b2c Revert "[CSBindings] Delay inference through OptionalObject if "object" is l-value capable" 2024-04-30 13:43:37 -07:00
Pavel Yaskevich
1038493ad4 [Tests] NFC: Add a test-case for invalid optional promotion 2024-04-17 10:31:21 -07:00
Pavel Yaskevich
ab230b94d9 Revert "[Tests] NFC: Add a test-case for invalid optional promotion"
This reverts commit 6b287bf31b.
2023-12-04 11:36:20 -08:00
Pavel Yaskevich
6b287bf31b [Tests] NFC: Add a test-case for invalid optional promotion 2023-10-20 15:21:06 -07:00
Pavel Yaskevich
58ffca8f74 [ConstraintSystem] Don't produce partially matching note if none of the overloads matched 2023-05-09 14:11:38 -07:00
Hamish Knight
b305324179 [CS] Remove obsolete check for a type variable
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.
2023-05-04 14:53:57 +01:00
Pavel Yaskevich
4f2a1752c2 [ConstraintSystem] Produce ambiguity notes based on the fix aggregate
It shouldn't matter how many fixes does the solution have overall,
what matter is how many fixes are there for the location in question.
2023-03-06 10:17:51 -08:00
Pavel Yaskevich
bb1da5e599 [CSFix] Diagnose extraneous force unwraps in ambiguous context 2023-02-24 23:48:03 -08:00
Dylan Perry
a29686f349 Issue-60730: Addressing the situation where we were saying that (any … (#61191)
* 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
2022-10-10 08:56:19 -03:00
Anthony Latsis
da8d035b0b Gardening: Migrate test suite to GH issues: Constraints (2/5) 2022-08-19 06:43:11 +03:00
Suyash Srijan
b2593ee5e3 [CSDiagnostics] Optional closure diagnostic improvement (#60321)
* [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
2022-08-04 21:40:17 +01:00
Luciano Almeida
c6f00fae98 [Sema] Allow TreatArrayLiteralAsDictionary fix to handle literals with more than one element 2022-06-05 22:36:35 -03:00
Luciano Almeida
6ddd31fabd [test] Add regression test for nil literal in pattern binding 2022-05-08 13:22:38 -03:00
Pavel Yaskevich
a0d132582e [Diagnostics] Improve diagnostic message for extraneous &
Resolves: https://github.com/apple/swift/issues/58389
2022-04-25 15:08:04 -07:00
Holly Borla
12459cff80 [Diagnostics] Print 'any' in diagnostic arguments. 2022-03-05 14:26:45 -08:00
Pavel Yaskevich
c4b3f86a70 [Diagnostics] Don't assume that contextual type for optional chain is optional
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
2021-11-09 09:42:45 -08:00
Pavel Yaskevich
29cb7ddbcb [Diagnostics] Handle ambiguities related to use of nil literal
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
2021-05-05 12:56:25 -07:00
Pavel Yaskevich
1f33624f40 [TypeChecker] NFC: Fix a small typo in a test-case comment 2021-03-19 11:13:40 -07:00
Pavel Yaskevich
5e6820466c Merge pull request #36459 from xedin/rdar-75146811
[ConstraintSystem] Teach `getFunctionArgApplyInfo` about `inout` expr…
2021-03-17 16:18:23 -07:00
Pavel Yaskevich
4ca67f6d27 [ConstraintSystem] Teach getFunctionArgApplyInfo about inout expressions
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
2021-03-16 20:12:34 -07:00
Pavel Yaskevich
b6946980fe [Diagnostics] Don't record unwrap fix if ? was used incorrectly
In situations like `""?` or `42?`, let's not record an unwrap fix
since it was incorrect to use `?` in the first place.
2021-03-15 17:16:11 -07:00
Pavel Yaskevich
90e6fd4792 [CSGen] Check whether parent has a contextual type before diagnosing nil use
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)
}
```
2020-09-23 11:23:54 -07:00
tbkka
82c43a8ac7 [Dynamic Casting] Allow casts to "more optional" types (#33684)
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).
2020-08-31 17:52:22 -07:00
Luciano Almeida
c09af513a0 [tests] Adjusting regression tests argument mismatch diagnostics on suit 2020-07-23 20:05:27 -03:00
Pavel Yaskevich
b0070f5739 [Diagnostics] Check whether all contextual mismatches has the same type before diagnosing ambiguity
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.
2020-06-12 11:47:04 -07:00
Pavel Yaskevich
465b0e193a [ConstraintSystem] Diagnose more cases of invalid nil use during constraint generation
Move check for `nil` destination of conditional casts to `visitNilLiteral` and
add support for `nil?` which wasn't previously detected.
2020-03-24 14:57:27 -07:00
Hassan El Desouky
cdf5c5cbe3 Add more tests 2020-03-20 04:37:00 +02:00
Hassan El Desouky
1844578693 Add more tests 2020-03-19 22:35:14 +02:00
Hassan El Desouky
d7dece914f Handle nil with any number of parentheses 2020-03-19 21:43:34 +02:00
HassanElDesouky
8d5a33ee89 Move to visitNilLiteralExpr 2020-03-14 07:39:44 +02:00
HassanElDesouky
ceb3f3842f [SR-12309] Add more tests 2020-03-14 03:06:10 +02:00
Holly Borla
6898fbb0ef [ConstraintSystem] Don't assume the lhs type is for a
generic parameter in simplifyConformsToConstraint.
2020-03-05 14:39:30 -08:00
Suyash Srijan
543d649278 [Diagnostics] Warn when the result of a Void-returning function is ignored (by assigning into '_') (#29576) 2020-02-04 20:19:37 +00:00
Pavel Yaskevich
08baf67677 [ConstraintSystem] Fix a case of too eager force optional unwrap with optional contextual type
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
2019-12-06 15:03:19 -08:00
Pavel Yaskevich
1459974487 [TypeChecker] NFC: Add a test-case for SR-11104 2019-11-19 15:18:07 -08:00
Pavel Yaskevich
3feb25c958 [TypeChecker] Disambiguite cases of implicit pointer conversions with optionals
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)
2019-11-07 17:00:59 -08:00
Pavel Yaskevich
a1643d94f7 [Diagnostics] NFC: Update all of the improved test-cases 2019-09-13 22:35:52 -07:00
Hamish Knight
8e0ec8df66 [CS] Allow getCalleeLocator to look through optional chaining 2019-09-05 22:25:25 +01:00
Pavel Yaskevich
795a84ae6e [CSFix] Delay missing unwrap locator simplification until diagnostic
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
2019-07-28 21:09:44 -07:00
Pavel Yaskevich
0275689cd3 [ConstraintSystem] Tighten "missing call" fix conditions by checking whether other side is an optional
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
2019-05-03 13:02:28 -07:00
Pavel Yaskevich
f950360cdd [TypeChecker] NFC: Add a test-case for rdar://problem/47776586 2019-03-08 01:20:06 -08:00
Pavel Yaskevich
8f884c45c4 [CSBindings] Preserve l-valueness of optional object type
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
2019-02-11 21:28:16 -08:00
Pavel Yaskevich
202234f325 [CSDiagnostics] Diagnose invalid optional unwrap via fixes
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
2018-12-05 08:10:07 -08:00
Pavel Yaskevich
84cd99a3eb [Diagnostics] Enable extraneous/incorrect label diagnostics via solver fixes
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.
2018-07-25 14:34:26 -07:00
Pavel Yaskevich
9371aaa49b [TypeChecker/SE-0213] NFC: Add couple of additional test-cases 2018-07-17 12:08:59 -07:00
Mark Lacey
c40fd3966c [ConstraintSystem] Remove an unsound optimization hack for generic functions.
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).
2018-06-06 23:59:08 -07:00
Pavel Yaskevich
5208049ff8 [CSBindings] Form bindings correctly when they come from 'OptionalObject' constraint
Fixes a bug in `getPotentialBindings` when the source of the bindings
is 'OptionalObject' constraint and type variable is on the left-hand
side of that constraint, that makes such type variable always have an
optional type since right-hand side of 'OptionalObject' is its 'object'
type.

Resolves: rdar://problem/37508855
2018-02-13 14:25:00 -08:00
Doug Gregor
dee02c42a4 Update test cases for constraint solver performance hack 2018-02-08 22:57:05 -08:00
Mark Lacey
f08823757a IUO: Generate Optional<T> rather than ImplicitlyUnwrappedOptional<T>.
Stop creating ImplicitlyUnwrappedOptional<T> so that we can remove it
from the type system.

Enable the code that generates disjunctions for Optional<T> and
rewrites expressions based on the original declared type being 'T!'.

Most of the changes supporting this were previously merged to master,
but some things were difficult to merge to master without actually
removing IUOs from the type system:
- Dynamic member lookup and dynamic subscripting
- Changes to ensure the bridging peephole still works

Past commits have attempted to retain as much fidelity with how we
were printing things as possible. There are some cases where we still
are not printing things the same way:
- In diagnostics we will print '?' rather than '!'
- Some SourceKit and Code Completion output where we print a Type
  rather than Decl.

Things like module printing via swift-ide-test attempt to print '!'
any place that we now have Optional types that were declared as IUOs.

There are some diagnostics regressions related to the fact that we can
no longer "look through" IUOs. For the same reason some output and
functionality changes in Code Completion. I have an idea of how we can
restore these, and have opened a bug to investigate doing so.

There are some small source compatibility breaks that result from
this change:
- Results of dynamic lookup that are themselves declared IUO can in
  rare circumstances be inferred differently. This shows up in
  test/ClangImporter/objc_parse.swift, where we have
    var optStr = obj.nsstringProperty
  Rather than inferring optStr to be 'String!?', we now infer this to
  be 'String??', which is in line with the expectations of SE-0054.
  The fact that we were only inferring the outermost IUO to be an
  Optional in Swift 4 was a result of the incomplete implementation of
  SE-0054 as opposed to a particular design. This should rarely cause
  problems since in the common-case of actually using the property rather
  than just assigning it to a value with inferred type, we will behave
  the same way.
- Overloading functions with inout parameters strictly by a difference
  in optionality (i.e. Optional<T> vs. ImplicitlyUnwrappedOptional<T>)
  will result in an error rather than the diagnostic that was added
  in Swift 4.1.
- Any place where '!' was being used where it wasn't supposed to be
  allowed by SE-0054 will now treat the '!' as if it were '?'.
  Swift 4.1 generates warnings for these saying that putting '!'
  in that location is deprecated. These locations include for example
  typealiases or any place where '!' is nested in another type like
  `Int!?` or `[Int!]`.

This commit effectively means ImplicitlyUnwrappedOptional<T> is no
longer part of the type system, although I haven't actually removed
all of the code dealing with it yet.

ImplicitlyUnwrappedOptional<T> is is dead, long live implicitly
unwrapped Optional<T>!

Resolves rdar://problem/33272674.
2018-01-31 12:15:58 -08:00