Commit Graph

310 Commits

Author SHA1 Message Date
Doug Gregor
f9c34756ef Parameterize conformance lookup on whether "missing" conformances are allowed.
Many clients of the conformance lookup operations would prefer to get
an invalid conformance (== there is no conformance) rather than a
missing conformance. Parameterize the conformance lookup operations so
that most callers won't see missing conformances, by filtering them
out at the end. Opt-in those callers that do want to see missing
conformances so they can be diagnosed.
2021-08-03 00:10:45 -07:00
Slava Pestov
58cb851789 RequirementMachine: Relax some cross-checking assertions
getConcreteType() doesn't have to return a canonical type, and the GSB and
RequirementMachine disagree on how canonical the result should be.
2021-07-31 23:51:35 -04:00
Slava Pestov
212099be82 RequirementMachine: Move RequirementMachine class to swift::rewriting namespace 2021-07-23 17:21:58 -04:00
Slava Pestov
379359c08e RequirementMachine: Move RequirementMachine.h to lib/AST/RequirementMachine 2021-07-23 17:21:57 -04:00
Robert Widmann
1329f3cfbd [NFC] Lift getGenericEnvironment() into GenericSignature 2021-07-22 23:33:02 -07:00
Robert Widmann
74b653518d Lift GenericSignature::hasTypeVariable 2021-07-22 23:27:06 -07:00
Robert Widmann
d86551de67 Lift Requirement and Parameter Accessors up to GenericSignature
Start treating the null {Can}GenericSignature as a regular signature
with no requirements and no parameters. This not only makes for a much
safer abstraction, but allows us to simplify a lot of the clients of
GenericSignature that would previously have to check for null before
using the abstraction.
2021-07-22 23:27:05 -07:00
Slava Pestov
b718663719 RequirementMachine: Tri-state enable flag, and move queries to GenericSignatureQueries.cpp
The -enable-requirement-machine and -disable-requirement-machine flags are now
replaced by a new flag -requirement-machine={on,off,verify}.
2021-07-17 00:05:05 -04:00
Slava Pestov
3e47d94b6c RequirementMachine: Implement GenericSignature::getLocalRequirements() query 2021-07-17 00:05:05 -04:00
Slava Pestov
c2a275ef0f AST: Factor out GenericSignature::getLocalRequirements() method
This encapsulates GenericEnvironment's usage of the GSB.
2021-07-17 00:05:05 -04:00
Slava Pestov
a65effed60 AST: Lazy construct GenericSignatureBuilder in a GenericEnvironment 2021-07-16 18:15:23 -04:00
Saleem Abdulrasool
d783b6add0 Revert "Begin untangling GenericEnvironment from GenericSignatureBuilder" 2021-07-15 16:05:05 -07:00
Slava Pestov
323e52e66a AST: Lazy construct GenericSignatureBuilder in a GenericEnvironment 2021-07-14 17:18:15 -04:00
Slava Pestov
579173682f AST: Fix bad merge 2021-07-14 10:10:11 -04:00
Slava Pestov
043f5016b4 Merge pull request #38353 from slavapestov/requirement-machine-superclass
RequirementMachine: Fixes for nested type concretization, and some more queries
2021-07-14 08:46:55 -04:00
Slava Pestov
d7bb7e67d6 RequirementMachine: Implement GenericSignature::isCanonicalTypeInContext() query 2021-07-14 00:16:06 -04:00
Slava Pestov
499bff25bc RequirementMachine: Implement GenericSignature::getConformanceAccessPath() query
This is just a straight port of the existing code in the GSB, with minimal changes.

It could be made more efficient in the future by trafficking in Terms rather than
Types, avoiding some intermediate conversion and canonicalization steps.
2021-07-14 00:16:02 -04:00
Slava Pestov
b2ae546e17 RequirementMachine: Implement GenericSignature::lookupNestedType() query
This logic is mostly carried over from GenericSignatureBuilder::lookupNestedType().
2021-07-14 00:15:42 -04:00
Slava Pestov
422ae0ae5c RequirementMachine: Implement GenericSignature::getConcreteType() query 2021-07-14 00:15:42 -04:00
Slava Pestov
060f490983 RequirementMachine: Implement GenericSignature::getSuperclassBound() query 2021-07-14 00:15:42 -04:00
Slava Pestov
c5b299ceea AST: Factor out GenericSignature::lookupNestedType() 2021-07-14 00:06:17 -04:00
Slava Pestov
50bd7d2c03 AST: Factor out GenericSignature::lookupNestedType() 2021-07-14 00:02:56 -04:00
Slava Pestov
9a0c87b196 RequirementMachine: Implement GenericSignature::getCanonicalTypeInContext() query
We compute the canonical type by first simplifying the type term, and
then checking if it is a concrete type. If there's no concrete type,
we convert the simplified term back to an interface type and return
that; otherwise, we canonicalize any structural sub-components of
the concrete type that contain interface types, and so on.

