159 Commits

Author SHA1 Message Date
Hamish Knight
62f0926d25 [CS] Invalidate nested unresolved VarDecls when ignoring completion argument
Make sure we set types for any nested VarDecls in UnresolvedPatternExprs
to ensure we don't crash when attempting to solve the body.
2025-10-26 12:33:29 +00:00
swift-ci
3998a187f9 Merge remote-tracking branch 'origin/main' into rebranch 2025-07-30 09:36:41 -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
c1d794364b Adjust code after changes to llvm::TrailingObjects API
See:
- https://github.com/llvm/llvm-project/pull/138970
- https://github.com/llvm/llvm-project/pull/144930
2025-07-19 01:48:18 +01:00
Hamish Knight
deac7012cb [AST] Avoid setting questionLoc for implicit OptionalSomePatterns
Instead fix `getSourceRange` to handle cases where the question
location is missing.
2025-03-08 18:45:39 +00:00
Hamish Knight
fe68fab1e2 [AST] Simplify construction of EnumElementPattern
Add `EnumElementPattern::create` and
`EnumElementPattern::createImplicit`, and replace
existing constructions with them.
2024-07-06 22:56:30 +01:00
Joe Groff
6706feaf5e Parse _borrowing x in patterns as a borrow binding.
Treat it as a contextual keyword when followed by an identifier, like our
other ownership-related declarations and operators.
2024-01-30 17:33:42 -08:00
Joe Groff
4011b2f53c Add methods to determine the ownership behavior of patterns.
When matching against a noncopyable value, whether the match operation can
borrow the value in-place or needs to take ownership of it is significant.
This can generally be determined from the kind of pattern being used, except
in the case of expr patterns, where it depends on type-checking the `~=`
operator that was used.
2024-01-08 18:21:33 -08:00
Rintaro Ishizaki
409b2760f9 [ASTGen] Statements 2023-12-18 16:09:25 -08:00
Hamish Knight
e1d35d90aa [AST] Introduce TypedPattern::createPropagated 2023-12-15 21:44:30 +00:00
Hamish Knight
29dac4380e [Sema] Correctly re-contextualize if/switch exprs in lazy vars
With `if`/`switch` expressions, we may now have
local bindings within lazy initializers, and
therefore need to ensure we correctly
re-contextualize them. Adjust the walker to set
the DeclContext for all decls it encounters,
and make sure it handles some cases that the
ASTWalker does not currently visit.

rdar://119158202
2023-12-08 14:38:40 +00:00
Allan Shortlidge
8d7cf970a5 AST: Refactor pattern binding initializer expression state representation.
As a follow up to https://github.com/apple/swift/pull/69841, clarify the
possible states that initializer expression of a pattern can be in. The
possible states are not checked, checked, and "checked and contextualized"
(which is the new state that was introduced and requestified in the previous
PR). This refactoring encodes the states more explicitly and renames a few
compiler APIs to better align with the new naming. NFC.
2023-11-16 10:06:47 -08:00
Allan Shortlidge
111eea7f5d AST/SILGen: Requestify var initializer expression typechecking.
Allow initializer expressions to be emitted during SILGen when
`-experimental-lazy-typecheck` is specified by introducing a new request that
fully typechecks the init expressions of pattern binding declarations
on-demand.

There are still a few rough edges, like missing support for wrapped properties
and incomplete handling of subsumed initializers. Fixing these issues is not an
immediate priority because in the short term `-experimental-lazy-typecheck`
will always be accompanied by `-enable-library-evolution` and
`-experimental-skip-non-exportable-decls`. This means that only the
initializers of properties on `@frozen` types will need to be emitted and
property wrappers are not yet fully supported on properties belonging to
`@frozen` types.

Resolves rdar://117448868
2023-11-14 11:54:01 -08:00
Hamish Knight
d56f4633c3 [Sema] Ensure synthesized NamedPatterns have types 2023-04-06 16:11:11 +01:00
Rajveer
c34f099c7d Improved out-of-place binding diagnostic and reflecting 'var' or 'let' binding pattern
Fixes Issue #63993
2023-03-19 19:26:58 +05:30
Hamish Knight
98855963ac [Sema] Introduce EnumElementExprPatternRequest
This is needed to ensure we only ever synthesize
a single unique ExprPattern when solving an
EnumElementPattern that we failed to lookup a
member for.
2023-03-07 15:16:36 +00:00
Hamish Knight
37b0a6074f [Sema] Introduce ExprPatternMatchRequest
This replaces `synthesizeTildeEqualsOperatorApplication`,
and synthesizes the match expression and var
on-demand.

