Commit Graph

875 Commits

Author SHA1 Message Date
Chris Lattner
3802c986de Improve DML attribute caching, as recommended by Doug. (#14697) 2018-02-17 07:32:02 -08:00
Chris Lattner
a0fa5d11b4 Implement SE-0195, which introduces "Dynamic Member Lookup" Types (#14546)
* Implement the recently accepted SE-0195 proposal, which introduces "Dynamic
Member Lookup" Types.  This is a dusted off and updated version of PR13361,
which switches from DynamicMemberLookupProtocol to @dynamicMemberLookup as
was requested by the final review decision.  This also rebases it,
updates it for other changes in the compiler, fixes a bunch of bugs, and adds support for keypaths.  

Thank you to @rudx and @DougGregor in particular for the helpful review comments and test cases!
2018-02-16 16:19:50 -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
6310aca2c0 Restore a very narrow function argument conversion for -swift-version 4.
Allow functions with type `(()) -> T` to be passed in places where we
expect `() -> T`, but only for -swift-version 4 (for -swift-version 3
this already works due to other horrible things in CSSimplify.cpp).

We need to look at how we can help migrate these cases to
-swift-version 5, but in the meantime, but that is something we can
consider separately.
2018-02-07 18:07:43 -08:00
Pavel Yaskevich
b233f166dc Merge pull request #14441 from xedin/rdar-37160679
[CSSolver] Increment score when performing certain function conversions
2018-02-07 11:00:56 -08:00
Mark Lacey
1de9897644 Fix SR-6837 - allow function conversion for -swift-version 4 *only*
We inadvertantly allowed a function conversion for Swift 4 that we did
not intend to allow.

This commit adds an extremely narrow fix to continue to allow this for
-swift-verson 4 only.

Fixes rdar://problem/36875195 / https://bugs.swift.org/browse/SR-6837
2018-02-06 19:40:25 -08:00
Pavel Yaskevich
8316353e07 [CSSolver] Increment score when performing certain function conversions
Increase solution score when performing function conversions where only
one side has `@autoclosure`. That is going to help pick the best overload
when only difference lays in presence of such attribute.

e.g.

```swift
func foo(_: @autoclosure () -> Int) {}
func foo(_: () -> Int) {}
```

If the argument is itself `@autoclosure` it's preferable to use overload
with `@autoclosure` attribute, otherwise `() -> Int` should be used.

Resolves: rdar://problem/37160679
2018-02-06 18:55:10 -08:00
Mark Lacey
be8defb29e Rename lookThroughAllAnyOptionalTypes to lookThroughAllOptionalTypes. 2018-02-05 23:59:01 -08:00
Mark Lacey
7f805ba2bc Replace classifyAsOptionalType with isOptionalDecl. 2018-02-05 23:59:00 -08:00
Mark Lacey
b4b66bc8e8 Replace getAnyOptionalObjectType with getOptionalObjectType. 2018-02-05 23:59:00 -08:00
Mark Lacey
3654dcc8fe Remove getImplicitlyUnwrappedOptionalObjectType. 2018-02-03 10:57:11 -08:00
Mark Lacey
3f9689e477 Remove lookThroughImplicitlyUnwrappedOptionalType. 2018-02-03 10:57:10 -08:00
Mark Lacey
2008674495 Make ImplicitlyUnwrappedOptional<T> an unavailable typealias.
Also remove the decl from the known decls and remove a
bunch of code referencing that decl as well as a bunch of other
random things including deserialization support.

This includes removing some specialized diagnostics code that
matched the identifier ImplicitlyUnwrappedOptional, and tweaking
diagnostics for various modes and various issues.

Fixes most of rdar://problem/37121121, among other things.
2018-02-02 08:35:53 -08:00
Mark Lacey
8d115b8e73 Revert "Allow inout arguments that differ in optionality than the expected parameter."
This partially reverts commit 8685ee01a1.

The tests are still in place, but the code change is no longer necessary
now that IUOs are removed from the type system.

Fixes: rdar://problem/37013789
2018-01-31 12:15:58 -08:00
Mark Lacey
f19c6a1417 Merge pull request #14242 from rudkx/rdar36913150
Allow inout arguments that differ in optionality than the expected pa…
2018-01-29 16:56:17 -08:00
Mark Lacey
8685ee01a1 Allow inout arguments that differ in optionality than the expected parameter.
Allow passing Optional<T> as inout where
ImplicitlyUnwrappedOptional<T> is expected, and vice-versa.

Swift 4.1 added a warning that overloading inouts by kind of optional
was deprecated and would be removed, but we didn't actually allow
people to remove an overload and pass arguments of the other kind of
optional to the remaining function.

Fixes rdar://problem/36913150
2018-01-29 15:07:44 -08:00
Doug Gregor
ee99c8af99 [Type checker] Validate class constraints for generic arguments of types.
Class constraints (spelled T: AnyObject) on generic types were not
getting checked on generic arguments. This appears to be a regression
introduced in Swift 4.0 with the removal of AnyObject, leading to a
fairly significant soundness hole that could produce crashers later
on.

Fixes SR-6841 / rdar://problem/36884025.
2018-01-25 16:34:08 -08:00
gregomni
90f0edc009 Need to check that the keypath decl is an AbstractStorageDecl instead of assuming it. 2018-01-14 15:05:44 -08:00
Pavel Yaskevich
4e6822db6b [CSSimplify] NFC: Switch matchTypes methods to use TypeMatchResult 2018-01-10 15:06:43 -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
Slava Pestov
d843b10bcc Sema: Fix crash with unresolved dot expression with optional protocol metatype base
Mostly fixes <rdar://problem/35945827>, but there is a case that should
work that does not type check. At least we don't crash though.
2018-01-05 20:33:12 -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
David Zarzycki
62795501d9 [AST] NFC: De-boilerplate "has ParenType sugar" 2017-12-29 11:37:20 -05:00
David Zarzycki
41a0553228 [AST] NFC: De-boilerplate TupleType "is paren like" checks
Also, make callers be explicit when they're ignoring the field name.
2017-12-29 08:32:28 -05:00
Slava Pestov
10ca955806 Sema: Remove TypeChecker::forceExternalDeclMembers()
It doesn't appear to do anything, or at least it was being
called in places that make no sense.
2017-12-12 14:01:54 -08:00
Slava Pestov
21168dabad Sema: Simplify a bit in performMemberLookup() 2017-12-12 14:01:54 -08:00
Pavel Yaskevich
6ca3d3fc72 Merge pull request #13138 from xedin/rdar-35702810
[CSSolver/SILGen] Fix solver to support function conversion with collection subtyping
2017-12-04 15:05:37 -08:00
Mark Lacey
6cebc924af [ConstraintSystem] Only record fixes that result in a correct solution.
It's probably harmless to record the ones for failures which later cause
us to back out in the solver, but it means we cannot easily set
breakpoints in places like recordFix and stop only in the places where
the fixes actually cause the solution to make progress.
2017-12-01 17:27:12 -08:00
Mark Lacey
4f7f5d7c09 [ConstraintSystem] Simplify some logic around attempting fixes for Optionals.
Only record the fixes when they have made the solution to this point
successful.
2017-12-01 17:15:37 -08:00
Mark Lacey
fda51bb6b2 [ConstraintSystem] Eliminate the notion of FixKind::None
These purportedly mark that we should stop attempting fixes for a given
constraint, but in fact the only code creating these is clearly
unreachable so these serve no purpose.
2017-12-01 16:18:16 -08:00
Pavel Yaskevich
515520e524 [CSSolver/SILGen] Fix solver to support function conversion with collection subtyping
Fix collection subtyping relation in function argument position
by emiting special re-abstraction thunk with collection upcast.

Resolves: rdar://problem/35702810
2017-11-30 16:47:18 -08: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
Doug Gregor
e3a5318b97 [Type checker] Teach conformsToProtocol() to check conditional requirements.
conformsToProtocol() is the main way in which we check whether a given type
conforms to a given protocol. Extend it to check conditional requirements by
default, so that an unmodified caller will get the "does not conform" result
(with diagnostics when a location is present) rather than simply ignoring
the conditional requirements.

Some callers take responsibility for conditional requirements, e.g., to
push them into the constraint system. Allow those callers to opt out of
this checking, and do so wherever appropriate.

Fixes rdar://problem/35518088, where we were ignoring the conditional
requirements needed to verify that Equatable synthesis could be performed.
2017-11-21 21:02:00 -08:00
Doug Gregor
33810d9a6a [Constraint solver] Add a constraint locator element for conditional requirements.
Using this constraint locator element, we can check when a failed
constraint is due to an unsatisfied conditional requirement of a
protocol conformance. Unfortunately, it's hard to turn this into
an actionable diagnostic right now.
2017-11-20 21:07:13 -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
d58a596be1 [Constraint solver] Remove a now-unnecessary hack for conditional conformances.
We added this hack to work around the use of context types within normal
protocol conformances, which created tautological constraint systems.
With the switch to interface types in normal protocol conformances, this
hack is no longer necessary.
2017-11-19 22:07:18 -08:00
Joe Groff
4b401e257a Merge pull request #12835 from jckarter/key-path-type-checking
Sema: Fixes for KeyPath type checking.
2017-11-14 13:10:44 -08:00
Huon Wilson
1339685c5f [CS] Fix conditional conformances in overload ranking.
Resolves rdar://problem/35480952.
2017-11-13 13:14:39 -08:00
Joe Groff
73d2e9a47f Sema: Fixes for KeyPath type checking.
When testing KeyPathApplication constraints, we would keep going after rejecting a concrete KeyPath application by trying PartialKeyPath and AnyKeyPath, even though that's not what we want, since any key path application expression can type check with an AnyKeyPath. We would then miscompile by building the AST such that we applied the mismatched key path expression directly to the base. We also didn't handle expressions where the base was a subtype of the key path's base type correctly—the conversion means the base can't be written through in this situation, and we hardcoded the concrete-to-existential case instead of handling general conversions. Fix these problems, and add an AST verifier for KeyPathApplicationExprs to help catch problems in the future. Fixes SR-6300 | rdar://problem/35368903.
2017-11-09 11:57:23 -08:00
Pavel Yaskevich
66e13bcc85 [ConstraintSolver] Avoid unnecessarily increasing score when matching function types
Remove function-to-function type match score increase, which should only
happen contextually in presence of other restrictions, this used to fix
the case related to matching of arrays of functions with and w/e `throws`
as function parameters which used to be ambigious, and now handled by
collection-upcast conversion score.

Resolves: rdar://problem/35142121
2017-10-31 23:14:35 -07:00
Robert Widmann
ebdd126a2c [NFC] Remove more uses of <InOutType> 2017-10-22 02:00:07 -04: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
96172048d3 [Sema/CS] Convenience function for adding a Requirement as a constraint. 2017-10-10 20:17:40 -07:00
Pavel Yaskevich
f95c979cbf [ConstraintSolver] Remove hack related to single parameter handling 2017-10-03 15:15:57 -07:00
Pavel Yaskevich
4317074a9a [ConstraintSolver] Prioritize certain type variables while looking for bindings
Presence of some constraints (Subtype at least) requires a certain
contextual ranking of the type variables associated with them when
it comes to picking bindings, otherwise it might lead to no or
invalid solutions, because only a set of the bindings for the best
type variable is attempted.

Resolves: rdar://problem/22898292
2017-10-03 10:41:35 -07:00
Pavel Yaskevich
695baccc3e Revert "[ConstraintSolver] Prioritize certain type variables while looking for bindings"
This reverts commit 001b09747a.
2017-10-02 17:30:18 -07:00
Pavel Yaskevich
001b09747a [ConstraintSolver] Prioritize certain type variables while looking for bindings
Presence of some constraints (Subtype at least) requires a certain
contextual ranking of the type variables associated with them when
it comes to picking bindings, otherwise it might lead to no or
invalid solutions, because only a set of the bindings for the best
type variable is attempted.

Resolves: rdar://problem/22898292
2017-09-30 14:36:16 -07:00
Pavel Yaskevich
3b06f2e897 Merge pull request #12072 from xedin/rdar-33429010
[ConstraintGraph] Don't try to contract edge of parameter bindings with `inout` attribute
2017-09-26 11:51:09 -07:00
Pavel Yaskevich
80e4a2226b [ConstraintGraph] Don't try to contract edge of parameter bindings with inout attribute
Currently edge related to the parameter bindings is contracted
without properly checking if newly created equivalence class has
the same inout & l-value requirements. This patch improves the
situation by disallowing contraction of the edges related to parameter
binding constraint where left-hand side has `inout` attribute set.

Such guarantees that parameter can get `inout` type assigned when
argument gets `l-value` type.

Resolves: rdar://problem/33429010
2017-09-22 17:23:12 -07:00