Commit Graph

520 Commits

Author SHA1 Message Date
Mark Lacey
9385dbb3fb Fix exponential type checking of tuple literals.
This fixes two easy cases where we would go exponential in type
checking tuple literals.

Instead of generating a conversion to a single type variable (which
results in one large constraint system), we generate a conversion ot
the same type that appears in the initializer expression (which for
tuples is a tuple type, which naturally splits the constraint system).

I experimented with trying to generalize this further, but ran into
problems getting it working, so for now this will have to do.

Fixes rdar://problem/20233198.
2018-03-26 14:46:03 -07:00
Nathan Hawes
d6894b82b3 Merge pull request #15247 from nathawes/rdar38144409-code-completion-assertion-hit-in-getTypeOfReference
[code-completion] Fix assertion hit in getTypeOfReference via code completion
2018-03-23 12:08:41 -07:00
Nathan Hawes
781539c5c3 [code-completion] Use the SanitizeExpr ASTWalker along all paths to the constraint based type checker
Code completions calls typecheckUnresolvedExpression when completing unresolved members.
This calls an overload of solve() that bypasses sanitization, and without it we hit an
assertion failure in getTypeOfReference.

Resolves rdar://problem/38144409.
2018-03-22 10:21:11 -07:00
Mark Lacey
994c2d1acd Improve type join for function types.
It is still not completely general, but this moves things along a
little bit.
2018-03-21 11:51:54 -07:00
Pavel Yaskevich
49a25f4d0f [ConstraintSystem] NFC: Use correct locator for function result type variables 2018-03-14 11:41:47 -07:00
Huon Wilson
e307e54098 [AST] Explicitly track things marked __owned. 2018-03-08 12:36:24 +11:00
Huon Wilson
78bdc95ce3 Merge pull request #14874 from huonw/at-owned-cleanup
Various refactorings for __owned.
2018-03-02 14:20:21 -08:00
Huon Wilson
b94c5364f5 [NFC] Rename 'Ownership' to 'ReferenceOwnership'.
There's really two forms of ownership: references and values. Renaming
to make way for better distinguishing of the two.
2018-03-02 11:38:28 -08:00
Mark Lacey
758570982a Start stubbing out more parts of Type::join().
Improve support for Optional among other things.

Return Any when it is really the best answer given the types involved,
or nullptr if we cannot yet produce an accurate result.

This implementation returns Any? when joining Any with an
Optional<T>. In our type system both Any and Any? are effectively
subtypes of one another since each can hold all the values that the
other can hold. So this choice is somewhat arbitrary, but does line up
nicely with the notion that T is always a subtype of T?.
2018-02-28 23:13:59 -08:00
Pavel Yaskevich
79d706463b Merge pull request #13664 from xedin/finalize-expr-type-cache
[ConstraintSystem] Only set types on expressions in presence of solution
2018-02-13 21:43:34 -08:00
Mark Lacey
dc8c88058f Fix an issue with the constraint favoring code in the constraint optimizer.
The code was favoring overloads where *either* argument matched its
parameter type and where both parameter types were the same. This
is really overly broad and can result in bugs like the one here, where
we only attempt the
  (Int, Int) -> Bool
overload of '!=' despite the fact that it means forcing an
optional. We should select the
  <T>(T?, T?) -> Bool
overload instead, and inject the non-optional operand into an optional.

Making this require *both* arguments to match mostly disables this
optimization, though, since for somethin like "a"+"b"+"c" the second
add has a type variable and string literal as arugments.

So instead, we'll just disable this in cases where one argument is the
optional of the other, to ensure we never try forcing one side. This
also means we'll disable the optimization in cases where we would
inject one operand into an optional, but that's likely find as we
don't have long chains of expressions where that happens.

