Commit Graph

6411 Commits

Author SHA1 Message Date
Aidan Hall
63da6499eb LifetimeDependence: Disable indices in Swift lifetime dependencies
Lifetime indices are never necessary in Swift, they unnecessarily expose
implementation details, and they make lifetime annotations more error-prone,
since they may need to be updated if a function's parameter list changes.

The Swift Syntax parser also cannot handle lifetime annotations where the target
is an index. The main reason the C++ parser supports them is because it is also
used for SIL.
2026-02-06 10:27:01 +00:00
Aidan Hall
7b9db38984 LifetimeDependence: Support function types
To a function type's lifetimes, a base version of the type is first created with
no lifetime dependence info. This is then passed to the dependence checker, and
the resulting dependencies are added to it.

It would be possible to do this analysis by passing just the parameter list and
result type (which are available before the type is created), but this approach
lets us avoid dealing with a header inclusion cycle between Types.h, ExtInfo.h,
and LifetimeDependence.h, since it does not require AnyFunctionType::Param to be
defined in LifetimeDependence.h.
2026-02-05 14:50:27 +00:00
Slava Pestov
437589d1b6 Merge pull request #86950 from slavapestov/remove-more-solver-hacks
Sema: Add flag to disable more solver performance hacks
2026-02-04 08:13:31 -05:00
Meghana Gupta
27bd05b70b Merge pull request #86948 from meg-gupta/trivialresultconv
Fixes for borrow accessors
2026-02-04 01:15:37 -08:00
Slava Pestov
1b918ded02 Merge pull request #86163 from youngbash88/fix-54030
[Sema] Move anonymous closure argument check from parser to semantic analysis
2026-02-04 02:03:50 -05:00
Slava Pestov
9623b85b7b Parse: Remove last remnants of 'operator designated types' 2026-02-03 16:34:10 -05:00
Meghana Gupta
e27a94af40 Diagnose when a borrow/mutate accessor is defined along with other accessors 2026-02-01 17:39:38 -08:00
elsa
5e9f215f31 Merge pull request #86010 from elsakeirouz/rework-for-each-desugar
Rework ForEachStmt Desugaring
2026-01-24 13:55:51 +00:00
Elsa Keirouz
d54a572f7f [Sema] desugar ForEachStmt at AST level 2026-01-23 15:17:29 +00:00
Tim Kientzle
1594ad5c70 Merge pull request #86688 from tbkka/tbkka-yielding_borrow-Part3
Preparation for switching .swiftinterface to `yielding` terminology
2026-01-22 20:12:30 -08:00
Tim Kientzle
1c2bd08c77 Preparation for switching .swiftinterface to yielding terminology
For now, we write `read`/`modify` to .swiftinterface files
so they can be read by the draft implementation in current
compilers.  Here are some of the issues:

* We _cannot_ support `read`/`modify` in Swift sources without
  the user specifying a flag. That's because the idiom below occurs
  in real code, and would be broken by such support.  So when
  we enable the `CoroutineAccessors` flag by default, we _must_
  not support `read`/`modify` as accessor notations in source.

```
struct XYZ {
  // `read` method that takes a closure
  func read(_ closure: () -> ()) { ... }

  // getter that uses the above closure
  var foo: Type {
    read { ... closure ... }
  }
}
```

* .swiftinterface files don't have the above problem.
  Accessor bodies aren't stored at all by default, and
  when inlineable, we always write explicit `get`.
  So we can continue to accept `read`/`modify` notation
  in interface files.

So our strategy here is:

* We'll accept both `read`/`modify` and `yielding borrow`/`yielding mutate`
  in interface files for a lengthy transition period.

* We'll write `read`/`modify` to swiftinterface files for
  a little longer, then switch to `yielding borrow`/`yielding mutate`.

