Commit Graph

186 Commits

Author SHA1 Message Date
Hamish Knight
3141b5a8cd [AST] NFC: Introduce PrintOptions::forDebugging 2025-09-21 23:19:06 +01:00
Hamish Knight
4423399fce [CS] Use apply component locator for verifyThatArgumentIsHashable
For a method key path use the locator for the apply itself rather
than the member, ensuring we handle invalid cases where the apply is
the first component, and providing more accurate location info.
2025-08-15 21:30:22 +01:00
Hamish Knight
61d0486d12 [CS] NFC: Factor out hasKeyPathComponent 2025-08-15 21:30:22 +01:00
Hamish Knight
1f4cca6f4d [CS] Fix nested subscript dynamic member lookups
Previously we would incorrectly attempt to treat a nested dynamic member
lookup for a subscript as a member reference. Fix `isSubscriptMemberRef`
such that we treat it as a subscript reference.
2025-08-01 12:29:22 +01:00
Amritpan Kaur
a96b780a28 [Sema] Diagnose method arg captures that are not Hashable/Equatable. 2025-03-19 08:56:03 -07:00
Slava Pestov
3a01f206c5 Sema: Rename LocatorPathElt::OptionalPayload to ::OptionalInjection 2025-02-25 17:22:41 -05:00
Slava Pestov
fca570ca91 Sema: Don't use ExistentialLayout for DeepEquality matching 2024-09-24 19:12:44 -04:00
Anthony Latsis
d18a7fd073 Sema: Require CS to certify conversions associated w/ existential member accesses 2024-04-18 22:54:19 +03:00
Anthony Latsis
c079082245 Merge pull request #72988 from AnthonyLatsis/nfcs
[NFC] Two trivial NFCs
2024-04-12 14:23:10 +03:00
Anthony Latsis
d34742063f [NFC] Fix a dump style inconsistency between ConstraintLocatorBuilder & ConstraintLocator 2024-04-11 23:24:51 +03:00
Pavel Yaskevich
31e385f20e [Sema] WitnessMatching: Anchor ProtocolRequirement on the requrement declaration
Locators used to support only expressions which is no longer the case.
2024-03-18 10:43:41 -07:00
Hamish Knight
61a4148925 [CS] Generalize implied result handling
Track the implied result exprs in the constraint
system, and allow arbitrary propagation of
implied results down if/switch expression
branches. This is required for allowing implied
results in non-single-expression closures.
2024-02-07 18:14:22 +00:00
Pavel Yaskevich
d542ab99f0 [ConstraintSystem] Add a new locator for key path subscript parameter 2023-11-17 13:21:42 -08:00
Pavel Yaskevich
a4bda00e5b [ConstraintSystem] Add a locator element to represent a fallback type 2023-11-01 09:14:15 -07:00
Hamish Knight
042ccdda01 Merge pull request #68344 from hamishknight/do-it 2023-10-10 10:26:12 +01:00
Hamish Knight
33f94bc874 Introduce do expressions 2023-10-06 11:17:48 +01:00
Doug Gregor
4b3d2f47d9 [Typed throws] Handle function conversions involving different thrown errors
Teach the constraint solver about the subtyping rule that permits
converting one function type to another when the effective thrown error
type of one is a subtype of the effective thrown error type of the
other, using `any Error` for untyped throws and `Never` for
non-throwing.

With minor other fixes, this allows us to use typed throws for generic
functions that carry a typed error from their arguments through to
themselves, which is in effect a typed `rethrows`:

