Commit Graph

119 Commits

Author SHA1 Message Date
Henrik G. Olsson
a43c8474bc [ClangImporter] Add clang module imports to Swift wrapper module
When macros like _SwiftifyImport are added to a wrapper module for a
clang module, they may need to refer to symbols declared in another
clang module that the wrapped module imports (e.g. because they are used
in the original signature). This adds all the imported clang modules as
implicit imports to the wrapper module.

rdar://151611573
2025-09-08 17:25:26 -07:00
Hamish Knight
89feb8fabb [Diags] Remove some now unnecessary calls to flush 2025-08-17 11:48:21 +01:00
Anthony Latsis
fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01:00
Doug Gregor
84a4a8bedb [Importer] Ensure that we can see macro-expanded declarations in C++ namespaces
Lookup into C++ namespaces uses a different path from C++ record declarations.
Augment the C++ namespace lookup path to also account for the auxiliary
declarations introduced by peer macro expansions.
2025-03-14 14:10:44 -07:00
Anthony Latsis
a84dfc8387 [Gardening] Fix some set but not used variables 2025-01-30 21:34:38 +00:00
Hamish Knight
e2ba36f7f4 Factor out ModuleDecl overload of import resolution
This resolves imports for the entire module.
2024-11-17 14:17:20 +00:00
Doug Gregor
11ed132614 [Clang importer + macros] Handle name lookup and type checking for expanded macros
Introduce a number of fixes to allow us to fully use declarations that
are produced by applying a peer macro to an imported declarations.
These changes include:
* Ensuring that we have the right set of imports in the source file
containing the macro expansion, because it depends only on the module
it comes from
* Ensuring that name lookup looks in that file even when the
DeclContext hierarchy doesn't contain the source file (because it's
based on the Clang module structure)

Expand testing to be sure that we're getting the right calls,
diagnostics, and generated IR symbols.
2024-11-13 21:21:56 -08:00
Rintaro Ishizaki
d4db99ce9d [Parse] Remove unnecessary dependencies to Parser.h
C++ swift::Parser is going to be replaced with SwiftParser+ASTGen.
Direct dependencies to it should be removed. Before that, remove
unnecessary '#include "swift/Parse/Parser.h"' to clarify what actually
depends on 'swift::Parser'.

Split 'swift::parseDeclName()' et al. into the dedicated files.
2024-11-02 01:23:59 -07:00
Allan Shortlidge
2ec8efea91 Sema: Allow some @_implementationOnly imports in stdlib modules.
The `_Concurrency` module imports a C module that is not available in the
toolchain or SDK, which means that `@_implementationOnly` must be used to hide
the dependency on this module from clients.
2024-09-09 12:48:28 -07:00
Allan Shortlidge
99fd29c3b9 Sema: Refactor @_implementationOnly deprecation warning suppression. 2024-09-09 12:24:25 -07:00
Alexis Laferrière
6c3c108b1e Sema: Limit the warning about @_implementationOnly imports deprecation to Swift targets
Preserve the warning to use `internal import` instead of `@_implementationOnly`
to imports of Swift modules only. This warning can be noisy, limiting it may
prevent users from outright learning to ignore it. Typical uses of an
`@_implementationOnly` import of a clang module is often for a project
internal module instead of a layering concern as we have with Swift module
targets. We can leave legacy uses of `@_implementationOnly` in peace for now.
2024-09-05 10:40:36 -07:00
Alexis Laferrière
3995ee4bbf Sema: Warn on all non-resilient uses of @_implementationOnly, even for clang targets
The warnings about `using '@_implementationOnly' without enabling library evolution for 'client'
may lead to instability during execution` and `@_implementationOnly' is deprecated, use
'internal import' instead` were wrongly restricted to only Swift import targets.
Make sure they are raised for clang module targets as well.

rdar://135233043
2024-09-04 16:40:49 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Doug Gregor
aa90b432e9 Drop a very, very dated comment that we don't need. The FIXME suffices 2024-06-14 22:21:41 -07:00
Alexis Laferrière
9c868f8454 Merge pull request #74351 from xymus/cross-import-hidden-imports
Sema: Support access-level on imports with cross-import overlays
2024-06-12 14:56:49 -07:00
Alexis Laferrière
17b8e86a6d Sema: Support newer import restrictions with cross-import overlays
Add support for access-level on imports and `@_spiOnly imports` to cross
import overlays. The overlay inherits the most restrictive import
access-level of the declaring module and the bystander module.

rdar://129606112
2024-06-12 11:08:49 -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
Artem Chikin
d2ed753d52 Merge pull request #73907 from artemcm/ExplicitAutoLinkScanner
[Dependency Scanning] Collect and report each module dependency's Link Libraries
2024-06-05 19:26:34 -07:00
artemcm
d70863501e [Dependency Scanning] Collect and report each module dependency's Link Libraries 2024-06-05 10:59:41 -07:00
Alexis Laferrière
f9fe643e48 Sema: Don't report ambiguities on an explicit public import
Silence errors on ambiguous implicitly public imports when a different import
is explicitly public. In this case all imports are public so the ambiguity is
not dangerous and this configuration can happen during adoption of
access-levels on imports. Keep the error when an explicit import is package,
internal or below.

