Commit Graph

4265 Commits

Author SHA1 Message Date
Kuba Mracek
adeb40f261 SE-0492: Stabilize @_section/@_used into @section/@used
Removes the underscored prefixes from the @_section and @_used attributes, making them public as @section and @used respectively. The SymbolLinkageMarkers experimental feature has been removed as these attributes are now part of the standard language. Implemented expression syntactic checking rules per SE-0492.

Major parts:
- Renamed @_section to @section and @_used to @used
- Removed the SymbolLinkageMarkers experimental feature
- Added parsing support for the old underscored names with deprecation warnings
- Updated all tests and examples to use the new attribute names
- Added syntactic validation for @section to align with SE-0492 (reusing the legality checker by @artemcm)
- Changed @DebugDescription macro to explicitly use a tuple type instead of type inferring it, to comply with the expression syntax rules
- Added a testcase for the various allowed and disallowed syntactic forms, `test/ConstValues/SectionSyntactic.swift`.
2025-10-22 16:05:39 -07:00
John Hui
1e5194c9a4 [cxx-interop] Re-word message and documentation for FRT diagnostics
Several changes:

- shorten the warning at the call-site of unannotated functions that
  return FRTS
- place the call to action ("annotate 'X' with 'Y'") at the diagnostic
  note attached to the unannotated callee, where the annotation should
  be made
- re-word the foreign-reference-type.md documentation to (1) reflect the
  diagnostic wording change, (2) give a little bit more background about
  why the annotations are needed, and (3) not be specific to C++ (since
  we can import FRTs from C as well)
2025-10-22 14:53:06 -07:00
Alexis Laferrière
4c9a9ca9ad Merge pull request #85039 from xymus/exportability-var-diag
Sema: Custom diagnostic for var decls referencing a restricted type
2025-10-22 10:01:09 -07:00
Alexis Laferrière
2d339c1260 Sema: Custom diagnostic for var decls referencing a restricted type
Replace the `here` part of the generic exportability diagnostic for
variables with: `in a property declaration marked public or in a
'@frozen' or '@usableFromInline' context`.

The full diagnostic now looks like:
```
error: cannot use struct 'ImportedType' in a property declaration marked
public or in a '@frozen' or '@usableFromInline' context;
'HiddenDependency' has been imported as implementation-only
```

This should be improved further to support implicitly exported memory
layouts in non-library-evolution and embedded.
2025-10-21 11:30:52 -07:00
John Hui
d47a2626e2 [cxx-interop] Clean up diagnostics for functions returning FRTs
- Delete baseline language feature WarnUnannotatedReturnOfCxxFrt,
  which won't really be useful to users
- Remove some references to "cxx", since FRTs are not exclusively
  imported from C++
- Clean up lit test RUN lines to use %{fs-sep}
2025-10-19 22:22:15 -07:00
Fahad Nayyar
facef0e034 [cxx-interop] Enabling WarnUnannotatedReturnOfCxxFrt on by default and add it to a diagnostic group
This change makes the warning for unannotated C++ functions returning foreign
reference types (FRT) enabled by default, improving memory safety for Swift/C++
interop users. Also added CxxForeignReferenceType diagnostic group for better control
2025-10-17 17:06:44 -07:00
Joe Groff
6f2811110d Sema: discard self should not be allowed in inlinable methods of non-frozen types.
`discard self` requires knowledge of the internal layout of the type in order to clean
up its fields while bypassing its public `deinit`. Inlinable code can get copied into
and run from outside of the defining module, so this is impossible to implement.
Fixes rdar://160815058.
2025-10-17 16:14:58 -07:00
Alexis Laferrière
1b086e1bfc Sema: Embedded functions are fragile unless marked otherwise
Consider functions in embedded mode to be fragile unless marked with
`@_neverEmitIntoClient`. Basically treating them as if they were
`@_alwaysEmitIntoClient` by default. A fragile function cannot reference
restricted imports such as `@_implementationOnly`, `internal import`,
etc.

We consider them as fragile only for type-checking, at the SIL level
they remain treated as normal functions like before. This allows the
later optimization to work as expected. We may want to likely align both
ends of the compiler once this is properly supported in sema.

