Commit Graph

24 Commits

Author SHA1 Message Date
gregomni
3eed876e84 Always allow the extension where a conformance is declared to be checked
for witnesses to that conformance.
2018-07-28 22:16:04 -07:00
gregomni
8cd1a74a67 dependent member of dependent member of generic type param would crash
here because of Type() not being passed back up through the recursion
cleanly.
2018-07-21 20:55:05 -07:00
Huon Wilson
1bccf58bd8 [Sema] -debug-generic-signatures prints conformances after they've been filled out.
If they're printed before being checked, they have little useful content:
just (normal_conformance type=Basic protocol=P1), with no details about their
contents at all.
2018-05-15 22:17:51 +10:00
Huon Wilson
2e5799e26b [Sema] Diagnose redundant conditional conformances more specifically.
We don't allow things like

    extension Type: P where Param: P {}
    extension Type: P where Param: Q {}

or

    extension Type: P where Param == Int {}
    extension Type: P where Param == Float {}

or

    extension Type: P where Param: P {}
    extension Type: P where Param == SomethingThatIsntP {}

and the default error message "redundant conformance" message doesn't convey
this very well.

Fixes rdar://problem/36262409.
2018-04-27 11:50:17 +10:00
Huon Wilson
ae1b06b041 [Sema] Display simplified conditional conformance fixits on the command line.
Fixes rdar://problem/39170601 and SR-7352.
2018-04-05 11:41:53 +10:00
Huon Wilson
c1044529ff [AST] Don't allow conditional conformances to imply others.
Many uses of conditional conformance to protocols with super-protocols
are for wrappers, where the conformances to those super-protocols
usually ends up using weaker bounds, such as:

  struct MyWrapper<Wrapped: Sequence> {}
  extension Wrapped: Sequence {} // Wrapped: Sequence
  extension Wrapped: Collection where Wrapped: Collection {} // *
  extension Wrapped: BidirectionalCollection where Wrapped: BidirectionalCollection {}

If this code was instead written:

  struct MyWrapper<Wrapped: Sequence> {}
  extension Wrapped: Sequence {} // Wrapped: Sequence
  extension Wrapped: BidirectionalCollection where Wrapped: BidirectionalCollection {}

Inferring a Collection conformance would have to mean

  extension Wrapped: Collection where Wrapped: BidirectionalCollection {}

which is unnecessarily strong.

It is a breaking change to change a protocol bound, and so we're
thinking we'd prefer that the compiler didn't magic up that incorrect
conformance. It also only is a small change (and the compiler even
suggests it, with a fixit) to explicitly get the implying behaviour:
declare the conformance explicitly.

  extension Wrapped: Collection, BidirectionalCollection where Wrapped: BidirectionalCollection {}

Fixes rdar://problem/36499373.
2018-04-04 10:34:33 +10:00
Huon Wilson
0e337acae0 Revert "[AST] Ensure requirements are correctly identified as conditional." 2018-03-14 08:12:37 +11:00
Huon Wilson
88b9f2c94d [AST] Ensure requirements are correctly identified as conditional.
Previously, the following code would result in different sets of
conditional requirements:

  struct RedundancyOrderDependenceGood<T: P1, U> {}
  extension RedundancyOrderDependenceGood: P2 where U: P1, T == U {}

  struct RedundancyOrderDependenceBad<T, U: P1> {}
  extension RedundancyOrderDependenceBad: P2 where T: P1, T == U {}

The T: P1 requirement is redundant: it is implied from U: P1 and T == U,
but just checking it in the signature of the struct isn't sufficient,
the T == U requirement needs to be considered too. This uses a quadratic
algorithm to identify those cases. We don't think the quadratic-ness is
too bad: most cases have relatively few requirements.