Fixes rdar://problem/37073401.
2018-02-13 10:24:30 -08:00
Pavel Yaskevich
8c17b925e6 [ConstraintSystem] Add TypeLoc caching to constraint system
This is useful for explicit casts and type expressions, where
type loc and expression types might be different, and allows
constraint solver to avoid setting opened types to expressions
which resolves multiple crashes.
2018-02-13 00:08:56 -08:00
Pavel Yaskevich
c0f5711ee5 [ConstraintSystem] Don't apply types to expressions until solution is found
Resolves: rdar://problem/36744895
2018-02-13 00:08:45 -08:00
Mark Lacey
d63bb3fc53 Remove most uses of OptionalTypeKind.
What remains are places where we are conflating optionality with
either nullability or failability.
2018-02-10 16:24:09 -08:00
Mark Lacey
5613d39636 IUO: Hoist the IUO disjunction creation to a better place.
We should just create the disjunction straight-away in resolveOverload
rather than doing it deep inside type variable binding.
2018-01-09 15:45:52 -08:00
Mark Lacey
ad1dbac694 IUO: Create disjunctions for optional choices.
When binding an optional value, or function that returns an optional
value, if that value was produced from a decl that was declared an
IUO, create a disjunction.

After solving, make use of the disjunction choices in rewriting
expressions to force optionals where needed.

This is disabled for now, as it results in a source compatibility
issue without associated changes that actually start generating
Optional<T> in place of ImplicitlyUnwrappedOptional<T>. It's
complicated, but basically having two '??' (one returning T, one
returning T?) and creating a disjunction where the first (favored)
choice is ImplicitlyUnwrappedOptional<T> and second is T results in
our selecting the wrong '??' in some cases.
2018-01-08 14:31:03 -08:00
Mark Lacey
3a7c523129 IUO: Finish up support for subscript. 2018-01-04 19:32:31 -08:00
Mark Lacey
41479f6334 IUO: Start creating the DeclForImplicitlyUnwrappedOptional OverloadChoice.
Use this in places where we have a decl that is marked with the
ImplicitlyUnwrappedOptionalAttr so that we can distinguish in the
solver which decls need to be potentially unwrapped in order to type
check successfully.
2018-01-04 14:00:36 -08:00
Mark Lacey
dbb293289a IUO: Replace one ConstraintLocator with two.
Replace ImplicitlyUnwrappedCoercionResult with the more general
ImplicitlyUnwrappedValue.

Add ImplicitlyUnwrappedDisjunctionChoice as a marker that indicates
that we've already created a disjunction from an
ImplicitlyUnwrappedValue, in order to avoid infinite recursion during
binding.

Also add support to buildDisjunctionForImplicitlyUnwrappedOptional for
functions returning optionals.

NFC.
2018-01-04 10:30:25 -08:00
Mark Lacey
8d86619fd7 IUO: Put the pieces in place to handle coercions and casts to IUO types.
For casts like 'x as T!' or 'x as! T!', generate a disjunction that
attempts to bind both the optional and non-optional T, preferring the
optional branch of the disjunction.

Note that this should effectively be NFC at the moment because we're
still generating the IUO type for T! rather than a plain optional, so
we first attempt the IUO type (as opposed to an Optional<T>) which
goes through existing logic that handles the IUOs as part of the type
system.

The new rewriting logic will actually do something once we switch over
to generating Optional<T> when T! is uttered.
2018-01-04 05:12:05 -08:00
Slava Pestov
422000a2f2 Sema: Remove isProtocolExtensionUsable()
It doesn't do anything that isExtensionApplied() doesn't do.
2017-12-12 21:12:46 -08:00
Mark Lacey
116b6649eb [ConstraintSystem] Use locator from result of cast rather than source.
Be consistent with CoerceExpr and for the constraint we create use the
locator from the result of the expression rather than the locator from
the source of the expression.
2017-12-10 14:20:06 -08:00
Mark Lacey
66a11ce66b Revert "[ConstraintSystem] Use semantics providing exprs when dealing with favored types."
This reverts commit 2f80af15ec.

I expected this to have no effect, but it results in one of the
expression type checker tests taking longer, so that test was disabled.

This commit also re-enables the test since it now passes again.
2017-12-02 21:46:31 -08:00
Mark Lacey
2f80af15ec [ConstraintSystem] Use semantics providing exprs when dealing with favored types.
This allows us to eliminate some special casing around things like
parens, and allows the code to work for other code involving expressions
that are semantically neutral.
2017-11-30 19:11:59 -08:00
Pavel Yaskevich
0783890b03 [CSGen] Fix LinkedExprAnalyzer greedy operator linking
Let's not attempt to link arithmetic operators together in
presence of concrete and literal (int, float, string) types.

