Commit Graph

288 Commits

Author SHA1 Message Date
Anthony Latsis
2cd90bdd69 AST: Quote attributes more consistently in DiagnosticsSema.def 2025-04-22 18:23:36 +01:00
Slava Pestov
0882a078f7 Regression test for rdar://problem/143338891 2025-02-02 00:43:16 -05:00
Pavel Yaskevich
c922ab5634 [CSBindings] Allow optional subtype inference when closure result is not yet resolved
Similar to `Void?` we need to unwrap `$T?` because it could be later
bound to `Void` from context.

Resolves: rdar://143474313
2025-01-29 13:12:56 -08:00
Pavel Yaskevich
116d1af911 [CSBindings] Allow subtype inference from Void? for closure result types
We don't want to do that in general because injection should happen
only in one place but Void is special because it allows conversions
in that position.
2025-01-25 00:18:31 -08:00
Pavel Yaskevich
bdabde7f1a [Tests] NFC: Add a test-case for rdar://139238255 2024-11-08 16:31:11 -08:00
Pavel Yaskevich
55b8d9538d [CSSimplify] Rework how/when mismatches between optional types are fixed
- Don't attempt to insert fixes if there are restrictions present, they'd inform the failures.

  Inserting fixes too early doesn't help the solver because restriction matching logic would
  record the same fixes.

- Adjust impact of the fixes.

  Optional conversions shouldn't impact the score in any way because
  they are not the source of the issue.

- Look through one level of optional when failure is related to optional injection.

  The diagnostic is going to be about underlying type, so there is no reason to print
  optional on right-hand side.
2024-09-10 10:35:05 -07:00
Hamish Knight
89a3390aa1 [Sema] Move SingleValueStmtUsageChecker into pre-checking
These diagnostics are better suited for pre-checking
since we ought to be emitting them even if there's
some other error with the expression.
2024-08-30 18:55:48 +01:00
Slava Pestov
074684fe06 AST: Use TypeTransform::transformSubMap() to transform TypeAliasType 2024-08-21 13:21:47 -04:00
Jamie
6c676c9f22 [AST][SILOptimizer]: unify missing return diagnostics in some cases
Previously, missing return diagnostics for unreachable subscripts
differed from the treatment unreachable functions received, leading to
inconsistent diagnostic behavior. This change removes the responsibility
for handling the relevant diagnostics from the AST code, in favor of the
diagnostics implemented via the SIL optimizer. Additionally, where the
AST-generation code would previously have diagnosed a missing return for
an implicit empty getter, it will now admit as valid, deferring the
missing return diagnostics to the later SIL passes.
2024-08-07 08:14:11 -05:00
Karoy Lorentey
cafc99369b [stdlib] Round out ~Copyable generalizations in stdlib primitives
- `Optional.map`, `.flatMap`: Allow noncopyable results. Implement typed throws.

- `Result.map`, `.flatMap`: Allow noncopyable types for the new success.
- `Result.flatMapError`: Generalize for noncopyable Success.

