Commit Graph

22 Commits

Author SHA1 Message Date
Hamish Knight 588a5f1981 [Sema] Fix availability scope source range for SwitchStmt
Ensure we extend the end loc of the range, same as we do for the case
scope. This ensures the case scope is contained by the parent.
2026-06-09 23:49:23 +01:00
Allan Shortlidge e722685790 Merge pull request #89724 from tshortli/switch-stmt-availability-refinement
AST: Refine availability of switch case bodies using enum element availability
2026-06-08 09:02:49 -07:00
Paul Passeron 425cd0ebd7 [experimental] Infrastructure for derived conformances via macros (#89419)
**Overview**:
This PR introduces the basic infrastructure needed to eventually migrate
derived macros generation from hand-crafted AST nodes to macros. No
conformance have been migrated yet.

**Motivation**:
Derived conformances (e.g. `Equatable`, `Hashable`, `Codable`, ...) are
currently implemented as a special case in the compiler, producing
synthetic AST nodes directly. Migrating this to macros will hopefully
unify the code path with the existing macro expansion infrastructure,
make conformance synthesis easier to extend and test as well as reducing
the amount of special cases in the compiler.

**Changes**:
- New experimental feature flag `DeriveConformancesViaMacros`:
Introduces the flag that will eventually gate the new derived
conformance code paths. It does not control any behaviour for the moment
as none have been migrated yet but this enables future changes to be
built incrementally.
- New GeneratedSourceInfo and SourceFile kinds `SyntheticMacro`:
Introduces new GSI and SourceFile kinds named `SyntheticMacro` to
represent macros synthesized by the compiler. Since macros need a real
buffer to expand, this is the kind of source file and GSI associated
with those buffers.
- Conformance derivation via macros API:
Introduces the `deriveRequirementViaMacro` function that produces the
required witness via macro expansion.

See https://github.com/swiftlang/llvm-project/pull/13124 for
llvm-related changes.

**Next steps**:
- Macros do not contain any semantic information, especially regarding
types. Therefore it is necessary to provide them with type information
as an argument so they can eventually derive the conformances. A
separate PR is being created to generate this type information as
strings containing swift-parsable code for easy parsing on the macro
end.
- Implement derived conformance synthesis for individual protocols using
the new infrastructure, like `Equatable` or `Hashable` for starters.
- Wire the experimental flag to gate the new path once an implementation
exists

---------

Co-authored-by: Hamish Knight <hamish_knight@apple.com>
2026-06-08 14:23:58 +01:00
Allan Shortlidge 35bd17fefc AST: Refine availability of switch case bodies using enum element availability.
When a case statement body inside a switch statement is only reachable if a
specific enum element matches the switch subject, the compiler now generates an
availability scope for the case body that matches the availability of the enum
element:

```
@available(macOS 26, *)
func f() -> Int { 0 }

enum E {
  @available(macOS 26, *)
  case a
}

func multiplex(_ e: E) -> Int {
  switch e {
  case .a:
    return f() // OK, .a is only available in macOS 26
  }
}
```

Supporting the same refinement for cases in switch expressions will need to be
handled as a follow-up as it may require changes to the order in which switch
expression case bodies are type checked relative to the case label.

Resolves https://github.com/swiftlang/swift/issues/46662 and rdar://20937722.
2026-06-05 20:15:14 -07:00
Allan Shortlidge 9b7c531fd8 AST/IDE/Sema: Remove unnecessary AvailabilityInference.h includes.
NFC.
2025-10-24 16:14:49 -07:00
Allan Shortlidge aeb5a46d9e AST: Emit warnings for redundant availability checks in custom domains. 2025-07-18 13:27:08 -07:00
Hamish Knight edca7c85ad Adopt ABORT throughout the compiler
Convert a bunch of places where we're dumping to stderr and calling
`abort` over to using `ABORT` such that the message gets printed to
the pretty stack trace. This ensures it gets picked up by
CrashReporter.
2025-05-19 20:55:01 +01:00
Allan Shortlidge f34e135cfa AST: Add verification for AvailabilityContext. 2025-02-28 19:09:34 -08:00
Allan Shortlidge e6d77e1eb0 AST: Adopt modern assertions in AvailabilityScope. 2025-02-28 19:09:33 -08:00
Allan Shortlidge eb6506a1ad AST: Introduce SemanticAvailabilitySpec.
It wraps an type-checked `AvailabilitySpec`, which guarantees that the spec has
a valid `AvailabilityDomain` associated with it. This will unblock moving
AvailabilitySpec domain resolution from parsing to sema.
2025-02-23 10:53:06 -08:00
Allan Shortlidge 5bccbe567d AST: Refactor AvailabilityScope::getAvailabilityConditionVersionSourceRange().
Modify it to take an `AvailabilityDomain` and a `DeclContext` which will be
necessary in order to compare `AvailabilitySpec`s in a later refactor.
2025-02-23 10:53:06 -08:00
Allan Shortlidge 1ff1a8d7bc AST: Store the active DeclContext in AvailabilityScope.
In the future, the `DeclContext` for a given scope will be needed as an input
in order to query for the `AvailabilityDomain` associated with an
`AvailabilitySpec`.
2025-02-20 08:57:45 -08:00
Allan Shortlidge 670084a9ac AST: Retire PlatformVersionConstraintAvailabilitySpec. 2025-02-12 22:46:58 -08:00
Allan Shortlidge 6daea78ccf AST: Remove AvailabilitySpec.h include from Stmt.h.
Include it where it's actually used instead to improve compile times.
2025-02-11 20:03:01 -08:00
Allan Shortlidge ba822d1e23 AST: Eliminate remaining direct access to AvailableAttr's fields.
Most of the compiler should use SemanticAvailableAttr instead. In contexts like
ASTDumper where a semantic attribute is unavailable use accessors on
AvailableAttr.

NFC.
2025-01-21 22:56:07 -08:00
Allan Shortlidge 100a794ffd AST: Adopt SemanticAvailableAttr in getAvailabilityConditionVersionSourceRange(). 2025-01-08 08:17:27 -08:00
Allan Shortlidge efa1afff11 AST: Introduce SemanticAvailableAttr::getIntroducedRange().
It replaces the overload of AvailabilityInference::availableRange() that takes
an AvailableAttr.
2025-01-07 07:31:12 -08:00
Allan Shortlidge 4ed62e3154 AST: Introduce Decl::getAvailableAttrForPlatformIntroduction().
It replaces `AvailabilityInference::attrForAnnotatedAvailableRange()`.
2025-01-07 07:31:12 -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
Allan Shortlidge 36230cd9c6 AST: Use an accessor to get the PlatformKind from an AvailableAttr. 2024-11-21 09:10:36 -08: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
Allan Shortlidge 239720897a AST: Rename TypeRefinementContext to AvailabilityScope. 2024-11-12 11:34:25 -08:00