Commit Graph

28352 Commits

Author SHA1 Message Date
Doug Gregor
ee9c066050 Introduce a new Initializer subclass for the arguments of custom attributes
Since the introduction of custom attributes (as part of property
wrappers), we've modeled the context of expressions within these
attributes as PatternBindingInitializers. These
PatternBindingInitializers would get wired in to the variable
declarations they apply to, establishing the appropriate declaration
context hierarchy. This worked because property wrappers only every
applied to---you guessed it!---properties, so the
PatternBindingInitializer would always get filled in.

When custom attributes were extended to apply to anything for the
purposes of macros, the use of PatternBindingInitializer became less
appropriate. Specifically, the binding declaration would never get
filled in (it's always NULL), so any place in the compiler that
accesses the binding might have to deal with it being NULL, which is a
new requirement. Few did, crashes ensued.

Rather than continue to play whack-a-mole with the abused
PatternBindingInitializer, introduce a new CustomAttributeInitializer
to model the context of custom attribute arguments. When the
attributes are assigned to a declaration that has a
PatternBindingInitializer, we reparent this new initializer to the
PatternBindingInitializer. This helps separate out the logic for
custom attributes vs. actual initializers.

Fixes https://github.com/swiftlang/swift/issues/76409 / rdar://136997841
2024-12-06 17:40:32 -08:00
Joe Groff
ac7a61679d Add an @_addressableSelf attribute to mark the self param of methods as addressable. 2024-12-06 16:54:01 -08:00
Pavel Yaskevich
7701f4e0dd [CSSimplify] Avoid resolving extraneous (trailing) closures
If a (trailing) closure is determined to be an extraneous argument
for one of the overload choices it needs to be marked as hole as
eagerly as possible and prevented from being resolved because
otherwise it's going to be disconnected from the rest of the
constraint system and resolution might not be able to find all of
the referenced variables. This could result either in crashes
or superfluous diagnostics.

Resolves: rdar://141012049
2024-12-06 16:41:49 -08:00
Slava Pestov
302b163edc AST: Optimize TypeAliasType representation 2024-12-06 17:39:55 -05:00
Egor Zhdan
8859b629fa [cxx-interop] Allow using C-like structs in public Swift interfaces
When compiling with C++ interop enabled, we enable extra safety checks to prevent library authors from accidentally exposing ABI-fragile C++ symbols in resilient Swift interfaces.

The heuristic we use is overly strict, and it prevents the compiler from being able to typecheck various modules from their interfaces when C++ interop is enabled. Darwin and System are two of such modules.

The underlying challenge is that there isn't a good distinction between C structs and C++ structs: whenever parsing a header file in C++ language mode, Clang assumes that every struct is a C++ struct.

This relaxes the heuristic to allow exposing C-like structs in resilient interfaces.

rdar://140203932
2024-12-06 20:18:53 +00:00
Anders Bertelrud
d2cdf0fe1c [Playground] Missing playground results for property accessors in a class wrapped in a struct (#77530)
When a decl that has properties with accessors (`get`, `set`, `willSet`, `didSet`, `subscript()`, etc) is nested inside another type, those accessor implementations aren't playground-transformed.

The reason is that the playground transform uses an `ASTWalker` to get to the top-level structure, but then once it’s inside a type, it does directly nested `transformDecl()` calls. And that inner check is missing a case for accessors.

This change adds an else-clause that checks if the declaration is a `AbstractVarDecl`, and if so, calls `transformDecl()` on each accessor.

It's unfortunate that the playground transform reaches decls in two different ways (using an `ASTWalker` to get to the top-level decls but then using directly nested calls below). That issue seems to be worth resolving at some point (perhaps by using the `ASTWalker` for the whole traversal?)... but that would be a larger change requiring more thought, and so the missing results being fixed here are worth addressing with a safer short-term fix.

rdar://139656464
2024-12-06 09:41:32 -08:00
Slava Pestov
d6b08c3c6d Merge pull request #77997 from slavapestov/csapply-astcontext
Sema: Stash the ASTContext in the ExprRewriter
2024-12-06 04:59:48 -05:00
Allan Shortlidge
4da7f83f84 AST: Remove Decl::getSemanticAvailableRangeAttr().
This query's functionality was not useful enough to be exposed on `Decl` and
cached in the request evaluator. Instead, just share a local implementation of
it in `TypeCheckAttr.cpp`.
2024-12-05 15:47:24 -08:00
Slava Pestov
8b3e219e37 Sema: Stash the ASTContext in the ExprRewriter 2024-12-05 18:41:31 -05:00
Hamish Knight
c50a656db8 Merge pull request #77983 from hamishknight/ref-wrap
[CS] Correctly handle compound-applied functions with property wrappers
2024-12-05 22:36:12 +00:00
Slava Pestov
351c36f327 Merge pull request #77959 from slavapestov/fix-issue-73245
Sema: Fix soundness hole with variable initializers and opaque return types
2024-12-05 15:36:55 -05:00
Joe Groff
a045d665bb Merge pull request #75282 from jckarter/addressable-params-1
[WIP] Prototype an `@_addressable` attribute that puts an argument at a stable address.
2024-12-05 09:17:26 -08:00
Pavel Yaskevich
464ff30cfa Merge pull request #77952 from xedin/print-changes-in-a-more-readable-way
[CSTrail] NFC: Make `dumpActiveScopeChanges` friendlier to humans
2024-12-05 08:20:26 -08:00
Pavel Yaskevich
6694c163f4 Merge pull request #77887 from aphananthe42/56350
Refactor the remaining parts of replacing starts_with("_") with hasUnderscoredNaming()
2024-12-05 08:11:20 -08:00
Hamish Knight
48dc186269 [CS] Correctly handle compound-applied functions with property wrappers
Avoid wrapping parameters in the function reference
for compound applies, and make sure we consult
the parameter label in the compound name if it's
present to determine whether to match using the
projected value or not. This matches the existing
logic in `unwrapPropertyWrapperParameterTypes`.
2024-12-05 15:55:20 +00:00
Hamish Knight
c4efa0d5f0 [AST] Factor out Expr::getNameLoc
There are a bunch of AST nodes that can have
associated DeclNameLocs, make sure we cover them
all. I don't think this makes a difference for
`unwrapPropertyWrapperParameterTypes` since the
extra cases should be invalid, but for cursor info
it ensures we handle UnresolvedMemberExprs.
2024-12-05 15:55:19 +00:00
Hamish Knight
00f6df91d6 Merge pull request #77944 from hamishknight/compound-ume
[CS] Correctly set compound bit for UnresolvedMemberExprs
2024-12-04 20:22:40 +00:00
Slava Pestov
c0787f337f Sema: Fix soundness hole with variable initializers and opaque return types
Just because the type of the initializer expression is an opaque return type,
does not mean it is the opaque return type *for the variable being initialized*.

It looks like there is a bit of duplicated logic and layering violations going
on so I only fixed one caller of openOpaqueType(). This addresses the test case
in the issue. For the remaining calls I added FIXMEs to investigate what is
going on.

Fixes https://github.com/swiftlang/swift/issues/73245.
Fixes rdar://127180656.
2024-12-04 15:13:22 -05:00
Anders Bertelrud
065eb832f8 [Playground] Missing playground results for init and deinit in a class wrapped in a struct (#77498)
The problem here is when wrapping a type that has constructors and destructors inside another type.

The reason is that the playground transform uses an `ASTWalker` to get to the top-level structure, but then once it’s inside a type, it does directly nested transformDecl() calls.  And that inner check was for too narrow of a type.

The problem in this case was that the `dyn_cast<FuncDecl>(D)` was too narrow and didn’t include constructors/destructors.  We want `dyn_cast<AbstractFunctionDecl>(D)`.

We should probably resolve this duality (using an `ASTWalker` to get to the top-level decls but then using directly nested calls below) at some point, but that’s a larger change, and so the specific problem covered by this commit is worth addressing with this safer short-term fix.

Changes:
- change a `dyn_cast<FuncDecl>` to a `dyn_cast<AbstractFunctionDecl>` in PlaygroundTransform.cpp
- add a unit test nested init and deinit (this test also tests the unnested case)

rdar://137316110
2024-12-04 11:55:28 -08:00
Pavel Yaskevich
f94685e704 [CSTrail] NFC: Add dump() methods to SolverTrail 2024-12-04 10:36:12 -08:00
Pavel Yaskevich
f18b772293 [CSTrail] NFC: Make dumpActiveScopeChanges friendlier to humans
`dumpActiveScopeChanges` is used as part of `-debug-constraints`
and could be overwhelming if there are a lot of changes in the scope
because it prints every change including binding inference from
every applicable constraint.

These changes make `dumpActiveScopeChanges` more of summary of
what happened with type variables and constraints so far which
is much easier to navigate while debugging.
2024-12-04 10:36:05 -08:00
Pavel Yaskevich
08720eef82 Merge pull request #77936 from xedin/revert-void-favoring
Revert changes related to `Void` binding favoring
2024-12-04 10:24:20 -08:00
Anthony Latsis
73cabe303b Sema: Account for existential member access limitations in doesStorageProduceLValue 2024-12-04 15:03:39 +00:00
aphananthe42
f4a3a65411 Fix: Adjust the declaration positions of writtenBase and correctedBase 2024-12-04 23:30:10 +09: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
Michael Gottesman
87495c6b83 Merge pull request #77900 from gottesmm/rdar127477211
[region-isolation] Perform checking of non-Sendable results using rbi rather than Sema.
2024-12-03 22:08:49 -08:00
Joe Groff
3c0b08dbcb Prototype an @_addressable attribute that puts an argument at a stable address.
Many APIs using nonescapable types would like to vend interior pointers to their
parameter bindings, but this isn't normally always possible because of representation
changes the caller may do around the call, such as moving the value in or out of memory,
bridging or reabstracting it, etc. `@_addressable` forces the corresponding parameter
to be passed indirectly in memory, in its maximally-abstracted representation.
[TODO] If return values have a lifetime dependency on this parameter, the caller must
keep this in-memory representation alive for the duration of the dependent value's
lifetime.
2024-12-03 20:39:23 -08:00
Hamish Knight
8d65fb3250 Merge pull request #77919 from hamishknight/remove-hack
[CS] Remove hack for rdar://139234188
2024-12-04 01:05:57 +00:00
Pavel Yaskevich
ffac97a1ee Revert "[CSBindings] Adjust hasConversions to handle Void has having not conversions"
This reverts commit 76f0bcb05c.
2024-12-03 13:15:49 -08:00
Pavel Yaskevich
8aa6280bf3 Revert "[CSBindings] Don't favor application result types before application happens"
This reverts commit bc949c3680.
2024-12-03 13:15:46 -08:00
Allan Shortlidge
ecf7ac910e Merge pull request #77907 from tshortli/semantic-decl-availability
AST: Refactor semantic unavailability queries
2024-12-03 08:30:13 -08:00
Kuba (Brecka) Mracek
8792efedf0 Merge pull request #77115 from kubamracek/embedded-mangling-prefix
[Mangling] [NFC] Prepare for a new mangling prefix for Embedded Swift: $e
2024-12-03 08:10:49 -08:00
Egor Zhdan
475e5442ed Merge pull request #77897 from swiftlang/egorzhdan/allow-nonresilient-cxx-in-overlay
[cxx-interop] Allow using non-resilient C++ symbols in resilient overlays
2024-12-03 12:57:19 +00:00
Hamish Knight
d25bdfa4a3 [CS] Remove hack for rdar://139234188
Now that "is compound" is a separate bit in
FunctionRefInfo, we can correctly track the
application level for an EnumElementPattern.
2024-12-03 11:32:11 +00:00
Hamish Knight
0fdb038fc9 Merge pull request #77896 from hamishknight/functionref-split
[AST] Split out "is compound" bit on FunctionRefKind
2024-12-03 09:53:37 +00:00
Allan Shortlidge
64f9d5b945 AST: Remove Decl::getSemanticUnavailableAttr().
Also remove the underlying `SemanticUnavailableAttrRequest`, which used memory
very inefficiently in order to cache a detailed answer to what was usually a
much simpler question.

The only remaining use of `Decl::getSemanticUnavailableAttr()` that actually
needed to locate the semantic attribute making a declaration unavailable was in
`TypeCheckAttr.cpp`. The implementation of the request could just be used
directly in that one location. The other remaining callers only needed to know
if the decl was unavailable or not, which there are simpler queries for.

 # Please enter the commit message for your changes. Lines starting
2024-12-02 23:00:43 -08:00
Allan Shortlidge
b2cc10cd94 AST: Introduce Decl::isSemanticallyUnavailable().
Adopt it in a few places as a replacement for `Decl::getSemanticUnavailableAttr()`.
2024-12-02 23:00:43 -08:00
aphananthe42
022af38d17 Refactor: Replace starts_with("_") with hasUnderscoreNaming() 2024-12-03 09:54:00 +09:00
Pavel Yaskevich
8a0a083a45 Merge pull request #77797 from xedin/locatable-types
[AST/Sema]  Introduce a new type that has associated location in source
2024-12-02 16:33:43 -08:00
Kuba Mracek
9c77074cac [Mangling] Establish a new mangling prefix for Embedded Swift: $e 2024-12-02 15:01:24 -08:00
Kuba Mracek
6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00
Allan Shortlidge
e131682e19 Merge pull request #77886 from tshortli/consolidate-availability-queries-on-decl
AST: Consolidate queries for `@available` attributes on `Decl`
2024-12-02 14:21:45 -08:00
Michael Gottesman
7e05f1d562 Merge pull request #77817 from gottesmm/pr-ade4fb7f7990b00ba46cfd66429dc97a2af58c7a
[assume-mainactor] Only perform the change for items that are within the current module.
2024-12-02 16:54:56 -05:00
Michael Gottesman
cff835e061 [region-isolation] Perform checking of non-Sendable results using rbi rather than Sema.
In terms of the test suite the only difference is that we allow for non-Sendable
types to be returned from nonisolated functions. This is safe due to the rules
of rbi. We do still error when we return non-Sendable functions across isolation
boundaries though.

The reason that I am doing this now is that I am implementing a prototype that
allows for nonisolated functions to inherit isolation from their caller. This
would have required me to implement support both in Sema for results and
arguments in SIL. Rather than implement results in Sema, I just finished the
work of transitioning the result checking out of Sema and into SIL. The actual
prototype will land in a subsequent change.

rdar://127477211
2024-12-02 16:54:12 -05:00
Pavel Yaskevich
1f7b76edd3 Merge pull request #77816 from xedin/fix-property-wrapper-projected-value-applies
[CSApply] Avoid shortcutting argument conversion when parameter has a…
2024-12-02 13:52:30 -08:00
Egor Zhdan
f420456899 [cxx-interop] Allow using non-resilient C++ symbols in resilient overlays
If a Swift module built with library evolution enabled is an overlay of a C++ module, allow referring to the non-resilient C++ symbols from the Swift code.

Overlays are usually built and shipped along with the C/C++ modules, so library evolution is less of a concern there. A developer providing a Swift overlay for a C++ library would expect to be able to refer to the symbols from the C++ library within the overlay.
2024-12-02 19:44:32 +00:00
Anton Korobeynikov
8e53ccd671 [AutoDiff] Peel off @sendable implicit conversion when checking for unsupported differentiable function conversion (#77869) 2024-12-02 10:29:37 -08:00
Rintaro Ishizaki
30cf6472af Merge pull request #77813 from rintaro/member-chain-specialize
[PreCheckTarget] Consider UnresolvedSpecializeExpr a chain expression
2024-12-02 09:33:38 -08:00
Allan Shortlidge
d8c946d186 AST: Introduce Decl::getActiveAvailableAttrForCurrentPlatform(). 2024-12-02 07:35:58 -08:00
Allan Shortlidge
5cdcb5d116 AST: Introduce Decl::getNoAsyncAttr(). 2024-12-02 07:35:58 -08:00