Commit Graph

159 Commits

Author SHA1 Message Date
Xi Ge 00897a0efb [Serialization] Add precomputed layout table for hidden types
Add the module-format machinery that lets a Swift library record the
physical layout of hidden types (currently limited to C types imported via internal bridging header).
into binary modules, so downstream consumers can pull the layouts of these hidden types without
loading the internal dependency.

To test this, this change also added a frontend action to print hidden types' layouts
from both the module under compilation and all the modules being imported.
2026-05-15 10:07:19 -07:00
Alexis Laferrière dd798e0079 Embedded: Accept and enforce @_implementationOnly class properties
When hiding dependencies in embedded mode there are special rules for
classes. Classes properties can safely reference the hidden
dependencies, however code referencing these properties must be marked
`@export(interface)`.

We previously added a check to report implicit code without the requited
`@export(interface)`. However explicit references from user written code
wasn't fully checked, only explicit references to the imported type or
the type's services would be reported, not references to the property
itself.

We patch that hole here by introducing new requirements and a new layer
of check specific to class properies in embedded mode.

---

Class properties referencing a hidden dependency must be marked
`@_implementationOnly`. This adds on top of the requirement for the
class itself to have an explicit `@export(interface) deinit`.

This allows to report references from user written code using existing
diagnostics.
2026-03-24 13:18:23 -07:00
Alexis Laferrière 569ec1c06f Sema: Custom note with fixit for required init in classes 2026-03-12 09:59:59 -07:00
Alexis Laferrière 7c1b4802c8 Sema: Require classes to have a deinit for references non Embedded
Even without library-evolution, we allow references to hidden
dependencies from class properties as long as the class is not marked
open. In embedded, references from functions are only accepted when
marked `@export(interface)` as it can't be inlined in clients.

Combine both requirements to protect the implicitly generated destructor
as well. Add a requirement for classes with such a property to
explicitly declare a `@export(interface)` deinit. Otherwise that deinit
may be inlined in clients and cause a deserialization failure.

rdar://170855491
2026-03-12 09:59:59 -07:00
Alexis Laferrière 74f82aadba Sema: Custom text for error on references to IOI from open classes in NLE 2026-03-12 09:59:59 -07:00
Anthony Latsis 85db41932d Switch ASTContext::isLanguageModeAtLeast to LanguageMode 2026-02-10 16:06:58 +00:00
Anthony Latsis 31cafb0a27 Switch DiagnosticEngine::warnUntilLanguageMode and co. to LanguageMode 2026-02-10 16:06:56 +00:00
Sam Pyankov 03f9c8f5b7 Merge pull request #86667 from sepy97/warnuntilfutureversion
Downgrade StrictAccessControl errors to warnings
2026-01-22 15:25:27 -08:00
Alexis Laferrière c8f99af698 Merge pull request #86335 from xymus/exportability-nle-warn
Sema: Enable exportability checks in non-library-evolution mode as warnings by default
2026-01-22 09:47:52 -08:00
Alexis Laferrière 974e61d45a Sema: Loosen exportability checking for non-publicly imported types
This one is specific. Allow extensions to define public members to
package types implicitly exporting their memory layout when imported
non-publicly.

Enabling exportability checks for non-library-evolution by default as a
warning triggered marking such types as implicitly exported to clients.
This in turn enabled erroring on the extensions in clients when imported
non-publicly. This error would be source breaking and shouldn't be
required in such a context. We may even consider lifting this
restriction entierly, even for public types imported non-public.
2026-01-21 11:30:36 -08:00
Sam Pyankov 8e97334dab Refactors access check diagnostic to automatically downgrade errors to
warnings based on feature flag and/or swift version
2026-01-21 09:59:28 -08:00
Alexis Laferrière 8ab9047cbf Sema: Downgrade more exportability diags to warning by default
Centralize the logic downgrading diagnostics to warning for newly
introduced exportability checks along with the one silencing the errors.
Update client sites to the new API and to uniformly apply the downgrade
to warning behavior.

