Commit Graph

132 Commits

Author SHA1 Message Date
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
Pavel Yaskevich
123068c64e [Sema] Resilience: Diagnose uses of init accessors in inlinable contexts if they are not marked as @usableFromInline 2024-07-12 09:54:00 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Alexis Laferrière
67694c2ab0 Sema: Report public conformances to non-publicly imported protocols
Exportability checking for non-public imports relies on classic
access-level checks for some of the work. However while conforming
to a local internal protocol from a public type is allow we should
disallow it for imported types, even when imported as internal.

Track exportability issues on conformances to protocols separately
from the general category. Use that information to improve the
diagnostics and report these issues for access-level on imports.

rdar://128420980
2024-06-12 14:48:46 -07:00
Xi Ge
6f5b40543d Revert "Handle package exportability."
This reverts commit d182d01c28.
2024-06-11 11:54:00 -07:00
Xi Ge
7ccd2adfb2 Revert "Update to ensure downgradeToWarning doesn't get reset"
This reverts commit c4251d2f45.
2024-06-11 11:53:43 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Ellie Shin
c4251d2f45 Update to ensure downgradeToWarning doesn't get reset 2024-05-02 15:46:44 -07:00
Ellie Shin
d182d01c28 Handle package exportability.
This PR treats package access level as exportable, preventing
internally imported types from accidentally being declared in
package decl signatures.

Added package-specific cases to ExportabilityReason and
DisallowedOriginKind to track the validity of imported types
at use sites with package access scope. Added tests to cover
variety of use cases.

Resolves rdar://117586046&125050064&124484388&124306642
2024-05-02 05:17:03 -07:00
Allan Shortlidge
41c414381b Frontend: Remove conformance availability error flags.
Now that the diagnostics are automatically errors in Swift 6, we don't need an
`-enable-conformance-availability-errors` flag to control whether unavailable
conformances are diagnosed as errors. Nobody was using the flag so it should be
safe to remove.

Part of rdar://88210812
2024-01-04 17:53:34 -08:00
Alexis Laferrière
a63687059c [Sema] Report public extensions to non-publicly imported types
Access levels on extensions are special. Let's make sure we report
public extensions referencing non-public imported types using the
preexisting general exportability checks.

Record and remark on the use of the import at the same time.
2023-10-03 15:18:39 -07:00
Alexis Laferrière
c15a576a1c [Sema] Introduce remarks about the source of each decl in API
Using `-Rmodule-api-import` the compiler prints a remark about the
import bringing in every decl used in public function signatures or
inlinable code. It also remarks on the source of conformances where they
are used and the source of typealias underlying types.
2023-09-28 13:00:04 -07:00
Alexis Laferrière
a0b848ea96 [Sema] Report imports marked as public that are not used in API 2023-09-28 13:00:04 -07:00
Alexis Laferrière
8dcfadd546 [Sema] Track the access level modifier on import full range 2023-09-27 22:30:28 -07:00
Alexis Laferrière
1bf368609e Merge pull request #68776 from xymus/access-level-import-indirect-checks
Sema: report conformances and typealias underlying types behind non-public imports
2023-09-27 12:54:35 -07:00
Alexis Laferrière
8f195b0a17 [Sema] Note which import restricts conformances and typealiases in API 2023-09-27 08:09:33 -07:00
Alexis Laferrière
2322164854 [Sema] Report typealiases desugaring to a non-public import in API
Typealiases with an underlying type from a non-public import cannot be
used in API or inlinable code. Report this as an error.
2023-09-26 14:24:10 -07:00
Alexis Laferrière
411ad875f5 [Sema] Ignore "exportability" check for non-public code
Exportability checking logic detects when decls are referenced from API
or inlinable code. This duplicates the check applied for the access
level. Skip the exportability reports about non-public imports and keep
the more familiar access-level check.
2023-09-26 14:23:37 -07:00
Alexis Laferrière
e90e2deb5d [Sema] Report use of conformances from non-public imports in inlinable code
Conformances imported as non-public cannot be used in API or inlinable
code, report it as an error.
2023-09-26 14:22:47 -07:00
Alexis Laferrière
af478fd819 [Sema] Only complain about the import when it does restrict the access level
Fix the note pointing to the import when using a package type in a
public declaration or inlinable code. We want a note on the import only
when it actually lowers the access level of the imported decl.

The note about a `package import` was simply be superfluous, while the
same note about a `public import` would trigger an assert later on.
2023-09-19 16:18:33 -07:00
Alexis Laferrière
d77007536c [Sema] Use new services to check access-level on imports in inlinable code
This cleans up the implementation on the call site.
2023-09-13 15:40:33 -07:00
Slava Pestov
1f23f7ad67 Sema: Don't emit bogus warnings about Sendable tuple conformance 2023-08-10 16:01:17 -04:00
Becca Royal-Gordon
fe6753485f [NFC] Adopt new diagnostic features across Sema 2023-07-20 15:23:47 -07:00
Becca Royal-Gordon
fc2c66f674 [NFC] Adopt %kind for Sema ValueDecl diagnostics 2023-07-19 13:08:11 -07:00
Becca Royal-Gordon
3732c75e97 Adopt %kind for certain access control diagnostics
These particular changes slightly alter some diagnostics.
2023-07-19 13:06:51 -07:00
Allan Shortlidge
496d29c47f AST: Remove FragileFunctionKind.allowUsableFromInline.
It was effectively always true after allowing default argument expressions to
reference `@usableFromInline` decls.
2023-07-12 12:47:06 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Allan Shortlidge
f11eaf16eb Sema: Diagnose accessor exportability.
Properties may have `@_spi` setters and therefore the exportability of a
referenced accessor may differ from the exportability of the storage.
2023-05-01 15:34:08 -07:00