Commit Graph

223 Commits

Author SHA1 Message Date
Alexis Laferrière
cfae7bba3d Avoid cycles when computing a requirement signature
Replace most remaining uses of isRequirementSignatureComputed by
isRequirementSignatureComputing which uses Evaluator::hasActiveRequest
to detect if the requirements are currently being computed.
2019-05-09 10:50:45 -07:00
David Zarzycki
236a0dcfe2 Revert "[GSB] NFC: Precompute or cache DependentMemberType when possible"
This reverts commit 8876c9e78f, which has
a memory leak.
2019-03-06 10:01:37 -05:00
David Zarzycki
8876c9e78f [GSB] NFC: Precompute or cache DependentMemberType when possible
Calling DependentMemberType::get() repeatedly pollutes the processor
caches because the global hash table can be quite large. With this
change, we either precompute DependentMemberTypes or cache them on
demand. This change makes the release/no-assert build of
Swift.swiftmodule 4.1% faster.
2019-03-02 10:14:49 -05:00
Suyash Srijan
60da82b70d [GSB] [Diag] Constraint to concrete type using ":" should offer a fix-it (#22152)
* [diag] add a diagnostic note for the fixit

* [gsb] emit a diagnostic with fixit to replace ':' with '=='

* [gsb] rename variable

* [gsb] replace dyn_cast with isa

* [test] add a test case

* [test] update tests

* [gsb] emit diagnostic for protocols as well

* [gsb] simplify if statement

* [gsb] rename a variable

* [gsb] Create a helper to remove Self. prefix and add a new test case

* [gsb] simplify checks

* [gsb] move the diagnostic code to finalize()

* [gsb] re-indent

* [gsb] fix a typo

* [gsb] pass values as copy

* [gsb] show a fixit if the subject type is a member type

* [test] update diagnostics in existing tests

* [gsb] check if the subject type has an assoc type decl

* [gsb] use requirement source

* [test] add new tests

* [gsb] use constraint struct and rename to invalidIsaConstraints
2019-02-08 11:02:32 -05:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Slava Pestov
7ffe361709 Sema: Clean up generic signature checking 2018-11-16 01:18:18 -05:00
Slava Pestov
8acc11f80c Sema: Fix ambiguity resolution when doing unqualified lookup of associated types and type aliases
In structural lookup mode, let's resolve protocol typealiases to
dependent member types also. This is because areSameType() has
no way to see if a type alias is going to be equal to another
associated type with the same name, and so it would return false,
which produced ambiguity errors when there should not be any.

This exposes a deficiency in how we diagnose same-type constraints
where both sides are concrete. Instead of performing the check on
the requirement types, which might be dependent member types that
later on resolve to concrete types, do the check on the actual
equivalence classes further down in the GSB instead.

However, this in turn produces bogus diagnostics in some recursive
cases where we add same-type constraints twice for some reason,
resulting in a warning the second time around. Refine the check by
adding a new predicate to FloatingRequirementSource for requirements
that are explicitly written in source... which is not what
isExplicit() currently means.
2018-10-15 01:52:11 -07:00
Saleem Abdulrasool
d281b98220 litter the tree with llvm_unreachable
This silences the instances of the warning from Visual Studio about not all
codepaths returning a value.  This makes the output more readable and less
likely to lose useful warnings.  NFC.
2018-09-13 15:26:14 -07:00
Doug Gregor
868f763406 [GSB] Ensure that we don’t pick a self-recursive requirement source.
When computing the conformance access path, make sure that we don’t pick
a self-recursive requirement source.
2018-08-23 14:20:25 -07:00
Doug Gregor
fcfd4c8a8c [Type checker] Introduce a request to evaluate “where” clause requirements.
Introduce a new form of request that produces the ith Requirement of the
where clause of a given entity, which could be a protocol, associated type,
or anything with generic parameters. The requirement can be evaluated
at any type resolution stage; the “interface” type stage will be cached
in the existing RequirementReprs.

Fixes SR-8119 / rdar://problem/41498944.
2018-08-23 09:25:58 -07:00
Jordan Rose
f8fd85b51b Cache ConformanceAccessPaths in GSB::EquivalenceClass for big wins
This dramatically speeds up SubstitutionMap::lookupConformance,
shaving off 1/3 of the time spent compiling the standard library.
2018-08-09 11:58:19 -07:00
Huon Wilson
10b30fef78 [GSB] Explicit error on unsupported conditional conformance recursion.
This doesn't fix the fundamental problem of correctly handling such cases, but
it is better than the "error message" that occurred previously:

    Assertion failed: ((bool)typeSig == (bool)extensionSig && "unexpected generic-ness mismatch on conformance").

Fixes the crash rdar://problem/41281406 (that in
https://bugs.swift.org/browse/SR-6569 (rdar://problem/36068136)),
https://bugs.swift.org/browse/SR-8019 (rdar://problem/41216423) and
https://bugs.swift.org/browse/SR-7989 (rdar://problem/41126254).
2018-06-21 10:53:30 +10:00
Huon Wilson
1ba49949f9 [NFC]/[GSB] Move addConditionalRequirements to be a method. 2018-06-20 18:30:27 +10:00
Slava Pestov
bd6281c558 AST: Change SubstitutionMap conformance lookup callbacks to take ProtocolDecl and not ProtocolType 2018-05-19 01:09:17 -07:00
Huon Wilson
0e337acae0 Revert "[AST] Ensure requirements are correctly identified as conditional." 2018-03-14 08:12:37 +11:00
Doug Gregor
31792ac3f7 [GSB] Eliminate potential archetypes from the same-type constraint representation.
Same-type constraints are one of the primary places where potential
archetypes are used explicitly in the GenericSignatureBuilder. Switch
the representation over to a dependent type (represented by a `Type`)
instead, furthering the demise of PotentialArchetype.
2018-03-12 10:33:39 -07:00
Doug Gregor
4481a42c49 [GSB] Push potential archetype realization slightly deeper.
Same-type constraints are (still) described in terms of potential
archetypes, so push the "realization" operation for potential
archetypes down into the function that adds a same-type constraint
between type parameters.
2018-03-11 23:55:23 -07:00
Doug Gregor
0f7c822371 [GSB] Add same-type rewrite rules via dependent types.
GenericSignatureBuilder::addSameTypeRewriteRule() was described in
terms of an equivalence class and a potential archetype. Rework it in
terms of two dependent types (i.e., the anchors of their equivalence
classes), so we don't need to rely on the PotentialArchetype
mechanism.
2018-03-11 23:55:23 -07:00
Doug Gregor
35902bbee2 [GSB] Simplifying via the term rewriting system cannot fail.
Now that we no longer have DependentMemberTypes within the GSB that don't
have associated type declarations, we can no longer fail when
unpacking type into a RewritePath, so remove a bunch of optionals and
null Type checks that are now superfluous.
2018-03-11 23:55:22 -07:00
Huon Wilson
be28f6830c Merge pull request #15094 from huonw/conditional-requirement-order
[AST] Ensure requirements are correctly identified as conditional.
2018-03-12 09:53:02 +11:00
Doug Gregor
91aa964714 [GSB] Minimize the right-hand sides of rules in the term-rewriting system.
Introduce the first step of a minimization algorithm for the
term-rewriting system used to produce anchors of equivalence
classes. This step simplifies the right-hand sides of each rewrite
rule, so that each rewrite step goes to the minimal result.

This code is currently not enabled; it *can* be enabled by minimizing
before computing anchors, but it ends up pessimizing compile times to
do so.
2018-03-09 10:42:14 -08: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
Pavel Yaskevich
4b05449c37 [GSB] Eliminate concrete potential archetypes by early substitution
To make generic signature builder more robust it's imperative to
eliminate possibility of out-of-order typealias substitution.

These changes try to make it so potential archetypes could only be
constructed with associated types by doing early concrete type (typealias)
subsitution while trying to resolve equivalence class.
2018-03-06 23:34:19 -08:00
Doug Gregor
bd266b0520 [GSB] Introduce type-rewriting rules only when we merge equivalence classes.
Instead of representing every same-type constraint we see as a rewrite rule,
only record rewrite rules when we merge equivalence classes, and record
rules that map the between the anchors of the equivalence classes. This
gives us fewer, smaller rewrite rules that (by construction) build correct
anchors.
2018-02-16 15:09:58 -08:00
Doug Gregor
1e7562a527 [GSB] Simplify the anchor cache to always rely on term rewriting.
Use term rewriting exclusively in the computation of the canonical
dependent type (anchor) for an equivalence class, eliminating any dependence
on the specific potential archetypes and eliminating the hacks around
generic type parameters.
2018-02-07 16:37:29 -08:00
Doug Gregor
f2bef2da54 [GSB] Generalize rewrite paths to support “absolute” paths.
Previously, the term rewriting logic for same-type constraints was only 
able to express “relative” rewrites, where the base of the type being 
rewritten (i.e., the generic type at the root) is unchanged. This meant
that we were unable to capture same-type constraints such as “T == U” or
“T.Foo == T” within the rewriting system.

Separate out the notion of a rewrite path and extend it with an optional
new base. When we’re simplifying a term and we encounter one of these
replacements, the whole type from the base up through the last-matched
path component gets replaced with the replacement path.
2018-02-07 16:37:29 -08:00
Doug Gregor
a41922169a [GSB] Use term rewriting to compute anchors of equivalence classes.
Fully rewriting a given type will produce the canonical representation of that type, because all rewrite rules take a step toward a more-canonical type. Use this approach to compute the anchor of an equivalence class, replacing the existing ad hoc approach of enumerating known potential 
archetypes—which was overly dependent on having the “right” set of
potential archetypes already computed.
2018-02-07 16:37:29 -08:00
Doug Gregor
bfeb846475 [GSB] Record same-type constraints as term rewrite rules.
Introduce a new representation of same-type constraints as rewrite
rules in a term-rewriting system, where each same-type constraint maps
to a rewrite rule that produces a "more canonical"
term. Fully-simplifying a type according to these rewrite rules will
produce the canonical representation of that type, i.e., the "anchor"
of its equivalence class.

The rewrite rules are stored as a prefix tree, such that a "match"
operation walks the tree matching the prefix of a path of associated
type references, e.g., SubSequence -> SubSequence -> Element, and any
node within the tree can provide a replacement path (e.g.,
"Element"). The "dump" operation provides a visualization of the tree,
e.g.,

::
  `--(cont'd)
      `--Sequence.Iterator
      |   `--IteratorProtocol.Element --> [Sequence.Element]
      `--Sequence.SubSequence --> []
      |   `--Sequence.Element --> [Sequence.Iterator -> IteratorProtocol.Element]
      |   |   `--(cont'd) --> [Sequence.Element]
      |   `--Sequence.Iterator --> [Sequence.Iterator]
      |   |   `--IteratorProtocol.Element --> [Sequence.Iterator -> IteratorProtocol.Element]
      |   `--Sequence.SubSequence --> []
      |   |   `--Sequence.Element --> [Sequence.Element]
      |   |   `--Sequence.Iterator --> [Sequence.Iterator]
      |   `--C.Index --> [C.Index]
      |   `--C.Indices --> [C.Indices]
      |       `--Sequence.Element --> [C.Indices -> Sequence.Element]
      |       `--Sequence.SubSequence --> [C.Indices -> Sequence.SubSequence]
      |       `--C.Index --> [C.Indices -> C.Index]
      |       `--C.Indices --> [C.Indices -> C.Indices]
      `--C.Index --> [Sequence.Element]
      `--C.Indices
          `--Sequence.Element --> [C.Index]

Thus far, there are no clients for this information: this commit
starts building these rewriting trees and can visualize them for debug
information.
2018-02-07 16:33:10 -08:00
David Zarzycki
3da6fe9c0d [AST] NFC: Do not reinterpret_cast pointers into CanTypeWrappers
This also introduces 'TypeArrayView' for when a 'Type' is statically
known to be a given TypeBase subclass.
2018-01-31 11:20:05 -05:00
Doug Gregor
e10ecbd80e [GSB] Only check protocol requirements not generated from the signature.
If we used the requirement signature to create a protocol requirement
element in a requirement source, there's no need to verify that it's
from the requirement signature (duh).
2017-12-13 20:17:18 -08:00
Doug Gregor
5d5e6122f3 [GSB] Add verification of all generic signatures within a module.
Add a verification pass to ensure that all of the generic signatures in
a module are both minimal and canonical. The approach taken is quite
direct: for every (canonical) generic signature in the module, try
removing a single requirement and forming a new generic signature from
the result. If that new generic signature that provide the removed
requirement, then the original signature was not minimal.

Also canonicalize each resulting signature, to ensure that it meets the
requirements for a canonical signature.

Add a test to ensure that all of the generic signatures in the Swift
module are minimal and canonical, since they are ABI.
2017-11-07 15:20:38 -08:00
Doug Gregor
6fc218ba7d [GSB] Make compareDependentTypes() visible to other translation units. 2017-11-04 22:46:01 -07:00
Doug Gregor
5b8c914582 [GSB] Reimplement equivalence class "anchor" logic.
Replace the pair of PotentialArchetype's getArchetypeAnchor() and
getNestedArchetypeAnchor() with a straightforward, more-efficient
computation based on equivalence classes. This reduces the number of
times we query the archetype anchor cache by 88% when building the
standard library, as well as eliminating some
PotentialArchetype-specific APIs.
2017-10-27 21:46:45 -07:00
Doug Gregor
4ff05fa51b [GSB] Eliminate a bunch of unnecessary PotentialArchetype* interfaces.
All of these constraint-checking functions work on the equivalence class.
2017-10-27 14:00:43 -07:00
Doug Gregor
1117eff2a8 [GSB] Eliminate PotentialArchetype from enumerateRequirements(). 2017-10-27 13:49:18 -07:00
Doug Gregor
29390e095a [GSB] Switch the anchor of a derived same-type component to UnresolvedType.
It was a PotentialArchetype*, but we don't want to have to realize a
potential archetype to model this.
2017-10-27 13:32:59 -07:00
Doug Gregor
16ea8aedf2 [GSB] Switch some more APIs away from PotentialArchetype. 2017-10-27 12:58:42 -07:00
Doug Gregor
d2f0df4b3b [GSB] Eliminate unused Constraint<T>::realizeSubjectPotentialArchetype(). 2017-10-24 16:18:07 -07:00
Doug Gregor
f5e44a4968 [GSB] Don't realize potential archetypes for same-type-to-concrete constraints. 2017-10-24 11:11:21 -07:00
Doug Gregor
642b45835c [GSB] Use result of recordSameTypeConstraint() to short-circuit.
Centralizes the logic for short-circuiting based on same-type
constraints.
2017-10-23 21:51:54 -07:00
Doug Gregor
bd0625f083 [GSB] Flatten list of same-type requirements.
Equivalence classes stored their same-type constraints in a MapVector
keyed on the source potential archetype, which allowed traversal along the
paths of the graph. However, this capability isn't required, because we
end up walking all of the edges each time. Flatten the list of same-type
constraints to a single vector, eliminating constraint duplication between
the source and the target out-edge lists.

This cuts down on the number of same-type constraints we record by 50%,
but performance gains are limited (6% of stdlib type-checking time)
because most of the time these same-type constraints were skipped
anyway.
2017-10-23 21:33:41 -07:00
Doug Gregor
e8c9b3ffa2 [GSB] Eliminate the GenericSignatureBuilder from PotentialArchetype.
Now that getBuilder() is gone, stash an ASTContext into root
PotentialArchetypes instead of a GenericSignatureBuilder. This eliminates
some ickiness where we had to re-root potential archetypes when moving a
GenericSignatureBuilder.
2017-10-23 14:21:43 -07:00
Doug Gregor
2f3b2dfc7b [GSB] Plumb generic params through getDependentType() fully.
This allows us to eliminate `getBuilder()`.
2017-10-23 14:11:38 -07:00
Doug Gregor
214f9d54ca [GSB] Eliminate the penultimate caller to PotentialArchetype::getBuilder(). 2017-10-23 11:38:36 -07:00
Doug Gregor
a510e9b56d [GSB] Eliminate most uses of PotentialArchetype::getBuilder().
The remaining two uses after this refactor are... stubborn.
2017-10-23 10:39:21 -07:00
Doug Gregor
42c1619088 [GSB] Eliminate a debugging-only source of potential archetype realization. 2017-10-20 21:57:23 -07:00
Doug Gregor
972f04832b [GSB] Teach expandConformanceRequirement() to use a resolved type.
Eliminates another too-early source of realization of potential archetypes.
2017-10-20 21:52:40 -07:00
Doug Gregor
28e3e5cf40 [GSB] Eliminate potential archetypes from requirement sources entirely. 2017-10-20 21:38:12 -07:00
Doug Gregor
1f09f3bc10 [GSB] Eliminate potential archetypes from minimizing requirement sources.
Refactor the interfaces that involve “walking” a requirement source
(e.g., minimization, computation of the affected type, etc.) to rely
only on interface types and not potential archetypes.
2017-10-20 16:42:39 -07:00
Doug Gregor
aa9755d149 [GSB] Start moving RequirementSource off of potential archetypes.
Eliminate potential archetypes from most of the public interface of
RequirementSource.
2017-10-20 15:18:36 -07:00