- `Unsafe[Mutable][Buffer]Pointer.withMemoryRebound`: Alllow typed throws.
- `Unsafe[Mutable]BufferPointer: Generalize CustomDebugStringConvertible conformance for noncopyable `Element`s.

- `OpaquePointer.init(_: UnsafeMutablePointer)`, `.init(_: UnsafeMutablePointer?)`: Allow noncopyable pointee types.

- `ManagedBuffer.withUnsafeMutablePointerToHeader`, `.withUnsafeMutablePointerToElements`, `.withUnsafeMutablePointers`: Generalize for typed throws and noncopyable return types. Avoid `@_preInverseGenerics`.
- `ManagedBufferPointer`: Ditto.

- `withExtendedLifetime`: Use typed throws. Stop using `@_preInverseGenerics`.

rdar://117753275
2024-04-16 01:15:33 -07:00
Holly Borla
9ba481ad53 [Diagnostics] Clarify the wording of error_in_future_swift_version. 2024-03-01 12:05:51 -08: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
Ben Barham
8bb1ac950b [Parse] Update error if closure has unnamed parameter to warning
This reverts commit 4ba4da45b9 and updates
the warning to a warning until Swift 6.
2024-01-19 12:42:27 -08:00
Doug Gregor
f316c5fcfe Update tests for map switching to typed throws 2024-01-12 10:17:59 -08:00
Pavel Yaskevich
859106eed1 [Diagnostics] Expand trailing closure failure to handle all closure arguments 2024-01-08 14:09:18 -08:00
Pavel Yaskevich
92577723fb [Diagnostics] Add a tailored note when passing a closure to a non-closure accepting parameter 2024-01-08 14:09:18 -08:00
Alex Hoppen
4ba4da45b9 [Parse] Error if closure has an unnamed parameter
We accepted unnamed closure parameters if the type was an array literal, dictionary literal, tuple or function (because the `[` or `(` starting the type was sufficient to disambiguate the type from the parameter’s name). This was never an accepted syntax and we should disallow it.
2023-11-28 11:07:57 -08:00
Doug Gregor
3baf6ac31a Revert "[Typed throws] Support overrides that are contravariant in the thrown error" 2023-11-16 10:40:23 -08:00
Doug Gregor
b93e228f9d Update tests for map switching to typed throws 2023-11-13 14:13:44 -08:00
Pavel Yaskevich
ed046e5efe Merge pull request #68215 from xedin/rdar-112426330
[CSSimplify] Rework detection of missing/extraneous arguments in closure
2023-08-31 13:15:52 -07:00
Pavel Yaskevich
1c8b8d981a [CSSimplify] Rework detection of missing/extraneous arguments in closure
Checking type variables is no longer necessary because constraint
system now stores types of all closures it encountered, this makes
detection logic more reliable as well.

Resolves: rdar://112426330
2023-08-30 10:47:41 -07: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
Pavel Yaskevich
4b84391953 [Tests] Adjust a couple of string interpolation test-cases
`test/Constraints/interpolation_segments.swift` has been removed
because interpolations are now type-checked together with their
context, so the separate type-checking test no longer applies.
2023-07-07 19:50:46 +02: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
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
Pavel Yaskevich
7588434007 [CSSimplify] Account for tuple splat when resolving closure parameters
SE-0110 allows tuple slat between function types. The solver needs to
account for that when trying to infer parameter types from context
while resolving a closure in order to propagate types and speed up
type-checking.

Resolves: https://github.com/apple/swift/issues/62390
2022-12-05 00:28:52 -08:00
Anthony Latsis
672908d5b3 Gardening: Migrate test suite to GH issues: Constraints (4/5) 2022-08-19 05:46:49 +03:00
Anthony Latsis
a07f2b4889 Sema, DiagQoI: Fix and tailor diagnosis of explicit closure result type conflicts 2022-06-20 22:20:32 +03:00
Luciano Almeida
42cf1b73de [Sema] Adjusting repairArraLiteralUsedAsDictionary to also ignore OptToOpt promotions 2022-06-07 01:18:35 -03:00
Pavel Yaskevich
f3ff87b6f4 [Diagnostics] Diagnose re-labeling failures in ambiguity conditions
If all solutions point to the same overload choice that needs
re-labeling it's safe to diagnose it as if there was no ambiguity
because the call site is static.
2022-05-13 14:25:14 -07:00
Josh Soref
8b89b4c995 Spelling constraints (#42548)
* spelling: availability

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: conditional

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: deferred

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: disambiguate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: failed

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: interferes

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: magnify

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: occurrences

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: overload

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: redeclarations

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: simplification

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: string

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: the

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-22 09:43:26 -07:00
Doug Gregor
4db1f7d4e7 Map case variable types out of context.
Otherwise, we end up recording contextual types here.
2022-03-28 11:06:50 -07:00
Pavel Yaskevich
966f58f044 [Tests] NFC: Adjust all the test-cases improved by multi-statement inference 2022-03-08 01:13:44 -08:00
LucianoAlmeida
29879ea822 [Parser] Improving wording for invalid empty computed properties and subscripts 2022-01-04 23:46:09 -03:00
Pavel Yaskevich
bc54bc6bb7 Revert "[TypeChecker] SE-0326: Enable multi-statement closure inference by default" 2021-11-29 17:26:08 -08:00
Pavel Yaskevich
67d87e104f [Tests] NFC: Adjust all the test-cases improved by multi-statement inference 2021-11-15 16:42:06 -08:00
Pavel Yaskevich
396a93b5d3 [Diagnostics] Ignore warnings while diagnosing ambiguities
Warnings cannot lead to failures or ambiguity so let's remove
them from consideration when attempting to diagnose ambiguity
potentially caused by the same fix appearing in different
solutions.

Resolves: rdar://81228501
2021-09-08 13:13:27 -07:00
jiaren wang
d8780d33e9 [Parse] give more useful errors for forget 'do' keyword. 2021-09-01 21:18:56 +08:00
jiaren wang
c5de1f0800 [SR-14824] Improve diagnostic for multi-statement closures instead of saying "too complex closure return type" 2021-06-30 14:36:38 +08:00
Pavel Yaskevich
81ff342f4e [CSBindings] Overload variable shouldn't be delayed by its application
When inference is determining bindings of a type variable that represents
an overload set (e.g. member or operator reference), let's not consider
it as delayed due to presence of `ApplicableFunction` constraint since
argument/result type inference depends on overload set but not vice versa.

Resolves: rdar://78917861
2021-06-08 13:36:19 -07:00
Pavel Yaskevich
2b207e8f07 [Diagnostics] Augment "expected parameter" note with an argument type
Currently ambiguity notes attached to a candidate only mention
expected type and its position. To improve clarify of such notes
it's useful to print argument type as well since it's not always
clear what it is at the first glance at the code.

Resolves: SR-14634
Resolves: rdar://78224323
2021-05-26 12:58:35 -07:00
Holly Borla
e1591314cf Merge pull request #37380 from hborla/property-wrapper-inference-crash
[ConstraintSystem] Fix a constraint system crash with property wrapper inference using the $ syntax.
2021-05-12 08:55:51 -07:00
Holly Borla
c297070106 [Diagnostics] Always use the parameter name for closure parameter diagnostics,
because the $ prefix does not indicate that the parameter is anonymous.
2021-05-11 18:30:27 -07:00
Pavel Yaskevich
c346bbd514 [Diagnostics] Point out where anonymous closure parameters are used in a multi-statement closure 2021-05-11 15:07:44 -07:00
Pavel Yaskevich
3c0388d945 [CSBindings] Don't infer subtypes/supertype bindings for a closure type
A type representing a closure expression is always bound to its
"inferred" type based on the body, so contextual bindings just
serve as a trigger to "resolve" a closure. Let's not attempt any
subtype/supertype inference for a type variable representing a
closure since if "direct" bindings have failed, it wouldn't be bound
to such types regardless.

Resolves: rdar://problem/77022842
2021-05-06 13:35:45 -07:00
Luciano Almeida
f40b9b6c29 [Sema] Minor adjusting on RemoveExtraneousArguments record logic 2021-04-15 20:33:46 -03:00
Luciano Almeida
c1f43c8cc6 [tests] [SR-12483] Adding regression tests 2021-04-14 09:29:08 -03:00
Pavel Yaskevich
b088aea88b [ConstraintSystem] Allow fixing r-value -> l-value mismatch without a fix for placeholders
If left-hand side of a conversion that requires l-value is a placeholder type,
let's fix that by propagating placeholder to the order side (to allow it to
infer a placeholder if needed) without recording a fix since placeholder can
be converted to `inout` and/or l-value and already indicates existence of a
problem at some other spot in the expression.

Resolves: rdar://76250381
2021-04-12 17:14:07 -07:00
Pavel Yaskevich
df8113b5dd [Diagnostics] Contextual mismatch should be attached to the closure if its body is empty/implicit
If there is a contextual mismatch associated with a closure body,
make sure that the diagnostic is attached to the closure even
if the body is empty or implicit.

Resolves: rdar://52204608
2021-04-07 10:06:34 -07:00
Pavel Yaskevich
225e2dbeed [ConstraintSystem] Bind external closure parameter type to a concrete contextual type
Performance optimization.

If there is a concrete contextual type we could use, let's bind
it to the external type right away because internal type has to
be equal to that type anyway (through `BindParam` on external type
i.e. <internal> bind param <external> conv <concrete contextual>).

```swift
func test(_: ([String]) -> Void) {}

test { $0 == ["a", "b"] }
```

Without this optimization for almost all overloads of `==`
expect for one on `Equatable` and one on `Array` solver would
have to repeatedly try the same `[String]` type for `$0` and
fail, which does nothing expect hurts performance.

Resolves: rdar://19836070
Resolves: rdar://19357292
Resolves: rdar://75476311
2021-04-02 22:11:03 -07:00