Commit Graph

808 Commits

Author SHA1 Message Date
Becca Royal-Gordon
f82881cc8e Parse module selectors in permitted locations 2025-10-16 13:30:29 -07:00
Rintaro Ishizaki
b119407966 [Parse] Change whitespace rule between attribute name and '(' in Swift 6
In Swift 6 and later, whitespace is no longer permitted between an
attribute name and the opening parenthesis. Update the parser so that
in Swift 6+, a `(` without preceding whitespace is always treated as
the start of the argument list, while a '(' with preceding whitespace is
considered the start of the argument list _only when_ it is unambiguous.

This change makes the following closure be parsed correctly:

  { @MainActor (arg: Int) in ... }

rdar://147785544
2025-09-09 16:44:29 -07: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
Anthony Latsis
99f63ed933 DiagnosticEngine: Support TypeAttribute diagnostic arguments 2025-04-03 01:52:38 +01:00
Rintaro Ishizaki
5eac58e1e0 [AST] SwitchStmt only hold CaseStmt
Now that there is no way SwitchStmt to hold AST nodes other than
CaseStmt.
2025-03-08 09:14:40 -08:00
Rintaro Ishizaki
002d7d7cdf [Parse/AST] Remove PoundDiagnosticDecl
There is no reson to make a AST node for '#error' and '#warning'
directives. Parser knows the diagnostics should be emitted or not.
2025-03-07 21:48:01 -08:00
Allan Shortlidge
aaa0e0a7b3 AST/Parse: Parse custom availability domain specs in if #available(...).
Delay resolution of availability domain identifiers parsed in availability
specifications until type-checking. This allows custom domain specifications to
be written in `if #available` queries.
2025-02-27 23:07:55 -08:00
Doug Gregor
939d4d7d91 [SE-0458] Disambiguate "unsafe" expression and for..in effect
Port over the disambiguation code we already had in the new Swift parser
to the existing C++ parser for the "unsafe" expression and for..in effect.
2025-02-26 13:03:55 -08:00
Doug Gregor
b7b5a2a19d [SE-0458] Enable unsafe expressions / attributes / for..in effects by default
With the acceptance of SE-0458, allow the use of unsafe expressions, the
@safe and @unsafe attributes, and the `unsafe` effect on the for..in loop
in all Swift code.

Introduce the `-strict-memory-safety` flag detailed in the proposal to
enable strict memory safety checking. This enables a new class of
feature, an optional feature (that is *not* upcoming or experimental),
and which can be detected via `hasFeature(StrictMemorySafety)`.
2025-02-26 12:30:07 -08:00
Doug Gregor
115abcb637 Merge pull request #79573 from DougGregor/unsafe-for-in-loop
[SE-0458] Implement "unsafe" effect for the for-in loop
2025-02-24 14:49:45 -08:00
Doug Gregor
50801f9c05 [SE-0458] Implement "unsafe" effect for the for-in loop
Memory unsafety in the iteration part of the for-in loop (i.e., the part
that works on the iterator) can be covered by the "unsafe" effect on
the for..in loop, before the pattern.
2025-02-23 22:50:39 -08:00
Allan Shortlidge
db29a1c6f4 Parse/Sema: Move #available query wildcard diagnostics to Sema.
In order to unblock resolution of availability domains during type-checking
instead of parsing, diagnostics about missing or superfluous wildcards in
availability specification lists need to move to Sema.
2025-02-23 22:09:33 -08:00
Allan Shortlidge
ff17263e56 Merge pull request #79480 from tshortli/exclude-parser-diags
Parse: Avoid including `DiagnosticsParse.h` in `Parser.h`
2025-02-19 18:19:29 -08:00
Allan Shortlidge
7492e961dc Merge pull request #79494 from tshortli/type-check-availability-spec
Parse/Sema: Move some `AvailabilitySpec` diagnostics from Parse to Sema
2025-02-19 17:07:24 -08:00
Allan Shortlidge
e90530990a Parse/Sema: Move some AvailabilitySpec diagnostics from Parse to Sema.
Eventually, querying the `AvailabilityDomain` associated with an
`AvailabilitySpec` will require invoking a request that takes a `DeclContext`.
This means that any diagnostics related to the domain identified by an
`AvailabilitySpec` need to be emitted during type-checking rather than parsing.
This change migrates several `AvailabilitySpec` diagnostics from Parse to Sema
to unblock further work.
2025-02-19 11:40:56 -08:00
Allan Shortlidge
e8329d292b Parse: Avoid including DiagnosticsParse.h in Parser.h.
This prevents the include of DiagnosticsParse.h from leaking into `.cpp` files
in libraries besides Parse.
2025-02-19 07:56:38 -08:00
Allan Shortlidge
efab3ff0a0 Parse: Broaden an availability spec diagnostic.
The "can't be combined with shorthand specification" diagnostic doesn't need to
be specific to platform availability attributes.
2025-02-18 21:31:48 -08:00
Allan Shortlidge
670084a9ac AST: Retire PlatformVersionConstraintAvailabilitySpec. 2025-02-12 22:46:58 -08:00
Allan Shortlidge
0722efe97e AST: Retire PlatformAgnosticVersionConstraintAvailabilitySpec. 2025-02-12 20:41:00 -08:00
Allan Shortlidge
6844cfe714 AST: Retire OtherPlatformAvailabilitySpec. 2025-02-12 19:39:30 -08:00
Allan Shortlidge
09f8622b1b AST: Simplify AvailabilitySpec subclasses further.
Remove methods that are specific to AvailabilitySpec subclasses and replace
them with methods on the superclass if necessary.