Resolves: rdar://problem/35740653
2017-11-29 22:13:45 -08:00
Arnold Schwaighofer
c544a2779d Revert "[CSGen] Fix LinkedExprAnalyzer greedy operator linking"
This reverts commit 29ea599f30.

SR-6505
2017-11-29 19:42:47 -08:00
Pavel Yaskevich
e4d83a7a2e Merge pull request #13152 from xedin/rdar-35740653
[CSGen] Fix `LinkedExprAnalyzer` greedy operator linking
2017-11-29 16:57:52 -08:00
Pavel Yaskevich
29ea599f30 [CSGen] Fix LinkedExprAnalyzer greedy operator linking
Let's not attempt to link arithmetic operators together in
presence of concrete and literal types.

Resolves: rdar://problem/35740653
2017-11-29 14:48:10 -08:00
David Zarzycki
a550042391 Merge pull request #13050 from davezarzycki/nfc_enum_class_TypeResolutionFlags
NFC: Use 'enum class' for TypeResolutionFlags
2017-11-27 07:07:11 -05:00
Chris Lattner
3f5790fb37 Enhance MemberLookupResult's UnviableCandidates list, NFC.
We now store an entire OverloadChoice in the unviable candidates
list (which is used for error recovery), just like we store them
for viable candidates.

The additional information isn't used, so NFC.
2017-11-26 14:29:00 -08:00
David Zarzycki
835d1a221c NFC: Use 'enum class' for TypeResolutionFlags 2017-11-22 09:14:33 -05:00
Pavel Yaskevich
7b121de1a0 [CSRanking] Change ranking to weight overload choices in evaluation order
Consider different overload choices for the same location in evaluation
order, this makes overload resolution more predictable because it's going
to follow expression bottom-up, that prevents situations when some
expressions are considered ambigious because choices taken further up
equate the score, instead each level is given distinct weight
based on evaluation order.

Resolves: rdar://problem/31888810
2017-11-16 13:38:24 -08:00
Robert Widmann
ebdd126a2c [NFC] Remove more uses of <InOutType> 2017-10-22 02:00:07 -04:00
Huon Wilson
96172048d3 [Sema/CS] Convenience function for adding a Requirement as a constraint. 2017-10-10 20:17:40 -07:00
Pavel Yaskevich
8f14126b0e [CSGen] Give a correct locator to each type var representing closure parameter
Fixes a bug where all of the type variables related to closure parameters
without type would be attached to the same locator.
2017-10-02 13:58:54 -07:00
Slava Pestov
b232b8f604 Fix some warnings 2017-09-29 00:24:22 -06:00
Doug Gregor
46e4c6d365 [Constraint system] Eliminate some dead code in constraint optimization.
The code is mistakenly dereferencing an "end" iterator and currently
has no effect. Remove it to make valgrind happier.
2017-09-28 13:47:37 -07:00
Mark Lacey
c0c848d2b3 Add Builtin.type_join* family of functions.
These will be used for unit-testing the Type::join functionality in the
type checker. The result of the join is replaced during constraint
generation with the actual type.

There is currently no checking for whether the arguments can be used to
statically compute the value, so bad things will likely happen if
e.g. they are type variables. Once more of the basic functionality of
Type::join is working I'll make this a bit more bullet-proof in that
regard.

They include:
  // Compute the join of T and U and return the metatype of that type.
  Builtin.type_join<T, U, V>(_: T.Type, _: U.Type) -> V.Type

  // Compute the join of &T and U and return the metatype of that type.
  Builtin.type_join_inout<T, U, V>(_: inout T, _: U.Type) -> V.Type

  // Compute the join of T.Type and U.Type and return that type.
  Builtin.type_join_meta<T, U, V>(_: T.Type, _: U.Type) -> V.Type

