Commit Graph

2885 Commits

Author SHA1 Message Date
Becca Royal-Gordon
8651af4325 Make @objcImpl work with @_cdecl
No real diagnostics yet, but we’re emitting mostly correct code.
2023-12-13 11:09:49 -08:00
Slava Pestov
52e3031bce Sema: Fancier handling of associated type defaults
Consider this code:

    protocol P {
      associatedtype A
      ...
    }

    protocol Q: P {
      associatedtype A = Int
      ...
    }

    struct S: Q {
      ...
    }

If we check the [S: Q] conformance first, we get the right type witness
assignment, but if we check [S: P] first, conformance checking fails.

Instead of looking at an associated type declaration and any associated
types that it overrides, we now look through all associated types with the
same name among the protocols the adoptee conforms to. This allows us to
find the default assignment 'A = Int' from Q regardless of request
evaluation order.

Fixes rdar://problem/119052782.
2023-12-06 14:59:19 -05:00
Doug Gregor
651d4f87b8 Merge pull request #70236 from DougGregor/required-inits-69965
Diagnose *all* cases where a subclass fails to override a required init
2023-12-05 11:59:14 -08:00
Doug Gregor
257a51e724 Update test for improved require initializer checking 2023-12-05 08:46:03 -08:00
Alex Hoppen
aa60c427ec Merge pull request #70065 from ahoppen/ahoppen/error-unnamed-parameter
[Parse] Error if closure has an unnamed parameter
2023-12-04 18:52:15 -08:00
Doug Gregor
428d718635 Diagnose *all* cases where a subclass fails to override a required init
In cases where a subclass was unable to synthesize any initializers
(for example, because there were missing designated initializers in the
superclass), we were skipping checking of superclass required
initializers. This meant that we would silently accept subclasses that
cannot be initialized directly (that would produce an error), but
could at runtime be initialized via a required initializer... that
didn't account for the subclass.

Fixes the original problem from https://github.com/apple/swift/issues/69965,
but not the most minimal one.
2023-12-04 16:09:28 -08:00
Holly Borla
1f5abe0d53 Merge pull request #70153 from hborla/discoverable-isolated-witness
[Concurrency] Make actor-isolated witness diagnostics more discoverable.
2023-12-02 09:50:17 -08:00
Holly Borla
b5286580ee [Concurrency] Diagnose actor-isolated witnesses to nonisolated protocol
requirements for explicitly isolated types under minimal checking.
2023-12-01 18:23:37 -08:00
Alex Hoppen
4ba4da45b9 [Parse] Error if closure has an unnamed parameter
We accepted unnamed closure parameters if the type was an array literal, dictionary literal, tuple or function (because the `[` or `(` starting the type was sufficient to disambiguate the type from the parameter’s name). This was never an accepted syntax and we should disallow it.
2023-11-28 11:07:57 -08:00
Slava Pestov
1b1963e904 Sema: Remove 'inferred result type requires explicit coercion' diagnostic 2023-11-27 14:05:36 -05:00
Doug Gregor
236418dbf8 Make the "typechecked function body" request more central and resilient
The "typechecked function body" request was defined to type-check a
function body that is known to be present, and not skipped, and would
assert these conditions, requiring its users to check whether a body
was expected. Often, this means that callers would use `getBody()`
instead, which retrieves the underlying value in whatever form it
happens to be, and assume it has been mutated appropriately.

Make the "typechecked function body" request, triggered by
`getTypecheckedBody()`, more resilient and central. A `NULL` result is
now acceptable, signifying that there is no body. Clients will need to
tolerate NULL results.

* When there is no body but should be one, produce an appropriate
error.
* When there shouldn't be a body but is, produce an appropriate error
* Handle skipping of function bodies here, rather than elsewhere.