Diagnostics about typealiases and conformances are now properly
downgraded to warnings if not opt-in.
2026-01-20 13:55:11 -08:00
Alexis Laferrière 0ce07f1cd7 Merge pull request #86565 from xymus/exportability-internal-header
Sema: Enforce non-library-evolution exportability checking with the internal bridging header
2026-01-15 16:06:22 -08:00
Alexis Laferrière 251f0a266c Sema: Fix missing diagnostics with internal bridging headers
rdar://166322874
2026-01-14 13:24:47 -08:00
Sam Pyankov b0eee67a7b Emit an errors instead of warnings access control violations that should be fixed in the future when StrictAccessControl upcoming feature is enabled 2026-01-08 14:24:35 -08:00
Alexis Laferrière 2c59e8c659 Sema: Check exportability in non-library-evolution as warnings by default
Always enable checking for references to `@_implementationOnly` imports
even without library-evolution enabled but downgrade errors to warnings
by default. Clients should enable them as errors by adopting
`CheckImplementationOnly`.
2026-01-06 15:11:50 -08:00
Sam Pyankov 12e619fbba Merge pull request #86076 from sepy97/inlinable-function-private-setter
Diagnosing a private setter being accessed from the inlinable function.
2026-01-05 22:23:12 -08:00
Sam Pyankov 922874819d Diagnosing a private setter being accessed from the inlinable function.
rdar://81879146
2026-01-05 15:42:30 -08:00
Alexis Laferrière 7924c73f20 Merge pull request #86154 from xymus/exportability-nle-more
Sema: Extend exportability checking for non-library-evolution to more reasons
2025-12-22 13:58:44 -08:00
Alexis Laferrière cd253e7748 Sema: Add diagnostics specific to enum associated values 2025-12-19 15:13:25 -08:00
Alexis Laferrière 8b55b25b34 Sema: Add diagnostic for implicitly public inheritance 2025-12-19 15:13:25 -08:00
Slava Pestov ea587322dc Sema: New StrictAccessControl upcoming feature
Some access control holes were unconditionally downgraded to warnings, and
others were conditional on Swift 6 language mode. Let's hang them off an
upcoming feature instead.
2025-12-15 18:02:52 -05:00
Alexis Laferrière de9148e6f1 Merge pull request #85868 from xymus/exportability-nle-split
Sema: Distinguish implicitly visible memory layouts from the typical `@frozen` ones
2025-12-08 13:29:16 -08:00
Alexis Laferrière 8e04430498 Sema: Differentiate the reason for implictly public memory layouts 2025-12-05 09:25:37 -08:00
Alexis Laferrière 834bb63d5f Sema: Refactor source of truth for exceptions to exportability check 2025-12-04 14:12:40 -08:00
Anthony Latsis 88220a33c3 [NFC] "SwiftVersion" → "LanguageMode" in DiagnosticEngine::warnUntilSwiftVersion, etc. 2025-12-04 15:11:07 +00:00
Henrik G. Olsson ba7b9b0d6b Revert "Sema: Prefer the submodule import for access-levels diagnostics"
This reverts commit ca8792ca37.

Revert "[ImportResolution] Gracefully handle importing broken clang module"

This reverts commit edb48dff8c.

Revert "[ImportResolution] Don't deduplicate scoped imports"

This reverts commit da96079eb0.

Revert "[ImportResolution] Deduplicate top-level clang modules from import list"

This reverts commit 0ea5677aa1.

Reverting due to namelookup errors.

rdar://164588082
2025-11-19 23:10:14 -08:00
Alexis Laferrière ed85a12b59 Merge pull request #85317 from xymus/non-neic-funcs-and-typealiases
Sema: Allow more embedded code to reference non-public imports
2025-11-07 15:19:52 -08:00
Alexis Laferrière e04dac5523 Sema: Embedded can reference typealiases to non-publicly imported types 2025-11-04 14:41:08 -08:00
Alexis Laferrière 3fedebfe6c Sema: Embedded can reference non-publicly imported conformances
rdar://163965839
2025-11-04 14:41:07 -08:00
Alexis Laferrière 488321256e Sema: Consider non-LE structs to be of a restricted type by default
In non-library-evolution mode, gated behind the CheckImplementationOnly
feature flag, report references to structs marked with
`@_implementationOnly` from a fragile context. Preventing references
from inlinable functions and structs not marked `@_implementationOnly`.
2025-11-04 13:50:37 -08:00
Alexis Laferrière 0e7077db0a Sema: Allow embedded code to reference SPI
Embedded mode use only binary swiftmodules for distribution, SPI will
always be visible. We can allow implicitly always-emit-into-client
function bodies from the embedded mode to references SPIs.

rdar://163519075
2025-10-28 11:29:57 -07:00
Alexis Laferrière ba54b6d3d8 Sema: Don't enforce access-level on imports in embedded mode
Access-level on imports don't hide the dependency in
non-library-evolution mode. We can allow implicitly
always-emit-into-client function bodies from embedded mode
to references the dependencies.

rdar://163519185
2025-10-28 11:29:41 -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
Alexis Laferrière 860f2db0f8 Merge pull request #84630 from xymus/non-neic-funcs-are-fragile
Sema: In embedded, consider functions as fragile unless `@_neverEmitIntoClient`
2025-10-16 13:31:48 -07:00
Alexis Laferrière ca8792ca37 Sema: Prefer the submodule import for access-levels diagnostics
Update logic finding the import to point in diagnostics to avoid relying
on the implicit import of the top-level module when importing a clang
submodule. A recent change avoids duplicating the implicit imports and
broke the old logic.

