Commit Graph

75 Commits

Author SHA1 Message Date
Doug Gregor
38a189f650 [SubstitutionMap] Introduce hasAnySubstitutableParams().
Introduce an operation to check that a given substitution map is non-empty
(i.e., corresponds to a generic signature) and that the generic signature
has type parameters that aren’t bound to concrete types. This is the
appropriate predicate for SIL-and-later to determine whether there
will be any substitutions.
2018-05-11 13:18:06 -07:00
Doug Gregor
dc7a251d4c [SubstitutionMap] Handle substitution of non-canonical generic parameters.
SubstitutionMap::lookupSubstitution() assumed that any type parameter it was
given would already be canonical, and fail (return a null Type) when given
a type parameter that was equivalent to another type parameter but is
not itself canonical. Canonicalize, recursive, and record the replacement
type appropriately.
2018-05-11 13:18:06 -07:00
Doug Gregor
791df5ba55 [AST] Cache the computed SubstitutionList for a SubstitutionMap.
Every SubstitutionMap::toList() invocation would ASTContext-allocate
arrays for all of the conformances, then return a SmallVector of the
underlying substitutions, which both wastes memory *and* puts the onus
on the caller to allocate a copy of that outer SmallVector into the
ASTContext if it's going to be stored anywhere.

Stop the madness by caching an ASTContext-allocated SubstitutionList
within the storage for the SubstitutionMap. This both reduces repeated
allocations and eliminates the potential for errors.
2018-05-03 10:38:01 -07:00
Doug Gregor
4136a1897a [AST] Split SubstitutionMap::Storage into its own header.
Now that SubstitutionMap is used in so many places, reduce it's header
dependencies by moving SubstitutionMap::Storage into its own separate
implementation header. Use forward declarations of other entities
(GenericSignature, Substitution) instead.

Good for build times and general sanity.
2018-05-03 09:49:09 -07:00
Pavel Yaskevich
01312d9623 Merge pull request #16276 from xedin/add-implicit-inits-with-getparams
[Sema] Refactor member type matching from `checkOverrides` to make it reusable
2018-05-03 00:16:24 -07:00
Pavel Yaskevich
394ac6f4f1 [AST] lookupSubstition should produce nothing if there are not substitutions
If `SubstitutionMap` is empty there is nothing to replace so
we should always return empty substitution type and/or conformance ref.
2018-05-02 15:11:25 -07:00
Doug Gregor
bc5bbe8b64 [AST] Use SubstitutionMap, not SubstitutionList, in SILBoxType.
Eliminate the last place in the AST proper that stores
SubstitutionLists rather than SubstitutionMaps. Randomly fixes a
crasher, too.
2018-05-02 13:39:21 -07:00
Doug Gregor
3e2bed119b [AST] Switch SpecializedProtocolConformance over to SubstitutionMap.
Eliminate another common use of SubstitutionList from the AST.
2018-05-02 13:38:15 -07:00
Doug Gregor
192234415d [AST] Store SubstitutionMaps in ConcreteDeclRef and Witness data structures.
Replace two prominent uses of SubstitutionList, in ConcreteDeclRef and
Witness, with SubstitutionMap. Deal with the myriad places where we
now have substitution maps and need substitution lists (or vice versa)
caused by this change.

Overall, removes ~50 explicit uses of SubstitutionList (of ~400).
2018-05-02 13:38:14 -07:00
Doug Gregor
cdff7427f8 [AST] Unique and ASTContext-allocate SubstitutionMaps.
Prepare for SubstitutionMaps to be stored in other AST nodes by making
them ASTContext-allocated and uniqued (via a FoldingSet). They are now
cheap to copy and have trivial destructors.
2018-04-30 10:42:03 -07:00
Doug Gregor
03a47b87ad [AST] Minor cleanups to SubstitutionMap. 2018-04-30 09:30:44 -07:00
Doug Gregor
cebbe45c23 [AST] Flatten internal representation of SubstitutionMap.
Collapse the out-of-line array of replacement types and the DenseMap used
for conformances into a single field that references out-of-line,
tail-allocated storage for all of the contents of a SubstitutionMap.
The conformances are now represented as a flat array whose indices
line up with the indices of the conformance requirements in the
underlying generic signtature.

