Commit Graph

845 Commits

Author SHA1 Message Date
Slava Pestov
58f03af32d RequirementMachine: Print variadic generic parameter symbols in debug output 2022-08-10 23:44:36 -04:00
Ben Barham
a59fa46813 Merge pull request #60426 from bnbarham/include-smallvec
[Basic] Include SmallVector.h for Clang <= 5
2022-08-10 15:54:48 -07:00
Slava Pestov
4a041c57d0 AST: Rename ConformanceAccessPath to ConformancePath 2022-08-09 13:34:27 -04:00
Slava Pestov
9d96ed940f AST: Rename 'canonical wrt. generic signature' to 'reduced'
We had two notions of canonical types, one is the structural property
where it doesn't contain sugared types, the other one where it does
not contain reducible type parameters with respect to a generic
signature.

Rename the second one to a 'reduced type'.
2022-08-09 12:46:31 -04:00
Ben Barham
6de34f37e5 [NFC] Revert SmallVector<T> -> SmallVector<T, N> fixes
With the change to include `SmallVector.h` directly in `LLVM.h` rather
than forward declaring in the only case it matters (ie. Clang <= 5),
these fixes are no longer needed. Since defaulted version is preferred
when there's no better choice (which is presumably the case if that's
how they were originally added), use it instead. Some uses were instead
changed to add `llvm::` so remove that too.
2022-08-05 21:25:55 -07:00
Slava Pestov
25fb866239 RequirementMachine: Fix a request cycle
RequirementSignatureRequest
    => TypeAliasRequirementsRequest
       => isConstrainedExtension()
          => GenericSignatureRequest
             => RequirementSignatureRequest

Instead, use getTrailingWhereClause() as an approximation of
isConstrainedExtension().

Fixes rdar://problem/97236936.
2022-07-19 11:06:01 -04:00
Slava Pestov
901cdaf85e RequirementMachine: Relax assertions in verifyRewriteSystem()
We don't care as much about rules with weird shapes as long
as they're LHS- or RHS-simplified, since the new minimal conformances
algorithm no longer relies on certain invariants.

Fixes rdar://problem/94746399.
2022-07-06 14:34:26 -04:00
Slava Pestov
d3b7aab725 RequirementMachine: Remove completion procedure hacks for old minimal conformances algorithm 2022-07-06 11:57:21 -04:00
Slava Pestov
919101d4b3 RequirementMachine: Remove old minimal conformances algorithm 2022-07-06 11:57:21 -04:00
Slava Pestov
506cc356c1 RequirementMachine: New minimal conformances algorithm 2022-07-05 21:40:27 -04:00
Slava Pestov
93b94f9211 RequirementMachine: Relax an invariant in decomposeTermIntoConformanceRuleLeftHandSides() 2022-07-05 21:40:06 -04:00
Slava Pestov
e3488cecb2 RequirementMachine: Move decomposeTermIntoConformanceRuleLeftHandSides() to RewriteSystem 2022-07-05 21:40:06 -04:00
Slava Pestov
2caca13e41 RequirementMachine: Const correctness 2022-07-05 21:40:06 -04:00
Slava Pestov
cd118ea19b RequirementMachine: Don't filter out unavailable Sendable conformance if allowMissing=true
Fixes rdar://problem/94456989.
2022-06-22 00:48:59 -04:00
Slava Pestov
e805339de4 RequirementMachine: Relax assertion in verifyRewriteSystem()
We can end up with a rule that has a protocol symbol on the right hand side:

    X.Y.Z => X.W.[P]

This will occur if X.W.[P] was obtained by simplifying a term X.W.U.V via
a rule (U.V => [P]), and before completion discovers a rule
(X.W.[P] => X.W).

Fixes rdar://problem/94854326, rdar://problem/94980084.
2022-06-17 16:13:42 -04:00
Slava Pestov
bd46bdaaaa AST: Narrow the filtering of unavailable conformances to Sendable only
Remove the allowUnavailable parameter to lookupConformance(), and instead
explicitly check the result for hasUnavailableConformance() in the places
where we used to pass 'false'.

Also, narrow down this check in those places to the Sendable protocol
only, fixing a regression with Hashable conformance synthesis.

