Commit Graph

347 Commits

Author SHA1 Message Date
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
525b6effc6 Sema: Use diagnoseOrDefer() in ConformanceChecker::ensureRequirementsAreSatisfied() 2024-01-21 08:34:41 -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
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
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
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
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
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
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
Allan Shortlidge
5da4e2df40 Sema: Allow unavailable decls to witness requirements in more conformances.
In https://github.com/apple/swift/pull/63898 conformance requirement
typechecking was relaxed to allow unavailable decls to witness conformance
requirements as long as the conforming nominal was also unavailable. However,
only nominals that were directly marked unavailable were accepted. Nominals
that are declared in unavailable scopes should also be allowed to have
unavailable wintesses.

Resolves rdar://107052715
2023-03-22 12:37:51 -07:00
Allan Shortlidge
9521764b18 Sema: Allow unavailable protocol witnesses in unavailable nominals or extensions.
Resolves rdar://99451416
2023-02-28 13:54:35 -08:00
Anthony Latsis
0ab77fb849 Gardening: Migrate test suite to GH issues (file names): decl 2022-09-18 17:54:58 +03:00
Anthony Latsis
4ee63da498 Gardening: Migrate test suite to GH issues: decl/protocol 2022-08-30 04:08:00 +03:00
Slava Pestov
4473363844 Sema: Check conditional requirements in inferTypeWitnessesViaValueWitnesses()
Otherwise, a conditional conformance could not rely on a default
implementation in a protocol extension.

Fixes rdar://problem/91451771.
2022-06-29 12:54:07 -04:00
Slava Pestov
0254c0ee7c RequirementMachine: Fix handling of unavailable Sendable conformances in concretizeNestedTypesFromConcreteParent()
We can't just ignore unavailable conformances because the
generic signature we're computing might itself be attached
to an unavailable declaration.

Until we get a proper fix, only drop unavailable conformances
to Sendable here.

Fixes rdar://problem/94305457.
2022-06-10 17:46:40 -04:00
Anthony Latsis
94796e03ff Add regression test to close #52588 2022-06-06 20:04:42 +03:00
Anthony Latsis
927d09915b Add regression test to close #52024 2022-06-06 05:39:54 +03:00
Anthony Latsis
0e9a3cb70a Add regression test to close #45389 2022-06-05 21:17:36 +03:00
Doug Gregor
c9c50b4ae0 [Requirement machine] Ignore unavailable conformances on superclass constraints.
When determining whether a superclass conforms to a particular protocol,
skip unavailable conformances. This way, we don't minimize away a
constraint that might only apply to subclasses of the specified
superclass.

Fixes rdar://91853658.
2022-05-27 13:09:15 -07:00
Slava Pestov
dac8d666ee Stop passing -requirement-machine-{abstract,inferred,protocol}-signatures flags in tests
These flags are now no-ops.
2022-05-10 12:56:17 -04:00
Slava Pestov
f39372b33d RequirementMachine: Turn off redundant requirement warnings by default and add -warn-redundant-requirements frontend flag 2022-05-10 01:49:56 -04:00
Slava Pestov
4e89f73e90 RequirementMachine: Allow markConflicting() on already-conflicting frozen rules 2022-04-01 01:05:54 -04:00
Slava Pestov
01fea564fb RequirementMachine: Skip emitting diagnostics containing ErrorTypes 2022-04-01 01:04:54 -04:00
Slava Pestov
b4b873332f Update -requirement-machine-* flags in various tests
- Don't pass 'verify' since it's now the default
- Update tests where diagnostics changed in a correct way to pass 'on' instead
- Delete compiler_scale/explicit_requirements_perf.swift since it's not testing anything with the requirement machine
2022-03-31 15:57:36 -04:00
Anthony Latsis
ba7f301e83 ConformanceChecker: Always diagnose a conformance failure if the requirement check fails in 'ensureRequirementsAreSatisfied' 2022-03-28 23:22:36 +03:00
Anthony Latsis
f37ac3ecec [NFC] Add a test that does not work with experimental associated type inference enabled 2022-03-25 08:59:43 +03:00
Anthony Latsis
38e48ac15d TypeWitnessSystem: Disable by default 2022-03-25 08:45:54 +03:00
Slava Pestov
441fa1679a RequirementMachine: Splitting concrete equivalence classes in protocol requirement signatures 2022-03-22 15:02:06 -04:00
Slava Pestov
df49e112e3 RequirementMachine: Skip 'verify' check if completion failed
We obviously don't have a valid generic signature in this case.
2022-03-14 12:33:18 -04:00
Slava Pestov
1f83cd0b49 RequirementMachine: Don't eliminate a rule via a path involving a protocol typealias rule
If a rule is not a protocol typealias rule, and does not contain any unresolved
symbols, do not attempt to eliminate it via a protocol typealias rule.

This fixes a bunch of cases where the RequirementMachine was overly-eager to
remove rules.
2022-03-11 17:28:01 -05:00
Holly Borla
397684909b [RequirementMachine] Suppress redundancy warnings when an explicit, redundant
rule has a non-explicit, non-redundant rule in its rewrite path.

This fixes bogus redundancy diagnostics in cases where the canonical form
of a redundant rule is not explicit in source, e.g.

protocol Swappable2 {
  associatedtype A
  associatedtype B
  associatedtype Swapped : Swappable2
    where Swapped.B == A,
          Swapped.Swapped == Self
}

in the above case, the canonical rule for `Swapped.B == A` is the rule
[Swappable2:Swapped].[Swappable2:A] => [Swappable2:B], which is not
explicit.
2022-03-09 12:14:58 -08:00