Commit Graph

1081 Commits

Author SHA1 Message Date
Kavon Farvardin
74a3839cf3 Merge pull request #71515 from kavon/ncgenerics-test-fixes-kavon-v7
Ncgenerics test fixes kavon v7
2024-02-09 19:39:18 -08:00
Slava Pestov
06b1aee360 Evaluator: Cache circular evaluation to avoid redundant diagnostics
Previously, if a request R evaluated itself N times, we would emit N
"circular reference" diagnostics. These add no value, so instead let's
cache the user-provided default value on the first circular evaluation.

This changes things slightly so that instead of returning an
llvm::Expected<Request::OutputType>, various evaluator methods take
a callback which can produce the default value.

The existing evaluateOrDefault() interface is unchanged, and a new
evaluateOrFatal() entry point replaces
llvm::cantFail(ctx.evaluator(...)).

Direct callers of the evaluator's operator() were updated to pass in
the callback. The benefit of the callback over evaluateOrDefault() is
that if the default value is expensive to constuct, like a dummy
generic signature, we will only construct it in the case where a
cycle actually happened, otherwise we just delete the callback.

(cherry picked from commit b8fcf1c709efa6cd28e1217bd0efe876f7c0d2b7)
2024-02-09 16:02:24 -08:00
Anthony Latsis
ef931342ad AST/ASTWalker: Refactor for recursive MemberTypeRepr representation 2024-02-09 17:33:21 +03:00
Doug Gregor
2370214f06 Allow rethrow-like typed throws functions as witnesses
Extend an existing source compatibility hack that allows a typed
throws-using function that *looks* like a rethrows function to be
treated like a rethrows function. Here, do so when a typed-throws
function is witnessing a rethrows requirement.

Fixes rdar://122588459.
2024-02-08 22:45:52 -08:00
Kavon Farvardin
9f3743dec0 NCGenerics: mark hanging tests as unsupported 2024-02-04 19:14:53 -08:00
Slava Pestov
387156ae11 Merge pull request #71354 from slavapestov/check-type-witness-fix
Sema: Fix problems with checkTypeWitness()
2024-02-03 08:28:14 -05:00
Slava Pestov
1867b38a5f Sema: Fix problems with checkTypeWitness()
Our tentative witness might have weird concrete DependentMemberTypes
in it. In that case, perform a more relaxed check that can succeed
as long as the witness is not completely invalid.

This problem was exposed by existing test cases starting to fail
once implicit Copyable requirements were introduced, because these
concrete DependentMemberTypes do not conform to protocols. This now
postpones the check until all witnesses have been fully substituted.

This also fixes a long-standing bug with superclass requirements on
associated types.

Fixes https://github.com/apple/swift/issues/48770,
https://github.com/apple/swift/issues/51772,
rdar://95729075.
2024-02-02 23:30:44 -05:00
Slava Pestov
a0480b613b Add two regression tests for bugs already fixed 2024-02-02 23:30:44 -05:00
Slava Pestov
ea15d9f9b2 Stop passing -warn-redundant-requirements in tests 2024-02-02 14:57:19 -05:00
Slava Pestov
960c1b0fce Sema: Performance optimization for associated type solver
Don't explore parts of the solution space that would lead to a
worse solution than what we already have.
2024-01-29 23:38:45 -05:00
Slava Pestov
96b6e9e9f1 Sema: Fix logic error in associated type inference solver
This problem was introduced in 2017 by commit bbaa7f7163.

If a protocol requirement has multiple potential witnesses, and one
of those witnesses only introduces tautological type witness bindings,
we would be forced to choose among the remaining witnesses.

However, this did not account for the possibility that the
tautological witness is the correct choice; it's possible that we
will infer the same type witnesses via a different protocol
requirement.
2024-01-29 23:38:45 -05:00
Slava Pestov
9c778f3ee4 Sema: Don't let DependentMemberTypes with concrete bases leak out 2024-01-29 21:22:55 -05:00
Slava Pestov
35730d6bcf Sema: Don't be so eager to force type witnesses when resolving a value witness
resolveWitnessViaLookup() was only called from one place and didn't
need to force anything at all, and resolveSingleWitness() now only
forces the type witnesses actually referenced by the requirement.