Fixes rdar://problem/94460143.
2022-06-14 21:24:08 -04:00
Slava Pestov
56f2f44592 Merge pull request #59369 from slavapestov/rqm-unavailable-conformance-fix
RequirementMachine: Fix handling of unavailable Sendable conformances in concretizeNestedTypesFromConcreteParent()
2022-06-10 21:41:10 -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
Slava Pestov
9695b1957a RequirementMachine: Concrete contraction needs to substitute the parent type of a subject type sometimes
If you have generic parameters <T, U> and requirements of the form:

- T : P
- T == ConcreteType<U>
- T.[P]U : SomeClass
- T.[P]U : SomeProto

And furthermore SomeClass does not conform to SomeProto, we can't leave
`T.[P]U : SomeClass` unsubstituted; we still have to replace `T` with
`ConcreteType<U>` to transform the latter two requirements into:

- U : SomeClass
- U : SomeProto

"Concrete contraction" is easily the hackiest part of the Requirement
Machine; I need to come up with a more principled solution for the
problem that it solves sooner or later.

Fixes rdar://problem/94150249.
2022-06-08 00:40:30 -04:00
Slava Pestov
ea980d6fbd RequirementMachine: Dump rewrite system when minimal conformances detects an invariant violation 2022-06-08 00:34:55 -04:00
Doug Gregor
904a2ff64c Allow unavailable conformances to non-Sendaable protocols during contraction
Refusing to acknowledge unavailable conformances at this point in the
requirement machine doesn't allow us to make use of unavailable
conformances from unavailable contexts, something which code currently
depends on. Limit the new filtering behavior to `Sendable` conformances
where we need them, as a narrow fix for this regression. We'll revisit
the approach here later.

Fixes rdar://94154905.
2022-06-02 13:24:37 -07: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
bfcaa39d37 Remove the GenericSignatureBuilder
Resolves rdar://problem/88136582.
2022-05-10 11:47:06 -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
f4cb13fa83 RequirementMachine: Pack the RequirementID field of Rule into a bitfield 2022-05-10 01:49:56 -04:00
Slava Pestov
77e2f4c002 RequirementMachine: getRequirementForDiagnostics() doesn't need to return an Optional 2022-05-10 01:49:56 -04:00
Slava Pestov
f901cc72b4 RequirementMachine: Move diagnostics code into a new Diagnostics.cpp 2022-05-10 01:49:56 -04:00
Slava Pestov
307aa46525 RequirementMachine: Fix a case where we diagnose requirements made redundant by an inferred requirement
We infer requirements from types appearing in parameter and result types,
like this:

    func foo<T>(_: Set<T>) // 'T : Hashable' inferred from 'Set<T>'

Normally we muffle the warning if the requirement is re-stated redundantly:

    func foo<T>(_: Set<T>) where T : Hashable // no warning

However, in some cases we failed to do this if the requirement was inferred
from a type appearing in a 'where' clause, like this:

    struct G<A, B> {}
    extension G where B : Hashable, A == Set<B> {}

This is because in this case the redundancy was detected by
RewriteSystem::addRule() returning false.

The simplest fix here is to change InferredGenericSignatureRequest
to re-order requirements so that inferred requirements appear last.
This way, if any are redundant, we won't diagnose them since it is
the inferred requirement that is redundant and not the user-written
one.

Fixes rdar://problem/92092635.
2022-05-10 01:49:56 -04:00
Slava Pestov
ae56af36d2 RequirementMachine: Workaround invariant violation when requirement signature completion fails
- If P is declared to inherit from Q and Q has an associated type named U, then
  isValidTypeInContext() returns true for a type 'T.U' where 'T' is a generic
  parameter conforming to P, and 'U' is the unresolved DependentMemberType, and
  the type 'T.U' will simplify to the term 'T.[P:U]'.

- However, if completion failed while building the rewrite system for P's
  requirement signature, then the requirement signature for P won't have a
  rule [P].[Q] => [P]. As a result, getTypeForTerm() will fail when given
  'T.[P:U]', because the property map entry for 'T' will not contain a
  conformance to Q.

Work around this by manually adding protocol inheritance rules when building
a signature from a protocol whose requirement signature failed to compute.