rdar://162151660
2025-10-14 16:25:49 -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
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
Allan Shortlidge b047396246 AST: Use availability to control decl visibility in public swiftinterfaces.
Declarations that are unavailable at runtime because of an `@available`
attribute referencing a custom domain that was imported `@_spiOnly` should be
hidden from public swiftinterface files in `-library-level=api` modules. For
remaining declarations that do get printed in the public swiftinterface, skip
printing any `@available` attribute that refers to the domains from those
`@_spiOnly` dependencies. This allows API developers to control declaration
visibility using availability defined by another module.

Resolves rdar://156512028.
2025-09-04 12:32:19 -07:00
Allan Shortlidge ddca4b7404 Sema: Refactor diagnoseValueDeclRefExportability().
This is meant to make it clearer how exceptions for each `DisallowedOriginKind`
are handled.
2025-09-04 09:13:23 -07:00
Allan Shortlidge 91ddf93d0f Sema: Introduce an ExportabilityReason for availability attributes.
This allows diagnostics to be more precise and will also support logic that
allows for special cases for `@available` attributes in exportability checking.

Also fixes a bug where the exportability of `@available` attributes attached to
extensions were diagnosed twice for slightly differing reasons.
2025-09-02 23:13:08 -07:00
Allan Shortlidge e5adbe9910 Sema: Simplify recording required import access levels.
To reduce code duplication, implement a convenience version of
`swift::recordRequiredImportAccessLevelForDecl()` that handles the common case
of recording required import access for a reference to a `ValueDecl`.

NFC.
2025-08-21 18:24:30 -07:00
Allan Shortlidge aca604660f AST/Sema: Make MemberImportVisibility a migratable feature.
The migration to `MemberImportVisibility` can be performed mechanically by
adding missing import declarations, so offer automatic migration for the
feature.

Resolves rdar://151931597.
2025-05-30 15:34:08 -07:00
Allan Shortlidge ac8aec32ea Sema: Correct the decl kind in a resilience diagnostic.
Not everything is a type, type checker.
2024-10-26 09:26:39 -07:00
Allan Shortlidge c868378d96 ConstraintSystem: Use scoring to implement MemberImportVisibility.
Previously, the constraint solver would first attempt member lookup that
excluded members from transitively imported modules. If there were no viable
candidates, it would perform a second lookup that included the previously
excluded members, treating any candidates as unviable. This meant that if the
member reference did resolve to one of the unviable candidates the resulting
AST would be broken, which could cause unwanted knock-on diagnostics.

Now, members from transitively imported modules are always returned in the set
of viable candidates. However, scoring will always prioritize candidates from
directly imported modules over members from transitive imports. This solves the
ambiguities that `MemberImportVisibility` is designed to prevent. If the only
viable candidates are from transitively imported modules, though, then the
reference will be resolved successfully and diagnosed later in
`MiscDiagnostics.cpp`. The resulting AST will not contain any errors, which
ensures that necessary access levels can be computed correctly for the imports
suggested by `MemberImportVisibility` fix-its.

Resolves rdar://126637855.
2024-09-10 09:47:42 -07:00
Allan Shortlidge 7e1eac7f08 Sema: Avoid emitting superfluous resilience diagnostics with MemberImportVisibility.
When `MemberImportVisibility` is enabled, if the import that would bring a
member declaration into scope is missing it is diagnosed as an error. The
existing resilience diagnostics that would also diagnose the same problem in
contexts that are visible in the module interface are therefore superflous with
the feature enabled.
2024-08-07 18:17:10 -07:00
Allan Shortlidge fff4bebc44 AST: Clarify SourceFile's existing missing imports record.
In anticipation of adding a new kind of missing import record to `SourceFile`,
clarify the purpose of the existing "missing imports" record with more specific
naming and documentation.
2024-08-05 11:28:26 -07:00
Allan Shortlidge a077469d61 Sema: Consolidate logic for -Rmodule-api-import.
`recordRequiredImportAccessLevelForDecl()` now handles the decision to emit a
remark which consolidates some duplicated logic.
2024-08-01 17:15:47 -07:00
Allan Shortlidge 53a137b78f Sema: Refactor superfluous public import tracking.
In anticipation of reusing minimum access level information for diagnostics
related to the `MemberImportVisibility` feature, refactor the way the type
checker tracks the modules which must be imported publicly. Recording minimum
access levels is no longer restricted to modules that are already imported in a
source file since `MemberImportVisibility` diagnostics will need this
information when emitting fix-its for modules that are not already imported.

Unblocks rdar://126637855.
2024-08-01 17:15:47 -07:00