Commit Graph

1487 Commits

Author SHA1 Message Date
Holly Borla
2468bb06fd [TypeCheckEffects] Do not diagnose missing try/await in async let
initializers that involve `if` or `switch` expressions.
2024-01-10 11:27:05 -08:00
Pavel Yaskevich
859106eed1 [Diagnostics] Expand trailing closure failure to handle all closure arguments 2024-01-08 14:09:18 -08:00
Sophia Poirier
073f541734 [Concurrency] promote GlobalConcurrency from experimental to future feature 2024-01-06 19:07:23 -08:00
Doug Gregor
baaa8f3e5b Merge pull request #70677 from DougGregor/se-0413-typed-throws
Enable SE-0413 "Typed Throws" by default
2024-01-04 16:11:26 -08:00
Doug Gregor
b7fd43949a Remove the TypedThrows experimental feature from the remaining tests 2024-01-03 22:08:56 -08:00
Anthony Latsis
ff2a5e06f6 [NFC] test: Move unqualified init expression tests out of test/Parse 2024-01-02 16:27:57 +03:00
Pavel Yaskevich
e4f9965ff7 [ConstraintSystem] Fail key path capability inference if root is a placeholder
If root type of a key path has been determined to be a hole there
is no reason to delay the inference decision which should be a
failure because none of the components would be inferrable from
a placeholder root.
2024-01-01 12:48:31 -08:00
Doug Gregor
8ad137fb51 [Typed throws] Infer thrown error type for do..catch blocks within closures.
Start classifying all potential throw sites within a constraint
system and associate them with the nearest enclosing catch node. Then,
determine the thrown error type for a given catch node by taking the
union of the thrown errors at each potential throw site. Use this to
compute the error type thrown from the body of a `do..catch` block
within a closure.

This behavior is limited to the upcoming feature `FullTypedThrows`.
2023-12-13 14:59:23 -08:00
Pavel Yaskevich
911ba101e7 [CSBindings] Extend early array literal favoring to cover dictionaries
Follow-up to https://github.com/apple/swift/pull/70148

Just like with arrays it's advantageous to favor dictionary
literals over disjunctions to bind the elements together and
enable inference across elements and, as a consequence,
type pruning.

Resolves: rdar://119040159
2023-12-04 15:01:27 -08:00
Pavel Yaskevich
f6254f7599 [ConstraintSystem] Support existential opening on key path subscript index 2023-11-17 15:49:31 -08:00
Pavel Yaskevich
9f21b95fcb [CSDiagnostics] A tailored diagnostic when passing key path as an argument to non-key path parameter
Produce a tailored diagnostic that omits a fully unresolved key path
type (`KeyPath<_, _>`) when key path without an explicit root type is
passed as an argument to non-keypath parameter type (i.e. `Int`).
2023-11-08 19:54:32 -08:00
Pavel Yaskevich
0a2f1b14a7 [CSBindings] Delay key path type inference until literal capability is known
Since key path root is now transitively inferred. Key path type
inference can be delayed until key path is resolved enough to
infer its capability.

This solves multiple problems:

- Inference fully controls what key path type is bound to;
- KeyPath constraint simplification doesn't have to double-check
  the capability and attempt to re-bind key path type;
- Custom logic to resolve key path type is no longer necessary;
- Diagnostics are improved because capability and root/value type
  mismatch are diagnosed when key path is matched against the
  contextual type.
2023-11-08 19:54:31 -08:00
Pavel Yaskevich
d066881243 [Tests] NFC: Adjust changed test-cases 2023-11-01 09:15:14 -07:00
Pavel Yaskevich
62d15cda1b [CSGen] Use a correct locator for subtype between explicit and inferred root types
Covariant conversions between explicit and contextual/inferred root
types of a key path are simulated through a subtype constraint which
has to be attached to a `KeyPathRoot` element.
2023-11-01 09:15:14 -07:00
Pavel Yaskevich
8b4985054a [CSDiagnostics] Differentiate between key path type and value issues
Make sure that contextual mismatch uses a correct locator when
the issue is with key path value type instead of the key path
type.
2023-11-01 09:15:14 -07:00
Hamish Knight
042ccdda01 Merge pull request #68344 from hamishknight/do-it 2023-10-10 10:26:12 +01:00
Pavel Yaskevich
12630f4c5c Merge pull request #69031 from xedin/rdar-116376651
[ConstraintSystem] A few improvements to key path handling and diagnostics
2023-10-09 23:27:45 -07:00
Pavel Yaskevich
b17ec0e385 [CSSimplify] Broaden delay of the key path assignment
If key path is connected to a disjunction directly or indirectly
(i.e. via ApplicableFunction constraint) do not attempt to bind
it until disjunction is taken, otherwise there is a risk to miss
a valid keypath-to-function conversion.
2023-10-06 17:47:10 -07:00
Holly Borla
3038f9d4ef [NFC][Concurrency] Add a regression test for a bogus missing try diagnostic
with `async let`.
2023-10-06 17:32:49 -07:00
Pavel Yaskevich
6fd6c16bf7 [CSSimplify] Key path should use a default type is contextual type is a placeholder
If there is something wrong with the context, let's assign a default
KeyPath type to the key path literal to avoid propagating placeholder
into the key path inference.
2023-10-06 16:59:53 -07:00
Pavel Yaskevich
4830c5be9a [CSSimplify] Unwrap of key path root can only yield one type
For methods there are two possibilities - force unwrap and
conditional, that's why we need a disjunction with two choices.