Due to a quirk of how the existing declaration checker works, we also
need to handle "purely concrete" member types, eg if I have a
signature `<T where T == Foo>`, and we're asked to canonicalize the
type `T.[P:A]` where Foo : A.

This comes up because we can derive the signature `<T where T == Foo>`
from a generic signature like `<T where T : P>`; adding the
concrete requirement 'T == Foo' renders 'T : P' redundant. We then
want to take interface types written against the original signature
and canonicalize them with respect to the derived signature.

The problem is that `T.[P:A]` is not a valid term in the rewrite system
for `<T where T == Foo>`, since we do not have the requirement T : P.

A more principled solution would build a substitution map when
building a derived generic signature that adds new requirements;
interface types would first be substituted before being canonicalized
in the new signature.

For now, we handle this with a two-step process; we split a term up
into a longest valid prefix, which must resolve to a concrete type,
and the remaining suffix, which we use to perform a concrete
substitution using subst().
2021-07-09 00:04:36 -04:00
Slava Pestov
05b3f79cec RequirementMachine: Implement GenericSignature::getRequiredProtocols() query 2021-06-30 01:34:20 -04:00
Slava Pestov
a37ad9fc89 RequirementMachine: Implement GenericSignature::areSameTypeParametersInContext() query 2021-06-30 01:34:20 -04:00
Slava Pestov
5d91b59e87 RequirementMachine: Implement GenericSignature::isConcreteType() query 2021-06-30 01:34:20 -04:00
Slava Pestov
62e0ad0f02 RequirementMachine: Add a dump() method 2021-06-30 01:34:20 -04:00
Slava Pestov
53d68144f8 RequirementMachine: Implement GenericSignature::getLayoutConstraint() query 2021-06-30 01:34:20 -04:00
Slava Pestov
8053b91c0e RequirementMachine: Implement GenericSignature::requiresProtocol() query 2021-06-30 01:34:20 -04:00
Slava Pestov
7df09f14ee RequirementMachine: Implement GenericSignature::requiresClass() query 2021-06-30 01:34:20 -04:00
Slava Pestov
6fa7a3fedb AST: Introduce GenericSignatureImpl::getRequirementMachine() 2021-06-30 01:34:20 -04:00
Slava Pestov
6cb64c4a8d AST: Refactor GenericSignature::isRequirementSatisfied() to use Requirement::isSatisfied()
This becomes a utility that maps the requirement's types into the
generic environment if needed before calling out to the new
Requirement::isSatisfied().
2021-05-17 16:34:17 -04:00
Slava Pestov
fe06df8288 AST: Move guts of checkGenericArguments() to a new Requirement::isSatisfied() method 2021-05-17 16:34:17 -04:00
Slava Pestov
e6ff771d59 GSB: Rewrite getConformanceAccessPath(), again
The new approach is to not look at RequirementSources at all. Instead,
we exhaustively enumerate all conformance access paths, beginning
from the root conformance requirements in the signature, then doing
all conformance requirements from those protocols' requirement
signatures, and so on.

We enumerate conformance access paths in breadth first order by
length until we find the one we want. The results are memoized.

This fixes a regression with another change I'm working on. The
test case does not fail with this PR alone, but I'm adding it now
anyway.
2021-05-06 17:55:43 -04:00
Slava Pestov
61f796da5a GSB: Remove unused parameter from lookupConformance() 2021-04-03 22:33:14 -04:00
Slava Pestov
568320c1d3 GSB: Move guts of getCanonicalTypeInContext() from GenericSignature to the GSB 2021-03-24 18:45:04 -04:00
Slava Pestov
6f4f9f8c0f Merge pull request #36411 from slavapestov/fix-gsb-verification
GSB: Move signature verification from CanGenericSignature::getCanonical() to computeGenericSignature()
2021-03-12 09:36:35 -05:00
Slava Pestov
91ebe4485c GSB: Move signature verification from CanGenericSignature::getCanonical() to computeGenericSignature()
Doing this when computing a canonical signature didn't really
make sense because canonical signatures are not canonicalized
any more strongly _with respect to the builder_; they just
canonicalize their requirement types.

