Commit Graph

102 Commits

Author SHA1 Message Date
Doug Gregor
5ec385797f [GSB] Remove self-derived same-type-to-concrete constraints.
Introduce an operation on RequirementSource to determine whether a
constraint with such a source, when it lands on a given potential
archetype, is "self-derived": e.g., the final constraint is derived
from the original constraint. Remove such constraints from the system
during finalization, because otherwise they would make the original
constraint redundant.

Fixes rdar://problem/30478915, although we still need to apply this
same logic to other kinds of constraints in the system.
2017-02-28 09:47:30 -08:00
Doug Gregor
c2da97172b [GSB] Store the root potential archetype in RequirementSource.
Use TrailingObjects to help us efficiently store the root potential
archetype within requirement sources, so we can reconstruct the
complete path from the point where a requirement was created to the
potential archetype it affects.

The test changes are because we are now dumping the root potential
archetype as part of -debug-generic-signatures.
2017-02-28 09:47:11 -08:00
Doug Gregor
e71788d7a6 [GenericSigBuilder] Conformances due to concrete types can be abstract.
When a type parameter is made concrete via an existential type,
conformance requirements on that type parameter will be
abstract. Fixes rdar://problem/30610428.
2017-02-23 13:41:08 -08:00
Doug Gregor
23f3ba53f8 [GenericSig Builder] Track and canonicalize same-type-to-concrete constraints.
Track each same-type-to-concrete constraint on the potential archetype
against which it was written, and ensure that the requirement sources
for such same-type constraints stay with the potential archetypes on
which they were described. This is similar to the way we track
same-type constraints among potential archetypes.

Use this information to canonicalize same-type-to-concrete constraints
appropriately. For each connected component within an equivalence
class of potential archetypes, select the best requirement source to
the concrete type, or substitute in an abstract requirement source if
none exists. This approach ensures that components that would be
equivalent to that concrete type anyway get a derived source, while
components that get the concrete-type equivalence by being tied to
another

To get here, we also needed to change the notion of the archetype
anchor so that potential archetypes with no same-type constraints
directly in their path are preferred over potential archetypes that do
have a same-type constraint in their path. Otherwise, the anchor might
be something that is always concrete and is, therefore, not terribly
interesting.

Fixes the new case that popped up in rdar://problem/30478915.
2017-02-21 22:19:18 -08:00
Doug Gregor
da39d9b17b [GenericSig Builder] Rework RequirementSource to describe requirement path.
Reimplement the RequirementSource class, which captures how
a particular requirement is satisfied by a generic signature. The
primary goal of this rework is to keep the complete path one follows
in a generic signature to get from some explicit requirement in the
generic signature to some derived requirement or type, e.g.,

1) Start at an explicit requirement "C: Collection"
2) Go to the inherited protocol Sequence,
3) Get the "Iterator" associated type
4) Get its conformance to "IteratorProtocol"
5) Get the "Element" associated type

We don't currently capture all of the information we want in the path,
but the basic structure is there, and should also allow us to capture
more source-location information, find the "optimal" path, etc. There are
are a number of potential uses:

* IRGen could eventually use this to dig out the witness tables and
  type metadata it needs, instead of using its own fulfillment
  strategy
* SubstitutionMap could use this to lookup conformances, rather than
  it's egregious hacks
* The canonical generic signature builder could use this to lookup
  conformances as needed, e.g., for the recursive-conformances case.

... and probably more simplifications, once we get this right.
2017-02-17 13:50:51 -08:00
Doug Gregor
e3479e25fc [Generic signature builder] Concrete constraints across equivalence classes.
When emitting a requirement for a same-type-to-concrete constraint,
use the known concrete source for just the first of the component
anchors. For the rest, we need to explicitly model the constraint lest
it get lost. Fixes rdar://problem/30478915.
2017-02-13 12:49:09 -08:00
Huon Wilson
51da51dfc0 [AST] Use the requirement signature in the ArchetypeBuilder.
The requirement signature is far more compact than looking at all the members.
2017-02-08 13:09:36 -08:00
Doug Gregor
0c76a9d828 [Archetype builder] Clean up PotentialArchetype a bit.
Clean up the representation of PotentialArchetype in a few small ways:

