Commit Graph

47 Commits

Author SHA1 Message Date
Slava Pestov
91503710bc AST: Remove origType parameter from ProtocolConformanceRef::getTypeWitness() 2025-04-03 17:35:32 -04:00
Slava Pestov
afbd81a599 AST/Sema: Use ProtocolConformanceRef::getTypeWitness() 2024-09-19 14:18:33 -04:00
Slava Pestov
da0130d297 AST: Replace calls to substBaseType() with getAssociatedType() 2024-09-19 14:18:32 -04:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Slava Pestov
86d567f95a AST: ModuleDecl::lookupConformance() is a static method 2024-07-06 12:05:47 -04:00
Slava Pestov
fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04:00
Slava Pestov
273c4b2b1a RequirementMachine: Convert to new assertions 2024-06-22 08:53:22 -04:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Slava Pestov
bee9fee58b RequirementMachine: Don't call ProtocolDecl::getSuperclass() in concrete contraction
This changes the minimized signature in a very narrow edge case.

If you have

    class C : P {}

and also

    protocol P : C {}
    protocol P where Self : C {}

then <T where T : P, T : C> now becomes <T : P> both with spellings;
before, the first one gave <T : P> and the second <T : C>.
2024-03-21 14:49:05 -04:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Slava Pestov
d2f136254e AST: Remove fromDefault and inferred from StructuralRequirement 2024-02-02 14:57:20 -05:00
Kavon Farvardin
63b3e7624d [NCGenerics] fold InverseType into PCT
We already need to track the inverses separate from the members in a
ProtocolCompositionType, since inverses aren't real types. Thus, the
only purpose being served by InverseType is to be eliminated by
RequirementLowering when it appears in a conformance requirement.

Instead, we introduce separate type InverseRequirement just to keep
track of which inverses we encounter to facilitate cancelling-out
defaults and ensuring that the inverses are respected after running
the RequirementMachine.
2023-12-07 22:14:23 -08: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
Holly Borla
45722bf277 [RequirementMachine] Use pack expansion types as a source for requirement inference.
Previously, only the pattern type was added as a requirement inference source, but
the pack expansion type is necessary for inferring same-shape requirements for
packs that are expanded in parallel.
2022-10-23 19:51:05 -07:00
Holly Borla
38a2c8218b [Requirement] Rename RequirementKind::SameCount to SameShape. 2022-10-06 20:48:40 -07:00
Holly Borla
ae5ebba2c1 [RequirementMachine] Add same-length requirement inference for pack
expansion types.
2022-10-06 20:38:30 -07:00
Slava Pestov
5c32f2136e AST: Introduce RequirementKind::SameCount 2022-08-23 11:12:00 -04:00
Slava Pestov
15b99ae416 RequirementMachine: Concrete contraction gives up on recursive same-type and superclass requirements
A requirement of the form T == G<T> is never valid, and T == G<T.A>
will leave behind a term T.[P:A] once the conformance T : P is
dropped in contraction. So just ignore recursive requirements
here, allowing them to be properly diagnosed later.
2022-08-11 14:10:01 -04:00
Slava Pestov
7eeb56309b RequirementMachine: Small concrete contraction cleanup 2022-08-11 14:10:01 -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
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
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
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
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
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
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
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
41c04c5f7b RequirementMachine: Add a comment explaining a recent change to concrete contraction 2022-04-04 12:41:57 -04:00
Slava Pestov
02bfb79935 RequirementMachine: Concrete contraction discards errors from inferred requirements 2022-04-01 23:55:19 -04:00
Slava Pestov
175d5860cf RequirementMachine: Don't substitute unsatisfied layout requirements in concrete contraction 2022-04-01 23:06:19 -04:00
Slava Pestov
d385b73cd6 RequirementMachine: Plumb source locations through concrete contraction 2022-04-01 22:33:03 -04:00
Slava Pestov
ac7efc1de7 Merge pull request #41935 from slavapestov/rqm-concrete-contraction-unrelated-alias
RequirementMachine: Another silly GenericSignatureBuilder compatibility hack for concrete contraction
2022-03-21 22:44:40 -04:00
Slava Pestov
9f9b81b156 RequirementMachine: Explicitly specify SmallVector size 2022-03-21 16:59:03 -04:00
Slava Pestov
dc82433c78 RequirementMachine: Another silly GenericSignatureBuilder compatibility hack for concrete contraction 2022-03-21 16:31:18 -04:00
Slava Pestov
b0a4efba54 RequirementMachine: Fix lookupConcreteNestedType() to cope with opaque archetypes 2022-03-17 17:45:52 -04:00
Slava Pestov
d90e8122aa RequirementMachine: Factor out duplicated lookupConcreteNestedType() utility 2022-03-16 13:26:37 -04:00
Slava Pestov
8176b61d60 RequirementMachine: Tweak debug output and add an assertion 2022-03-16 12:24:10 -04:00
Slava Pestov
64f049a3d8 RequirementMachine: Fix ConcreteContraction::substTypeParameter() for non-nominal base type
We can handle a non-nominal base type here as long as the DependentMemberType
we're substituting this into is resolved.
2022-03-03 14:05:02 -05:00
Slava Pestov
589c1a3c11 RequirementMachine: A couple of improvements to concrete contraction
- Allow duplicate concrete type and superclass requirements on the
  same generic parameter, as long as they're identical. This can
  arise if requirement inference infers a requirement which the
  user then explicitly re-states.