Instead, let's do these checks after creating the signature in
computeGenericSignature().

The old behavior had another undesirable property; since the
canonicalization was done by registerGenericSignatureBuilder(),
we would always build a new GSB from scratch for every
signature we compute.

The new location also means we do these checks for protocol
requirement signatures as well. This flags an existing fixed
crasher where we still emit bogus same-type requirements in
the requirement signature, so I moved this test back into
an unfixed state.
2021-03-11 21:07:33 -05:00
Slava Pestov
fce1f2c7fe GSB: getConformanceAccessPath() doesn't need to use getMinimalConformanceSource()
Previously we would look for a derived source before an explicit one,
on account of the explicit one possibly being redundant. However, the
presence of 'self-derived' sources meant that we had to call
getMinimalConformanceSource() to ensure the derived sources
were actually usable and would not produce an infinite conformance
access path.

I'd like to remove getMinimalConformanceSource() now that we have
an alternate algorithm to identify redundant explicit requirements.

Instead, we can handle the explicit case first, by checking for a
conformance requirement in the generic signature -- its presence
means it was not redundant, by construction.

Then once we handle that case, we know we're going to use a derived
source, and finding the shortest one seems to be good enough.

This fixes the IRGen crash in https://bugs.swift.org/browse/SR-11153;
the requirement signatures in that test still have unnecessary
same-type requirements printed, so I added a separate RUN: line
for those, and it's marked as known-failing with 'not %FileCheck'.
2021-03-11 15:23:50 -05:00
Slava Pestov
53e06d69b5 AST: Factor out a new Requirement::getProtocolDecl() utility method 2021-02-25 17:21:18 -05:00
Slava Pestov
f3f9555c55 GSB: New implementation of getConformanceAccessPath()
A new implementation from "first principles". The idea is that
for a given conformance, we either have an explicit source
which forms the root of the requirement path, or a derived
source, which we 'factor' into a parent type/parent protocol
pair, and a requirement signature requirement.

We recursively compute the conformance access path of the
parent type and parent protocol, and append the path element
for the requirement.

This fixes a long-standing crasher, and eliminates two hacks,
the 'usesRequirementSource' flag in RequirementSource, and
the 'HadAnyRedundantConstraints' flag in GenericSignatureBuilder.

Fixes https://bugs.swift.org/browse/SR-7371 / rdar://problem/39239511
2021-02-23 16:26:04 -05:00
Slava Pestov
b0208a134f AST: Move getSugaredType() from GenericEnvironment to GenericSignature
None of this actually involves archetypes, so it can just be an
operation on the GenericSignature itself.
2020-08-24 19:16:36 -04:00
Slava Pestov
92bc89b78f GSB: Add an inferred AnyObject constraint to @objc protocol requirement signatures
This simplifies GenericSignatureImpl::requiresClass(), which no longer
has to look through the conformances of the equivalence class.
2020-08-13 00:23:47 -04:00
Slava Pestov
844ab508e6 AST: Add more asserts for same-type constraints in generic signatures 2020-07-01 23:03:33 -04:00
Robert Widmann
9357c85209 Merge pull request #31838 from AnthonyLatsis/o-getinnermostgps
[NFC] AST: Optimize GenericSignatureImpl::getInnermostGenericParams
2020-06-04 10:46:02 -07:00
Anthony Latsis
9fd1aa5d59 [NFC] Pre- increment and decrement where possible 2020-06-01 15:39:29 +03:00
Anthony Latsis
b25c4665e4 GenericSignatureImpl, #31712: Plug remaining relevant methods with type param. assertions 2020-05-20 23:49:47 +03:00
Anthony Latsis
95c16fc9e1 [NFC] AST: Optimize GenericSignatureImpl::getInnermostGenericParams 2020-05-16 08:26:20 +03:00
Anthony Latsis
44a92a926c [NFC] GenericSignatureImpl: Spell conformsToProtocol & getConformsTo in terms of requirements 2020-05-14 22:51:44 +03:00
Anthony Latsis
df9103e9d7 [NFC] AST: Const-qualify GenericSignatureImpl 2020-05-13 01:03:08 +03:00