Skip to content

[4.0] [GSB] Improve handling of concrete types, type aliases, recursion #10596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

DougGregor
Copy link
Member

Explanation: Improves our handling of concrete conformances, type aliases in protocols, and same-type-to-concrete constraints in the generic signature builder to make them all more consistent and robust.
Scope: Affects many uses of generic code; will reject a number of ill-formed cases that would previously crash the compiler.
Radar: SR-4295 / rdar://problem/31372308, SR-4757 / rdar://problem/31912838, SR-4786 / rdar://problem/31955862, SR-5014 / rdar://problem/32402482, SR-4737 / rdar://problem/31905232, plus a few other dupes.
Risk: Low-ish. The generic signature builder is key to all generics handling in Swift, so changes there can cause breakage... but it's also fairly well-tested via the standard library, so it's likely we'd have caught regressions.
Testing: Compiler regression testing, plus lots of new tests from the various fixed radars.

@DougGregor DougGregor added this to the Swift 4.0 milestone Jun 26, 2017
@DougGregor
Copy link
Member Author

@swift-ci please test

NestedTypeUpdate was mostly just the internal name for
ArchetypeResolutionKind, but the translation was a bit lossy and there
was no point in having separate enums. Standardize on
ArchetypeResolutionKind, adding a new case (WellFormed) to capture the
idea that we can create a new potential archetype only when we know
there is a nested type with that name---and avoid creating unresolved
potential archetypes.

(cherry picked from commit fafeec0)
(cherry picked from commit 05bc0ef)
Rather than abusing the "superclass" requirement source with a null
protocol conformance, introduce a separate "structurally derived"
requirement source kind for structurally-derived requirements that
don't need any additional information, e.g., the class layout
requirement derived from a superclass requirement.

(cherry picked from commit ffea1b3)
(cherry picked from commit 830da21)
Centralize and simplify the handling of conformance requirements
resolved by same-type-to-concrete requirements in a few ways:

* Always store a ProtocolConformanceRef in via-superclass and
  via-concrete requirement sources, so we never lose this information.

* When concretizing a nested type based on its parent, use the
  via-concrete conformance information rather than performing lookup
  again, simplifying this operation considerably and avoiding
  redundant lookups.

* When adding a conformance requirement to a potential archetype that
  is equivalent to a concrete type, attempt to find and record the
  conformance.

Fixes SR-4295 / rdar://problem/31372308.

(cherry picked from commit 52e52b5)
(cherry picked from commit 50a7ad5)
(cherry picked from commit a4e35ed)
(cherry picked from commit 7c68366)
…ments for.

(cherry picked from commit b095c8a)

(cherry picked from commit 2167a3f)
…pes.

In some circumstances, we could end up growing increasingly-nested
potential archetypes due to a poor choice of representatives and
anchors. Address this in two places:

* Always prefer to use the potential archetype with a lower nesting
  depth (== number of nested types) to one with a greater nesting
  depth, so we don't accumulate more nested types onto the
  already-longer potential archetypes, and

* Prefer archetype anchors with a lower nesting depth *except* that we
  always prefer archetype anchors comprised of a sequence of
  associated types (i.e., no concrete type declarations), which is
  important for canonicalization.

Fixes SR-4757 / rdar://problem/31912838, as well as a regression
involving infinitely-recursive potential archetypes caused by the
previous commit.

(cherry picked from commit a72a2bf)
(cherry picked from commit 398b99a)
…Formed.

(cherry picked from commit 791ac7f)
(cherry picked from commit 075a4c3)
…ents.

Ensures that we don't admit invalid cases where the concrete type does
not conform to the required protocol.

(cherry picked from commit c879b95)
(cherry picked from commit 6c4e4fd)
When two potential archetypes are merged and only one of them was a
concrete type beforehand, concretize the nested types in the
equivalence class of the non-concrete potential archetype. Otherwise,
we're liable to miss redundancies.

This feels like an ad hoc extension to an ad hoc mechanism, but gets
us back to parity with this patch series.

(cherry picked from commit bf730ff)
(cherry picked from commit 8295561)
(cherry picked from commit b51529f)
(cherry picked from commit 9d681fa)
When we see two type(aliase)s with the same name in a protocol
hierarchy, make them equal with an implied same-type requirement. This
detects inconstencies in typealiases across different protocols, and
eliminates the need for ad hoc consistency checking. This is a step
toward simplifying away the need for direct-diagnosis operations
involving concrete type mismatches.

While here, warn when we see an associated type with the same as a
typealias from an inherited protocol; in this case, the associated
type is basically useless, because it's going to be equivalent to the
typealias.

(cherry picked from commit c47aea7)
(cherry picked from commit e01e302)
Specifically, we need to be able to add a new potential archetype for
the anchor. This API might need refinement.

(cherry picked from commit aeb5b01)
(cherry picked from commit 9fdd79f)
(cherry picked from commit 2f00a08)
(cherry picked from commit efe8fde)
When a concrete requirement is invalid due to the concrete type
lacking a conformance to a particular, required protocol, don't emit
that incorrect requirement---it causes invalid states further down the
line.

Fixes SR-5014 / rdar://problem/32402482.

While here, fix a comment that Huon noticed trailed off into oblivion.

(cherry picked from commit dd38697)
(cherry picked from commit e1395ea)
(cherry picked from commit 6862ef1)
(cherry picked from commit 3b8b73b)
…ass.

Fixes one recently-found crasher.

(cherry picked from commit e256a9d)
(cherry picked from commit f72fec0)
Fixes two more recently-found GSB crashers.

(cherry picked from commit 19f4ee7)
@DougGregor DougGregor force-pushed the gsb-concrete-types-recursion-4b3 branch from cda8e58 to c9a648c Compare June 27, 2017 14:37
@DougGregor
Copy link
Member Author

@swift-ci please test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor DougGregor merged commit 7f07724 into swiftlang:swift-4.0-branch-06-23-2017 Jun 27, 2017
@DougGregor DougGregor deleted the gsb-concrete-types-recursion-4b3 branch June 27, 2017 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant