Commit Graph

1221 Commits

Author SHA1 Message Date
Hamish Knight
3c67271869 [AST] Handle a few more cases in getStartLoc()
Handle PatternBindingDecls with missing var locations, which can
happen for loop iterator vars, and FuncDecls with missing name and
func locations, which can happen for `defer`. Also while here make
sure we set the source location of a parser-produced ErrorExpr.
2025-05-14 11:15:42 +01:00
shiz
d12cb84586 [Parse] Avoid parsing unsafe expression before binary or postfix op (#81429)
rdar://150751248
2025-05-14 10:02:58 +01:00
Rintaro Ishizaki
533ae2b994 Merge pull request #80928 from kntkymt/kntk-trailing-comma-string-interpolation
[Parser] Fix String interpolation accepts invalid argument label syntax without expression
2025-04-22 01:38:50 +09:00
kntkymt
af6b4ad7a1 [Parser] fix parse trailing commna in string interpolation at the end of parseListItem flow instead 2025-04-19 14:36:11 +09:00
Pavel Yaskevich
54b62ae983 [AST/Parse] Implement nonisolated(nonsending) type modifier 2025-04-11 15:59:25 -07:00
Allan Shortlidge
703126a3ce Parse: Improve MemberImportVisibility diags for appendLiteral(_:) calls.
Ensure compiler generated calls to `appendLiteral(_:)` have source locations for
diagnostics.

Partially resolves rdar://144535697.
2025-04-05 09:48:20 -07:00
Hamish Knight
2e1c37f1ab [ASTScope] Allow try in unfolded sequence to cover following elements
Rather than fixing-up in the parser, adjust the ASTScope logic such
that a `try` element in a SequenceExpr is considered as covering all
elements to the right of it. Cases where this isn't true are invalid,
and will be diagnosed during sequence folding. e.g:

```
0 * try foo() + bar()
_ = try foo() ~~~ bar() // Assuming `~~~` has lower precedence than `=`
```

This ensures we correctly handle `try` in assignment sequences, and
allows ASTGen to get the behavior for free.

rdar://132872235
2025-04-02 12:46:21 +01:00
Hamish Knight
f8ab391737 Introduce type sugar for InlineArray (#80087)
* [CS] Decline to handle InlineArray in shrink

Previously we would try the contextual type `(<int>, <element>)`,
which is wrong. Given we want to eliminate shrink, let's just bail.

* [Sema] Sink `ValueMatchVisitor` into `applyUnboundGenericArguments`

Make sure it's called for sugar code paths too. Also let's just always
run it since it should be a pretty cheap check.

* [Sema] Diagnose passing integer to non-integer type parameter

This was previously missed, though would have been diagnosed later
as a requirement failure.

* [Parse] Split up `canParseType` 

While here, address the FIXME in `canParseTypeSimpleOrComposition`
and only check to see if we can parse a type-simple, including
`each`, `some`, and `any` for better recovery.

* Introduce type sugar for InlineArray

Parse e.g `[3 x Int]` as type sugar for InlineArray. Gated behind
an experimental feature flag for now.
2025-03-23 15:31:37 -07:00
Amritpan Kaur
0c614e09d1 [Expr/AST] Add unresolvedApply component to handle method arguments. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
811d54901b [NFC] Rename unresolvedProperty to unresolvedMember
to generalize for both properties and method members.
2025-03-19 08:53:18 -07:00
Tony Allevato
d94bd80c62 Add support for raw identifiers.
Raw identifiers are backtick-delimited identifiers that can contain any
non-identifier character other than the backtick itself, CR, LF, or other
non-printable ASCII code units, and which are also not composed entirely
of operator characters.
2025-03-11 17:18:43 -04:00
Doug Gregor
aae0881dbc Merge pull request #79854 from DougGregor/se-0458-eof-disambig 2025-03-08 07:52:53 -08:00
Doug Gregor
b0383761cf Merge pull request #79842 from DougGregor/se-0458-postfix-disambig 2025-03-07 18:55:58 -08:00
Doug Gregor
b9cb5ce791 [SE-0458] Disambiguate "unsafe" expression within string interpolation
String interpolation uses an end-of-file token, which we weren't
checking for. Fixes rdar://146493296
2025-03-07 15:10:27 -08:00
Evan Wilde
28f96411c9 Merge pull request #79778 from etcwilde/ewilde/yo-dawg-heard-you-liked-swift
CMake: option to disable swift in swift
2025-03-07 13:12:00 -08:00
Doug Gregor
6cf56e9eb7 SE-0458: Disambiguate postfix expressions vs. unsafe expressions more generally
Handle call-vs-tuple and subscript-vs-collection-expr disambiguation using the
same "no trivia" rule that we used to disambiguite "unsafe.x" (which we
treat as a member access) from "unsafe .x" (which we treat as an unsafe
expression with a leading-dot member access).

Fixes rdar://146459104.
2025-03-07 10:19:26 -08:00
Doug Gregor
0ec13f9a90 Build compiler and runtimes without Swift
Remove dependency on macros from compiler and stdlib build when
bootstrapping the compiler without Swift available.
2025-03-06 13:32:52 -08:00
Doug Gregor
be7e87565c [SE-0458] Improved disambiguation for unsafe expressions
Disambiguate `unsafe` in a few more common contexts:
* Before a comma in a list of whatever form
* Before a left brace somewhere that we cannot have a closure

Fixes a few more source compatibility regressions found in the wild,
rdar://146125433.
2025-03-04 12:54:06 -08:00
Doug Gregor
a1180e570c More disambiguation for "unsafe" expressions
Fixes rdar://145870868 / #79704
2025-02-28 14:46:42 -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
Allan Shortlidge
e5cd36eeea Merge pull request #79656 from tshortli/defer-availability-domain-resolution
AST/Parse: Always delay AvailabilityDomain lookup to type-checking
2025-02-27 03:41:57 -08:00
Allan Shortlidge
b9ad07bc6f Merge pull request #79652 from tshortli/availability-platforms-are-not-keywords
IDE: Stop requiring availability platforms to be classified as keywords
2025-02-27 00:09:24 -08:00
Allan Shortlidge
b0afd07e14 AST/Parse: Always delay AvailabilityDomain lookup to type-checking.
This will unblock parsing and type-checking availability queries that specify
custom availability domains, e.g.:

```
if #available(CustomDomain) {
  // Use declarations protected by @available(CustomDomain)
}
```
2025-02-26 21:46:14 -08:00
Allan Shortlidge
332d770a5c IDE: Stop requiring availability platforms to be classified as keywords. 2025-02-26 18:02:00 -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
Allan Shortlidge
c54825b4c3 AST: Use AvailabilityDomainOrIdentifier to store domains in AvailabilitySpec. 2025-02-25 09:02:47 -08:00
Becca Royal-Gordon
5c28f1d73d Merge pull request #78313 from beccadax/llvm_reachable 2025-02-18 01:04:55 -08:00
Rintaro Ishizaki
0ced6e06b4 [ASTGen] Generate CaptureListExpr
Move the capture list entry construction logic to
CaptureListEntry::createParsed() so that ASTGen can use it.
2025-02-16 05:22:46 -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
3142803d2f AST: Remove runtime version from AvailabilitySpec.
Instead of canonicalizing platform versions during parsing and storing two
versions, just canonicalize the parsed version on-demand when its requested.
2025-02-11 20:10:07 -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
cb049d8ed1 [ASTGen] Generate anonymous closure parameters
E.g. '$0'
2025-02-07 19:39:57 -08:00
Rintaro Ishizaki
635494df39 [Parse] Adjust assertions for ParsingFlags::PoundIfAllActive mode
In this mode, multiple #if clauses are active. For now, just use the
first clause as the result.
2025-02-06 12:07:04 -08:00
Doug Gregor
8bb5bbedbc Implement an unsafe expression to cover uses of unsafe constructs
Introduce an `unsafe` expression akin to `try` and `await` that notes
that there are unsafe constructs in the expression to the right-hand
side. Extend the effects checker to also check for unsafety along with
throwing and async operations. This will result in diagnostics like
the following:

    10 |   func sum() -> Int {
    11 |     withUnsafeBufferPointer { buffer in
    12 |       let value = buffer[0]
       |                   |     `- note: reference to unsafe subscript 'subscript(_:)'
       |                   |- warning: expression uses unsafe constructs but is not marked with 'unsafe'
       |                   `- note: reference to parameter 'buffer' involves unsafe type 'UnsafeBufferPointer<Int>'
    13 |       tryWithP(X())
    14 |       return fastAdd(buffer.baseAddress, buffer.count)

These will come with a Fix-It that inserts `unsafe` into the proper
place. There's also a warning that appears when `unsafe` doesn't cover
any unsafe code, making it easier to clean up extraneous `unsafe`.

This approach requires that `@unsafe` be present on any declaration
that involves unsafe constructs within its signature. Outside of the
signature, the `unsafe` expression is used to identify unsafe code.
2025-01-10 10:39:14 -08:00
Becca Royal-Gordon
90f7724ec6 Fix parsing of @_unavailableFromAsync
There’s a very easy to reach `llvm_unreachable()` in this code which ought to be a diagnostic, as well as a couple of other issues. Rework it into something that’s a bit better at handling the edge cases.
2024-12-20 17:59:23 -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
Rintaro Ishizaki
8e8e4d0193 [Parse/Sema] Move OptionalEvaluationExpr wrapping to PreCheckTarget
This simplify the Parser diagnostics and some type checker logic.
2024-10-11 13:12:48 -07: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
05e8140c6d Provide macro module name in MacroExpansionExpr creation
This properly passes the module name through from attached macros to
the freestanding macro that are used under-the-hood for type checking.
2024-09-16 16:44:17 -07: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
Ben Barham
d72f5b12c4 Update StringRef::equals references to operator==
`equals` has been deprecated upstream, use `operator==` instead.
2024-06-27 19:14:06 -07: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
Holly Borla
8d6434410b [Parser] Hoist up 'try' exprs out from sequence exprs in the parser.
If the left-most sequence expr is a 'try', hoist it up to turn
'(try x) + y' into 'try (x + y)'. This is necessary to do in the
parser because 'try' nodes are represented in the ASTScope tree
to look up catch nodes. The scope tree must be syntactic because
it's constructed before sequence folding happens during preCheckExpr.
Otherwise, catch node lookup would find the incorrect catch node for
'try x + y' at the source location for 'y'.

'try' has restrictions for where it can appear within a sequence
expr. This is still diagnosed in TypeChecker::foldSequence.
2024-06-18 21:31:44 -07:00