Over time, we should move clients off of `getBody` and `hasBody`
entirely, and toward `getTypecheckedBody` or some yet-to-be-introduced
forms like `getBodyAsWritten` for the pre-typechecked body.
2023-11-26 09:09:29 -08:00
Doug Gregor
628f5cbc2c [Typed throws] Support overrides that are contravariant in the thrown error 2023-11-16 10:42:55 -08:00
Doug Gregor
3baf6ac31a Revert "[Typed throws] Support overrides that are contravariant in the thrown error" 2023-11-16 10:40:23 -08:00
Doug Gregor
975519b7b4 Merge pull request #69839 from DougGregor/typed-throws-override
[Typed throws] Support overrides that are contravariant in the thrown error
2023-11-16 09:53:32 -08:00
Slava Pestov
c019aef217 Merge pull request #69866 from slavapestov/requirement-lowering-cleanup-and-objc-fix
Requirement lowering cleanup and accept-invalid fix for @objc protocols
2023-11-16 08:22:56 -05:00
Doug Gregor
aeaa523698 Merge pull request #69869 from DougGregor/rethrows-parameter-packs
Fix effects checking for rethrowing functions involving parameter packs
2023-11-15 12:21:08 -08:00
Doug Gregor
46f3ba6968 [Effects] Ensure that we use the original parameter type when determining rethrows
Rethrows checking is based on the interface type of the parameter, with
substitutions applied only when we need to figure out the thrown error
type. Fixes a regression caused by my attempt at dealing with parameter
packs in rethrows functions.
2023-11-15 09:45:24 -08:00
Doug Gregor
ae1c0232c7 Fix effects checking for rethrowing functions involving parameter packs
Effects checking for rethrowing functions was using unsubstituted
interface types for the parameter list. When a rethrows function has
parameter pack arguments, the interface type may have a different
number of parameters from the resulting argument list, causing the
effects checking to bail out early.

This has been wrong since the introduction of parameter packs, but
recent refactoring of effects checking ended up promoting this bug to
a compiler crash. Fix the bug, and make sure we don't crash if the
effects checker hits an issue here.

Fixes rdar://116740385.
2023-11-14 14:28:48 -08:00
Slava Pestov
08f8781798 RequirementMachine: Don't drop errors on the floor with @objc protocols 2023-11-14 15:46:43 -05:00
Slava Pestov
d9ed08870b Merge pull request #69826 from slavapestov/assoc-type-inference-cycle
Sema: Associated type inference skips witnesses that might trigger a request cycle
2023-11-14 15:10:58 -05:00
Slava Pestov
e65290c2bd Sema: Associated type inference skips witnesses that might trigger a request cycle
This implements a structural walk over the TypeRepr to catch
situations where we attempt to infer `A` from `func f(_: A)`,
which references the concrete `A` that will be synthesized
in the conforming type.

Fixes:
- rdar://34956654 / https://github.com/apple/swift/issues/48680
- rdar://38913692 / https://github.com/apple/swift/issues/49066
- rdar://56672411
- https://github.com/apple/swift/issues/50010
- rdar://81587765 / https://github.com/apple/swift/issues/57355
- rdar://117442510
2023-11-14 12:08:59 -05:00
Doug Gregor
33d694468c [Typed throws] Support overrides that are contravariant in the thrown error 2023-11-13 22:30:26 -08:00
Doug Gregor
eb3d7c5200 Drop throws(any Error) and throws(Never) sugar after substitution
When substitution into a function type with `throws(E)` produces either
`throws(any Error)` or `throws(Never)`, adjust the resulting function
type to the equivalent `throws` or non-throwing.
2023-11-13 14:24:13 -08:00
Doug Gregor
0e3b10a2e1 Drop throws(any Error) and throws(Never) sugar after substitution
When substitution into a function type with `throws(E)` produces either
`throws(any Error)` or `throws(Never)`, adjust the resulting function
type to the equivalent `throws` or non-throwing.
2023-11-13 13:43:46 -08:00
Doug Gregor
de16b53ab4 [Typed throws] Add compatibility carve-out within rethrows functions
Allow a rethrows function to call a typed-throws function that has a
specific form that looks like it only rethrows, i.e., it is generic
over its thrown error type and carries the thrown error type from its
closure parameters to itself. This is a compatibility carve-out to
allow existing rethrows functions to move to typed throws without
breaking their clients.