This is not the case for key path root type it could only
be force unwrapped.
2023-10-06 16:14:01 -07:00
Pavel Yaskevich
0a9e960420 [CSDiagnostics] Don't suggest unwrapping a key path root without an explicit type
A key path without an explicit root cannot be unwrapped since the
optionality is inferred from context and we cannot suggest removing
that, adding explicit type is not going to change anything and
`!` and `?` after the leading dot is not supported.
2023-10-06 13:36:35 -07:00
Pavel Yaskevich
5a6c562b8c [CSSimplify] Adjust locators associated with keypath root/value matching constraints
`tryMatchRootAndValueFromType` anchored both root and value match
constraints directly on the key path expression. That is incorrect
because we have special locators for that.
2023-10-06 10:35:42 -07:00
Hamish Knight
33f94bc874 Introduce do expressions 2023-10-06 11:17:48 +01:00
Hamish Knight
231e17950a [test] Add additional if expr test 2023-10-06 11:17:46 +01:00
Pavel Yaskevich
6a1783d48e [CSSimplify] Prevent overly eager solving of key path constraint
If key path literal is passed as an argument to a function/subscript
application its type cannot be resolved until the overload for the
call is selected, otherwise it could prevent a valid keypath-to-function
conversion in cases were argument ends up being a function type.
2023-10-05 17:10:47 -07:00
Pavel Yaskevich
faeb55b227 Merge pull request #68940 from xedin/rdar-93103421
[CSBindings] Infer key path types from superclass constrained existen…
2023-10-04 10:06:45 -07:00
Pavel Yaskevich
5c83b18f64 [CSBindings] Infer key path types from superclass constrained existential values
If type of the key path expression is convertible to an existential
value with a superclass constraint represented as a known key path
type, we can use it to inform key path inference.

Resolves: rdar://93103421
2023-10-03 10:15:35 -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
fa406ed17c [Sema] Fix try/await handling for if/switch expressions
Previously we allowed an attached `try` or `await`
on an `if`/`switch` expression to cover the branches,
which does not match what was proposed, and is
especially harmful for multi-statement cases. Fix
the effect handling logic such that we reset
effect coverage for `if`/`switch` expressions
similar to closures, but continue to maintain the
state needed for rethrows checking.