rdar://129037503
2024-06-05 10:34:26 -07:00
Alexis Laferrière
fd59933ff5 Merge pull request #73179 from xymus/deprecated-impl-only
Sema: Warn that resilient uses of `@_implementationOnly` are deprecated
2024-04-23 09:22:12 -07:00
Alexis Laferrière
3d611e2a85 Sema: Simplify warning about @_implementationOnly being deprecated 2024-04-22 14:44:44 -07:00
Alexis Laferrière
bd9c385ce5 Sema: Offer solution to silence inconsistent import access-level warnings
Adoption InternalImportsByDefault provides a safe access-level by default
to imports, as such ambiguities are not a risk and showing this warning is
superflous. When this warning is shown, make sure we note this alternative.
2024-04-22 10:52:21 -07:00
Alexis Laferrière
ff797cfd66 Sema: Warn on resilient uses of @_implementationOnly as deprecated
Report uses of `@_implementationOnly` in resilient modules as deprecated.
With a fixit to replace it with `internal` or delete it when imports
are internal by default.

Uses of `@_implementationOnly` in non-resilient modules is already reported
as being unsafe.
2024-04-22 10:32:58 -07:00
Kuba Mracek
7d44c2be48 [Volatile] Add a -enable-experimental-feature Volatile 2024-04-05 15:13:30 -07:00
Ben Barham
1fdda023b3 Rename StringRef::endswith references to StringRef::ends_with
Missed this when doing the `startswith` renaming. `endswith` has also
been deprecated upstream (and presumably soon to be removed).
2024-04-01 10:59:16 -07:00
Ben Barham
9779c18da3 Rename startswith to starts_with
LLVM is presumably moving towards `std::string_view` -
`StringRef::startswith` is deprecated on tip. `SmallString::startswith`
was just renamed there (maybe with some small deprecation inbetween, but
if so, we've missed it).

The `SmallString::startswith` references were moved to
`.str().starts_with()`, rather than adding the `starts_with` on
`stable/20230725` as we only had a few of them. Open to switching that
over if anyone feels strongly though.
2024-03-13 22:25:47 -07:00
Ben Barham
f292ec9784 Use the new template deduction guides rather than makeArrayRef
LLVM has removed `make*ArrayRef`, migrate all references to their
constructor equivalent.
2024-02-23 20:04:51 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Ellie Shin
7d4e5678c7 Compare the package names of the accessing module and the decl's defining module.
Update tests.
2024-02-13 19:11:32 -08:00
Ellie Shin
4588cc2261 Support bypassing resilience checks for package decls at use site in a package.
By default package decls are treated as resilient, similar to public (non-frozen).
This PR adds support to allow direct access to package decls at use site if opted-in.
Requires the loaded module to be a binary module in the same package.

Resolves rdar://121626315
2024-02-13 19:09:31 -08:00
Alexis Laferrière
6c55bb6b4b Sema: ignore inconsistent bare imports from a file generated by Xcode
In Swift 5 mode, the compiler reports when two files import the same
module, where one file has a bare import and the other an access-level
import. This avoids unintentionally promoting the import to public when
preexisting code marked it otherwise.

Exempt a file generated by Xcode from this check as it's not user
modifiable.

In the future, we should identify such generated files properly. Either
with a new attribute or in the way they are passed to the compiler.

rdar://122032472
2024-02-12 11:09:38 -08:00
Rintaro Ishizaki
b839718351 [AST] Use scoped enum for attribute kinds
Align with other kind enum e.g. DeclKind.
2024-02-02 09:36:48 -08:00
Allan Shortlidge
e056c73fb1 AST: Cache underlying clang module during import resolution.
As recommended in feedback on https://github.com/apple/swift/pull/71302, cache
the underlying clang module after loading it in `ImportResolver`, rather than
filtering it out of the overall set of resolved imports. This is more efficient
and results in less duplicated code that must identify the underlying clang
module.
2024-02-01 17:00:11 -08:00
Alexis Laferrière
cd32aae59d Sema: exempt some imports from the warning on ioi use from non-resilient module
The correct use @_implementationOnly from a non-resilient module is not
enforced by the compiler at this time. Using that configuration can
cause memory corruption at runtime if it affects the memory layout of
types, and it can lead to compiler crashes at compilation in general.

Some modules rely on this configuration with a very limited use for it.
We can grandfather them in its use and silence the warning on specific
import edges.
2024-01-11 12:57:22 -08:00
Alexis Laferrière
8efd30ec56 Merge pull request #70675 from xymus/access-level-import-same-file 2024-01-03 12:12:03 -08:00
Alexis Laferrière
73904ec12f Sema: report inconsistent access levels on imports of one module from one file
Warn on imports of the same target module from the same source file when
they have different access-levels. This situation can lead to unexpected
behavior as the compiler will only take into account the most permissive
import.

In the following example, the module Foundation in functionally imported
as public. The `internal` modifier will be ignored. The new diagnostic
reports it as such.

```
public import Foundation
internal import Foundation // warning: module 'Foundation' is imported
as 'public' from the same file; this 'internal' access level will be
ignored
```

This could be an easy mistake to do when using scoped imports where this
diagnostic will also help.

rdar://117855481
2024-01-03 09:04:49 -08:00
Ellie Shin
e5ca8e5c0b Allow loading package interface if in same package.
Add a new flag to enable package interface loading.
Use the last value of package-name in case of dupes.
Rename PrintInterfaceContentMode as InterfaceMode.
Update diagnostics.
Test package interface loading with various scenarios.
Test duplicate package-name.
2023-11-09 18:44:06 -08:00
Artem Chikin
9c58b9f5f5 [Dependency Scanning] Detect and ignore (and warn about) tautological imports
This matches the current behavior in `ImportResolution`. The change refactors an existing utility function to do this check from `UnboundImport` to a common utility used now also in the scanner.
2023-11-01 06:46:42 -07:00
swift-ci
93ba352fec Merge remote-tracking branch 'origin/main' into rebranch 2023-10-11 22:34:43 -07:00
Alexis Laferrière
7b1bc0050f [Sema] Update diagnostic about public imports of non-resilient modules
The compiler reports public imports of non-resilient modules from a
resilient module. Make sure that when imports default to internal, which
also implies Swift 6, this is treated as an error and the fixit simply
deletes the `public` keyword.

We keep using the AccessLevelOnImport flag only to report these as
errors in Swift 5 for early adopters.
2023-10-10 09:09:22 -07:00
swift-ci
2e9555437f Merge remote-tracking branch 'origin/main' into rebranch 2023-09-29 20:19:54 -07:00
Alexis Laferrière
8dcfadd546 [Sema] Track the access level modifier on import full range 2023-09-27 22:30:28 -07:00
swift-ci
7fc36edb98 Merge remote-tracking branch 'origin/main' into rebranch 2023-09-27 09:34:12 -07:00
Alexis Laferrière
291b09e6b8 [Sema] Add sourceloc to error on importing a package module from swiftinterface
Importing a package module from swiftinterface loses all package decls
and is reported as an error. Make the error point to the import.
2023-09-26 13:50:16 -07:00
Evan Wilde
11fcbbeea2 Cleaning up more deprecation warnings
Pointer `llvm/Support/Host.h` at `llvm/TargetParser/Host.h`.
Replacing deprecated API `startswith_insensitive` with replacement
`starts_with_insensitive`.
2023-08-10 16:19:24 -07:00
Evan Wilde
309aed4925 Add SmallSetVector replacement
llvm::SmallSetVector changed semantics
(https://reviews.llvm.org/D152497) resulting in build failures in Swift.
The old semantics allowed usage of types that did not have an
`operator==` because `SmallDenseSet` uses `DenseSetInfo<T>::isEqual` to
determine equality. The new implementation switched to using
`std::find`, which internally uses `operator==`. This type is used
pretty frequently with `swift::Type`, which intentionally deletes
`operator==` as it is not the canonical type and therefore cannot be
compared in normal circumstances.

This patch adds a new type-alias to the Swift namespace that provides
the old semantic behavior for `SmallSetVector`. I've also gone through
and replaced usages of `llvm::SmallSetVector` with the
`Swift::SmallSetVector` in places where we're storing a type that
doesn't implement or explicitly deletes `operator==`. The changes to
`llvm::SmallSetVector` should improve compile-time performance, so I
left the `llvm::SmallSetVector` where possible.
2023-07-25 12:28:27 -07:00
Becca Royal-Gordon
1b81c3de7b [NFC] Adopt ValueDecl * for decl_declared_here 2023-07-19 13:08:12 -07:00
Alexis Laferrière
fabb345624 [Sema] Warn on uses of @_implementationOnly imports without library-evolution
@_implementationOnly was designed for use from resilient modules only,
using it from non-resilient modules in unsupported. This change adds a
warning about it.

If anyone hits this warning, they should either enable library-evolution
or consider adopting the new `internal import` when it is available as
it handles this scenario properly.

What leads to a crash: an @_implementationOnly import fully hides the
dependency from indirect clients. This can lead to the compiler being
unaware of some internal details of a non-resilient module when building
a client against it. In turn this may lead to run time crashes from
miscompilation.

In general one could still use @_implementationOnly in a non-resilient
modules as long as it's referenced only from function bodies. However,
references from even non-public properties does lead to important memory
layout information being hidden from clients of the module.

rdar://78129903
2023-07-05 16:20:42 -07:00
Alexis Laferrière
671cc805a8 [Sema] Refactor validateResilience to prepare for one more check 2023-07-05 16:20:42 -07:00