* Eliminate the GenericTypeParamType* at the root, and instead just
  store a GenericParamKey. That makes the potential archetypes
  independent of a particular set of generic parameters.

* Give potential archetypes a link back to their owning
  ArchetypeBuilder, so we can get contextual information (etc.) when
  needed. We can remove the "builder" arguments as a separate step.

Also, collapse getName()/getDebugName()/getFullName() into
getNestedName() and getDebugName(). Generic parameters don't have
"names" per se, so they should only show up in debug dumps.

In support of the former, clean up some of the diagnostics emitted by
the archetype builder that were using 'Identifier' or 'StringRef'
where they should have been using a 'Type' (i.e., the type behind the
dependent archetype).
2017-02-07 11:15:11 -08:00
Doug Gregor
ea90fa6345 [Archetype builder] Suppress redundant constraints more effectively.
Fix a couple of places where we were not managing to consider
redundant constraints as 'redundant', so the canonicalized generic
signatures were not in fact minimal. Address this in several places:

* When a type is made concrete, any superclass requirements on it are
  redundant
* When a type is made concrete, any same-type relationships among its
  nested types are redundant
* Only emit potential-archetype-to-concrete constraints in the
  resulting signature for the component anchors; not for every type.

The first two bullets were existing problems that are now fixed; the
last is something I appear to have introduced with last week's
refactoring of canonicalized same-type constraints.

The recent work to make the canonicalization deterministic exacerbated
the issue considerably, leading to huge blow-up in the size of
canonical generic signatures that cost ~100k in standard library
size. This change recovers that 100k.
2017-02-03 10:15:44 -08:00
Doug Gregor
ded7e83aab [Archetype builder] Make canonicalization of dependent types deterministic.
The canonicalization of dependent member types had some
nondeterminism. The root of the problem was that we couldn't
round-trip dependent member types through the archetype
builder---resolving them to a potential archetype lost the specific
associated type that was recorded in the dependent member type, which
affected canonicalization. Maintain that information, make sure that
we always get the right archetype anchor, and tighten up the
canonicalization logic within a generic signature.

Fixes rdar://problem/30274260 and should unblock some other work
that depends on sanity from the archetype builder and generic
signature canonicalization.
2017-02-02 21:25:29 -08:00
Doug Gregor
f7f703ad04 [Archetype builder] Canonicalize and minimize same-type constraints.
Introduce an algorithm to canonicalize and minimize same-type
constraints. The algorithm itself computes the equivalence classes
that would exist if all explicitly-provided same-type constraints are
ignored, and then forms a minimal, canonical set of explicit same-type
constraints to reform the actual equivalence class known to the type
checker. This should eliminate a number of problems we've seen with
inconsistently-chosen same-type constraints affecting
canonicalization.
2017-02-01 10:51:02 -08:00
Doug Gregor
a232b41f87 [Archetype builder] Use archetype anchors exclusively in requirements.
When enumerating requirements, always use the archetype anchors to
express requirements. Unlike "representatives", which are simply there
to maintain the union-find data structure used to track equivalence
classes of potential archetypes, archetype anchors are the
ABI-stable canonical types within a fully-formed generic signature.

The test case churn comes from two places. First, while
representatives are *often* the same as the archetype anchors, they
aren't *always* the same. Where they differ, we'll see a change in
both the printed generic signature and, therefore, it's
mangling.

