Commit Graph

28 Commits

Author SHA1 Message Date
Becca Royal-Gordon
fa0e6285fc Merge pull request #67925 from beccadax/objcimpl-warning-main
Temporarily soften @objcImpl errors into warnings
2023-08-16 01:34:39 -07:00
Becca Royal-Gordon
ad960a1e2d Temporarily soften @objcImpl errors into warnings
Temporarily cherry-pick Swift 5.9’s behavior of turning @objcImplementation errors into warnings to 5.10 until we fix the last few bugs in these diagnostics.
2023-08-14 16:49:51 -07:00
Becca Royal-Gordon
37e42d178f Handle inherited required inits in @objcImpl
Because `required init`s do not have the `override` keyword, they are always treated as member implementations (if they pass other checks). However, these methods sometimes actually are overrides, and when they are, they should not be treated as member implementations. This results in required inits being treated as candidates when there won’t be a requirement for them to match.

Hack around this by separately checking for this situation and skipping the affected members.

Fixes rdar://112910098.
2023-08-14 11:06:35 -07:00
Becca Royal-Gordon
1d6b041e58 Reword @objcImpl diagnostics to avoid “an”
…and also adopt new DiagnosticEngine features.
2023-07-19 13:08:12 -07:00
Becca Royal-Gordon
a8fbad3551 Match @objcImpl members’ foreign error conventions
ClangImporter can import some methods as throwing that `@objc` cannot generate. For instance, an imported Objective-C method with an error out parameter in an unconventional position can still be imported as throwing no matter its selector, but `@objc` can only generate an error out parameter in an unconventional position if the matching selector part consists of the word `error` or (for the first part) ends with `Error`. Detect and diagnose these situations.

Note that the tests do not cover all of the new diagnostics because some of these conditions (like the `Void` parameter) cause selector mismatches and others (like the owned error parameter) are representable in the compiler but cannot currently be imported. I have chosen to add these diagnostics anyway in case there is a corner case that I haven’t discovered.

Fixes rdar://110100071.
2023-06-27 16:08:49 -07:00
Becca Royal-Gordon
49831a9e93 Fix @objcImpl crash with async/sync overloads
The @objcImpl checker would accidentally dereference a null pointer when it tried to check if an async requirement could be satisfied by a non-async method. Fix that mistake.

Fixes rdar://111064481.
2023-06-22 13:15:56 -07:00
Becca Royal-Gordon
1532fb188c Merge pull request #66609 from beccadax/just-empty-protocol
Diagnose conformances on @objcImpl extensions
2023-06-14 14:12:13 -07:00
Becca Royal-Gordon
6758fdb518 Diagnose conformances on @objcImpl extensions
@objcImpl extensions aren’t allowed to declare new conformances; instead, they should either be declared in the header or in an ordinary extensions. (If they were permitted, they’d be ignored.)

Fixes rdar://110669366.
2023-06-13 17:21:44 -07:00
Becca Royal-Gordon
b14c00d521 Support required inits in @objcImpl
• Allow `required init`s in @objcImpl extensions of a class’s main body
• Validate that the presence or absence of a `required` modifier matches the imported header declaration.

Fixes rdar://110016760.
2023-06-01 13:21:02 -07:00
Becca Royal-Gordon
9f0928e239 Match @objcImpl member types
Check the types of @objcImpl candidates against their requirements and diagnose *most* mismatches.

Unlike typical type matching rules, @objcImpl allows an implementation’s parameter *and* result types to be an IUO when the requirement isn’t. This runs against the normal covariance rules in the case of the result type. It’s meant to allow an implementation to handle nils passed to it and return nils even if the declaration formally claims nils are not permitted; this is occasionally necessary to reimplement Objective-C APIs without breaking ABI compatibility.

Fixes rdar://102063730.
2023-05-23 11:34:51 -07:00
Becca Royal-Gordon
2333ff5a20 Diagnose @objcImpl kind and settability errors
Sema now diagnoses @objcImpl implementations with:

• The wrong settability (i.e. a `let` used for a `readwrite` property)
• The wrong kind (i.e. a method used for a property)
2023-05-19 11:48:25 -07:00
Becca Royal-Gordon
00e55cb92d Don’t require @optional ObjC methods in @objcImpl
rdar://108461441
2023-05-03 19:58:45 -07:00
Doug Gregor
79e26970ba Handle async vs. completion-handler mismatches in @_objcImplementation.
Fixes rdar://108160837.
2023-04-18 08:52:20 -07:00
Becca Royal-Gordon
a2f1d357ca Refactor and expand @objcImpl checking
Create a checker for @_objcImplementation member implementations that considers all of a class’s interface and implementation decls at once. This allows us to handle several things better:

• Unimplemented requirements are now diagnosed

• Header members that can match several implementations, or implementations that could match several header members, are now diagnosed