* We'll disable `read`/`modify` support in source files
  when we enable `CoroutineAccessors` by default.
  (We can't even diagnose, due to the above idiom.)

This means that early adopters will have to update their sources
to use the new terminology.  However, swiftinterface files
will be exchangeable between the new and old compilers for a little
while.
2026-01-21 07:01:46 -08:00
Meghana Gupta
5676bfc46f Add Parser support for borrow and mutate protocol constraints 2026-01-20 10:33:23 -08:00
Ian Anderson
60f2c405d0 Merge pull request #86309 from ian-twilightcoder/system-prefix
[Frontend][Darwin] Use the system prefix from SDK when constructing the default search paths
2026-01-10 00:03:58 -08:00
Slava Pestov
aae77c16bb Merge pull request #86412 from slavapestov/shrink-smallvectors
Shrink obscenely large SmallVectors
2026-01-09 20:15:36 -05:00
Ian Anderson
48ab4b0595 [Frontend][Darwin] Use the system prefix from SDK when constructing the default search paths
Some Darwin platforms like DriverKit use a system prefix on all of their search paths. Even though DriverKit isn't supported, add support to get the system prefix from SDKSettings when constructing the default search paths.

This requires the DarwinSDKInfo to be gotten earlier in CompilerInvocation, pass that down to ASTContext through CompilerInstance.

-platform-availability-inheritance-map-path is no longer needed to support visionOS in tests, remove that and its supporting code that gets an alternative DarwinSDKInfo.

rdar://166277280
2026-01-09 12:41:32 -08:00
Slava Pestov
b1591356ea Parse: Fix obscenely large SmallVectors 2026-01-09 09:31:57 -05:00
Tim Kientzle
a0125d4657 Fix @derivative(of:) handling
This implements two approaches for specifying derivatives of
yielding mutate and borrow accessors:

1. Using backticks to specify a yielding accessor:
```
  // expected-note @+1 {{cannot register derivative for yielding borrow accessor}}
  var computedProperty2: T {
    yielding borrow { yield x }
    yielding mutate { yield &x }
  }

  // expected-error @+1 {{referenced declaration 'computedProperty2' could not be resolved}}
  @derivative(of: computedProperty2.`yielding borrow`)
  mutating func vjpPropertyYieldingBorrow(_ newValue: T) -> (
    value: (), pullback: (inout TangentVector) -> T.TangentVector
  ) { ...  }
```
This requires it to be spelled with exactly one space.

2. Use .borrow or .mutate and resolve in Sema:
```
  // expected-note @+1 {{cannot register derivative for yielding borrow accessor}}
  var computedProperty2: T {
    yielding borrow { yield x }
    yielding mutate { yield &x }
  }

  // expected-error @+1 {{referenced declaration 'computedProperty2' could not be resolved}}
  @derivative(of: computedProperty2.borrow)
  mutating func vjpPropertyYieldingBorrow(_ newValue: T) -> (
    value: (), pullback: (inout TangentVector) -> T.TangentVector
  ) { ...  }
```

In order to support the latter, I've had to refactor the
resolution for these names so that error messages can show
the type (e.g., "yielding borrow") of the actual resolved
accessor, even if that's different from the specification.
2026-01-05 16:42:08 -08:00
Tim Kientzle
7fb6fedf6b Fix indentation 2026-01-04 08:50:43 -08:00
Tim Kientzle
8eabeeb8ca [SE-0474] Read2/Modify2 => YieldingBorrow/YieldingMutate
This updates a large number of internal symbols, function names,
and types to match the final approved terminology.  Matching the
surface language terminology and the compiler internals should
make the code easier for people to understand into the future.
2026-01-03 16:05:12 -08:00
Tim Kientzle
104dba920b [SE-0474] Implement yielding borrow and yielding mutate syntax
This does not rename all the internal variables, functions, and types
whose names were based on the old syntax.

I think it adds new syntax support everywhere it's needed while
retaining enough of the old syntax support that early adopters will
see nice deprecation messages guiding them to the new syntax.
2026-01-03 15:07:10 -08:00
bashir
176229aeb4 [Sema] Move anonymous closure argument check from parser to semantic analysis 2025-12-22 01:35:04 +02:00
Meghana Gupta
5c4ce2f942 Merge pull request #86023 from meg-gupta/miscborrowfixes
Minor fixes to borrow accessors
2025-12-16 20:03:19 -08:00
Rintaro Ishizaki
3de0ae6270 Merge pull request #85927 from rintaro/parser-comment-retention 2025-12-15 06:14:00 -08:00
Meghana Gupta
20b23d631c Diagnose borrow/mutate accessors in enums
We don't have support for borrowing switch on Copyable types.
It is supported for ~Copyable types, but the return expression emission for borrow accessors is not yet implemented.
Diagnose instead of crashing the compiler.
2025-12-12 11:38:37 -08:00
Artem Chikin
0a4d352844 De-register temporary buffer 'SourceFile's created during availability attribute argument parsing
Otherwise these source file objects linger in the source manager but they refer to a fully-temporary 'ASTContext' which does not exist after parsing is complete, which means they cannot be used in any way and attempting to do so would lead to a crash.
2025-12-12 10:14:21 -08:00
Artem Chikin
8e97cb4d8d Implement support for unified warning group behavior queries per-diagnostic
Unified across module-wide configuration flags (`-Wwarning`, `-Werror`, etc.) and syntactic configuration attribute `@warn`.
2025-12-12 10:14:20 -08:00
Artem Chikin
e593fd97a0 Add parsing for a declaration attribute '@warn' for source-level warning group behavior control 2025-12-12 10:14:14 -08:00
Rintaro Ishizaki
dffd88ee51 [Parser] Eliminate 'CommentRetentionMode::None' in Lexer
Lexer should always set `Token.CommentLength` correctly because it
necessary for restoring to the token position with comments.
Otherwise, 'Token::isAtStartOfLine()' might not correctly set.
2025-12-10 10:10:51 -08:00
Hamish Knight
0068438385 [Parse] Fix buffer overrun in advanceIfMultilineDelimiter
Make sure we don't scan off the end of the buffer, and scan for the
delimiter before attempting the lookahead.
2025-12-07 20:05:43 +00:00
Anthony Latsis
153dd02cd8 Merge pull request #85833 from swiftlang/jepa-main
[NFC] "SwiftVersion" → "LanguageMode" in `DiagnosticEngine::warnUntilSwiftVersion`, etc.
2025-12-05 09:34:30 +00:00
Anthony Latsis
88220a33c3 [NFC] "SwiftVersion" → "LanguageMode" in DiagnosticEngine::warnUntilSwiftVersion, etc. 2025-12-04 15:11:07 +00:00
Anthony Latsis
8572b7e38c Address llvm::StringSwitch deprecations in advance
There's a whole bunch of these, e.g.
- https://github.com/llvm/llvm-project/pull/163405
- https://github.com/llvm/llvm-project/pull/164276
- https://github.com/llvm/llvm-project/pull/165119
- https://github.com/llvm/llvm-project/pull/166016
- https://github.com/llvm/llvm-project/pull/166066
2025-12-02 17:13:17 +00:00
Doug Gregor
020b69d4b6 [SE-0497] Implement @export attribute syntax
Implement the @export(implementation) and @export(interface) attributes
to replace @_alwaysEmitIntoClient and @_neverEmitIntoClient. Provide a
warning + Fix-It to start staging out the very-new
@_neverEmitIntoClient. We'll hold off on pushing folks toward
@_alwaysEmitIntoClient for a little longer.
2025-11-07 22:00:40 -08:00
Doug Gregor
5b642f548f Extend @_extern to global and static variables
Allow external declaration of global variables via `@_extern(c)`. Such
variables need to have types represented in C (of course), have only
storage (no accessors), and cannot have initializers. At the SIL
level, we use the SIL asmname attribute to get the appropriate C name.

While here, slightly shore up the `@_extern(c)` checking, which should
fix issue #70776 / rdar://153515764.
2025-11-06 11:09:31 -08:00
Kuba (Brecka) Mracek
b6c29f1ce6 Merge pull request #85136 from kubamracek/section-top-level
SE-0492: Handle top-level `@section`-annotated globals
2025-11-04 11:15:50 -08:00
Becca Royal-Gordon
393965090e Merge pull request #34556 from beccadax/mod-squad-2
[SE-0491] Implement lookup and diagnostics for module selectors (MyMod::someName)
2025-10-28 16:00:26 -07:00
Alexis Laferrière
74ea47a159 Merge pull request #84489 from xymus/c-identifier-only
Parser: Reject `@c` attributes using the string format
2025-10-27 09:17:31 -07:00
Hamish Knight
b5627c9337 Merge pull request #85145 from a7medev/autodiff-comma-fix-its
[Diagnostics] Add missing fix-its for unexpected/expected comma in attribute arguments
2025-10-27 09:08:16 +00:00
Ahmed Mahmoud
d0c2d8b317 [Diagnostics] Add fix-its for unexpected/expected comma in auto-diff attributes 2025-10-26 23:47:19 +03:00
Kuba Mracek
5f81c1b793 SE-0492: Handle top-level @section-annotated globals
Currently, normal globals are represented as a PatternBindingDecl and a VarDecl in the AST, directly under the SourceFile:

```
// var variable_name = 42, compiled with -parse-as-library
(source_file ...
  (pattern_binding_decl ...
    (pattern_entry ...
      (pattern_named ... "variable_name") ...
  (var_decl "variable_name" ...
```

Top-level globals are represented more like local variables, under a TopLevelCodeDecl. Note that the VarDecl is still at the file scope. In SILGen, this case has some special handling to use the a storage of a global variable, and to avoid cleanups (see `emitInitializationForVarDecl`). Effectively, this means the globals are initialized inside the `main` function.

```
// var variable_name = 42, compiled without -parse-as-library
(source_file ...
  (top_level_code_decl ...
    (brace_stmt ...
      (pattern_binding_decl ...
        (pattern_named ... "variable_name") ...
  (var_decl "variable_name" ... top_level_global
```

SE-0492 needs top-level globals that have a `@section` annotation to behave like a normal global -- initialization must happen statically, and not in `main`. This PR changes the parsing of those globals to match normal globals, without the TopLevelCodeDecl wrapper. SILGen and IRGen then handles them correctly.
2025-10-25 10:48:44 -07:00
Becca Royal-Gordon
e6d8b02626 Make module selectors non-experimental
Approved by SE-0491.
2025-10-24 16:23:50 -07:00
Kuba (Brecka) Mracek
eb23d3bc0a Merge pull request #85074 from kubamracek/section
SE-0492: Stabilize @_section/@_used into @section/@used
2025-10-24 12:29:48 -07:00
Alexis Laferrière
7ec1d36215 Parser: Reject @c attributes using the string format
Reject `@c` attributes in the format of `@c("customName")` to accept
only `@c(customName)` and of course the bare `@c`.
2025-10-24 11:55:34 -07:00
Kuba (Brecka) Mracek
66eef684ee Merge pull request #80212 from kubamracek/object-file-format
Add #objectFormat compilation conditional (SE-0492)
2025-10-23 12:58:23 -07:00
Kuba Mracek
2713872bef Handle @_section -> @section rename as a warning only 2025-10-23 08:42:29 -07:00
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
Becca Royal-Gordon
96e6b1d618 Fix malformed arg label error with module selector
The legacy parser has a special case for code like `fn(:)` which corrects it to `fn(_:)`, but the new `::` token was interfering with cases where there were two adjacent colons (e.g. `fn(::)`). Correct this issue.
2025-10-18 03:15:53 -07:00
Becca Royal-Gordon
d8d226c966 Merge pull request #84362 from beccadax/mod-squad-parse
[SE-0491] Parse and ASTGen module selectors
2025-10-17 23:34:11 -07:00
Rintaro Ishizaki
6a1604ba09 [ASTPrinter/Parse] Disambiguate accessor block in .swiftinterface
.swiftinterface sometimes prints a pattern binding initializer and the
accessor block. However the parser doesn't expect such constructs and
the disambiguation from trailing closures would be fragile. To make it
reliable, introduce a disambiguation marker `@_accessorBlock` .
`ASTPrinter` prints it right after `{` only if 1) The accessor block is
for a pattern binding declaration, 2) the decl has an initializer
printed, and 3) the non-observer accessor block is being printed. In the
parser, if the block after an initializer starts with
`{ @_accessorBlock`, it's always parsed as an accessor block instead of
a trailing closure.

rdar://140943107
2025-10-16 22:16:29 -07:00
Kuba Mracek
c3f865be4a Add #objectFormat compilation conditional (SE-0492) 2025-10-16 15:15:27 -07:00