Commit Graph

27 Commits

Author SHA1 Message Date
Pavel Yaskevich
4132aa04f9 [Tests] NFC: Update all of the test-cases improved by changes to generic argument mismatch handling 2025-06-03 00:49:06 -07:00
Anthony Latsis
35f1370f6e TypeCheckType: Rework IUO diagnostics using behavior limitation
Also stop suggesting a '?' fix-it for casts, where it is not likely to
be helpful because the common intention is either to force the optional
or declare an IUO.
2025-01-21 21:41:00 +00:00
Hamish Knight
3c662c6276 [CS] Support IUOs for compound function references
This was previously an artificial limitation of the
FunctionRefKind representation, there's no reason
we shouldn't support IUOs for functions referenced
using a compound name.
2024-12-04 11:11:32 +00:00
Anthony Latsis
da8d035b0b Gardening: Migrate test suite to GH issues: Constraints (2/5) 2022-08-19 06:43:11 +03:00
Hamish Knight
287fa8e8de [CS] Refactor IUO handling
The current IUO design always forms a disjunction
at the overload reference, for both:

- An IUO property `T!`, forming `$T := T? or T`
- An IUO-returning function `() -> T!`, forming `$T := () -> T? or () -> T`

This is simple in concept, however it's suboptimal
for the latter case of IUO-returning functions for
a couple of reasons:

- The arguments cannot be matched independently of
  the disjunction
- There's some awkwardness when it comes e.g wrapping
  the overload type in an outer layer of optionality
  such as `(() -> T!)?`:
  - The binding logic has to "adjust" the correct
    reference type after forming the disjunction.
  - The applicable fn solving logic needs a special
    case to handle such functions.
- The CSApply logic needs various hacks such as
  ImplicitlyUnwrappedFunctionConversionExpr to
  make up for the fact that there's no function
  conversion for IUO functions, we can only force
  unwrap the function result.
  - This lead to various crashes in cases where
    we we'd fail to detect the expr and peephole
    the force unwrap.
  - This also lead to crashes where the solver
    would have a different view of the world than
    CSApply, as the former would consider an
    unwrapped IUO function to be of type `() -> T`
    whereas CSApply would correctly see the overload
    as being of type `() -> T?`.

To remedy these issues, IUO-returning functions no
longer have their disjunction formed at the overload
reference. Instead, a disjunction is formed when
matching result types for the applicable fn
constraint, using the callee locator to determine
if there's an IUO return to consider. CSApply then
consults the callee locator when finishing up
applies, and inserts the force unwraps as needed,
eliminating ImplicitlyUnwrappedFunctionConversionExpr.

This means that now all IUO disjunctions are of the
form `$T := T? or T`. This will hopefully allow a
further refactoring away from using disjunctions
and instead using type variable binding logic to
apply the correct unwrapping.

Fixes SR-10492.
2021-10-12 14:14:33 +01:00
Hamish Knight
5c01efa478 [test] Add a couple of IUO test cases 2021-09-28 12:13:33 +01:00
Hamish Knight
d74f515a07 [CS] Handle placeholder in buildDisjunctionForOptionalVsUnderlying
If we encounter a placeholder type here, propagate
it to the type variable, as we don't know whether
it should be optional or non-optional, and we
would have already recorded a fix for it.

SR-15219
rdar://83352038
2021-09-24 15:51:10 +01:00
Hamish Knight
f10992f2c2 [test] Add curried IUO test case 2021-09-17 10:52:44 +01:00
Doug Gregor
3abea6be65 [Constraint system] Use the PatternBindingDecl context when possible.
Make sure that we're resolving types and patterns using the
PatternBindingDecl context, both for the type resolver context and the
contextual pattern used for pattern resolution.