Reference this storage via a single shared_ptr, so that copies of
SubstitutionMaps are cheap but we aren't (yet) uniqueing them or
storing them within the ASTContext.
2018-04-30 00:41:28 -07:00
Doug Gregor
7f2c88fad5 [AST] Internalize details of constructing a SubstitutionMap. 2018-04-29 23:03:12 -07:00
Doug Gregor
f02c1051ad [AST] Reimplement SubstitutionMap::subst() in terms of getSubstitutionMap().
The primary implementation for substituting into substitution maps was
cloning the contents of the SubstitutionMap and then performing
in-place updates to the (new) data structure, which both had too many
dependencies on the data structure and subverted some of the checking
we get through the normal path.

Reimplement this function in terms of
GenericSignature::getSubstitutionMap(), which is the primary/canonical
way to form a substitution map.
2018-04-29 22:16:43 -07:00
Doug Gregor
43931a985c [AST] Record generic signature in BoundNameAliasType.
The generic signature used to perform substitutions for a BoundNameAliasType
in ill-formed code might not line up with the generic signature in the
typealias. Separately record the signature we used to build the
BoundNameAliasType to make the AST more robust against such issues.
2018-03-25 20:46:07 -07:00
Doug Gregor
80eae200b6 [AST] Preserve type sugar for generic typealiases
Introduce a new Type node, BoundNameAliasType, which describes a
reference to a typealias that requires substitutions to produce the
underlying type. This new type node is used both for references to
generic typealiases and for references to (non-generic) typealiases
that occur within generic contexts, e.g., Array<Int>.Element.