Note that it is possible to write a sneaky function that passes this
check but throws when it shouldn't, so this compatibility carve-out is
phased out with the upcoming feature FullTypedThrows.
2023-11-12 02:47:39 -08:00
Doug Gregor
4bbfba790a [Typed throws] Rethrows functions always throw any Error
Rethrows functions only throw when their closure arguments throw.
However, they are free to translate the errors thrown from the closure
arguments in any way they want, and are therefore untyped.

Ensure that calls to `rethrows` functions are always treated as
throwing `any Error` if their closure arguments throw anything.
2023-11-09 10:02:21 -08:00
Danilo Camarotto
78adeb6070 Incorrect location of 'async' for protocol in actor's fix suggestion (#69246)
* implementing changes and tests

* added unit test using throws

* adding test with distributed actor

* moved distributed-actor tests to another file

* revert import Distributed
2023-11-06 13:44:46 -03:00
Doug Gregor
d3ede19150 Generalize inference of Error type requirements from typed throws 2023-10-27 12:52:07 -07:00
Doug Gregor
ff45fec1d8 [Typed throws] Handle key-path literal used with a function that has typed throws
When providing a key-path literal for a parameter of function type
where that function type has a generic parameter for its thrown error
type, infer `Never` for the generic argument because key paths don't
throw.

Thanks to @xedin for realizing that this would be an issue.
2023-10-27 12:52:05 -07:00
Doug Gregor
b362e3f408 Add a test case ensuring that we diagnose throws(any Codable & Error). 2023-10-26 14:23:36 -07:00
Doug Gregor
e4793cba65 Revert "Thrown types use "convertible to any Error" instead of "conforms to Error""
This reverts commit 6e0aeab149.
2023-10-26 13:12:47 -07:00
Becca Royal-Gordon
6b795a493e Diagnose lightweight generic classes with objcImpl
Extensions to lightweight generic classes are such a huge mess that we’re just removing these from the feature’s scope for now.

Fixes rdar://116066409.
2023-10-25 16:20:16 -07:00
Becca Royal-Gordon
81b5d59363 Diagnose root classes with @objcImpl
@objcImpl, like @objc, cannot be used to implement root classes. Diagnose an attempt to do so.

Fixes rdar://109130979.
2023-10-25 16:20:16 -07:00
Doug Gregor
91c75a2b0d Merge pull request #69379 from DougGregor/typed-throws-convert-to-any-error 2023-10-25 00:15:36 -07:00
Doug Gregor
6e0aeab149 Thrown types use "convertible to any Error" instead of "conforms to Error"
Allow one to throw a type that is convertible to `any Error` even if it
doesn't conform to the `Error` protocol, e.g., an existential type like
`any Codable & Error`.
2023-10-24 13:23:47 -07:00
Becca Royal-Gordon
1b7adbe6fd Diagnose invalid Swift-only @objcImpl inits
Required and designated inits have to be overridable—the former so that uses of the initializer on `any T.Type` will call the subclass initializer, the latter so that inherited convenience inits will call the subclass initializer. However, Swift-only members use vtables to dispatch to subclasses, and @objcImpl classes don’t have vtables, so their Swift-only inits cannot be made overridable. Upshot: Swift-only inits on @objcImpl classes must be `convenience`, not `required` or designated.

Enforce this rule in the ObjCImplementationChecker.

Fixes rdar://109121293.
2023-10-24 12:33:56 -07:00
Becca Royal-Gordon
9bb71ff91f Add @objcImpl vtable fallback diagnostic
Nothing in an `@_objcImplementation` block—including Swift-only declarations—should ever require a vtable entry. Diagnose any such members.