This is enabled only in embedded. Fragile functions in library-evolution
are marked with attributes and are already checked. We do not need this
is non-embedded non-library-evolution as CMO handles inlining and
already takes into consideration `@_implementationOnly` imports.

Note: We'll need a similar check for memory layouts exposed to clients.
We can still see compiler crashes and miscompiles in this scenario. This
will apply to both embedded and non-library-evolution modes. That will
likely need to extend the `@_neverEmitIntoClient` attribute and may
require a staged deployment.

rdar://161365361
2025-10-14 14:14:54 -07:00
Henrik G. Olsson
8d4e81c2fa [ImportResolution] Only disallow explicit std imports (allow implicit)
When expanding a Swift macro in a clang module where the original clang
module imported a submodule in a C++ standard library module other than
`std`, e.g. a submodule to `std_core`, this would result in an error.
This is because `std_core.math.abs` would be imported as
`CxxStdlib.math.abs`, which would later be translated back as
`std.math.abs` which doesn’t exist.

This changes the mapping to only map `std` to `CxxStdlib`. To prevent
errors when importing modules starting with `std_`, this error is moved
from the late-stage module import to the earlier processing of
`ImportDecl`s. This results in these module names still being forbidden
in explicit imports (i.e. naming them in source code), while still being
allowed in implicit imports inherited from clang modules.

This also fixes a fix-it bug where only the first 3 characters would be
selected for replacing with `CxxStdlib` when importing `std_core`.

This also fixes a diagnostic bug where aliased modules would refer to
the module name in the source code rather than the real module name, and
adds a note clarifying the situation.

rdar://161795429
rdar://161795673
rdar://161795793

fix non-fatal import error
2025-10-13 18:51:03 -07:00
Pavel Yaskevich
9d1df1f68b [Sema] Expand ~Sendable annotation checking and diagnostics
Diagnose incorrect conformance + suppression uses, make sure
that adding `Sendable` is not suggested is type is using `~Sendable`.
2025-10-09 13:46:01 -07:00
Pavel Yaskevich
c50602b2ce [Sema] Diagnose invalid uses of protocol suppression via ~ syntax 2025-10-09 10:41:27 -07:00
Pavel Yaskevich
389c240a2a [AST] Mark Sendable as a repressible protocol
This allows to spell `~Sendable` to suppress the conformance
inference on types when `TildeSendable` flag is enabled.
2025-10-09 10:41:27 -07:00
Allan Shortlidge
b9fd3e4604 Merge pull request #84778 from tshortli/swift-runtime-availability-domain
AST: Introduce a Swift runtime availability domain
2025-10-09 08:53:20 -07:00
Allan Shortlidge
2d8465b043 AST: Introduce the SwiftLanguageMode availability domain spelling.
Require `-enable-experimental-feature SwiftRuntimeAvailability` to use this new
spelling.
2025-10-08 15:46:34 -07:00
Anthony Latsis
537353074d Merge pull request #84730 from swiftlang/jepa-main2
RequirementMachine: Diagnose unsupported value generic parameter definitions properly
2025-10-08 21:12:00 +01:00
Artem Chikin
38a8b07ac3 Merge pull request #84684 from aviralg/perf-hint/existential-any
[Performance Hints] Implement check for existential any
2025-10-08 00:19:53 -04:00
Anthony Latsis
58fa8bf762 RequirementMachine: Diagnose unsupported value generic parameter definitions properly
The flow was such that we recorded subtype constraints regardless of the
subject type's nature. Extract value generics handling out of the
devious `else if` chain, and never record any subtype constraints if the
subject type is a non-type parameter.

While we're here, generalize the diagnostic message for user-written
subtype constraints on value generic parameters and emit it
consistently, not just if the right-hand side contains a protocol type.
2025-10-08 02:13:03 +01:00
Aviral Goel
1cf692f116 [Performance Hints] Implement check for existential any
This commit introduces a performance hint check that warns on the use of
existential any in variable declarations, function and closure parameters and
returns, and typealiases.
2025-10-07 11:18:31 -07:00
Hamish Knight
d65f28984a [CS] Improve diagnostics for non-metatype type(of:) contextual type
Emit a custom diagnostic for this case, and handle holes.
2025-10-06 21:45:48 +01:00
Doug Gregor
9fa9ea761b Merge pull request #84676 from DougGregor/c-implementation-import-lookup
[SE-0495] Make sure we can find imported C functions for @c @implementation
2025-10-03 21:42:00 -07:00
Doug Gregor
67c7fbc87b [SE-0495] Make sure we can find imported C functions for @c @implementation
@c @implementation relies on matching the original C declaration. The
lookup for the original C declaration was doing the wrong kind of
lookup, meaning that it could only find the C declaration if it came
through a bridging header, and not through a normal module import.
Using unqualified lookup here finds the name appropriately.