Additionally, it pushes the lookup logic into
pre-checking.
2023-03-07 15:16:35 +00:00
Michael Gottesman
c97121d3ee [reference-binding] Add support for inout binding parsing/serialization. 2023-03-01 20:48:54 -08:00
Michael Gottesman
4b354f6132 [reference-binding] Make InBindingState into a true type called PatternBindingState.
This cleans up the parser code a little bit around here and will make it easier
for me to add inout.
2023-03-01 20:48:33 -08:00
Michael Gottesman
ed57d882d3 [ast] Refactor BindingPattern to take a VarDecl::Introducer directly instead of a bool to mean a let.
I am going to be adding more VarDecl::Introducers so this is a small nice
cleanup that is cut off the larger patch.
2023-03-01 20:48:33 -08:00
Hamish Knight
9b8bc6033c [AST] NFC: Refactor ExprPattern construction
Introduce 3 separate factory constructors, and
remove the ability to specify the match expr and
var, as those should be synthesized on-demand.
2023-03-01 15:53:41 +00:00
Hamish Knight
81b1358a12 [AST] Improve OptionalSomePattern a bit
Rather than having Sema set the same Optional
some decl, let's just store a pointer to the
ASTContext and retrieve it on demand.
Additionally, add `create` and `createImplicit`
factory methods.
2023-02-21 16:00:27 +00:00
Slava Pestov
7a16b0275b AST: Allow one-element tuple types to be constructed
These will never appear in the source language, but can arise
after substitution when the original type is a tuple type with
a pack expansion type.

Two examples:
- original type: (Int, T...), substitution T := {}
- original type: (T...), substitution T := {Int}

We need to model these correctly to maintain invariants.

Callers that previously used to rely on TupleType::get()
returning a ParenType now explicitly check for the one-element
case instead.
2022-08-23 11:12:00 -04:00
Luciano Almeida
9a1008809c [AST] Serialize bit async let info on AnyPattern 2022-04-17 14:06:39 -03:00
Luciano Almeida
90c5dfa9ed [Parse] Track whether an let _: pattern is an async let pattern 2022-04-17 14:06:39 -03:00
Becca Royal-Gordon
59bb325e4b [NFC] Factor out ASTContext operator news
Many, many, many types in the Swift compiler are intended to only be allocated in the ASTContext. We have previously implemented this by writing several `operator new` and `operator delete` implementations into these types. Factor those out into a new base class instead.
2021-08-19 11:19:52 -07:00
Hamish Knight
1b2a4ac365 [AST] Add Pattern::hasAnyMutableBindings 2021-06-09 10:43:25 +01:00
Slava Pestov
5585cac9dd AST: Remove AnyValue type eraser now that it's no longer needed
I'm keeping AnyValue.h around, since it has some useful overloads of
hash_value() and simple_display() that don't have a good home at this
time.
2021-01-08 23:17:12 -05:00
Slava Pestov
d4cc35a938 AST: Remove VarDecl::hasNonPatternBindingInit() 2020-09-18 16:11:06 -04:00
Frederick Kellison-Linn
352adc3b5d Remove Argument from UnresolvedMemberExpr
Instead, an expresison like `.foo()` is represented as an `UnresolvedMemberExpr` nested inside a `CallExpr`.
2020-08-26 22:42:30 -04:00
Anthony Latsis
17f12da381 [NFC] AST: Remove TypedPattern:getTypeLoc() 2020-08-04 18:46:21 +03:00
Michael Gottesman
092edd6621 [ast] Rename VarPattern -> BindingPattern.
VarPattern is today used to implement both 'let' and 'var' pattern bindings, so
today is already misleading. The reason why the name Var was chosen was done b/c
it is meant to represent a pattern that performs 'variable binding'. Given that
I am going to add a new 'inout' pattern binding to this, it makes sense to
give it now a better fitting name before I make things more confusing.
2020-07-16 18:56:01 -07:00
Robert Widmann
2371e5c260 Strip TypeLoc from EnumElementPattern 2020-06-10 14:22:46 -07:00
Robert Widmann
fc9070c072 Strip TypeLoc from IsPattern 2020-06-10 13:29:39 -07:00
Robert Widmann
0342855e50 [NFC] Debride Pattern.h of Implicit Tri-State
Remove all of this in favor of explicit constructors to preserve the one-liners, or distribute the setImplicit() calls to the callsites if necessary.
2020-04-30 22:03:55 -07:00
Pavel Yaskevich
87b8f79d2c [AST] Extend ASTNode to support Pattern/TypeLoc 2020-04-29 17:03:45 -07:00
Doug Gregor
033f9c7927 [Type checker] Introduce "contextual patterns".
Contextual pattern describes a particular pattern with enough
contextual information to determine its type. Use this to simplify
TypeChecker::typeCheckPattern()'s interface in a manner that will
admit request'ification.
2020-01-03 15:41:21 -08:00
Brent Royal-Gordon
addbe3e5ed [NFC] Thread DeclNameRef through most of the compiler
This huge commit contains as many of the mechanical changes as possible.
2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
8551b32af2 [NFC] Make EnumElementPattern carry a DeclName
This has the side effect of threading compound name support through more enum pattern code, although it’s still not complete.
2019-12-11 00:45:07 -08:00
Brent Royal-Gordon
99faa033fc [NFC] Standardize dump() methods in frontend
By convention, most structs and classes in the Swift compiler include a `dump()` method which prints debugging information. This method is meant to be called only from the debugger, but this means they’re often unused and may be eliminated from optimized binaries. On the other hand, some parts of the compiler call `dump()` methods directly despite them being intended as a pure debugging aid. clang supports attributes which can be used to avoid these problems, but they’re used very inconsistently across the compiler.