The diagnostic emitted here is intended to be a fallback that will be used when a vtable entry is needed but we don’t know the specific reason. A future commit will add a more specific diagnostic for Swift-only non-convenience inits.
2023-10-24 12:32:42 -07:00
Becca Royal-Gordon
231de1a666 Add ObjCReason for @objcImpl
Besides improving diagnostics, this also allows us to track whether there were any @objc failures using the invalid bit on the @_objcImplementation attribute.
2023-10-24 10:21:37 -07:00
Becca Royal-Gordon
76f25e53d9 Allow @nonobjc inits in objcImpl extensions
Initializers can’t be made final, but they *can* be made @nonobjc. (This isn’t always enough to ensure they’re actually valid, but I’ll get to that in a follow-up commit.)
2023-10-24 10:21:37 -07:00
Becca Royal-Gordon
2ff70e00ce Replace existing access control in objcImpl fixit
The fix-it suggesting that an unmatched `@objc`-able member can be turned `private` always suggested adding a new modifier, even if one already existed. Make it suggest replacing the existing one instead.
2023-10-24 10:21:37 -07:00
Becca Royal-Gordon
a5967fa638 Merge pull request #69290 from beccadax/sometimes-an-override-is-just-an-override 2023-10-23 16:14:50 -07:00
Harlan Haskins
4ac34a40ea @retroactive conformance syntax and checking (#36068) 2023-10-20 14:27:03 -07:00
Becca Royal-Gordon
b69e416f7c Support overrides of unavailable inits in objcImpl
Suppose a superclass declares an initializer unavailable and then a subclass wants to redeclare and use it. Formally, the subclass declaration overrides the superclass one; however, Swift will not actually require the subclass to use the `override` keyword. As currently implemented, this means that the requirement will be skipped as an override, but the candidate will be included as a member implementation. Result: a “candidate does not match any requirement” diagnostic.

Fix this by skipping requirements that are overrides *only* if the declaration they override is not unavailable.

Fixes rdar://109541045.
2023-10-19 15:42:20 -07:00
Doug Gregor
2d7eafd155 Infer error conformance for type parameters used in typed throws
The type that occurs as the thrown error type must conform to the
`Error` protocol. Infer this conformance when the type is a type
parameter in the signature of a function.
2023-10-13 16:09:44 -07:00
Doug Gregor
7d7c726efe [Typed throws] Teach associated type inference to infer from thrown errors
When comparing a requirement that uses typed throws and uses an
associated type for the thrown error type against a potential witness,
infer the associated type from the thrown error of the
witness---whether explicitly specified, untyped throws (`any Error`),
or non-throwing (`Never`).
2023-10-09 21:21:21 -07:00
Doug Gregor
fc78ad3263 [Typed throws] Teach witness matching to check typed throws. 2023-10-09 21:21:21 -07:00
Doug Gregor
ccf8619453 [Typed throws] Refactor thrown error subtyping check for reuse.
Lift the subtyping check for thrown error types out of the constraint
solver, so we can re-use it elsewhere.

There is a minor diagnostic change, from one that is actively
misleading (it shows a legitimate conversion that's wrong) to one that
is correct, which comes from us not treating "dropping throws" as a
legitimate way to handle equality of function types.
2023-10-09 21:21:21 -07:00
Slava Pestov
f321dd3321 Merge pull request #66247 from karwa/nested-protocols
Allow protocols to be nested in non-generic contexts
2023-10-08 12:00:46 -04:00
Karl Wagner
853d3c8650 [SE-0404] Make test for protocol nested in actor conditionally-available 2023-10-06 21:04:03 +02:00
Karl Wagner
69c57045dc [SE-0404] Rebase fixes and better handling of generic superclasses with inferred params 2023-10-06 21:04:03 +02:00