At present, the new type node is mainly useful in preserving type
sugar for diagnostics purposes, as well as being reflected in other
tools (indexing, code completion, etc.). The intent is to completely
replace NameAliasType in the future.
2018-03-21 23:49:17 -07:00
Doug Gregor
46e623a089 [SubstitutionMap] Handle conformance lookup via superclass requirements.
Conformances that discoverably for a type parameter via a superclass
requirement occupy a bit of a gray area in the type checker and
generic signature handling right now: we have a type parameter (so the
paths that handle concrete conformances don't kick in), but the
conformance itself isn't modeled as a requirement because it is
available via lookup. Teach SubstitutionMap's conformance lookup to
detect this case and perform conformance lookup in the generic
signature (i.e., falling back to global lookup) in this case,
replicating the hack that IRGen uses to address the same issue when
accessing the conformance (see GenArchetype.cpp's TODO where we lookup
a conformance on the superclass). The removal of that hack (as well as
this one) are tracked by rdar://problem/34609744.

For now, fixes SR-7072 / rdar://problem/37904576.
2018-02-26 23:31:46 -08:00
Joe Groff
d4e03f2ba3 SIL: Add an 'external' KeyPathPatternComponent kind.
This will allow key paths to resiliently reference public properties from other binaries by referencing a descriptor vended by the originating binary. NFC yet, this just provides printing/parsing/verification of the new component.
2018-02-14 10:48:24 -08:00
Jordan Rose
10daf26bf7 [AST] Use CanType in SubstitutionMap
No functionality change---the code was already using canonical types
in practice.
2018-01-30 15:24:06 -08:00
Doug Gregor
9554e61705 [AST] Fix a crash when conformance lookup fails during substitution.
The enclosing conformance-lookup operation can fail (i.e., it returns
Optional), but the nested call was force-unwrapping the optional from
an inner call for no particular reason. Stop doing that, which fixes
SR-6466.

Note that the offending code is still something that should go away in
time, so this is merely polishing the band-aid to avoid a crash that
occurs often with conditional conformances.
2017-11-24 23:18:11 -08:00
Doug Gregor
a92d8d5760 [Cleanup] Reduce dependencies on swift/AST/LazyResolver.h. 2017-10-10 10:01:39 -07:00
Doug Gregor
1f1b75a56d [AST] Eliminate ModuleDecl parameters from GenericSignature. 2017-10-10 10:01:39 -07:00
Slava Pestov
f2582e1311 AST: Replace a few mapTypeOutOfContext() calls with ArchetypeType::getInterfaceType() 2017-10-07 05:44:57 -07:00
Doug Gregor
c0805a2bc8 [Substitution map] Don’t infinitely recurse when inferring type witnesses. 2017-09-14 09:16:14 -07:00
Slava Pestov
9f8760b942 AST: Remove unused 'resolver' parameter from ModuleDecl::lookupConformance()
... as well as a bunch of downstream plumbing that is no
longer necessary.
2017-09-07 03:36:17 -07:00
Slava Pestov
42014882ef AST: Fix potential null dereference in SubstitutionMap::lookupConformance()
If there's no lazy resolver and we're looking at an incomplete conformance,
fail with an error instead of crashing. The caller will likely crash also,
but at least this allows better recovery in this case in the future.
2017-09-07 00:24:12 -07:00
Slava Pestov
a2181092fb AST: Tweak a concrete-nested-type-of-archetype hack slightly
When an archetype conforms to a protocol abstractly (ie, it is
not class-constrained and we don't have any further information
other than that it conforms), we can recover nested types,
but we cannot recover the conformance of those nested types
to protocols if those conformances are concrete (the nested
type might be concrete, or it might be a class-constrained
archetype).

To work around this, we added a hack where if the conformance
lookup in the SubstitutionMap fails, we fall back to the module.

This is horrible and unprincipled, but has to remain in place
until more infrastructure is plumbed through.

Commit 620db5f74c made this
workaround apply in fewer cases, so that we could still catch
cases where the SubstitutionMap was constructed with missing
conformances.

Unfortunately this workaround didn't handle the case where the
nested type was an archetype with a superclass constraint.

This will all go away soon, but for now tweak the logic a bit,
since I really want to keep the "narrow" workaround in place
and not the general fallback, otherwise we run the risk of
SubstitutionMap conformance lookup bitrotting completely.

Fixes <https://bugs.swift.org/browse/SR-4088> and
<rdar://problem/32773028>.
2017-06-20 22:18:50 -07:00
Slava Pestov
b5721e8d8e AST: Remove AnyObject protocol 2017-05-02 19:45:00 -07:00
Roman Levenstein
0aff7c0c78 Produce specialized conformances to AnyObject for bound generic types.
Fixes rdar://31910351 (SR-4750)
2017-05-01 18:12:52 -07:00
Doug Gregor
6ef76a92f4 [AST] Use flat array storage for SubstitutionMap's replacement types.
The replacement types in a SubstitutionMap correspond with the generic
parameters of its generic signature, so replace the DenseMap storage
with a flat array of Types, one element for each generic parameter.
2017-04-28 16:22:07 -07:00
Doug Gregor
93435d17d6 [Substitution Map] Handle substitutions of generic parameters made concrete.
If SubstitutionMap is asked to form a substitution for a generic
parameter that has been made concrete by the generic signature,
substitute into the concrete type. This allows us to better deal with
non-canonical types.
2017-04-28 16:22:07 -07:00
Slava Pestov
0290c2d5d8 AST: Make GenericSignature and GenericEnvironment SubstitutionMaps interchangable
SubstitutionMap::lookupConformance() would map archetypes out
of context to compute a conformance path. Do the same thing
in SubstitutionMap::lookupSubstitution().

The DenseMap of replacement types in a SubstitutionMap now
always has GenericTypeParamTypes as keys.

This simplifies some code and brings us one step closer to
a more efficient representation of SubstitutionMaps.
2017-04-24 14:12:36 -07:00
Slava Pestov
b4aaaee956 AST: Fix crash in SubstitutionMap::dump() with empty SubstitutionMap
The SubstitutionMap type has a "null value" where there's no
generic signature and no entries. There's nothing to dump in
this case.
2017-04-24 00:52:10 -07:00
Slava Pestov
26aa02324b AST: Fix "concrete conformance under abstract conformance" hack for archetypes
This manifested as test/Prototypes/PatternMatching.swift failing
under 'ninja check-swift-validation-optimize'.

I'll add a test later if I have time to reduce a test case, but this
whole hack is hopefully going away soon anyway.

Fixes one of the three failures outlined in <rdar://problem/31780356>.
2017-04-24 00:52:10 -07:00
Slava Pestov
620db5f74c AST: Narrower workaround for "concrete conformance under abstract conformance" bug
Instead of just falling back to module lookup any time conformance
substitution fails, only do it in the case we know doesn't work.

This should shake out some more bugs, hopefully without causing
too much pain.
2017-04-23 01:49:01 -07:00
Slava Pestov
7b8e26b621 AST: Remove unnecessary canonicalization step in SubstitutionMap::lookupConformance() 2017-04-23 01:49:01 -07:00
Slava Pestov
f4b91cd118 AST: Remove unused 'resolver' argument from TypeBase::getSuperclass() 2017-04-20 00:37:38 -07:00
Doug Gregor
6878538ba1 [AST] Remove GenericEnvironment from SubstitutionMap; it's unnecessary. 2017-04-05 11:21:39 -07:00
Doug Gregor
8748601b26 [SubstitutionMap] Dump generic signature in debug output.
You can't interpret a conformance map without a generic signature, so
add it to the debug output.
2017-04-05 11:21:38 -07:00
Doug Gregor
ba10723e9c [SubstitutionMap] Make conformance lookup robust against ill-formed conformances. 2017-04-04 14:07:43 -07:00
Doug Gregor
97c6707910 [SubstitutionMap] Cope with missing conformances in lookupConformance().
When asking a substitution map for a conformance, it's okay if the
conformance isn't there---just detect this case and return None.

Also, collapse a redundant testcase Huon noted.
2017-04-04 10:58:01 -07:00
Doug Gregor
d0499a3613 [SubstitutionMap] Eliminate the parent map and handle archetype conformances.
When looking for a conformance for an archetype, map it out of context
to compute the conformance access path, then do the actual lookups
based on mapping the starting type back into the context. Eliminate
the parent map and "walk the conformances" functionality.
2017-04-04 10:58:01 -07:00
Doug Gregor
6bf4c461aa [SubstitutionMap] Use conformance access paths to find conformances.
When looking up a particular conformance in a SubstitutionMap, use the
associated generic signature to determine how to find that conformance
(via a conformance access path). Follow that conformance access path
to retrieve the desired conformance.

FIXME: There is still one failure, here

  Constraints/generic_overload.swift

which appears to be because we either have the wrong generic signature
for the override case or we are looking in the wrong map.
2017-04-04 10:58:01 -07:00
Doug Gregor
81c21b85f2 [AST] Track the generic signature/environment for a substitution map.
Substitution maps are effectively tied to a particular generic
signature or environment; keep track of that signature/environment so
that we can (eventually) use it to find conformances.
2017-04-04 10:58:01 -07:00
Slava Pestov
f10f00d887 Sema: Narrow fix for a circular protocol conformance checking case
We go to look at the conforming protocols of an associated type
but we haven't built the generic environment for the associated
type's protocol yet.

In the test case given, we find the conformance later via a
different path, so everything continues to work.

SubstitutionMap::lookupConformance() is hopefully getting a
makeover soon, and this hack will go away.

Fixes <rdar://problem/31302713>.
2017-03-30 02:03:52 -07:00
Roman Levenstein
791c449f7c Disable SubstitutionMap::verify() for now. We are not ready for it yet.
We should enable it again once the substitutions machinery and GenericSignatureBuilder always generate correct SubstitutionMaps.
But for now let’s disable it to unblock the CI jobs.

Should fix SR-4322 and rdar://31224655
2017-03-24 16:51:57 -07:00
Saleem Abdulrasool
ef141eb2c1 AST: avoid "deprecation" warning
The "dump()" interface is meant for use in the debugger only.  Use the
parameter form to avoid the -Wdeprecated-declaration warning.
2017-03-21 14:49:46 -07:00
Roman Levenstein
6f54798df8 Verify the construction of SubstitutionMaps
After substitution maps are constructed, check their invariants. It helps to catch very subtle bugs related to substitutions.
2017-03-20 15:30:12 -07:00
Slava Pestov
5381921131 AST: Generalize SubstitutionMap::combineSubstitutionMaps() 2017-03-08 13:54:28 -08:00
Slava Pestov
de2f5f78ac AST: Remove Substitution::subst()
I want to get rid of Substitution entirely, and now we have
the right abstractions to do everything with SubstitutionMap.
2017-03-07 16:04:54 -08:00