```swift
func mapArray<T, U, E: Error>(_ array: [T], body: (T) throws(E) -> U)
throws(E) -> [U] {
  var resultArray: [U] = .init()
  for value in array {
    resultArray.append(try body(value))
  }
  return resultArray
}
```
2023-10-05 11:55:05 -07:00
Doug Gregor
7f82b2a9aa [Typed throws] Enable checking of thrown types on closures.
Enable typed throws on explicit closures, either due to contextual
type information or due to explicit specification.
2023-09-29 10:51:54 -07:00
Hamish Knight
6ee44f09b4 Introduce then statements
These allow multi-statement `if`/`switch` expression
branches that can produce a value at the end by
saying `then <expr>`. This is gated behind
`-enable-experimental-feature ThenStatements`
pending evolution discussion.
2023-09-01 14:32:14 +01:00
Amritpan Kaur
37b9a695a2 [ConstraintLocator] Add key path component lookup 2023-08-21 10:51:30 -07:00
Hamish Knight
1dd86fccdb [CodeComplete] More efficient skipping for completions in if/switch exprs
Skip type-checking multi-statement branches if the
completion is in a single-expression branch, and
skip type-checking the expression as a whole if
the completion is in a multi-statement branch.
2023-08-01 15:21:29 +01:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Hamish Knight
b43d351197 [CS] Improve diagnostics a bit for pattern mismatch
There's still plenty of more work to do here for
pattern diagnostics, including introducing a
bunch of new locator elements, and handling things
like argument list mismatches. This at least lets
us fall back to a generic mismatch diagnostic.
2023-06-07 00:35:02 +01:00
Hamish Knight
b07f7b336e [CS] Improve handling of holes for Named/AnyPatterns
Rather than eagerly binding them to holes if the
sequence element type ends up being Any, let's
record the CollectionElementContextualMismatch fix,
and then if the patterns end up becoming holes,
skip penalizing them if we know the fix was
recorded. This avoids prematurely turning type
variables for ExprPatterns into holes, which
should be able to get better bindings from the
expression provided. Also this means we'll apply
the logic to non-Any sequence types, which
previously we would give a confusing diagnostic
to.
2023-05-04 14:53:58 +01:00
Pavel Yaskevich
ed23aec47e [ConstraintSystem] Record opened pack expansion type in the locator element 2023-05-02 09:32:19 -07:00
Pavel Yaskevich
6769e39ed7 [ConstraintSystem] Add a new locator element to identify pack expansion types 2023-05-02 09:31:56 -07:00
Anthony Latsis
82ac9328ae [NFC] CS: Misc minor debug output tweaks
* Use fancy arrows (`→`) because they are distinct from and shorter than `->`,
  and fancier.
* We have two ways of demarcating locators: `@ <locator>` and `[[<locator>]];`.
  Stick to the first, which is shorter and clearer.
* 'attempting type variable' → 'attempting binding'. *Bindings* are attempted,
  not type variables.
* `considering ->` → `considering:`. I think a colon is semantically more fit
  and makes things easier to read if the considered constraint has arrows in its
  description or types. It’s also shorter this way.
2023-04-18 22:42:59 +03:00
Alex Hoppen
cebc084367 [CS] Fix locator simplification with 'Member' path element
Previously, a `Member` path element in a `ConstraintLocator` was simplified to the base on which the member was accessed. This is incorrect.
2023-03-20 09:19:21 -07:00
Luciano Almeida
d5a9d6ab83 [ConstraintSystem] Introduce new locator path elemet to identify coercion 2023-03-11 18:47:53 -03:00
Pavel Yaskevich
17b09a801e [ConstraintSystem] Add a locator element to represent a generic type
This locator is going to be used as a "parent" element for
"generic argument" elements.
2023-03-03 18:50:46 -08:00
Hamish Knight
a40f1abaff Introduce if/switch expressions
Introduce SingleValueStmtExpr, which allows the
embedding of a statement in an expression context.
This then allows us to parse and type-check `if`
and `switch` statements as expressions, gated
behind the `IfSwitchExpression` experimental
feature for now. In the future,
SingleValueStmtExpr could also be used for e.g
`do` expressions.

For now, only single expression branches are
supported for producing a value from an
`if`/`switch` expression, and each branch is
type-checked independently. A multi-statement
branch may only appear if it ends with a `throw`,
and it may not `break`, `continue`, or `return`.