Additionally, requirement inference now takes much greater
care to make sure that the first types in the requirement follow
archetype anchor ordering, so actual conformance requirements occur in
the requirement list at the archetype anchor---not at the first type
that is equivalent to the anchor---which permits the simplification in
IRGen's emission of polymorphic arguments.
2017-01-12 11:07:05 -08:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Doug Gregor
4d4bac2f98 Revert "[Archetype builder] Mark same-type constraints on nested types as redundant." (#5866)
This reverts commit 6a7d257bb7. The fix
was too aggressive, and ended up dropping requirements that are
required.

Fixes rdar://problem/29332919
2016-11-19 21:53:47 -08:00
Doug Gregor
6a7d257bb7 [Archetype builder] Mark same-type constraints on nested types as redundant.
When a same-type constraint equates a potential archetype with a
same-type constraint, it implies equality of the nested types with the
appropriate witnesses. However, these were getting marked as
"explicit" when they should be "redundant".

Fixes rdar://problem/29295062.
2016-11-16 20:04:37 -08:00
Doug Gregor
125ad63f71 [Archetype builder] Remove RequirementSource::Kind::OuterScope.
"Explicit" covers everything we need now. NFC
2016-11-16 20:04:37 -08:00
Doug Gregor
921e14fe32 Update test cases that were checking for witness markers in debug output. 2016-11-09 21:45:17 -08:00
Slava Pestov
3b1721795f AST: Nuke GenericSignature::getCanonicalManglingSignature()
Now that the previous patches have shaken out implicit assumptions
about the order of generic requirements and substitutions, we can
make a more radical change, dropping redundant protocol requirements
when building the original generic signature.

This means that the canonical ordering and minimization that we
used to only perform when building the mangling signature is done
all of the time, and hence getCanonicalManglingSignature() can go
away.

Usages now either call getCanonicalSignature(), or operate on the
original signature directly.
2016-09-06 11:51:14 -07:00
Slava Pestov
f19192d498 ArchetypeBuilder: Sort requirements in enumerateRequirements()
Move the sorting algorithm from construction of the canonical mangling
signature to requirement enumeration.

This also changes how same-type constraints pick representatives, using
the more canonical total order.
2016-09-06 11:51:13 -07:00
Slava Pestov
71cf5919c6 ArchetypeBuilder: More principled usage of RequirementSource
We used RequirementSources to identify redundant requirements
when building the canonical mangling signature. There were a
few problems with how this worked:

- We used the Inferred requirement source for both requirements
  that were inferred from function parameter and result types
  (for example, T : Hashable in `func foo<T, U>(dict: [T : U])`
  and some completely unrelated things, like same-type
  constraints imposed on nested types by existing same-type
  constraints on the outer types.

- We used the Protocol requirement source for associated type
  requirements on protocols as well as conformance requirements
  on inherited protocols.

- Introducing a new Redundant requirement did not mark existing
  Explicit requirements as Redundant.

None of these were an issue because of how canonical mangling
signature construction works:

- We already started with a canonical signature, so there were
  no Inferred requirements of the first kind (which we *do* want
  to keep here)

- We dropped both Protocol and Redundant requirements, so it
  didn't matter that the distinction here was not sufficiently
  fine-grained

- We never introduced Explicit requirements that would be later
  superceded by a Redundant requirement, because we always
  started with an existing canonical signature where such Explicit
  requirements were already dropped.

However, I want to use the same algorithm for building the
original signature as the canonical mangling signature, so this
patch introduces these changes:

- The Inferred source is now only used for inferred requirements of
  the first kind; the second kind are now Redundant

- The Protocol source is now only used for associated type
  requirements; inherited protocol conformances are now Redundant

- updateRequirementSource() now does the right thing with
  introduced Redundant requirements

For now, this doesn't change much, but an upcoming patch
refactors ArchetypeBuilder::getGenericSignature() to also
use enumerateRequirements(), except dropping Redundant
requirements only, and not Protocol.
2016-09-06 11:51:13 -07:00
Dmitri Gribenko
d175b3b66d Migrate FileCheck to %FileCheck in tests 2016-08-10 23:52:02 -07:00
David Farler
7bfaeb57f1 [SE-0081] Warn on deprecated where clause inside angle brackets
and provide a fix-it to move it to the new location as referenced
in SE-0081.

Fix up a few stray places in the standard library that is still using
the old syntax.

Update any ./test files that aren't expecting the new warning/fix-it
in -verify mode.

While investigating what I thought was a new crash due to this new
diagnostic, I discovered two sources of quite a few compiler crashers
related to unterminated generic parameter lists, where the right
angle bracket source location was getting unconditionally set to
the current token, even though it wasn't actually a '>'.
2016-07-26 01:41:10 -07:00
Manav Gabhawala
7928140f79 [SE-0046] Implements consistent function parameter labels by discarding extraneous parameter names and adding _ where necessary 2016-04-06 20:21:58 -04:00
Doug Gregor
38c1de69e4 Reinstate "[SR-511][Parse] Add 'associatedtype' keyword and fixit"
This reverts commit ce7b2bcf09, tweaking
a few validation tests appropriately (1 crasher fixed, two -verify
tests that needed updating).
2016-01-14 00:21:48 -08:00
Erik Eckstein
ce7b2bcf09 Revert "[SR-511][Parse] Add 'associatedtype' keyword and fixit"
This reverts commit 2b2e9dc80e.

It broke some compiler crasher tests
2016-01-13 20:42:58 -08:00
gregomni
2b2e9dc80e [SR-511][Parse] Add 'associatedtype' keyword and fixit
Adds an associatedtype keyword to the parser tokens, and accepts either
typealias or associatedtype to create an AssociatedTypeDecl, warning
that the former is deprecated. The ASTPrinter now emits associatedtype
for AssociatedTypeDecls.

Separated AssociatedType from TypeAlias as two different kinds of
CodeCompletionDeclKinds. This part probably doesn’t turn out to be
absolutely necessary currently, but it is nice cleanup from formerly
specifically glomming the two together.

And then many, many changes to tests. The actual new tests for the fixits
is at the end of Generics/associated_types.swift.
2016-01-13 17:54:31 -08:00
Chris Lattner
7daaa22d93 Completely reimplement/redesign the AST representation of parameters.
Parameters (to methods, initializers, accessors, subscripts, etc) have always been represented
as Pattern's (of a particular sort), stemming from an early design direction that was abandoned.
Being built on top of patterns leads to patterns being overly complicated (e.g. tuple patterns
have to have varargs and default parameters) and make working on parameter lists complicated
and error prone.  This might have been ok in 2015, but there is no way we can live like this in
2016.

Instead of using Patterns, carve out a new ParameterList and Parameter type to represent all the
parameter specific stuff.  This simplifies many things and allows a lot of simplifications.
Unfortunately, I wasn't able to do this very incrementally, so this is a huge patch.  The good
news is that it erases a ton of code, and the technical debt that went with it.  Ignoring test
suite changes, we have:
   77 files changed, 2359 insertions(+), 3221 deletions(-)

This patch also makes a bunch of wierd things dead, but I'll sweep those out in follow-on
patches.

Fixes <rdar://problem/22846558> No code completions in Foo( when Foo has error type
Fixes <rdar://problem/24026538> Slight regression in generated header, which I filed to go with 3a23d75.

Fixes an overloading bug involving default arguments and curried functions (see the diff to
Constraints/diagnostics.swift, which we now correctly accept).

Fixes cases where problems with parameters would get emitted multiple times, e.g. in the
test/Parse/subscripting.swift testcase.

The source range for ParamDecl now includes its type, which permutes some of the IDE / SourceModel tests
(for the better, I think).

Eliminates the bogus "type annotation missing in pattern" error message when a type isn't
specified for a parameter (see test/decl/func/functions.swift).

This now consistently parenthesizes argument lists in function types, which leads to many diffs in the
SILGen tests among others.

This does break the "sibling indentation" test in SourceKit/CodeFormat/indent-sibling.swift, and
I haven't been able to figure it out.  Given that this is experimental functionality anyway,
I'm just XFAILing the test for now.  i'll look at it separately from this mongo diff.
2015-12-31 19:24:46 -08:00
Doug Gregor
efbba06906 Same-type requirements for the nested types of equivalent types are inferred.
The archetype builder was improperly propagating the requirement
source of a same-type requirement down to the inferred same-type
requirements for the nested types, causing a huge number of redundant
same-type requirements to show up in the canonical generic signatures
used for mangling. Mark such same-type requirements as inferred, which
shrinks the mangled name of LazyCollectionType.flatten from 4844 bytes
to 358 bytes.

Fixes rdar://problem/22861623.

Swift SVN r32356
2015-10-01 00:13:44 +00:00
Doug Gregor
f898887894 Eliminate getConformsTo() callback from the archetype builder.
The getConformsTo() callback was responsible for forcing the
collection of the requirements directly placed on an associated type,
which then get added to the archetype builder. This resulted in an
unhealthy dependency on the list of protocols attached to TypeDecl
(which should go away). Instead, retrieve the requirements from the
associated type's archetype (once it's been computed) or directly from
its list of "inherited" types (while we're building the generic
signature for the protocol itself).

Swift SVN r31332
2015-08-19 06:42:27 +00:00
Slava Pestov
00d6b27d80 ArchetypeBuilder: Attempt to reconcile differing superclass requirements when one is a subclass of the other
Swift SVN r30061
2015-07-10 04:59:20 +00:00
Slava Pestov
710df51450 ArchetypeBuilder: Only infer requirements for the innermost set of generic parameters
Fixes <rdar://problem/21366651>.

Swift SVN r30056
2015-07-10 03:01:54 +00:00
Roman Levenstein
984fcef576 Produce an error for generic parameters of functions if those parameters are not used in function signature.
If a generic parameter is not referred to from a function signature, it can never be inferred and thus such a function can never be invoked.

We now produce the following error:

generic parameter 'T' is not used in function signature
func f8<T> (x: Int) {}

This commit takes Jordan't comments on r28181 into account:
- it produces a shorter error message
- it does not change the compiler_crashers_fixed test and add a new expected error instead

Swift SVN r28194
2015-05-06 02:20:39 +00:00
Roman Levenstein
a3a25000ff Revert "Produce an error for generic parameters of functions if those parameters are not used in function signature."
This reverts commit r28181. I'll change it according to Jordan's comments and re-commit.

Swift SVN r28193
2015-05-06 02:20:36 +00:00
Roman Levenstein
ebe3fddbe6 Produce an error for generic parameters of functions if those parameters are not used in function signature.
If a generic parameter is not referred to from a function signature, it can never be inferred and thus such a function can never be invoked.

We now produce the following error:

There is no way to infer the generic parameter 'T' if it is not used in function signature
func f8<T> (x: Int) {}
             ^

Swift SVN r28181
2015-05-05 21:02:11 +00:00
Dmitri Hrybenko
3b04d1b013 tests: reorganize tests so that they actually use the target platform
Most tests were using %swift or similar substitutions, which did not
include the target triple and SDK.  The driver was defaulting to the
host OS.  Thus, we could not run the tests when the standard library was
not built for OS X.

Swift SVN r24504
2015-01-19 06:52:49 +00:00
Doug Gregor
16927ccb3d Revert "Move the computation of generic signatures into the archetype builder."
This reverts commit r23030.

This puts non-primary archetypes back in the all-archetypes list,
which is the deepest underlying problem behind
rdar://problem/19049566.

Conflicts:
	lib/AST/ArchetypeBuilder.cpp
	validation-test/compiler_crashers/0033-error.swift
	validation-test/compiler_crashers/035-multiple-typecheck-segfault.swift

Swift SVN r24333
2015-01-09 23:19:28 +00:00
Doug Gregor
72e9ca5d67 Move the computation of generic signatures into the archetype builder.
The archetype builder is responsible for figuring out what should go
into a generic signature anyway, so move the generic signature
creation there. This will also allow us to eliminate some code
duplication across Sema and AST.

Fixes compiler crasher 033.

Swift SVN r23030
2014-10-31 06:04:05 +00:00
Doug Gregor
d8a0c83345 Enumerate requirements in (roughly) the same order as we do when building generic signatures.
This will turn into "exactly" in a future commit, when we can build a
generic signature from an archetype builder (directly) and compare the
results.

Swift SVN r23010
2014-10-29 22:27:36 +00:00
Doug Gregor
1c712e069a Archetype builder: add witness markers to requirement enumeration
Swift SVN r23009
2014-10-29 22:27:35 +00:00
Doug Gregor
f270d7cf07 Archetype builder: don't prematurely lower to a dependent type when enumerating requirements.
Extend the contract for enumerateRequirements() a bit to preserve
archetype-to-archetype same-type constraints. Use that extra
information in the debug dump. NFC elsewhere.

Swift SVN r23008
2014-10-29 22:27:34 +00:00
Doug Gregor
dfa116600f Archetype builder: introduce "redundant" requirements to better track the requirements written in the source.
Essentially NFC at this point, but can be used for display purposes.

Swift SVN r22776
2014-10-15 21:51:31 +00:00
Mark Lacey
84b8f39cf8 Revert "Add requirements among distinct associated types with the same name."
This reverts commit r22680 because it causes a failure in one of the
check-swift-validation tests (parser/parse_stdlib.sil).

Swift SVN r22713
2014-10-13 23:09:03 +00:00
Doug Gregor
af898ff2e8 Add requirements among distinct associated types with the same name.
This pushes some of the extra same-type requirements introduced in r22649 into the generic signature. Doesn’t really have an effect outside of extending generic signatures slightly.

Swift SVN r22680
2014-10-10 23:48:01 +00:00
Doug Gregor
f9a6d88c19 Archetype builder: unify different associated types with the same name across protocols.
When we have two protocol conformance requirements for the same type
T, and each of those protocols has an associated type with a given
(shared) name N, infer a same-type requirement between the two. Only
really enabled for testing now; it doesn't feed into general type
checking yet.

Swift SVN r22649
2014-10-10 00:11:31 +00:00
Doug Gregor
59a355a67b Eliminate the notion of an archetype "index"; it's useless now. NFC
Swift SVN r22644
2014-10-09 22:43:54 +00:00
Doug Gregor
1380f758a5 Archetype builder: resolve nested types to associated types directly.
Effectively NFC because we're not making use of this information yet.

Swift SVN r22643
2014-10-09 21:52:34 +00:00
Doug Gregor
57ff959529 Add conformances with ArchetypeBuilder::PotentialArchetype::addConformance().
This means we'll get proper merging of requirement sources when
merging conformances due to same-type requirements.

Swift SVN r22642
2014-10-09 21:52:33 +00:00
Doug Gregor
f844ab4331 Store the generic type parameter type for non-associated potential archetypes.
This lets us map from potential archetypes back to dependent types
more efficiently, eliminating a linear search.

Swift SVN r22640
2014-10-09 21:52:32 +00:00
Doug Gregor
c4cd133737 Archetype builder: enumerate requirements directly from the potential archetypes.
Use this for the archetype builder debug dump so we can test it
without affecting compilation (yet).

Swift SVN r22639
2014-10-09 21:52:31 +00:00
Doug Gregor
91ddaf59eb Archetype builder: start tracking the locations and reasons for specific requirements.
Whenever we add a requirement, we now know

  (1) Why we added the requirement, e.g., whether it was explicitly written, inferred from a signature, or introduced by an outer scope.
  (2) Where in the source code that requirement originated.

Also add a debugging flag for dumping the archetype builder information, so we can write tests against it.

This is effectively NFC, but it's infrastructure to help a number of requirements-related tasks.

Swift SVN r22638
2014-10-09 21:52:28 +00:00