I've added a couple simple tests to start off, based on what currently
works (aka doesn't cause an assert, crash, etc.).
2017-09-21 14:43:26 -07:00
Jordan Rose
4e50e4ea1c Remove some unnecessary mapTypeIntoContext calls from CSGen. (#11743)
These helpers are all about trying to decide if a particular overload
is "favored", but the answer won't change if a local archetype is
actually the type we're looking for, so there's no advantage to using
contextual types over interface types. On top of that, the calls to
mapTypeIntoContext are forcing the deserialization of several generic
environments that would have otherwise been left untouched, resulting
in unnecessary calls to the generic signature builder.

This isn't a recent change, but we're seeing strain around the generic
signature builder and complex constraints in the standard library
anyway, so we'll take wins where we can get them.

Discovered as part of the performance investigation in
rdar://problem/33984386.
2017-09-05 13:24:49 -07:00
Slava Pestov
ce12e643b7 Sema: Fix crash when we try to generate constraints for invalid code
Perhaps we shouldn't visit AST nodes for which pre-checking failed
at all, but that would be a bigger change.

Fixes <rdar://problem/34077439>.
2017-08-28 18:09:51 -07:00
Pavel Yaskevich
bbaa232731 [ConstraintSolver] Fix computeFavoredTypeForExpr not to impose types on the merged binary operators
`LinkedExprAnalyzer` is not always precise in collecting types of expressions,
so let's not try to impose anything upon linked operator expressions, which are
"mergable", except actually merging argument/result types together to help
constraint solver.

Resolves: rdar://problem/27700622
2017-08-10 12:31:17 -07:00
Slava Pestov
a3cad02708 Sema: Stricter superclass constraints when opening a generic signature
A generic signature like <T : SomeClass> would create
a subtype constraint between the type variable for T and
SomeClass. This is too loose, because a subclass existential
like 'SomeClass & SomeProto' is a subtype of SomeClass, but
cannot bind to T, because it is not representationally a
single retainable pointer.

Fixes <rdar://problem/32617814>, <https://bugs.swift.org/browse/SR-5142>.
2017-07-24 22:58:04 -07:00
David Zarzycki
f882873996 [Sema] NFC: Remove dead code
ReferenceStorageTypes are handled elsewhere.
2017-07-21 11:25:49 +01:00
Robert Widmann
8cdddef2f8 Refactor Params to use flags
Also, begin to pass around base types instead of raw InOutType types.  Ideally, only Sema needs to deal with them, but this means that a bunch of callers need to unwrap any inouts that might still be lying around before forming these types.

Multiple parts of the compiler were slicing, dicing, or just dropping these flags.  Because I intend to use them for the new function type representation, I need them to be preserved all across the compiler.  As a first pass, this stubs in what will eventually be structural rules as asserts and tracks down all callers of consequence to conform to the new invariants.

This is temporary.
2017-07-19 09:49:32 -07:00
Alex Hoppen
f8c2692f79 Introduce special decl names
Special DeclNames represent names that do not have an identifier in the
surface language. This implies serializing the information about whether
a name is special together with its identifier (if it is not special)
in both the module file and the swift lookup table.
2017-07-11 19:04:13 +02:00
Joe Groff
51672d3bbf Sema: Allow KeyPath and KeyPathApplicationExprs to re-type-check during failure diagnosis.
Fixes SR-5034 | rdar://problem/32488872.
2017-07-10 16:43:34 -07:00
Joe Groff
6ad01d63f6 Sema: Feed argument label and constraint locator info from key path subscript components into getCalleeDeclAndArgs.
Fixes SR-5189 | rdar://problem/32713662.
2017-07-10 14:55:08 -07:00
Robert Widmann
957d633185 Rename getInOutOrLValueObjectType to getWithoutSpecifierType
Prepares the AST for a future in which more than just inout and
@lvalue need to be stripped off of ephemeral types.
2017-07-06 09:35:04 -07:00
Robert Widmann
4411223156 [NFC] Switch some more of the frontend to params 2017-06-30 10:24:52 -07:00
Joe Groff
3c82e981f9 KeyPaths: Add support for optional chaining/forcing components.
rdar://problem/31768715
2017-06-26 09:40:31 -07:00