Fixes rdar://problem/34944318.
2018-03-09 14:00:09 +11:00
Huon Wilson
f2c387035c Merge pull request #14588 from huonw/conditional-concrete-crash
[AST] Keep generic type params generic in RequirementEnvironments.
2018-02-14 07:21:29 +11:00
Huon Wilson
63a50936e4 [AST] Keep generic type params generic in RequirementEnvironments.
Before this patch, the use of GenericSignature::getSubstitutionMap to
create a substitution map would result in some generic parameters being
completely elided (well, being mapped to concrete types causing a
->castTo<GenericTypeParamType>() to crash). This patch changes that to
just do the reindexing of the type parameters directly, without trying
to be smart with more contextual information (i.e. τ_0_0 -> τ_1_0, even
if there's a τ_0_0 == Int requirement).

Fixes rdar://problem/37291254 and SR-6990.
2018-02-13 22:58:14 +11:00
Huon Wilson
715ca9d27d [AST] Store sugared requirements in NormalProtocolConformances.
No point in getting rid of the sugar so early.
2018-02-13 01:30:42 +11:00
Doug Gregor
5c831a71ee Revert "[SE-0143] Put conditional conformances behind an "experimental" flag."
This reverts commit b59c30c1af.
2017-12-18 22:54:31 -08:00
Doug Gregor
59ee97f4af [SE-0143] Try harder to substitute conditional requirements fully.
When forming a specialized protocol conformance, we substitute into the
conditional requirements. Allow this substitution to look into the module
to find conformances, which might be required to accurately represented
the requirements. Otherwise, we can silently end up dropping them.

We should rethink this notion of eagerly substituting conditional
requirements, and consider whether clients should always handle this
substitution. For now, fixes rdar://problem/35837054.

Allow conformance lookup in module context for conditional
2017-12-04 15:02:56 -08:00
Doug Gregor
b59c30c1af [SE-0143] Put conditional conformances behind an "experimental" flag.
Conditional conformances aren't quite ready yet for Swift 4.1, so
introduce the flag `-enable-experimental-conditional-conformances` to
enable conditional conformaces, and an error when one declares a
conditional conformance without specifying the flag.

Add this flag when building the standard library (which will vend
conditional conformances) and to all of the tests that need it.

Fixes rdar://problem/35728337.
2017-11-28 16:01:51 -08:00
Doug Gregor
a6913bb2ef [Constraint solver] Check conditional requirements for type erasure.
Rather than wantonly dropping the conditional requirements when checking
for type erasure, add them in the same way we do for (e.g.) conformance
checking for generics. Fixes rdar://problem/35480860.
2017-11-19 22:22:32 -08:00
Doug Gregor
b0760e149e [AST] Substitute into conditional requirements of a specialized conformance.
Substitute the type arguments of the conforming type into the conditional
requirements of the specialized conformance, so they reflect the specific
requirements of the specialized conformance.

Fixes rdar://problem/34944286.
2017-11-17 22:57:41 -08:00
Huon Wilson
5f70f68c0d [AST] Store only interface types in NormalProtocolConformances.
Rather than storing contextual types in the type witnesses and associated
conformances of NormalProtocolConformance, store only interface types.

@huonw did most of the work here, and @DougGregor patched things up to
complete the change.
2017-11-16 11:45:18 -08:00
Doug Gregor
40e0c5b842 Update now-fixed test case. 2017-11-13 14:32:29 -08:00
Huon Wilson
a94df16b62 [test] Add more tests for inheriting conformances etc from SE0143 proposal. 2017-11-09 17:51:48 -08:00
Huon Wilson
1372202cfa [test] More for conditional conformances; subclasses, devirtualization, etc. 2017-11-09 17:51:48 -08:00
Huon Wilson
ec6c83aa44 [Sema] Check conditional conformances for generic function calls early.
This allows for better (although not perfect) diagnostics, and more guarantees
of correctness.
2017-10-10 20:17:42 -07:00
Huon Wilson
f6729f17d6 [Sema] Add conditional requirements as CS constraints.
This tries to feed through the constraint system all the constraints
implied by a concrete type satisfying a conformance requirement.
2017-10-10 20:17:41 -07:00
Huon Wilson
6338c35542 [test] Add order-dependency conditional conformance edge case to test suite. 2017-10-10 20:17:40 -07:00
Huon Wilson
ade976893e [AST] Test conditional requirement selection by having -debug-generic-signatures include conformances.
And make conformances' dump include the conditional requirements too.
2017-10-10 20:17:39 -07:00