This commit adds `SWIFT_DEBUG_DUMP` and `SWIFT_DEBUG_DUMPER(<name>(<params>))` macros to declare `dump()` methods with the appropriate set of attributes and adopts this macro throughout the frontend. It does not pervasively adopt this macro in SILGen, SILOptimizer, or IRGen; these components use `dump()` methods in a different way where they’re frequently called from debugging code. Nor does it adopt it in runtime components like swiftRuntime and swiftReflection, because I’m a bit worried about size.

Despite the large number of files and lines affected, this change is NFC.
2019-10-31 18:37:42 -07:00
Jordan Rose
d96aebd147 [AST] Mark two more dump() methods as LLVM_ATTRIBUTE_USED (#23873)
...so that they don't get dead-code-stripped and become unusable from
the debugger.
2019-04-08 19:55:16 -07:00
Michael Gottesman
ed94b13d3f [ast] Add a helper method for checking if a pattern contains a var decl and adopt it. 2019-04-03 13:38:03 -07:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Harlan
eb75ad80dc [AST] Remove stored TypeLoc from TypedPattern (#19175)
* [AST] Remove stored TypeLoc from TypedPattern

TypedPattern was only using this TypeLoc as a means to a TypeRepr, which
caused it to store the pattern type twice (through the superclass and through
the TypeLoc itself.)

This also fixes a bug where deserializing a TypedPattern doesn't store
the type correctly and generally cleans up TypedPattern initialization.

Resolves rdar://44144435

* Address review comments
2018-09-07 21:14:04 -07:00
Huon Wilson
b6d44f31da [AST] std::function -> llvm::function_ref for some non-escaping params. 2018-05-01 08:29:06 +10:00
Slava Pestov
59cdc590fd AST: Ban type variable types from being stored into Patterns 2018-04-27 22:51:28 -07:00
David Zarzycki
296d1b8195 [AST] NFC: Repack two Pattern bits 2017-12-28 14:20:21 -05:00
David Zarzycki
da238539f5 [Basic] NFC: Inline bitfield cleanup
1) Remove SWIFT_INLINE_BITS boilerplate. Now that we're not using anonymous/transparent unions, we don't need the
SWIFT_BITFIELD_BITS macro.
2) Refine the the bitfield size check to better support templated bitfields.
3) Refine the SIL templated bitfields to not be prematurely "full".
2017-12-28 09:20:31 -05:00
David Zarzycki
54ea315586 [Basic] NFC: Stop using transparent unions 2017-12-16 10:43:49 -05:00
David Zarzycki
68a846e761 [AST] NFC: Do not hard code bit size of 'Kind' field
Also, give each class hierarchy at least 8 bits for the 'Kind' field.
In practice, no class hierarchy has more than 256 nodes, so this
optimizees code generation to make isa/dyn_cast faster.
2017-12-15 14:45:38 -05:00