Clarify the diagnostics here as well to not talk about umbrella and
bridging headers.

Fixes rdar://161909754.
2025-10-03 12:03:12 -07:00
Meghana Gupta
ea0e0173b4 Merge pull request #84595 from meg-gupta/moreborrowaccessors
Extend borrow accessor support
2025-10-03 11:14:49 -07:00
Artem Chikin
96ee032d70 Merge pull request #84578 from artemcm/SPA-Init
[Performance Hints] Add simple check for returning of values of array and dictionary type
2025-10-02 19:11:57 -04:00
Artem Chikin
7c74890bd8 [Performance Hints] Add simple check for returning of values of array and dictionary type
This check will run on each type-checked primary input of the current compilation and emit a warning diagnostic for all discovered occurences of this code pattern when the performance hint diagnostic is enabled
2025-10-02 10:31:13 -07:00
Arnold Schwaighofer
cfd0d6af35 Merge pull request #84624 from aschwaighofer/inline_always_error_non_final_class_methods
Error on usage of `@inline(always)` on non-final class methods
2025-10-02 08:53:21 -07:00
Meghana Gupta
fa7281d66e Introduce a new attribute @_unsafeSelfDependentResult
It can be used in borrow/mutate accessors to unsafely specify that the
result is dependent on the self access.
2025-10-02 07:42:07 -07:00
Sima Nerush
7122a11b43 Merge pull request #79895 from simanerush/allow-explicit-nonisolated-for-lazy-and-prop-wrappers
[Concurrency] Allow explicit 'nonisolated' on lazy properties and properties with property wrappers if they are a member of a non-'Sendable' type.
2025-10-01 15:53:43 -07:00
Arnold Schwaighofer
7ceebbee22 Error on usage of @inline(always) on non-final class methods 2025-10-01 09:19:32 -07:00
Arnold Schwaighofer
25a071efc8 Add experimental feature @inline(always)
The intent for `@inline(always)` is to act as an optimization control.
The user can rely on inlining to happen or the compiler will emit an error
message.

Because function values can be dynamic (closures, protocol/class lookup)
this guarantee can only be upheld for direct function references.

In cases where the optimizer can resolve dynamic function values the
attribute shall be respected.

rdar://148608854
2025-09-30 08:36:26 -07:00
Erik Eckstein
a322fd9209 Serialization: remove the IS_OSSA flag from the module file header 2025-09-26 08:01:08 +02:00
Kavon Farvardin
e93ffb762d Merge pull request #84504 from kavon/manual-ownership/usability-fixes-3
Usability improvements for ManualOwnership (part 3)
2025-09-25 03:17:14 -07:00
Sima Nerush
0f7d39dae8 [Concurrency] Allow explicit 'nonisolated' on lazy properties and properties with property wrappers if they are a member of a non-'Sendable' type. 2025-09-24 18:20:23 -07:00
Kavon Farvardin
0ec0292893 ManualOwnership: disallow @_noImplicitCopy
ManualOwnership effectively subsumes @_noImplicitCopy,
by diagnosing all implicit copies in a function.
2025-09-24 13:10:16 -07:00
Alexis Laferrière
e5a9b6463d Merge pull request #84381 from xymus/rename-cdecl-to-c
Parser: Rename the experimental attribute `@cdecl` to the shorter `@c`
2025-09-23 14:58:46 -07:00
Allan Shortlidge
d505527ff2 Merge pull request #84454 from tshortli/deprecated-custom-availability-domain-fix-its 2025-09-23 07:44:11 -07:00
Doug Gregor
90a5a0f87d Merge pull request #84410 from DougGregor/internal-import-bridging-header 2025-09-22 23:09:07 -07:00
Doug Gregor
6e6dc4b063 Merge pull request #81054 from DataCorrupted/FixTypedThrow
[Sema] @objc functions shall not have typed throw
2025-09-22 18:21:52 -07:00
Allan Shortlidge
8a1338dfb2 AST/Sema: Diagnose references to permanently enabled availability domains
A "permanently enabled" availability domain is one that has been declared
always available and is also simultaneously has either an attribute that
makes it deprecated or universally unavailable.