The placement of `if`/`switch` expressions is also
currently limited by a syntactic use diagnostic.
Currently they're only allowed in bindings,
assignments, throws, and returns. But this could
be lifted in the future if desired.
2023-02-01 15:30:18 +00:00
Hamish Knight
76da268b4b [CS] Debug: Print ASTNode for contextual type
If we don't have an associated TypeRepr for a
contextual type in the constraint system, dump the
ASTNode that it's for instead.

Before:

```
Contextual Type: $T2
```

After:

```
Contextual Type: $T2 at DeclRef@/Users/hamish/src/swift-test-arena/swift-test-arena/main.swift:1493:21
```
2023-01-26 17:21:49 +00:00
Luciano Almeida
e513d23c0b [Sema] Improving global actor function mismatch diagnostic 2023-01-13 20:47:23 -03:00
Holly Borla
6e28f996ce [ConstraintSystem] Create opened pack element generic environments lazily
in the constraint system.
2023-01-07 09:50:14 -08:00
Doug Gregor
806b5a8777 [Macros] Diagnose errors where a macro is used without the '#'. 2023-01-02 21:22:04 -08:00
Holly Borla
dc941de45e [ConstraintLocator] Add a locator path element that stores an opened pack element
generic signature.
2022-12-14 20:45:51 -08:00
Holly Borla
16b9654d4b [ConstraintSystem] Use specific locators when matching pack types. 2022-11-16 09:24:21 -08:00
Holly Borla
72d87b5ca5 [ConstraintSystem] Add a locator path element for the pattern of a pack expansion. 2022-11-16 09:06:58 -08:00
Holly Borla
5218a0d77a [ConstraintSystem] Add a locator path element for pack shapes, and use it for
type variables representing pack shapes.
2022-11-15 21:33:17 -08:00
Holly Borla
38a2c8218b [Requirement] Rename RequirementKind::SameCount to SameShape. 2022-10-06 20:48:40 -07:00
Slava Pestov
5c32f2136e AST: Introduce RequirementKind::SameCount 2022-08-23 11:12:00 -04:00
Slava Pestov
11eb11833f Sema: Fix deep equality matching for parameterized protocol types
Fixes rdar://problem/98356057.
2022-08-09 23:45:39 -04:00
Pavel Yaskevich
dee6a31ac9 [ConstraintLocator] Generalize pattern decl locator to support "named" and "any" 2022-08-03 15:50:33 -07:00
Alex Hoppen
a7e0358038 [Sema] Print pattern name for ConstraintLocators 2022-07-20 09:46:14 +02:00
Alex Hoppen
eba95bacbd [CS] Delay type resolution of variables for variables in named pattern 2022-07-20 09:46:14 +02:00
Alex Hoppen
6416640794 [Sema] Support dumping of ConstraintLocatorBuilder 2022-07-11 11:45:06 +02:00
Pavel Yaskevich
6a65810d30 [Constraint] NFC: Rename ClosureBodyElement to SyntacticElement
`SyntacticElement` represents a statement, pattern, declaration,
condition, or expression and could originate from i.e. a closure,
a function or a result builder body.
2022-04-26 09:55:04 -07:00
Pavel Yaskevich
9d5ee7a081 [ConstraintLocator] Add a new element - pattern binding element
The new element points to an index of a particular pattern in a
pattern binding declaration.
2022-03-07 10:51:40 -08:00
Robert Widmann
b3f70c960e Implement Pack Locators 2021-12-16 01:16:45 -08:00
Hamish Knight
f26f136569 [CS] Tweak dynamic member constraint generation
In order to fit with the new IUO model where
functions with IUO results have the disjunction
formed when matching result types, we need to
update this logic to form applicable fn
constraints for the implicit `x[dynamicMember:]`
subscript call.

This is done using a new ImplicitDynamicMemberSubscript
locator path element to allow easy identification of
what the right callee and argument list should be.
2021-10-12 14:14:32 +01:00