This was triggered by the test case I added in the previous commit to
test/Generics/non_confluent.swift.
2022-05-09 21:21:35 -04:00
Slava Pestov
0111618c50 RequirementMachine: Perform concrete contraction on protocol requirement signatures 2022-05-09 21:21:35 -04:00
Slava Pestov
75f6682969 RequirementMachine: Enable concrete contraction for nested type parameters
Fixes rdar://problem/91594361.
2022-05-09 21:21:35 -04:00
Slava Pestov
e5782a188d RequirementMachine: Refactor concrete contraction
Clean up the different cases by passing a 'Position' enum to
substTypeParameter().

Also generalize it to work with arbitrary type parameters instead
of generic parameters only, but leave this code path disabled
for now.
2022-05-09 21:21:35 -04:00
Robert Widmann
713b8b6843 Use Location of Inference Sources as a Fallback
It's possible for the requirement machine to fail to pick up a source location for its computed errors to attach to when
1) The declaration has no where clause
2) Nor does it have a generic parameter list

This is possible because of the magic of desugaring opaque types in input position to generic parameters a la

func foo(_ : some P<T, U>)

Try to use the first valid user-written inference source to derive a location.

rdar://92105516
2022-04-26 18:19:19 -07:00
Slava Pestov
f5078e2d2d Merge pull request #42538 from slavapestov/testcase-rdar92092635
Add test case for rdar://92092635
2022-04-21 21:33:07 -04:00
Josh Soref
81d3ad76ac Spelling ast (#42463)
* spelling: accessor

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

* spelling: accommodates

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

* spelling: argument

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

* spelling: associated

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

* spelling: availability

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

* spelling: available

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

* spelling: belongs

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

* spelling: bookkeeping

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

* spelling: building

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

* spelling: clazz

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

* spelling: clonable

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

* spelling: closure

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

* spelling: concatenated

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

* spelling: conformance

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

* spelling: context

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

* spelling: conversion

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

* spelling: correspondence

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

* spelling: declarations

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

* spelling: declared

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

* spelling: defining

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

* spelling: delayed

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

* spelling: dependency

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

* spelling: deployed

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

* spelling: descendants

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

* spelling: diagnose

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

* spelling: diagnostic

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

* spelling: equitable

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

* spelling: evaluation

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

* spelling: exclusivity

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

* spelling: existence

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

* spelling: existential

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

* spelling: explicit

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

* spelling: expressed

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

* spelling: for

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

* spelling: foreign

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

* spelling: function

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

* spelling: identifier

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

* spelling: implicit

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

* spelling: indices

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

* spelling: information

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

* spelling: instance

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

* spelling: interchangeable

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

* spelling: interface

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

* spelling: introduced

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

* spelling: invalid

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

* spelling: kind-in

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

* spelling: least

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

* spelling: library

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

* spelling: location

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

* spelling: namespace

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

* spelling: necessary

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

* spelling: nonexistent

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

* spelling: not

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

* spelling: number

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

* spelling: obtains

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

* spelling: occurs

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

* spelling: opaque

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

* spelling: overridden

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

* spelling: parameter

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

* spelling: precede

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

* spelling: preceding

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

* spelling: property

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

* spelling: protocol

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

* spelling: qualified

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

* spelling: recognized

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

* spelling: recursively

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

* spelling: references

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

* spelling: relaxing

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

* spelling: represented

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

* spelling: request

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

* spelling: requirement

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

* spelling: requirements

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

* spelling: retrieve

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

* spelling: returned

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

* spelling: satisfied

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

* spelling: satisfy

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

* spelling: scanner

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

* spelling: siblings

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

* spelling: simplified

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

* spelling: something

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

* spelling: source

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

* spelling: specializations

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

* spelling: specially

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

* spelling: statement

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

* spelling: stripped

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

* spelling: structure

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

* spelling: substitution

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

* spelling: the

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

* spelling: transform

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

* spelling: transformed

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

* spelling: transitively

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

* spelling: transparent

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

* spelling: typecheck

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

* spelling: unknown

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

* spelling: unlabeled

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

* spelling: unqualified

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

* spelling: whether

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

* spelling: with

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

* spelling: scanner

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

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-21 12:57:16 -07:00
Slava Pestov
504b2ebd4e RequirementMachine: Change an isExistentialType() to isConstraintType() 2022-04-21 13:49:34 -04:00
Holly Borla
de41a58051 Merge pull request #42257 from hborla/concrete-generic-params
[RequirementMachine] Diagnose type parameters that are made concrete by a same-type requirement.
2022-04-12 18:27:43 -07:00
Slava Pestov
7aee5b6cf8 Merge pull request #42324 from slavapestov/rqm-superclass-vs-objc-existential
RequirementMachine: Allow 'any C & P' to satisfy a superclass requirement 'C' if 'P' is an @objc protocol
2022-04-12 21:05:52 -04:00
Slava Pestov
4204e20508 RequirementMachine: Allow 'any C & P' to satisfy a superclass requirement 'C' if 'P' is an @objc protocol
The GenericSignatureBuilder did not actually allow this, but it's conflict
detection was imperfect so it did not flag the example in the radar;
variations on this code were rejected by the GenericSignatureBuilder.

Since the Requirement Machine finds all conflicts correctly, relax the
logic here to make this example work.

Fixes rdar://problem/91637621.
2022-04-12 16:43:56 -04:00
Holly Borla
7f30f5b039 [RequirementMachine] Downgrade concrete type parameter diagnostics to a
warning.

These diagnostics are stricter in the RequirementMachine than in the GSB,
and there's code that relies on the more relaxed diagnostics in the source
compatibility suite. Downgrade these diagnostics to a warning using
warnUntilSwiftVersion(6).
2022-04-11 19:18:00 -07:00
Slava Pestov
9170378d25 RequirementMachine: Fix handling of Sendable conformances for superclass requirements
Don't set allowMissing to true when checking conformance of a superclass requirement
to a protocol, since this prevents us from being able to express something like
'T : C, T : Sendable' to mean 'T can be any subclass of C which is also Sendable'.

Fixes rdar://problem/91530343.
2022-04-11 14:52:32 -04:00
Holly Borla
e26497a644 [RequirementMachine] Remove unnecessary conditions in concrete type parameter
diagnostics code.
2022-04-09 13:10:36 -07:00
Holly Borla
236e8e2b1d [RequirementMachine] Diagnose type parameters that are made concrete by a
same-type requirement.
2022-04-08 18:16:23 -07:00
Doug Gregor
f3fd64a71b Support missing Sendable conformances everywhere in the requirement machine 2022-04-08 16:10:38 -07:00
Doug Gregor
45c7d6f4b8 Allow missing Sendable conformances when type parametesr are made concrete.
Fixes rdar://91174106.
2022-04-08 15:42:33 -07:00
Slava Pestov
094130b9c7 RequirementMachine: Diagnose redundant concrete conformance requirements
A conformance requirement on a concrete type parameter is redundant if the
concrete type conforms to the protocol.

The replacement path for the conformance rule is based on a concrete
conformance rule introduced by the property map. Since the concrete
conformance rule is not associated with a requirement ID, this would
normally muffle the redundancy warning, because we don't want to
suggest removing a rule that depends on a non-redundant, non-explicit
rule.

However, concrete conformance rules don't actually appear in the
minimal signature, so skip them when computing the set of non-redundant,
non-explicit rules to ensure that the original conformance requirement
is still diagnosed as redundant.
2022-04-05 21:12:51 -04:00
Slava Pestov
4d15fa0087 RequirementMachine: Don't diagnose trivial circularity 'protocol P : P' as redundant
Every protocol gets an 'identity conformance' rule [P].[P] => [P].
A trivially-stated circularity is always redundant because of this
rule, and we diagnose circular inheritance elsewhere as a hard
error, so just add a special case to skip adding such a rule here
to avoid the useless warning on top of the existing error.
2022-04-05 21:12:51 -04:00
Slava Pestov
bc43cdd104 RequirementMachine: Generalize hack that allows associated type inheritance clauses to reference protocol typealiases
We want to allow this for all conformance requirements written in protocols
or the `where` clause of protocol extensions.

Fixes rdar://problem/91304291.
2022-04-05 18:42:35 -04:00
Slava Pestov
85e73ed021 RequirementMachine: Move a bit of code in computeCompletion() for clarity 2022-04-05 00:04:39 -04:00
Slava Pestov
c0c109d3f5 RequirementMachine: Tighten verifyRewriteRules() further
We should never have a rule with a protocol symbol on the *right* hand
side, like (T => U.[P]).
2022-04-04 23:52:58 -04:00