An old crasher test case that was considered "fixed" when we started
to diagnose it now type checks correctly.
2024-01-26 18:51:08 -05:00
Slava Pestov
23599b667b Sema: Heuristic to prefer shorter type parameters when resolving abstract witnesses 2024-01-26 13:57:29 -05:00
Slava Pestov
c614c01c56 Sema: Refine the 'tautological witness' condition 2024-01-25 22:03:50 -05:00
Slava Pestov
05f04c0996 Sema: Try to resolve type witnesses on a more specific conformance first 2024-01-25 22:01:44 -05:00
Slava Pestov
6306884abe Sema: Only call diagnoseMissingWitnesses() once 2024-01-24 12:05:24 -05:00
Slava Pestov
525b6effc6 Sema: Use diagnoseOrDefer() in ConformanceChecker::ensureRequirementsAreSatisfied() 2024-01-21 08:34:41 -05:00
Slava Pestov
fe8f6ae7d8 Sema: More checkConformance() => lookupConformance() 2024-01-18 16:15:02 -05:00
Slava Pestov
b59353bc61 Sema: Change a few checkConformance() calls to lookupConformance() 2024-01-18 12:32:04 -05:00
Slava Pestov
ce4b29ec71 Sema: Fix logic error in TypeReprCycleCheckWalker handling of 'Self.Foo'
We don't want to throw out a witness if it has any mention of 'Self.Foo';
we must also check that 'Foo' is one of the associated types being
inferred. Otherwise, it may be a protocol type alias, in which case
we want to proceed with the witness.

This fixes a regression from a recent change, a576984e2f.

Fixes rdar://problem/120743365.
2024-01-10 16:52:11 -05:00
Doug Gregor
b90177150f Enable availability attributes for associated types
Allow `@available` on associated types, which can be introduced after
the protocol was introduced. These work precisely how on expects,
because all of the infrastructure for introducing associated types
later on has been available for a while, with two small restrictions:

1. If one uses the primary associated type syntax (e.g., `P<A, B>`),
then the primary associated types must also be available in the
current context.
2. Adding a new associated type to a resilient protocol requires that
associated type to have a default.
2024-01-05 10:56:08 -08:00
Doug Gregor
b7fd43949a Remove the TypedThrows experimental feature from the remaining tests 2024-01-03 22:08:56 -08:00
Slava Pestov
7e39fdb9ea Sema: Teach TypeReprCycleCheckWalker to avoid more cycles
More declaration order issues uncovered by lazy type checking.

Fixes rdar://119499800.
2023-12-11 13:45:23 -05:00
Pavel Yaskevich
ebbb9de104 Merge pull request #70331 from xedin/replace-warn-concurrency-flag-in-tests
[Concurrency] NFC: Switch tests to use `-strict-concurrency=complete`…
2023-12-11 09:11:46 -08:00
Slava Pestov
07168d7ebd Sema: Open-code mapTypeOutOfContext() on weird types
Associated type inference likes to wrap concrete types in
DependentMemberTypes. Calling mapTypeOutOfContext() on such
a type can crash. Open-code the transform instead.

Longer term, I want to clean up the invariants here and stop
mapping things in and out of different contexts randomly.
2023-12-08 17:26:25 -05:00
Slava Pestov
d0bd026077 Sema: Prefer generic parameters over defaults and same-type requirements with -enable-experimental-associated-type-inference 2023-12-08 17:26:15 -05:00
Pavel Yaskevich
dbf1146963 [Concurrency] NFC: Switch tests to use -strict-concurrency=complete instead of -warn-concurrency 2023-12-08 14:10:51 -08:00
Slava Pestov
335d6ffea1 Frontend: Add -disable-experimental-associated-type-inference flag 2023-12-08 13:39:10 -05:00
Slava Pestov
d5bf5e331b Sema: Add another test that passes with -enable-experimental-associated-type-inference 2023-12-07 19:28:22 -05:00
Slava Pestov
569a73ee8c Sema: Add a test for an older failure with -enable-experimental-associated-type-inference 2023-12-07 19:27:59 -05:00
Slava Pestov
62545881d3 Sema: Add a test that passes with -enable-experimental-associated-type-inference 2023-12-07 19:27:59 -05:00
Slava Pestov
af070cbfbf Sema: Pass down tentative type witnesses in substCurrentTypeWitnesses() 2023-12-07 19:27:59 -05:00
Slava Pestov
52e3031bce Sema: Fancier handling of associated type defaults
Consider this code:

    protocol P {
      associatedtype A
      ...
    }

    protocol Q: P {
      associatedtype A = Int
      ...
    }

    struct S: Q {
      ...
    }

If we check the [S: Q] conformance first, we get the right type witness
assignment, but if we check [S: P] first, conformance checking fails.

