Commit Graph

425 Commits

Author SHA1 Message Date
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
Hamish Knight
c0ae9b950b Move out-of-place ThenStmt checking to constraint generation 2023-09-01 14:32:15 +01:00
Pavel Yaskevich
c50263c730 Merge pull request #66971 from xedin/fix-specialization-issues
[ConstraintSystem] Fix a couple of issues related to generic specialization
2023-06-29 12:25:11 -07:00
Pavel Yaskevich
be4df5afd2 [CSDiagnostics] Diagnose attempts to specialize with invalid number of generic arguments
```swift
struct Test<T, U> {}
_ = Test<Int>() // error
```
2023-06-27 17:24:39 -07:00
Pavel Yaskevich
cd057bba27 [CSFix] Add a fix to detect type specialization arity mismatches
The situations where number of parameters and arguments didn't match.
2023-06-27 17:24:39 -07:00
Pavel Yaskevich
40169c74ee [CSDiagnostics] Add a diagnostic for an attempt to specialize a concrete type
Diagnose attempts to specialize a concrete type or its alias:

```swift
struct Test {}
typealias X = Test

_ = X<Int>() // error
```
2023-06-27 17:24:38 -07:00
Pavel Yaskevich
ab2f47b92f [CSFix] Add a fix to detect invalid specialization of non-generic types 2023-06-27 17:24:38 -07:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07: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
Kavon Farvardin
66bf3c03fc describe illegal casts to existentials 2023-06-23 20:15:01 -07:00
Hamish Knight
f10d0df62f Merge pull request #66593 from hamishknight/pattern-error 2023-06-16 18:06:47 +01:00
Pavel Yaskevich
db024d973e [CSDiagnostics] InitAccessors: Implement invalid member reference diagnostics within init accessors 2023-06-13 10:58:50 -07:00
Pavel Yaskevich
3c924be2a0 [CSFix] InitAccessors: Add a fix that tracks invalid member references within init accessors 2023-06-13 10:58:50 -07:00
Hamish Knight
c6dd3ad839 [CS] Diagnose UnresolvedPatternExprs as part of constraint solving
Instead of diagnosing in CSApply, let's create a
fix and diagnose in the solver instead.
Additionally, make sure we assign ErrorTypes to
any VarDecls bound by the invalid pattern, which
fixes a crash.

rdar://110638279
2023-06-13 12:14:25 +01:00
Pavel Yaskevich
3b1f392d0a Merge pull request #65785 from angela-laar/fix-covariant-erasure-for-constrained-existentials
Fix covariant erasure for constrained existentials
2023-05-18 16:58:45 -07:00
Angela Laar
a9f1096839 [Generic Signature] Unify generic upper bound functions
Opened existentials should be erased to the dependent upper bound
if the dependent member can be reduced to a concrete type. This
allows the generic signature to support parameterized protocol types
and bound generic class types by producing a more specific constraint
instead of just a plain protocol or class.
2023-05-17 15:33:50 -07:00
Pavel Yaskevich
406b7a62c7 Merge pull request #65718 from xedin/diagnose-missing-each-in-expr-context
[ConstraintSystem] Detect and diagnose missing 'each' and provide a fix-it
2023-05-15 16:18:55 -07:00
Angela Laar
5c818b3fd4 [Constraint System] Fix covariant erasure for constrained existentials
Constrained existentials should be type erased to an upper bound that is dependent on other type parameters.
2023-05-15 16:17:59 -07:00
Pavel Yaskevich
e3d1317208 [ConstraintSystem] Detect and diagnose missing each for value pack reference
Detect that a value pack is missing 'each' keyword during constraint
generation and fix-it by injecting `PackElementExpr`.
2023-05-08 13:30:51 -07:00
Pavel Yaskevich
31e0a52be4 [CSFix] Add a skeleton of a fix for missing each in value pack reference 2023-05-08 13:30:51 -07:00
Pavel Yaskevich
ff527269ee Merge pull request #65610 from jreference/65330-unhelpful-error-contextually-required-as
[Sema] Fix issue 65330 Unhelpful error when missing contextually requ…
2023-05-06 12:00:03 -07:00
jreference
2cf01a18de [Sema] Fix issue 65330 Unhelpful error when missing contextually required as bridging conversion to AnyObject in structural position. Minor formatting change in previous test case 2023-05-06 10:47:15 +08:00
Hamish Knight
b5e9bbb68d [CS] Fix IgnoreUnresolvedPatternVar::diagnose such that it returns false
Returning `true` is wrong here as we could have
the error diagnosed by another fix, which if not
handled, would lead to us crashing as we assume
we diagnosed the issue. Instead, return `false`,
allowing us to at least bail with a bad error
rather than a crash.

We still need to go through and update argument
list diagnostic logic to handle patterns, but I'm
leaving that as future work for now.

rdar://107724970
rdar://107651291
2023-05-04 14:53:57 +01:00
Hamish Knight
91209d4226 [CS] NFC: Factor out AllowAssociatedValueMismatch
This seems better suited as its own fix, rather
than as part of ContextualMismatch.
2023-05-04 14:53:57 +01:00
Pavel Yaskevich
bff6a896b2 [CSGen] Detect and diagnose invalid pack expansion expressions
Detect that pack expansion expression doesn't have any pack
references during constraint generation.

