Commit Graph

1035 Commits

Author SHA1 Message Date
Michael Gottesman
cd38b78466 [swift-settings] Provide a default nullptr for a variable.
Just noticed it as I was reading some code. Even though it cannot happen today,
with code being added, we could allow for undefined behavior. Better to program
defensively than aggressively.
2025-03-17 13:17:20 -07:00
Rintaro Ishizaki
f3191c8068 [AST] Ignore UnvalidatedDeclsWithOpaqueReturnTypes in skipped functions 2025-03-16 17:41:21 -07:00
Allan Shortlidge
151c9dee39 AST: Consolidate abstract/contrete syntax decl lookup functions. 2025-03-15 23:47:29 -07:00
Allan Shortlidge
017dae382e ClangImporter: Look up availability domains defined in bridging headers.
This is very brittle in this first iteration. For now we require the
declaration representing the availability domain be deserialized before it can
be looked up by name since Clang does not have a lookup table for availabilty
domains in its module representation. As a result, it only works for bridging
headers that are not precompiled.

Part of rdar://138441266.
2025-03-15 07:44:37 -07:00
Tony Allevato
0844e274b0 Ensure that #fileID wraps raw identifier module names in backticks.
`#fileID` never accounted for the possibility that someone one have
a module alias _itself_, so it always generated the module's real
(physical) name. This _technically_ changes the behavior of `#fileID`
for self-aliased modules, but since nobody would have ever had a reason
to do that before raw identifiers, it's unlikely that this change would
affect anyone in practice.
2025-03-11 17:18:44 -04:00
Michael Gottesman
f64dd5a8d5 [concurrency] Add initial support for SwiftSettings to control defaultIsolation at the file level.
We introduce a new macro called #SwiftSettings that can be used in conjunction
with a new stdlib type called SwiftSetting to control the default isolation at
the file level. It overrides the current default isolation whether it is the
current nonisolated state or main actor (when -enable-experimental-feature
UnspecifiedMeansMainActorIsolated is set).
2025-03-10 17:33:45 -07:00
John Hui
bdf22948ce [cxx-interop] Allow Swift to access non-public C++ members (#79093)
This patch introduces an a C++ class annotation, SWIFT_PRIVATE_FILEID,
which will specify where Swift extensions of that class will be allowed
to access its non-public members, e.g.:

    class SWIFT_PRIVATE_FILEID("MyModule/MyFile.swift") Foo { ... };

The goal of this feature is to help C++ developers incrementally migrate
the implementation of their C++ classes to Swift, without breaking
encapsulation and indiscriminately exposing those classes' private and
protected fields.

As an implementation detail of this feature, this patch introduces an
abstraction for file ID strings, FileIDStr, which represent a parsed pair
of module name/file name.

rdar://137764620
2025-02-18 11:22:44 -08:00
Allan Shortlidge
770ddad742 AST/Sema: Look up custom availability domains by name.
Store the domain definitions defined on the command line on the `ModuleDecl` for
the main module.
2025-01-31 17:31:13 -08:00
Alexis Laferrière
63c2979e13 Merge pull request #78471 from xymus/fix-accessor-is-spi
Sema: Accessors's SPI are tied to the attributes on the storage
2025-01-31 08:59:43 -08:00
Anthony Latsis
34f9b80cbc Merge pull request #78750 from AnthonyLatsis/oryza-sativa
[Gardening] Fix some set but not used variables
2025-01-31 04:29:05 +00:00
Anthony Latsis
a84dfc8387 [Gardening] Fix some set but not used variables 2025-01-30 21:34:38 +00:00
Ellie Shin
2de333c9e0 Merge pull request #79035 from swiftlang/elsh/pcmo-imports
Package CMO: Enable serializing decls imported with `@_spiOnly` or `package import`.
2025-01-30 13:15:33 -08:00
QuietMisdreavus
ab26b8b9d7 add support to getTopLevelDecls for clang submodules (#76401)
rdar://126031510
2025-01-30 09:39:58 -07:00
elsh
67594f1d8b Package CMO: Enable serializing decls imported with @_spiOnly or package import.
Starting in Swift 6.0, `package` access level and `@_spiOnly` attribute have been increasingly used in import statements.
However, existing import filtering prevented serialization of package APIs that included such decls, leading to a
significant drop in overall serialization. This PR removes these restrictive filters, and allows decls from SDK or system
modules to be included in serialization.

rdar://130788606
2025-01-29 23:22:17 -08:00
Alexis Laferrière
aa75341b20 Sema: Accessors's SPI are tied to the attributes on the storage
Recent changes started using SPIGroupRequest on accessors specifically
to verify access to the wrappedValue of PropertyWrappers within the
direct access logic on variables using the property wrapper. Update
SPIGroupRequest to support this request and the type-checking logic to
accept the @_spi attribute on internal usable from inline accessors.

rdar://141964200
2025-01-13 17:02:40 -08:00
Allan Shortlidge
d0f63a0753 AST: Split Availability.h into multiple headers.
Put AvailabilityRange into its own header with very few dependencies so that it
can be included freely in other headers that need to use it as a complete type.

NFC.
2025-01-03 18:36:04 -08:00
Doug Gregor
d593442cc4 Add module trace information for strict memory safety
Extend the module trace format with a field indicating whether a given
module, or any module it depends on, was compiled with strict memory
safety enabled. This separate output from the compiler can be used as
part of an audit to determine what parts of Swift programs are built
with strict memory safety checking enabled.
2024-12-24 12:27:35 -08:00
Doug Gregor
f57217f8dd Merge pull request #78307 from DougGregor/strict-safety-diags
Improve and collate diagnostics for uses of unsafe constructs in declarations
2024-12-20 14:53:59 -08:00
Doug Gregor
29f23bb66a Improve diagnostics for uses of unsafe declarations in functions
Instead of producing a warning for each use of an unsafe entity,
collect all of the uses of unsafe constructs within a given function
and batch them together in a single diagnostic at the function level
that tells you what you can do (add `@unsafe` or `@safe(unchecked)`,
depending on whether all unsafe uses were in the definition), plus
notes identifying every unsafe use within that declaration. The new
diagnostic renderer nicely collects together in a single snippet, so
it's easier to reason about.

Here's an example from the embedded runtime that previously would have
been 6 separate warnings, each with 1-2 notes:

```
swift/stdlib/public/core/EmbeddedRuntime.swift:397:13: warning: global function 'swift_retainCount' involves unsafe code; use '@safe(unchecked)' to assert that the code is memory-safe
395 |
396 | @_cdecl("swift_retainCount")
397 | public func swift_retainCount(object: Builtin.RawPointer) -> Int {
    |             `- warning: global function 'swift_retainCount' involves unsafe code; use '@safe(unchecked)' to assert that the code is memory-safe
398 |   if !isValidPointerForNativeRetain(object: object) { return 0 }
399 |   let o = UnsafeMutablePointer<HeapObject>(object)
    |           |                              `- note: call to unsafe initializer 'init(_:)'
    |           `- note: reference to unsafe generic struct 'UnsafeMutablePointer'
400 |   let refcount = refcountPointer(for: o)
    |                  |                    `- note: reference to let 'o' involves unsafe type 'UnsafeMutablePointer<HeapObject>'
    |                  `- note: call to global function 'refcountPointer(for:)' involves unsafe type 'UnsafeMutablePointer<Int>'
401 |   return loadAcquire(refcount) & HeapObject.refcountMask
    |          |           `- note: reference to let 'refcount' involves unsafe type 'UnsafeMutablePointer<Int>'
    |          `- note: call to global function 'loadAcquire' involves unsafe type 'UnsafeMutablePointer<Int>'
402 | }
403 |
```

Note that we have lost a little bit of information, because we no
longer produce "unsafe declaration was here" notes pointing back at
things like `UnsafeMutablePointer` or `recountPointer(for:)`. However,
strict memory safety tends to be noisy to turn on, so it's worth
losing a little bit of easily-recovered information to gain some
brevity.
2024-12-20 07:34:51 -08:00
Becca Royal-Gordon
08e2a4ddae Type check ABI decls
Sema now type-checks the alternate ABI-providing decls inside of @abi attributes.

Making this work—particularly, making redeclaration checking work—required making name lookup aware of ABI decls. Name lookup now evaluates both API-providing and ABI-providing declarations. In most cases, it will filter ABI-only decls out unless a specific flag is passed, in which case it will filter API-only decls out instead. Calls that simply retrieve a list of declarations, like `IterableDeclContext::getMembers()` and friends, typically only return API-providing decls; you have to access the ABI-providing ones through those.

As part of that work, I have also added some basic compiler interfaces for working with the API-providing and ABI-providing variants. `ABIRole` encodes whether a declaration provides only API, only ABI, or both, and `ABIRoleInfo` combines that with a pointer to the counterpart providing the other role (for a declaration that provides both, that’ll just be a pointer to `this`).

Decl checking of behavior specific to @abi will come in a future commit.

Note that this probably doesn’t properly exercise some of the new code (ASTScope::lookupEnclosingABIAttributeScope(), for instance); I expect that to happen only once we can rename types using an @abi attribute, since that will create distinguishable behavior differences when resolving TypeReprs in other @abi attributes.
2024-12-19 15:49:34 -08:00
Rintaro Ishizaki
71ca3e129a [ASTGen] Use syntax tree based interface hash 2024-12-12 09:51:09 -08:00
Alexis Laferrière
3d9dc81f5b Merge pull request #78005 from xymus/access-level-import-reexports
Sema: Prioritize @_exported imports over local non-public imports
2024-12-09 13:35:44 -08:00
Alexis Laferrière
c9d0848163 Sema: Prioritize @_exported imports over local non-public imports
Update the logic selecting the most restrictive import for a given
reference to account for @_exported imports from the local module. We
should always prioritize @_exported imports from the local module over
more restrictive same file imports. Only if an import from the same file
is also public we prefer it as it's more useful for diagnostics and
generally recommended to locally declare dependencies.

Also update the test that was meant to check this configuration to apply
two different variations, one for a module local @_exported and one
relying on the underlying clang module.

rdar://140924031
2024-12-06 09:31:04 -08:00
Hamish Knight
4946c799af [AST] Remove ModuleDecl::addFile
Rather than exposing an `addFile` member on
ModuleDecl, have the `create` members take a
lambda that populates the files for the module.
Once module construction has finished, the files
are immutable.
2024-11-17 14:17:20 +00:00
Doug Gregor
24a12ebc34 Renable GeneratedSourceInfo::Attribute to GeneratedSourceInfo::AttributeFromClang 2024-11-15 09:02:49 -08:00
Doug Gregor
989c73d014 Ensure that buffers containing Clang swift_attr attributes are parsed as attributes
Previously, they were being parsed as top-level code, which would cause
errors because there are no definitions. Introduce a new
GeneratedSourceInfo kind to mark the purpose of these buffers so the
parser can handle them appropriately.
2024-11-13 21:19:37 -08:00
Allan Shortlidge
239720897a AST: Rename TypeRefinementContext to AvailabilityScope. 2024-11-12 11:34:25 -08:00
Allan Shortlidge
d6442504af NFC: Resolve a -Wunused-lambda-capture warning. 2024-11-12 11:34:25 -08:00
Doug Gregor
cf68d28400 Merge pull request #76951 from kovdan01/issue60102
[AutoDiff] Enhance performance of custom derivatives lookup
2024-11-11 14:55:54 -08:00
Rintaro Ishizaki
e566a746c8 [ASTGen] Support macro expanded buffer
* Make ExportedSourceFile hold any Syntax as the root node
* Move `ExportedSourceFileRequest::evaluate()` to `ParseRequests.cpp`
* Pass  the decl context and `GeneatedSourceFileInfo::Kind` to
  `swift_ASTGen_parseSourceFile()` to customize the parsing
* Make `ExportedSourceFile` to hold an arbitrary Syntax node
* Move round-trip checking into `ExportedSourceFileRequest::evaluate()`
* Split `parseSourceFileViaASTGen` completely from C++ parsing logic
  (in `ParseSourceFileRequest::evaluate()`)
* Remove 'ParserDiagnostics' experimental feature: Now that we have
  ParserASTGen mode which includes the swift-syntax parser diagnostics.
2024-11-05 11:00:33 -08:00
Daniil Kovalev
0d7e37e4ec [AutoDiff] Enhance performance of custom derivatives lookup
In #58965, lookup for custom derivatives in non-primary source files was
introduced. It required triggering delayed members parsing of nominal types in
a file if the file was compiled with differential programming enabled.

This patch introduces `CustomDerivativesRequest` to address the issue.
We only parse delayed members if tokens `@` and `derivative` appear
together inside skipped nominal type body (similar to how member operators
are handled).

Resolves #60102
2024-10-29 12:45:14 +03:00
Alex Hoppen
a287e2ae8e [SourceKit] Check if the realpath of a module is inside the SDK to decide if it's system
On Windows, we run into the following situation when running SourceKit-LSP tests:
- The SDK is located at `S:\Program Files\Swift\Platforms\Windows.platform\Developer\SDKs\Windows.sdk` with `S:` being a substitution drive
- We find `Swift.swiftmodule` at `S:\Program Files\Swift\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows\Swift.swiftmodule`
- Now, to check if `Swift.swiftmodule` is a system module, we take the realpath of the SDK, which resolves the substitution drive an results in something like `C:\Users\alex\src\Program Files\Swift\Platforms\Windows.platform\Developer\SDKs\Windows.sdk`
- Since we don’t take the realpath of `Swift.swiftmodule`, we will assume that it’s not in the SDK, because the SDK’s path is on `C:` while `Swift.swiftmodule` lives on `S:`

To fix this, we also need to check if a module’s real path is inside the SDK.

Fixes swiftlang/sourcekit-lsp#1770
rdar://138210224
2024-10-19 10:35:09 -07:00
Allan Shortlidge
07b84fccfb AST: Introduce ModuleDecl::isClangHeaderImportModule() convenience. 2024-09-27 12:00:03 -07:00
Steven Wu
e0541b0357 [Macro][Dependencies] Properly model macro dependencies in the scanner
Add function to handle all macro dependencies kinds in the scanner,
including taking care of the macro definitions in the module interface
for its client to use. The change involves:
  * Encode the macro definition inside the binary module
  * Resolve macro modules in the dependencies scanners, including those
    declared inside the dependency modules.
  * Propagate the macro defined from the direct dependencies to track
    all the potentially available modules inside a module compilation.
2024-09-19 16:41:53 -07:00
Tony Allevato
4bb9a587fa Merge pull request #76456 from allevato/system-module-check
[AST] Make `IsNonUserModuleRequest` consider `SourceFile` inputs as well.
2024-09-18 19:36:18 -04:00
Doug Gregor
8febd3fb32 Reimplement ModuleDecl::getSourceFileContainingLocation() using SourceManager
ModuleDecl kept track of all of the source files in the module so that it
could find the source file containing a given location, which relied on
a sorted array all of these source files. SourceManager has its own
similar data structure for a similar query mapping the locations to
buffer IDs.

Replace ModuleDecl's dats structure with a use of the SourceManager's version
with the mapping from buffer IDs to source files.
2024-09-18 07:45:50 -07:00
Doug Gregor
6f88c228df Maintain a buffer ID -> source file(s) mapping in the source manager
Now that every source file has a buffer ID, introduce the reverse mapping
so clients can find the source file(s) in their module that reference
that buffer ID.
2024-09-17 14:01:58 -07:00
Doug Gregor
0bd519599f Silence warning about mismatched signs 2024-09-17 13:16:15 -07:00
Doug Gregor
031be421d1 Ensure that we don't get a very-likely-wrong buffer ID
-1 was once used as a sentinel value for "no buffer"; make sure it
doesn't persist anywhere.
2024-09-17 07:41:02 -07:00
Tony Allevato
67d9eecd50 [AST] Make IsNonUserModuleRequest consider SourceFile inputs as well.
We're using a small custom frontend tool to generate indexstore data for `.swiftinterface` files in the SDKs. We do this by treating the `.swiftinterface` file as the input of an interface compilation, but this exits early because it treats it as a `SourceFile` instead of an external `LoadedFile`. This happens even if we call `setIsSystemModule(true)` unless we skip setting the SDK path, but that causes other problems. It seems harmless to check for `SourceFile`s as well, so that a tool processing an SDK interface as a direct input still gets the right state.
2024-09-17 09:44:42 -04:00
Doug Gregor
49aa0e966f Ensure that SourceFiles always have a backing buffer in the SourceManager
The "buffer ID" in a SourceFile, which is used to find the source file's
contents in the SourceManager, has always been optional. However, the
effectively every SourceFile actually does have a buffer ID, and the
vast majority of accesses to this information dereference the optional
without checking.

Update the handful of call sites that provided `nullopt` as the buffer
ID to provide a proper buffer instead. These were mostly unit tests
and testing programs, with a few places that passed a never-empty
optional through to the SourceFile constructor.

Then, remove optionality from the representation and accessors. It is
now the case that every SourceFile has a buffer ID, simplying a bunch
of code.
2024-09-16 21:46:42 -07:00
Alexis Laferrière
f9a8a674c6 Sema: fix getPublicModuleName to look only at loaded modules
When onlyIfImported is true, we should return the public module name
only when the public facing module is already imported. Replace the
call to getModuleByIdentifier with getLoadedModule to prevent trigering
loading that module.

Also fix the test where the CHECK lined ended up matching itself from
the diagnostics output.
2024-09-11 13:12:58 -07:00
Alexis Laferrière
9eaa0e5df8 Sema: getImportAccessLevel returns an authoritative import
Change how we pick the one import to point to in diagnostics about a
referenced decl. This mostly affects the warning about superfluously
public imports. This warning encourages the developer to delete imports,
let's make sure we push them towards deleting the right ones.

The order was previously not well defined, except that we always picked
one of the most public imports.

We now prioritize imports in this order:
1. The most public import. (Preserving the current behavior in
   type-checking of access-level on imports)
2. The import of the public version of the module defining the decl,
   determined via export_as or -public-module-name.
3. The import of the module defining the decl.
4. The first import in the sources bringing the decl via reexports.
5. Any other import, usually via an @_exported import in a different file.

rdar://135357155
2024-09-09 16:57:46 -07:00
Alexis Laferrière
8d28ed4fa9 Merge pull request #76269 from xymus/public-module-name
Diagnostics: Intro the public module name concept to hide support modules from clients
2024-09-09 16:57:12 -07:00
Alexis Laferrière
c60fea3aac Sema: Use clearer param name in getPublicModuleName
Use onlyIfImported instead of mustBeVisible.
2024-09-05 12:45:13 -07:00
Alexis Laferrière
214da85183 Sema: Keep public module name in ModuleDecl 2024-09-04 16:20:12 -07:00
Augusto Noronha
141c96fee1 Account for multiple modules when looking up the DeclContext of a type
When looking up the decl context of a type, ASTDemangler has to take
into account that there are multiple different modules where that type
could've come from. This is due to two facts:

- Thanks to the `-module-abi-name` flag, multiple modules can share
the same ABI name (which is the module name that is usually used when
mangling a type).
- In some situations mangling can use the module's real name, for
example, when mangling for the debugger or USRs coupled with @_originallyDefinedIn.

rdar://134095412
2024-09-03 16:44:41 -07:00
Hamish Knight
940f5f7c2a [AST] Treat platform modules as non-user
Modules defined within the SDK are considered
non-user modules, extend this to any module found
within the parent platform directory if there is
one. This ensures we include modules such as
XCTest and Testing.

rdar://131854240
2024-08-23 14:23:37 +01:00
Daniel Grumberg
3b093d7938 Avoid recursing into non swift modules when collecting exported imports (#75619)
rdar://132593532
2024-08-13 09:05:50 -06:00
Egor Zhdan
bfe72b4be9 Merge pull request #75589 from swiftlang/egorzhdan/linux-libcxx-interop
[cxx-interop] Allow compiling with libc++ on Linux
2024-08-09 13:42:29 +01:00