NFC.
2025-02-11 22:45:43 -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
Rintaro Ishizaki
df2ada37df [ASTGen] Generate AvailableAttr
* Move `AvailabilitySpec` handling logic to AST, so they can be shared
  between libParse and ASTGen
* Requestify '-define-availability' arguments parsing and parse them
  with 'SwiftParser' according to the 'ParserASTGen' feature flag
* Implement 'AvailableAttr' generation in ASTGen
2025-02-04 23:40:01 -08:00
Rintaro Ishizaki
694e815f07 [Parser] Include all AST nodes from every #if region in ParserUnit
'ParserUnit' is used for analyzing syntax structures _mainly_ in
SourceKit.

Since we removed IfConfigDecl from AST, ParserUnit didn't
inclue any AST in #if ... #endif regions even for active region because
it used to consider all inactive. Instead, consider every region
"active" and include all the AST nodes.

rdar://117387631
2025-02-03 16:22:11 -08:00
Rintaro Ishizaki
1fc00ae248 [ASTGen] Remove LegacyParser ping-pong mechanism
Now that ASTGen should be able to generate most Swift code. Let's
remove "legacy parser" call-in, and remove the unhealthy cyclic
dependency between lib/Parse and ASTGen.
2024-11-14 22:46:43 -08:00
Doug Gregor
d762dd53f8 Stop parsing into IfConfiDecl nodes in the C++ parser
When parsing #if...#endif regions, parse the active clause directly into
place in the AST without ever producing an IfConfigDecl instance.
2024-09-18 20:51:09 -07:00
Doug Gregor
cccf6c1114 Introduce @unsafe and the ability to prohibit use of unsafe declarations
Allow any declaration to be marked with `@unsafe`, meaning that it
involves unsafe code. This also extends to C declarations marked with
the `swift_attr("unsafe")` attribute.

Under a separate experimental flag (`DisallowUnsafe`), diagnose any
attempt to use an `@unsafe` declaration or any unsafe language feature
(such as `unowned(unsafe)`, `@unchecked Sendable`). This begins to
define a "safe" mode in Swift that prohibits memory-unsafe constructs.
2024-08-19 14:33:07 -07:00
Hamish Knight
55aed16ee6 Requestify FallthroughStmt source and destination lookup
Follow a similar pattern to BreakTargetRequest
and ContinueTargetRequest.
2024-08-14 19:59:05 +01:00
Mateus Rodrigues
b1ba769754 Minor changes for better clarity 2024-07-25 15:30:37 -03:00
Mateus Rodrigues
e0d416cdab Ungate accepted parts of SE439 2024-07-22 09:55:16 -03:00
Xiaodi Wu
e1f537107f Merge branch 'main' into trailing-comma 2024-06-29 16:36:06 -04:00
Mateus Rodrigues
2e3b9875c8 Update isStartOfConditionalStmtBody to check for oper_binary_unspaced 2024-06-27 10:58:24 -03:00
Hamish Knight
2fbc8b336f Merge pull request #74559 from hamishknight/pound-if-skip
[Parse] Handle `#if` in brace skipping logic
2024-06-21 15:04:19 +01:00
Tim Kientzle
598e5104ef Merge pull request #74184 from tbkka/tbkka-assertions2
Add `#include "swift/Basic/Assertions.h"` to a lot of source files
2024-06-20 12:13:28 -07:00
Hamish Knight
a85ca1315b [Parse] Unify recovery for invalid tokens following a #if body
Previously we would only diagnose and recover for
invalid tokens following a `#if` body for the decl
and postfix expression case. Sink this logic into
`parseIfConfigRaw`, ensuring that we do this for
all `#if` cases. This requires propagating the
context we're parsing in to customize the
diagnostic.
2024-06-19 21:39:40 +01:00
Konrad `ktoso` Malawski
0d3a4b44b8 [Concurrency] if let value on async value should be banned (not crash)
The issue is that the shorthand if let syntax injects an implicit
expression: https://github.com/apple/swift/pull/40694/ in ParseStmt and
that the 'diagnoseUnhandledAsyncSite' explicitly avoids reporting errors
in implicit expressions.