Instead of looking at an associated type declaration and any associated
types that it overrides, we now look through all associated types with the
same name among the protocols the adoptee conforms to. This allows us to
find the default assignment 'A = Int' from Q regardless of request
evaluation order.

Fixes rdar://problem/119052782.
2023-12-06 14:59:19 -05:00
Doug Gregor
257a51e724 Update test for improved require initializer checking 2023-12-05 08:46:03 -08:00
Slava Pestov
1b1963e904 Sema: Remove 'inferred result type requires explicit coercion' diagnostic 2023-11-27 14:05:36 -05:00
Slava Pestov
08f8781798 RequirementMachine: Don't drop errors on the floor with @objc protocols 2023-11-14 15:46:43 -05:00
Slava Pestov
e65290c2bd Sema: Associated type inference skips witnesses that might trigger a request cycle
This implements a structural walk over the TypeRepr to catch
situations where we attempt to infer `A` from `func f(_: A)`,
which references the concrete `A` that will be synthesized
in the conforming type.

Fixes:
- rdar://34956654 / https://github.com/apple/swift/issues/48680
- rdar://38913692 / https://github.com/apple/swift/issues/49066
- rdar://56672411
- https://github.com/apple/swift/issues/50010
- rdar://81587765 / https://github.com/apple/swift/issues/57355
- rdar://117442510
2023-11-14 12:08:59 -05:00
Danilo Camarotto
78adeb6070 Incorrect location of 'async' for protocol in actor's fix suggestion (#69246)
* implementing changes and tests

* added unit test using throws

* adding test with distributed actor

* moved distributed-actor tests to another file

* revert import Distributed
2023-11-06 13:44:46 -03:00
Harlan Haskins
4ac34a40ea @retroactive conformance syntax and checking (#36068) 2023-10-20 14:27:03 -07:00
Doug Gregor
7d7c726efe [Typed throws] Teach associated type inference to infer from thrown errors
When comparing a requirement that uses typed throws and uses an
associated type for the thrown error type against a potential witness,
infer the associated type from the thrown error of the
witness---whether explicitly specified, untyped throws (`any Error`),
or non-throwing (`Never`).
2023-10-09 21:21:21 -07:00
Doug Gregor
fc78ad3263 [Typed throws] Teach witness matching to check typed throws. 2023-10-09 21:21:21 -07:00
Kavon Farvardin
a69f9e434d experimental feature tests require asserts
When using an experimental feature that is not available
in production, a test needs to have `REQUIRES: asserts`
so that non-assert builds don't unexpectedly fail.
2023-09-23 19:52:45 -07: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
Slava Pestov
1a72ca133b Sema: Enforce coherence condition on type witnesses of tuple conformance
We want that (repeat each Element).[P]A == (repeat (each Element).[P]A),
where on the left is type witness projection from the tuple conformance,
and on the right is a tuple with a pack expansion.
2023-09-07 00:47:00 -04:00
Nishith Shah
8e2e625543 [Diagnostics] Use imperative msg for protocol conformance & switch-case fixits
This commit changes fixit messages from a question/suggestion to an
imperative message for protocol conformances and switch-case. Addresses
https://github.com/apple/swift/issues/67510.
2023-08-13 22:34:26 -07:00
Allan Shortlidge
d438bac68d AST: Fix EnumDecl's hasPotentiallyUnavailableCaseValue() cache.
The cached values for `hasOnlyCasesWithoutAssociatedValues()` and
`hasPotentiallyUnavailableCaseValue()` are computed in a single pass over the
elements of an `EnumDecl`. However, the pass would return early after finding
an element with an associated value, without checking whether any of the rest
of the elements were potentially unavailable. This made `Comparable` synthesis
succeed for any enum with potentially unavailable elements so long as the first
element in the enum has an associated value.
2023-08-10 21:59:55 -07:00
Pavel Yaskevich
b8bbc8a9ce Merge pull request #67679 from xedin/fix-distributed-actor-init-synthesis
[TypeChecker] Make sure that distributed actors always get "default" init
2023-08-03 09:35:49 -07:00
Pavel Yaskevich
e2bac24023 [TypeChecker] Make sure that distributed actors always get "default" init
Default initialization of stored properties doesn't play a role
in default init synthesis for distributed actors.
2023-08-02 10:30:44 -07:00
Allan Shortlidge
2442737a7d NFC: Fix %target-run-simple-swift invocations in a couple of tests.
The `-unavailable-decl-optimization` flag is a frontend flag and therefore
requires `-Xfrontend` when passed through the driver.
2023-07-31 17:26:34 -07:00