• Tailored diagnostic when the implementation's Swift name matches the header's selector instead of its Swift name

• Recommends inserting `@objc(<selector>)` when a Swift name matches but the implicit ObjC name doesn't

• An `@objc(<selector>)` on one implementation can eliminate its requirement from being considered for other implementations, resolving ambiguities

This does unfortunately regress the diagnostics when a requirement is implemented in the wrong extension. Some sort of whole-module checking would be needed to address this problem.
2023-03-25 14:53:29 -07:00
Becca Royal-Gordon
3bcefc0731 Diagnose objcImpl members with wrong name 2023-03-25 14:52:40 -07:00
Becca Royal-Gordon
cf86fd4a73 Correct and improve objcImpl member matching
• `@objc` is now inferred on non-`final` members of @objcImplementation extensions
• Diagnostics now suggest adding `private` to ObjC helper members, not `@objc`, in line with currently proposed behavior
• Better diagnostic for members implemented in the wrong extension

Part of rdar://103150189.
2023-03-25 14:52:40 -07:00
Doug Gregor
ef7f707fcc Revert "Improve @objcImplementation member checking" 2023-03-10 12:00:33 -08:00
Becca Royal-Gordon
7f688ef2c1 Refactor and expand @objcImpl checking
Create a checker for @_objcImplementation member implementations that considers all of a class’s interface and implementation decls at once. This allows us to handle several things better:

• Unimplemented requirements are now diagnosed

• Header members that can match several implementations, or implementations that could match several header members, are now diagnosed

• Tailored diagnostic when the implementation's Swift name matches the header's selector instead of its Swift name

• Recommends inserting `@objc(<selector>)` when a Swift name matches but the implicit ObjC name doesn't

• An `@objc(<selector>)` on one implementation can eliminate its requirement from being considered for other implementations, resolving ambiguities

This does unfortunately regress the diagnostics when a requirement is implemented in the wrong extension. Some sort of whole-module checking would be needed to address this problem.
2023-03-03 17:40:48 -08:00
Becca Royal-Gordon
e7c91b2236 Diagnose objcImpl members with wrong name 2023-03-01 09:00:43 -08:00
Becca Royal-Gordon
3212d2e60e Correct and improve objcImpl member matching
• `@objc` is now inferred on non-`final` members of @objcImplementation extensions
• Diagnostics now suggest adding `private` to ObjC helper members, not `@objc`, in line with currently proposed behavior
• Better diagnostic for members implemented in the wrong extension

Part of rdar://103150189.
2023-03-01 09:00:43 -08:00
Becca Royal-Gordon
3e4ea43adf Don’t diagnose @_objcImpl conflicts with inherited inits
Previously, Swift would reject an `override public init(…)` in an `@_objcImplementation` because ClangImporter would have already synthesized inherited initializers that conflicted with the overrides. Ignore these spurious conflicts, and also move a check out of IsObjCRequest and into the conflict-handling code.

Additional work towards rdar://70730077.
2022-10-27 17:00:43 -07:00
Becca Royal-Gordon
75965d0588 Exclude overrides from being member impls
Without this change, an `@_objcImplementation` cannot override parent class methods, because the special access control behavior breaks the access control checks for overrides.
2022-10-27 17:00:43 -07:00
Becca Royal-Gordon
881f1f90a8 Re-allow @objc final
Treating this as forbidden was incorrect; an `@objc final` method is simply one that isn’t a member implementation but does have an ObjC entry point.

Formalize and centralize the definition of what a member implementation is, tweak it so that it’s basically “non-final and internal or greater”, and permit `@objc final`. Also remove the inference of `final` on `let`s in @_objcImpl extensions.
2022-10-27 16:51:13 -07:00
Becca Royal-Gordon
ecf0ee65b8 Cache @_objcImpl pointers and diagnose dupes 2022-10-18 17:21:56 -07:00
Becca Royal-Gordon
3a2f12a099 Allow stored properties in @_objcImpls
Stored properties are only allowed in the extension implementing the class's main interface, not its categories. This also means banning `@objc final`, which is unenforceable anyway when ObjC subclasses are allowed, and therefore allowing `@objc let` and `@objc static` properties to be overridden if they're declared in objcImplementations.
2022-10-18 17:21:56 -07:00
Becca Royal-Gordon
0678a38ce4 Require objcImplementation members to avoid vtable
They either must be @objc dynamic (the dynamic is implicit) or final.
2022-10-18 17:21:56 -07:00
Becca Royal-Gordon
e544c21f99 Make all internal-or-more @_objcImpl members impls 2022-10-18 17:21:56 -07:00
Becca Royal-Gordon
f2a0ab79c7 Add basic Sema support for @_objcImplementation
Does not validate members yet; nor does it emit different metadata.
2022-10-18 17:21:56 -07:00