Commit Graph

2520 Commits

Author SHA1 Message Date
Kavon Farvardin 19119ad88a introduce @_preInverseGenerics(except:)
@_preInverseGenerics(except: <inverses>) is an extension of the existing
@_preInverseGenerics attribute that provides selective control over which
inverse requirements are mangled into a declaration's symbol name.

While the bare @_preInverseGenerics strips all inverse constraints
(~Copyable and ~Escapable) from mangling, the 'except:' form allows specific
inverses to be retained. This is needed when a type like Span already had
~Copyable mangled into its ABI-stable symbols and now needs to retroactively
adopt ~Escapable without changing those existing symbols. You can now express
that with `@_preInverseGenerics(except: ~Copyable)` to strip-out every inverse
except ~Copyable to preserve the pre-existing ~Copyable-containing symbols.

It requires the new experimental feature `PreInverseGenericsExcept`.

rdar://176395527
2026-05-14 18:27:00 -07:00
Artem Chikin 866371d73e [Source Warning Control] Rename '@warn' to '@diagnose'
Resolves rdar://173774670
2026-04-29 10:39:25 +01:00
Konrad Malawski 4e4769de49 Rename stale CallerIsolated to NonisolatedNonsending 2026-04-28 09:21:23 -07:00
Rintaro Ishizaki 40795eb5ee [Parse] Speculative fix for crash in parseExternAttribute()
* Don't try to use uninitialized `kind`
* Limit token skipping for finding `)` using `skipUntilDeclRBrace()`

rdar://174704688
2026-04-16 18:49:35 -07:00
Meghana Gupta d6e220669f Enable BorrowAndMutateAccessors by default 2026-03-25 16:52:39 -07:00
Meghana Gupta b8295c2d34 Merge pull request #87688 from meg-gupta/borrowlsg2
Extend borrow accessor support in enums and classes
2026-03-05 20:33:59 -08:00
Meghana Gupta 62b89ec16e Remove blanket restriction of borrow accessors in classes
borrow accessors can be supported in classes when they return 'let' properties, global 'let'.
Remove blanket restriction and add tests for supported cases
2026-03-04 16:03:25 -08:00
Meghana Gupta a067046270 Remove blanket restriction of borrow accessors in enums
borrow accessors on enums are not supported in most common cases.
borrowing switch isn't available for Copyable types and SILGen work to support
borrow accessors with borrowing switch on ~Copyable types has not been done.

However, enums can support borrow accessors that return global let declarations.
Remove blanket restriction and add tests.
2026-03-03 15:44:46 -08:00
Allan Shortlidge c7ce5a8aed Parse: Allow availability macros to contain empty expansions.
In some cases, it's useful to allow an availability macro to expand to an empty
availability specification list. Allow developers to express this using macros
that are defined to exactly the string `*`, while continuing to reject `*` in
availability macros that contain other entries.
2026-03-02 12:14:29 -08:00
Artem Chikin f53ccd656d Merge pull request #86934 from artemcm/LiteralExpressionEnumRawValues
[Literal Expressions] Add support for literal expressions in enum raw values
2026-02-10 22:28:59 +00: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
Artem Chikin 1e5ba5fca8 [Literal Expressions] Add support for literal expressions in enum raw values
Modify relevant portions of the type-checker and parser to allow, when the 'LiteralExpressions' experimental feature is enabled, for arbitrary integer-typed expressions in enum raw value specifiers. These expressions will be type-checked and constant-folded into an integer literal expression, keeping the current interface of 'EnumElementDecl' consistent for clients.

Previously, 'EnumRawValuesRequest' had two different "modes" which were discerned based on typechecking stage (structural | interface), where the former had the request compute all raw values, both user-specified literal expressions and computing increment-derived values as well; the latter would also type-check the user-specified expressions and compute their types.
- With the need to have enum case raw values support arbitrary integer expressions, the request ('EnumRawValuesRequest') has been refactored and simplified to *always* both compute all case raw values and perform type-checking of user-specified raw value expressions. This is done in order to allow the AST-based constant-folding infrastructure ('ConstantFoldExpression' request) to run on the expressions. Constant folding is invoked during the evaluation of 'EnumRawValuesRequest' on all user-specified raw value expressions, in order to be able to compute subsequent increment values and ensure the expressions are foldable. If they are not, i.e. if constant folding fails, a relevant diagnostic will be emitted.
- 'EnumElementDecl' continues to store the raw value expression, which is no longer a 'LiteralExpr' but rather an 'Expr'; however, the getter ('getRawValueExpr') continues to return a 'LiteralExpr' by invoking the constant-folding request on the stored value, which is guaranteed to return a cached result from a prior invocation in 'EnumRawValuesRequest', assuming it succeeded.
- Furthermore, the 'structural' request kind was previously not cached, whereas now because the request must always do the complete type-checking work, it is always cached.

Resolves rdar://168005520
2026-02-10 09:43:07 +00:00
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
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
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
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
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 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
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
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 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 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
Becca Royal-Gordon b6e22cb6f5 Allow :: as an alias for . in scoped imports 2025-10-16 13:30:29 -07:00
Becca Royal-Gordon f82881cc8e Parse module selectors in permitted locations 2025-10-16 13:30:29 -07:00