Commit Graph

2550 Commits

Author SHA1 Message Date
Henrik G. Olsson
ec8ea94fb9 Infer @PointerBounds macro from clang __counted_by parameters (#77387)
This results in an automatic wrapper function with safe pointer types
when the imported function has bounds attributes. This exercises similar
pathways as the recently added functionality for specifying macros from
swift_attr. The new functionality is guarded by the experimental
language feature SafeInteropWrappers.

rdar://97942270
2024-12-11 13:36:56 -08:00
Doug Gregor
41073a881c Merge pull request #77837 from lhoward/lhoward/retain-return-self
[cxx-interop] allow shared ref retain function to return self
2024-12-05 11:25:31 -08:00
Egor Zhdan
70ce8c67f5 Merge pull request #77932 from swiftlang/egorzhdan/spurious-lifetime-diags
[cxx-interop] Do not emit spurious lifetime diagnostics for C++ types
2024-12-05 17:17:12 +00:00
Doug Gregor
0c91a3409c Formalize the notion of "can clone" so we don't try to clone an attribute that won't work 2024-12-04 15:25:13 -08:00
Doug Gregor
ac448b865b [Clang importer] Don't cache swift_attr source files that have CustomAttrs with arguments
Since we can't do a proper "deep" clone of expression nodes, cloning
such a CustomAttr is necessarily shallow. In such cases, don't cache
the swift_attr source files at all, so we get fresh attribute nodes
for each such usage.
2024-12-04 15:24:00 -08:00
Egor Zhdan
8202dfdecd [cxx-interop] Do not emit spurious lifetime diagnostics for C++ types
In rare scenarios, Swift was emitting diagnostics that looked like this:
```
warning: 'import_owned' swift attribute ignored on type 'basic_string': type is not copyable or destructible
```

This change makes sure the compiler does not emit these (incorrect) warnings. See the inline comment for more details.
2024-12-04 16:10:08 +00:00
fahadnayyar
4bab2a8419 Merge pull request #77397 from fahadnayyar/frt-return-unannotated-warning-improve
[cxx-interop] Improve the warnings for unannotated c++ APIs returning SWIFT_SHARED_REFERENCE types
2024-12-04 00:26:03 -08:00
fahadnayyar
2e501754dd [cxx-interop] Improve the warnings for unannotated c++ APIs returning SWIFT_SHARED_REFERENCE types 2024-12-03 18:26:13 -08:00
Luke Howard
da23bcf1c6 [cxx-interop] allow shared ref retain function to return self
Many existing C APIs for retaining references, including Apple's own, return
the reference. Support this pattern, along with the existing void return
signature, with when importing reference types from C++.
2024-12-03 11:45:29 +11:00
Luke Howard
51fd394065 [cxx-interop] fix typo in RetainReleaseOperationKind enum 2024-12-03 11:45:29 +11:00
Allan Shortlidge
5143382265 AST: Introduce Decl::isUnavailableInCurrentSwiftVersion(). 2024-12-02 07:35:58 -08:00
Allan Shortlidge
2358712870 AST/Sema: Remove DeclAttr::isUnavailable().
Update callers to use `Decl::isUnavailable()` instead.
2024-12-02 07:35:58 -08:00
Allan Shortlidge
cd13d7d777 AST: Remove AvailableAttr::RenameDecl.
The renamed decl is now stored exclusively in the split request evaluator
storage, which is more efficient since most availability attributes do not
specify a renamed decl.
2024-11-29 10:35:02 -05:00
Allan Shortlidge
5de7211984 AST: Rename createForAlternative() to createForAsyncAlternative(). 2024-11-22 14:35:47 -08:00
Gabor Horvath
51e92d9db4 [cxx-interop] Use the new lifetime dependence language feature flag
Non-escapable types are now enabled by default. Use the new lifetime
dependence feature flag to gate interpreting lifetime annotations on the
C++ side and add tests to make sure we get the expected diagnostics when
the feaature flag is off.
2024-11-21 13:13:16 +00:00
Doug Gregor
c5de02f60e Merge pull request #77628 from DougGregor/clang-importer-parse-request
[Clang importer] Use ParseSourceFileRequest for parsing swift_attr attributes
2024-11-16 01:12:27 -08:00
Doug Gregor
24a12ebc34 Renable GeneratedSourceInfo::Attribute to GeneratedSourceInfo::AttributeFromClang 2024-11-15 09:02:49 -08:00
Doug Gregor
f8f14b4ab6 [Clang importer] Use ParseSourceFileRequest for parsing swift_attr attributes
The Clang importer was directly calling into the parser to parse the
attribute (or modifier) within swift_attr. Aside from being gross, this
isn't possible with ASTGen.

Instead, teach ParseSourceFileRequest to deal with modifiers in the
same way that the Clang importer was hardcoding, and have the Clang
importer pull the attributes/modifiers off of the "missing"
declaration introduced by the request.

One benefit of this approach is that we're only parsing each
swift_attr source buffer once, then cloning the attributes each time
it's used, so we should be doing less work overall.

Fixes rdar://139119159.
2024-11-15 09:02:47 -08:00
Gabor Horvath
558380f6f8 [cxx-interop] Support conditional escapability
This PR adds a variadic macro that builds a SwiftAttr string containing
the names of the template type parameters that need to be escapable for
the type to be considered escapable. It also adds logic to interpret
this annotation.

rdar://139065437
2024-11-15 14:27:54 +00:00
Doug Gregor
aa4c548c0f Clang importer: switch swift_attr attribute text cache over to be module-sensitive
We need different buffers for each imported module that has swift_attr attributes,
so cache them appropriately.
2024-11-13 21:19:39 -08:00
Doug Gregor
989c73d014 Ensure that buffers containing Clang swift_attr attributes are parsed as attributes
Previously, they were being parsed as top-level code, which would cause
errors because there are no definitions. Introduce a new
GeneratedSourceInfo kind to mark the purpose of these buffers so the
parser can handle them appropriately.
2024-11-13 21:19:37 -08:00
Egor Zhdan
6943986c71 [cxx-interop] Import private fields of C++ structs
While private and protected fields coming from C++ cannot be accessed from Swift, they can affect Swift typechecking.

For instance, the Swift typechecker mechanism that adds implicit `Sendable` conformances works by iterating over all of the struct's fields and checking whether all of them are `Sendable`. This logic was broken for C++ types with private fields, since they were never accounted for. This resulted in erroneous implicit `Sendable` confromances being added.

Same applies for `BitwiseCopyable`.

In addition to this, ClangImporter used to mistakenly mark all C++ structs that have private fields as types with unreferenceable storage, which hampered optimizations.

As a side effect of this change, we now also provide a better diagnostic when someone tries to access a private C++ field from Swift.

rdar://134430857
2024-11-12 12:47:26 +00:00
fahadnayyar
72b8d22362 Merge pull request #77457 from fahadnayyar/frt-ref-semantic-check-refactor
[cxx-interop] Refactoring reference semantic check for c++ records
2024-11-08 02:14:25 -08:00
fahadnayyar
f4a0dd6867 [cxx-interop] Refactoring reference semantic check for c++ records 2024-11-07 10:54:49 -08:00
Gábor Horváth
72f49bdf1b Merge pull request #77342 from swiftlang/gaborh/import-as-unsafe-for-real
[cxx-interop] Import some C++ methods as Unsafe
2024-11-06 12:40:14 +00:00
Gabor Horvath
d885dec4e8 [cxx-interop] Import some C++ methods as Unsafe
ClangImporter already had some logic in place to rename certain unsafe
C++ methods to make sure their name indicates unsafety. With the recent
push for auditability, we have a new @unsafe attribute so we can
automate parts of the auditing process. This patch makes sure whenever
we rename a method as "Unsafe", we also add the @unsafe attribute.
2024-11-01 13:03:59 +00:00
fahadnayyar
f430088616 Merge pull request #76798 from fahadnayyar/frt-return-unannotated-warning
[cxx-interop] Warning unannotated C++ APIs returning SWIFT_SHARED_REF…
2024-10-31 17:40:12 -07:00
Gábor Horváth
217b450a39 Merge pull request #77294 from swiftlang/gaborh/import-return-independent-as-immortal
[cxx-interop] SWIFT_RETURNS_INDEPENDENT_VALUE implies immortal lifetime
2024-10-31 14:53:46 +00:00
Gábor Horváth
09c89fb983 Merge pull request #77204 from swiftlang/gaborh/diagnostic-location
[cxx-interop] Import more C++ source locations into Swift
2024-10-31 11:06:11 +00:00
Tony Allevato
97186b0738 Merge pull request #69460 from dylansturg/indexstore
Enable indexing for refs to implicit declarations.
2024-10-30 16:38:37 -04:00
Gabor Horvath
17ef2f66fe [cxx-interop] Make SWIFT_RETURNS_INDEPENDENT_VALUE imply immortal lifetime
The semantics of returning independent value already matches what
immortal lifetimes are within Swift. This patch makes sure this
annotation works as expected with non-escapable types.

rdar://137671642
2024-10-30 15:35:33 +00:00
Gabor Horvath
86e708a39b [cxx-interop] Import more C++ source locations into Swift
Occasionally, when the Swift compiler emits a diagnostic for a construct
that was imported from C++ we get a diagnostic with unknown location.
This is a bad user experience. It is particularly bad with the
borrow-checker related diagnostics. This patch extends the source
location importing to declarations in ClangImporter. There are some
invariants enforced by the Swift compile, e.g., a source range is
comprised of two valid source locations or two invalid ones. As a
result, this patch adds approximate source locations to some separators
like braces or parens that are not maintained by Clang. Having slightly
incorrect ranges in this case is better than emitting unknown source
locations.
2024-10-30 10:58:26 +00:00
fahadnayyar
e640ed637d [cxx-interop] Warning unannotated C++ APIs returning SWIFT_SHARED_REFERENCE types 2024-10-29 17:31:15 -07:00
Ben Barham
f59b2f19e6 Merge branch 'main' into 2024-rebranch-to-main 2024-10-28 13:03:35 -07:00
Dylan Sturgeon
a1e888785d Enable indexing for refs to synthesized declarations.
Based on feedback in PR https://github.com/swiftlang/swift/pull/69460, enabling indexing for synthesized decls because they are usable by users and make sense to appear in the indexstore.

Sets `synthesized` on some additional decls:

  - derived `hashInto(...)`
  - Objc properties and methods derived from Objc protocols

https://github.com/apple/swift/issues/67446
2024-10-28 10:07:27 -07:00
Egor Zhdan
81c7fb015a [cxx-interop] Ensure that C++ structs are fully defined
Swift should never try to import C++ structs that are not fully defined. This is because Clang can't synthesize implicit members of such types, which might be necessary for Swift, e.g. a copy constructor.

This adds an assertion that makes sure that C++ structs are considered to be fully defined by Clang. This condition should already be satisfied.
2024-10-28 12:54:13 +00:00
swift-ci
ed4836f5b1 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-18 12:22:36 -07:00
Egor Zhdan
9c5519a74a Merge pull request #77098 from swiftlang/egorzhdan/chrono-linux-cxx23
[cxx-interop] Do not import `std::chrono::time_zone`
2024-10-18 20:52:02 +02:00
Egor Zhdan
c530d3c0db [cxx-interop] Do not import std::chrono::time_zone
This type is non-copyable and non-moveable despite having a defaulted move constructor. It cannot currently be expressed in Swift. Let's not try to import it into Swift.

rdar://138123064 / resolves https://github.com/swiftlang/swift/issues/76809
2024-10-18 15:25:50 +01:00
swift-ci
848b66dca6 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-17 12:15:00 -07:00
Gabor Horvath
e86cf185e2 [cxx-interop] Diagnose misuses of escapability and lifetimebound
When a type is explicitly annotated as escapable or non-escapable it has
requirements about the lifetime annotations. This patch introduces
diagnostics to detect that.
2024-10-17 15:35:55 +01:00
swift-ci
b4107795e1 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-16 04:15:19 -07:00
Gábor Horváth
e4d625cbec Merge pull request #76696 from swiftlang/gaborh/lifetimebound-on-ctors
[cxx-interop] Add test for lifetimebound annotation in ctor
2024-10-16 11:57:26 +01:00
swift-ci
8956a19bd8 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-15 20:34:48 -07:00
John Hui
b58a908082 [cxx-interop] Add Hashable conformance to imported enums (#76940)
* [cxx-interop] Add Hashable conformance to imported enums

Previously, imported enums only conformed to RawRepresentable and Equatable,
so they could not be used as members of a Set or keys of a Dictionary.
This patch adds Hashable conformance to give them that ability,
as well as some test cases to clarify the expected behavior.
Existing test cases are updated to reflect this new conformance.

rdar://129713687
2024-10-15 20:24:35 -07:00
Gabor Horvath
9551e9d9ca [cxx-interop] Add test for lifetimebound annotation in ctor
There was also an off by one error in this case.
2024-10-15 16:29:31 +01:00
swift-ci
fa5fc7948e Merge remote-tracking branch 'origin/main' into rebranch 2024-10-14 21:45:47 -07:00
Allan Shortlidge
7819dd7833 AST/Sema: Remove unnecessary ASTContext parameters from availability APIs.
Many of the methods on `AvailabilityInference` take both a `Decl` and an
`ASTContext`, which is redundant.
2024-10-14 17:46:53 -07:00
swift-ci
72676c2c4a Merge remote-tracking branch 'origin/main' into rebranch 2024-10-08 15:43:57 -07:00
Doug Gregor
ead028bb11 Support applying macros to imported-as-member declarations 2024-10-08 11:12:53 -07:00