rdar://116066748
2023-09-27 10:39:51 +01:00
Becca Royal-Gordon
8770c7f826 Rework ASTDumper (#68438)
This PR refactors the ASTDumper to make it more structured, less mistake-prone, and more amenable to future changes. For example:

```cpp
  // Before:
  void visitUnresolvedDotExpr(UnresolvedDotExpr *E) {
    printCommon(E, "unresolved_dot_expr")
      << " field '" << E->getName() << "'";
    PrintWithColorRAII(OS, ExprModifierColor)
      << " function_ref=" << getFunctionRefKindStr(E->getFunctionRefKind());
    if (E->getBase()) {
      OS << '\n';
      printRec(E->getBase());
    }
    PrintWithColorRAII(OS, ParenthesisColor) << ')';
  }

  // After:
  void visitUnresolvedDotExpr(UnresolvedDotExpr *E, StringRef label) {
    printCommon(E, "unresolved_dot_expr", label);

    printFieldQuoted(E->getName(), "field");
    printField(E->getFunctionRefKind(), "function_ref", ExprModifierColor);

    if (E->getBase()) {
      printRec(E->getBase());
    }

    printFoot();
  }
```

* Values are printed through calls to base class methods, rather than direct access to the underlying `raw_ostream`.
    * These methods tend to reduce the chances of bugs like missing/extra spaces or newlines, too much/too little indentation, etc.
    * More values are quoted, and unprintable/non-ASCII characters in quoted values are escaped before printing.
* Infrastructure to label child nodes now exists.
    * Some weird breaks from the normal "style", like `PatternBindingDecl`'s original and processed initializers, have been brought into line.
* Some types that previously used ad-hoc dumping functions, like conformances and substitution maps, are now structured similarly to the dumper classes.
* I've fixed the odd dumping bug along the way. For example, distributed actors were only marked `actor`, not `distributed actor`.

This PR doesn't change the overall style of AST dumps; they're still pseudo-S-expressions. But the logic that implements this style is now isolated into a relatively small base class, making it feasible to introduce e.g. JSON dumping in the future.
2023-09-11 23:56:38 -07:00
Luciano Almeida
9c3f2aa2c1 Merge pull request #68301 from LucianoPAlmeida/coerce-function 2023-09-06 06:57:52 -03:00
Luciano Almeida
43390175b0 [tests] Adding test for diagnostics for function type coerce failure 2023-09-05 18:17:58 -03:00
Manu
02b5fa2c8e Fix some typos in the codebase 2023-08-31 18:50:10 -03:00
Hamish Knight
94cf5620d5 [Sema] Catch invalid if/switch exprs in more places
Move out-of-place SingleValueStmtExpr checking into
`performSyntacticExprDiagnostics`, to ensure we
catch all expressions. Previously we did the walk
as a part of Decl-based MiscDiagnostics, but it
turns out that can miss expressions in property
initializers, subscript default arguments, and
custom attrs.

This does mean that we'll now no longer diagnose
out-of-place if/switch exprs if the expression
didn't type-check, but that's consistent with the
rest of MiscDiagnostics, and I don't think it will
be a major issue in practice. We probably ought to
consider moving this checking into PreCheckExpr,
but that would require first separating out
SequenceExpr folding, which has other consequences,
and so I'm leaving as future work for now.
2023-08-30 12:57:29 +01:00
Hamish Knight
c04281c1ef [Sema] Allow implicit nodes to wrap if/switch expressions
Previously we would only look through a handful of
AST node types when determining if an if/switch
expression is in a valid position. However this
doesn't handle cases where we synthesize code
around an if/switch expression, such as
`init(erasing:)` calls. As such, relax the logic
such that it can look through any implicit
expression.

rdar://113435870
2023-08-30 12:57:29 +01:00
Amritpan Kaur
6eadf2dce8 [Test] Update tests 2023-08-21 13:01:25 -07:00
Amritpan Kaur
366707d01b [Test] Update tests. 2023-08-16 14:28:08 -07:00
Pavel Yaskevich
cfea0d3d22 [TypeChecker] NFC: Add XFAIL'ed test-case for https://github.com/apple/swift/issues/67363 2023-08-14 10:06:52 -07:00
Pavel Yaskevich
64a654dc0d Revert "[CSBindings] Prefer conjunctions over closure variables without bindings"
This reverts commit 11ee82de7b.
2023-08-14 10:02:05 -07:00
Pavel Yaskevich
44cb14f849 Merge pull request #67441 from xedin/fix-overly-eager-closure-resolving
[CSBindings] Prefer conjunctions over closure variables without bindings
2023-08-04 09:42:50 -07:00
Amritpan Kaur
d5f0fff83c [Tests] Add and update tests 2023-07-26 20:32:30 -07:00
Pavel Yaskevich
11ee82de7b [CSBindings] Prefer conjunctions over closure variables without bindings
If a closure doesn't have a contextual type inferred yet it should
be delayed in favor of already resolved closure conjunction because
"resolving" such a closure early could miss result builder attribute
attached to a parameter the closure is passed to.

Partially resolves https://github.com/apple/swift/issues/67363
2023-07-21 09:46:09 -07:00
Crazy凡
4e346c1cf8 Add test cases for the 'nil coalescing operator'. 2023-07-04 18:21:26 +08:00
Slava Pestov
1957bd6065 Sema: Reword diagnostics to say 'without a type annotation' instead of 'without more context' 2023-06-09 17:44:42 -04:00
Luciano Almeida
fef908efac [Sema] Increase impact of ternary then branch aiming better diagnostics 2023-05-30 20:55:50 -03:00
Anthony Latsis
3f629315a9 Merge pull request #65731 from AnthonyLatsis/always-print-any
ASTPrinter: Turn on explicit `any` printing for everything and remove the option to disable it
2023-05-16 03:27:36 +03:00
Anthony Latsis
7f6d3bcd41 ASTPrinter: Turn on explicit any printing for everything and remove the option to disable it 2023-05-13 02:55:49 +03:00
Kavon Farvardin
3e4bc82aa8 rename _forget to discard; deprecate _forget
SE-390 concluded with choosing the keyword discard rather than forget for
the statement that disables the deinit of a noncopyable type. This commit
adds parsing support for `discard self` and adds a deprecation warning for
`_forget self`.

rdar://108859077
2023-05-08 21:42:19 -07:00
Kavon Farvardin
4943beb35e emit error if _forget is used on a type with no deinit
rdar://108877261
2023-05-05 14:50:48 -07:00