- If duplicate requirements are found that name different types, drop
  only that generic parameter from consideration without giving up
  entirely.

- If a generic parameter is subject to both a concrete type and a
  superclass requirement, proceed with the concrete type requirement
  since it is more specific instead of giving up.
2022-03-03 14:05:02 -05:00
Slava Pestov
0d094241d8 RequirementMachine: Fix rogue debug output when debug flag was disabled 2022-03-03 14:05:02 -05:00
Slava Pestov
bd5a437c39 Merge pull request #41615 from slavapestov/rqm-protocol-superclass-circular-conformance
RequirementMachine: Fix minimization when protocol is constrained to a class that conforms to the protocol
2022-03-02 12:17:12 -05:00
Slava Pestov
5281426569 RequirementMachine: Fix minimization when protocol is constrained to a class that conforms to the protocol
Consider this example:

    protocol P : C {}
    class C : P {}

    <T where T : P>

The GenericSignatureBuilder thinks the minimized signature is
<T where T : P>. The RequirementMachine would minimize it down to
<T where T : C>. The latter is more correct, since the conformance
here is concrete and no witness table needs to be passed in at
runtime, however for strict binary compatibility we need to produce
the same signature as the GenericSignatureBuilder.

Accomplish this by changing the minimal conformances algorithm to
detect "circular concrete conformance rules", which take the form

    [P].[concrete: C : Q]

Where Q : P. These rules are given special handling. Ordinarily a
protocol conformance rule is eliminated before a concrete conformance
rule; however concrete conformances derived from circular
conformances are considered to be redundant from the get-go,
preventing protocol conformances that can be written in terms of
such concrete conformances from themselves becoming redundant.

Fixes rdar://problem/89633532.
2022-03-02 03:13:41 -05:00
Slava Pestov
8570b5656c RequirementMachine: Fix compile error on Linux 2022-03-01 00:56:43 -05:00
Slava Pestov
8e09ba8b45 RequirementMachine: Introduce 'concrete contraction' pre-processing pass before building rewrite system
See the comment at the top of ConcreteContraction.cpp for a detailed explanation.

This can be turned off with the -disable-requirement-machine-concrete-contraction
pass, mostly meant for testing. A few tests now run with this pass both enabled
and disabled, to exercise code paths which are otherwise trivially avoided by
concrete contraction.

Fixes rdar://problem/88135912.
2022-02-25 11:48:38 -05:00