Emit fix-its that remove (or update) `@available` attributes that restrict
availability in a permanently enabled domain. Also, emit warnings about
`if #available` queries that always return true because they check a
permanently enabled domain.

Resolves rdar://157601761.
2025-09-22 17:48:55 -07:00
Doug Gregor
b7a83495fa Expand DisallowedOriginKind with an explicit entry for internal bridging headers 2025-09-22 11:08:51 -07:00
Doug Gregor
7bc02d6a70 Improve some diagnostic with @_implementationOnly violations from internal bridging headers 2025-09-19 16:49:24 -07:00
Doug Gregor
b13c2aeccd Treat internally-imported bridging headers as internally-imported
When using an internal import for a bridging header, semantically treat
the contents of the bridging header, and anything that it imports, as
if they were imported internally. This is the actual semantic behavior
we wanted from internally-imported bridging headers.

This is the main semantic checking bit for rdar://74011750.
2025-09-19 16:49:17 -07:00
Alexis Laferrière
9ec824c20b Parser: Rename the experimental attribute @cdecl to @c
There's no users of `@cdecl` yet so we can do a direct rename. The
legacy `@_cdecl` remains unaffected.
2025-09-19 11:55:07 -07:00
Hamish Knight
46df852610 [Sema] Reject placeholders in type resolution for param and result types
Not all clients can properly handle the presence of placeholders in
interface types and it doesn't seem worth the complexity for the 
type replacement diagnostic.
2025-09-19 14:22:39 +01:00
Doug Gregor
3e981b7d8f Merge pull request #84374 from DougGregor/embedded-swift-restrictions
Embedded swift restrictions
2025-09-19 01:09:19 -07:00
Kavon Farvardin
84cf0a3551 Merge pull request #81858 from kavon/kavon/manual-ownership
introduce @_manualOwnership performance attribute
2025-09-18 16:15:20 -07:00
Doug Gregor
a16c9f7ab4 [Embedded] Diagnose dynamic casts to existentials
Embedded Swift doesn't have protocol conformance metadata, so it cannot
handle dynamic casts to existentials (nor should it).

Another part of rdar://119383905.
2025-09-18 10:05:35 -07:00
Doug Gregor
74d5ee26f0 [Embedded] Downgrade type-checker error about non-final generic class methods to a warning
The existing SIL-level diagnostic for this check only triggers when
the type is actually used, while the type-checker version is more
eager. Stage in the stricter check as a warning and we'll clamp down
on things later.
2025-09-18 10:05:35 -07:00
Doug Gregor
ade6e55b0c [Embedded] Diagnose uses of generic methods on existential values
Generic methods declared in protocols (and extensions thereof) cannot
be used on existential values, because there is no way to specialize
them for all potential types. Diagnose such cases in Embedded Swift
mode and via `-Wwarning EmbeddedRestrictions`.

This adds a bunch more warnings to the standard library that we'll
need to clean up, probably by `#if`'ing more code out.

Part of rdar://119383905.
2025-09-18 10:05:35 -07:00
Doug Gregor
844ba5f4f8 [Embedded] Diagnose non-final generic methods in class in the type checker
Move the diagnostic about non-final generic methods in classes up to
the type checker, so that it is available to `-Wwarning
EmbeddedRestrictions` and earlier in the pipeline. The SIL version of
this is still available as a backstop.

Yet another part of rdar://133874555.
2025-09-18 10:05:33 -07:00
Kavon Farvardin
61fe8a9b8e introduce @_manualOwnership performance attribute
This attribute forces programmers to acknowledge every
copy that is required to happen in the body of the
function. Only those copies that make sense according
to Swift's ownership rules should be "required".

The way this is implemented as of now is to flag each
non-explicit copy in a function, coming from SILGen, as
an error through PerformanceDiagnostics.
2025-09-17 13:51:57 -07:00