This change is that we don't mark the implicit declref code emitted by
the `if let prop` as implicit anymore, and this way the reporting works
out as expected.

Added some tests covering this as well as properly erroring out for the
nonexistent syntax of shortand + awaiting which doesn't exist, and we
properly error on it.

Resolves rdar://126169564
2024-06-19 17:20:18 +09:00
Mateus Rodrigues
013997dae7 Implement trailing comma for comma-separated lists 2024-06-18 14:01:38 -03: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
Greg Titus
2b0fa1ca05 Add diagnostic for extraneous case keyword when multiple patterns. 2024-05-31 17:42:03 -07:00
Alex Hoppen
0b65641401 [CodeCompletion] Parse multiple catch clauses if the do body contains the code completion token
rdar://125303959
2024-04-08 11:07:48 -07:00
Alex Hoppen
a836195e97 Revert "Merge pull request #71238 from ahoppen/ahoppen/source-location-parser-changes"
This reverts commit 12fe866470, reversing
changes made to 956d6d23f2.
2024-02-23 11:09:10 -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
Alex Hoppen
4a386d40a6 [Parse] Disallow multiline string literals for filename in #sourceLocation and similar locations
This should allow us to eventually simplify parsing of simple string literals in the new parse by not having to handle indentation of multiline string literals.
2024-01-31 16:03:20 -08:00
Hamish Knight
c97d80b1c3 [AST] NFC: Add convenience constructors for ReturnStmt
Add `ReturnStmt::createParsed` and `createImplict`.
2024-01-23 19:30:18 +00:00
Anthony Latsis
d7b52e96c4 Move unqualified init diagnosis from Parse to Sema 2023-12-25 21:17:20 +03:00
Rintaro Ishizaki
409b2760f9 [ASTGen] Statements 2023-12-18 16:09:25 -08:00
Doug Gregor
ab5ab28010 Merge pull request #70454 from DougGregor/full-typed-throws-inference
[Typed throws] Implement thrown type inference for do..catch within closures
2023-12-14 16:09:33 -08:00
Rintaro Ishizaki
d6556434cd [AST] Make 'StmtConditionElement' a single 'PointerUnion'
Previously, 'IntroducerLoc' and 'ThePattern' were only used for pattern
binidng cases. Create a new 'ConditionalPatternBindingInfo' type to
cover such cases, and make 'StmtConditionElement' a pure 'PointerUnion'
type.

This makes it clear which fields are used in which condition kind. Also,
we can expect overall size reduction of StmtCondition when the
majority of the conditions are simple boolean expressions.
2023-12-13 12:52:53 -08:00
Doug Gregor
e1be9c312b Eliminate the DeclContext from ExplicitCaughtTypeRequest
Correctly determining the DeclContext needed for an
ExplicitCaughtTypeRequest is tricky for a number of callers, and
mistakes here can easily lead to redundant computation of the caught
type, redundant diagnostics, etc.

Instead, put a `DeclContext` into `DoCatchStmt`, because that's the
only catch node that needs a `DeclContext` but does not have one.
2023-12-13 11:42:56 -08:00
Doug Gregor
cfe2b3c87d [Typed throws] Implement support for do throws(...) syntax
During the review of SE-0413, typed throws, the notion of a `do throws`
syntax for `do..catch` blocks came up. Implement that syntax and
semantics, as a way to explicitly specify the type of error that is
thrown from the `do` body in `do..catch` statement.
2023-12-02 07:37:47 -08:00