Fixes a regression with implicitly-unwrapped options reported as
SR-11998 / rdar://problem/58455441.
2020-03-18 21:09:06 -07:00
Pavel Yaskevich
b79341538f [TypeChecker] NFC: Adjust test-cases improved by optional unwrap fix 2019-11-19 15:15:30 -08:00
Pavel Yaskevich
5210e9b7c2 Revert "[AST] Paren'd reference to an IUO function crashes the compiler in SILGen" 2019-09-02 11:02:01 -07:00
Suyash Srijan
b32386bcac [CS] If locator points to a function call, then compare the fn and semanticFn, otherwise fall back to paren check
This is because otherwise we would have false positives, like 'Foo(Bar())' where Foo's init accepts a non-optional Bar and Bar's init returns an IUO
2019-08-29 01:12:32 +01:00
Suyash Srijan
76c29af4fd [Test] Adds a test case 2019-08-27 21:25:55 +01:00
Pavel Yaskevich
c54913f786 [TypeChecker] NFC: Fix some diagnostics improved by porting contextual mismatches to new framework 2019-08-13 11:55:08 -07:00
Doug Gregor
945c09b1cc [Type checker] Improve diagnostics when an optional value is not unwrapped.
When we determine that an optional value needs to be unwrapped to make
an expression type check, use notes to provide several different
Fix-It options (with descriptions) rather than always pushing users
toward '!'. Specifically, the errors + Fix-Its now looks like this:

    error: value of optional type 'X?' must be unwrapped to a value of
        type 'X'
      f(x)
        ^
    note: coalesce using '??' to provide a default when the optional
        value contains 'nil'
      f(x)
        ^
          ?? <#default value#>
    note: force-unwrap using '!' to abort execution if the optional
        value contains 'nil'
      f(x)
         ^
         !

Fixes rdar://problem/42081852.
2018-07-13 11:02:04 -07:00
Mark Lacey
289b97134b Ensure we account for force unwrapping in scoring solutions.
This disappeared in the rework of IUOs but is needed when we have
multiple potential solutions involving different sets of overloads or
type bindings.

Fixes rdar://problem/37475971.
2018-04-11 11:45:31 -07:00
Mark Lacey
4bd648be05 Fix the handling of IUOs in 'as!' casts.
We just needed to select the final type based on which branch of the
disjunction successfully type-checked the expression.

Fixes [SR-7208](https://bugs.swift.org/browse/SR-7208)
  and rdar://problem/37159360.
2018-03-30 11:57:38 -07:00
Mark Lacey
b9cd30331d [ConstraintSystem] Do not attempt to force references of unapplied functions.
We were inserting function conversion expressions that were then
turned into forces of values in cases where we merely referenced
functions, but did not actually call them.

Fixes rdar://problem/37241550.
2018-02-20 17:49:10 -08:00
Mark Lacey
2008674495 Make ImplicitlyUnwrappedOptional<T> an unavailable typealias.
Also remove the decl from the known decls and remove a
bunch of code referencing that decl as well as a bunch of other
random things including deserialization support.

This includes removing some specialized diagnostics code that
matched the identifier ImplicitlyUnwrappedOptional, and tweaking
diagnostics for various modes and various issues.

Fixes most of rdar://problem/37121121, among other things.
2018-02-02 08:35:53 -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
Mark Lacey
f19c6a1417 Merge pull request #14242 from rudkx/rdar36913150
Allow inout arguments that differ in optionality than the expected pa…
2018-01-29 16:56:17 -08:00
Mark Lacey
8685ee01a1 Allow inout arguments that differ in optionality than the expected parameter.
Allow passing Optional<T> as inout where
ImplicitlyUnwrappedOptional<T> is expected, and vice-versa.

Swift 4.1 added a warning that overloading inouts by kind of optional
was deprecated and would be removed, but we didn't actually allow
people to remove an overload and pass arguments of the other kind of
optional to the remaining function.

Fixes rdar://problem/36913150
2018-01-29 15:07:44 -08:00
Mark Lacey
305a7eb87d IUO: Set the IUO attribute on ParamDecls for accessors arguments.
Add tests to show that for setter, getter, willSet, didSet,
and subscript setter we successfully implicitly unwrap this argument.
2018-01-27 17:22:33 -08:00
Mark Lacey
af61628af4 IUO: Add test for subscript getter returning IUO. 2018-01-04 14:50:09 -08:00
Mark Lacey
32989cc341 IUO: Add some additional tests. 2017-12-15 17:10:28 -08:00
Mark Lacey
b0f790982a IUO: Add tests for failable init and static functions with IUO results. 2017-12-12 16:45:43 -08:00
Mark Lacey
d07af8ad80 Add the test case I am using to bring up the new IUO implementation.
This is a nice contained test case that has the goal of testing all
the new functionality. Having this committed before the code changes
will make it clear whether any behavior changes between the old and
new implementation (I expect some small diagnostic changes like
uttering T? instead of T! for some types).
2017-12-08 11:16:39 -08:00