Resolves: rdar://107835215
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
d8c8a39cc5 [CSFix] Add a fix to allow value pack expansions without pack references
Situations like `repeat x` where `x` is `Int` where pack expansion
expression doesn't have any pack references.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
3006f55327 [CSSimplify] Detect and diagnose passing a tuple to pack expansion that expects individual arguments 2023-05-02 09:32:19 -07:00
Pavel Yaskevich
b9ef2cabe4 [CSFix] Add a skeleton of a fix for argument destructuring to match pack expansion 2023-05-02 09:32:19 -07:00
Holly Borla
da3079de05 [ConstraintSystem] Diagnose pack expansion expressions in non-variadic contexts. 2023-03-21 21:58:05 -07:00
Pavel Yaskevich
230dfcc30c Merge pull request #64490 from xedin/rdar-102412006
[CSFix] Diagnose an invalid member reference in ambiguous contexts
2023-03-21 14:39:59 -07:00
Holly Borla
cb19fc3a71 [ConstraintSystem] Enforce TVO_CanBindToPack, and diagnose pack references outside
of pack expansion expressions.
2023-03-20 20:13:48 -07:00
Pavel Yaskevich
d7a41c2aff [CSFix] Diagnose an invalid member reference in ambiguous contexts
For example @objc lookup could find multiple instance members
on `AnyObject`. It should be allowed to diagnose issues with
that as non-ambiguous if all fixes have the same kind/base type.

Resolves: rdar://102412006
2023-03-20 15:15:55 -07:00
Pavel Yaskevich
342e5f6725 Merge pull request #64036 from xedin/rdar-106054263
[CSSimplify] Detect and diagnose generic argument mismatches individually
2023-03-06 10:18:09 -08:00
Holly Borla
8012e45109 [Diagnostics] Diagnose pack element expressions containing a non-pack subexpression. 2023-03-05 00:11:54 -08:00
Pavel Yaskevich
059891771f [CSFix] Implement coalescing for generic argument mismatch fixes 2023-03-04 21:57:47 -08:00
Andrew Trick
185e6fabd5 Add TypeBase::isArrayType helper API.
This is also needed in SIL diagnostics, not just Sema diagnostics,
because implicit Array conversion generates special SIL patterns.
2023-02-27 21:51:17 -08:00
swift-ci
bf2816b7fd Merge pull request #62599 from kavon/ban-moveonly-generic-subst
Emit error when move-only type is used as a generic type
2023-02-02 03:23:14 -08:00
Kavon Farvardin
b7f4344888 make MustBeCopyable::diagnoseForAmbiguity robust
Previous implementation blindly took the first solution
and tried to diagnose, but that's not a safe assumption.

It's possible that among the solution-fix pairs, one
of `noncopyableTy`'s within the fix differs from the
others. Things probably can go wrong because the solution
doesn't correspond to that type.

Also, in that case we'd be emitting a diagnostic that
may not make any sense. In such cases, now we decline
to emit a diagnostic.

Thanks Pavel for catching this.
2023-02-01 23:38:28 -08:00
Kavon Farvardin
ab130883a3 Initial ban of move-only types from being used generically
Since values of generic type are currently assumed to always
support copying, we need to prevent move-only types from
being substituted for generic type parameters.

This approach leans on a `_Copyable` marker protocol to which
all generic type parameters implicitly must conform.

A few other changes in this initial implementation:

- Now every concrete type that can conform to Copyable will do so. This fixes issues with conforming to a protocol that requires Copyable.
- Narrowly ban writing a concrete type `[T]` when `T` is move-only.
2023-02-01 23:38:28 -08:00
Alex Hoppen
acca597cd0 Merge pull request #59984 from ahoppen/pr/typecheck-multistamtent-closures-in-result-builders
[Sema] Type check multi-statement closures inside result builders
2023-02-02 08:08:22 +01:00
Holly Borla
e41cdfbdd4 Merge pull request #63341 from hborla/generalize-macro-expansion-expr
[Macros] Generalize `MacroExpansionExpr` and use it for both freestanding and attached macros.
2023-02-01 11:04:39 -08:00
Alex Hoppen
2c5b193892 [Sema] Type check multi-statement closures inside result builders 2023-02-01 10:29:56 +01:00
Holly Borla
9b5cf1d2ff [Diagnostics] Remove the MacroMissingArguments constraint fix and its
associated failure diagnostic.

This constraint fix is unused now that MacroExpansionExpr always has an
argument list, and goes through the AddMissingArguments constraint fix for
this error.
2023-01-31 17:45:31 -08:00
Pavel Yaskevich
9b688e9ca6 [CSFix] Move diagnoseForAmbiguity to RequirementFix
The logic is common for all types of fixes that represent
a requirement failure.
2023-01-26 15:55:50 -08:00
Pavel Yaskevich
edb6ef0d9a [CSFix] NFC: Add a common base class for all requirement failures 2023-01-26 15:55:50 -08:00
Luciano Almeida
e513d23c0b [Sema] Improving global actor function mismatch diagnostic 2023-01-13 20:47:23 -03:00
Doug Gregor
71ca9c86e6 [Macros] Diagnose when we forget to provide macro arguments.
Unlike functions, you can't curry macros; diagnose when one omits the
arguments in a macro expansion of a macro that has a parameter list.
2023-01-02 21:22:04 -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
e966b4ef7d [CSDiagnostics] Add an error message for pack expansion expressions over
packs that don't have the same shape.
2022-12-21 08:25:10 -05:00
Slava Pestov
bb045423b3 Sema: Add diagnostics for ShapeOf constraint 2022-10-25 13:20:38 -04:00