Commit Graph

239 Commits

Author SHA1 Message Date
Mark Lacey
ea0050891a Merge pull request #18951 from hamishknight/concrete-is-better-swift5
[CSRanking] Favour concrete members over protocol requirements in Swift 5 mode
2018-08-29 22:32:08 -07:00
Hamish Knight
bee01ed6f5 Revert "[Revert Me] Disable language version check temporarily"
This reverts commit 37a949075f.
2018-08-29 16:54:35 +01:00
Slava Pestov
61152e6585 Sema: Move getUnlabeledType() to CSRanking and simplify it 2018-08-28 22:36:02 -07:00
Hamish Knight
db2897f114 Address #18952 review comments 2018-08-28 22:05:06 +01:00
Hamish Knight
9268fcd6ae [CSRanking] Swift 4.1 compatibility hack for favouring properties on concrete types over protocols
Changes in shadowing behaviour by #15412 caused a property on a concrete type to no longer shadow a protocol property member, which created unintentional ambiguities in 4.2. This commit ensures we at least keep these cases unambiguous in Swift 5 under Swift 4 compatibility mode. This is intentionally narrow in order to best preserve source compatibility under Swift 4 mode by ensuring we don't introduce any new ambiguities.

Resolves SR-7425, SR-7940 & SR-8343.
2018-08-24 17:09:58 +01:00
Hamish Knight
37a949075f [Revert Me] Disable language version check temporarily 2018-08-24 15:50:28 +01:00
Hamish Knight
1912d289e1 [CSRanking] Exclude concrete-over-protocol rule from dynamic lookup ranking
This rule caused us to lose ambiguities in places where we really want ambiguity for `AnyObject` lookup, so only apply it when not comparing such overloads. This whole situation is a bit of a hack – really we shouldn't be applying any type-based or context-based overload ranking rules to overloads found through `AnyObject` lookup, but unfortunately we don't have syntax to precisely disambiguate overloads.

This commit can be reverted if/when we ever remove `AnyObject` lookup.
2018-08-24 15:50:28 +01:00
Hamish Knight
286f16404b [CSRanking] Favour members on concrete types over protocol members
This commit adds a new rule to `isDeclAsSpecializedAs` in order to favour a member on a concrete type over a protocol member. This rule is effectively an extension of the existing rule that prefers concrete type members over protocol extension members.
2018-08-24 15:50:27 +01:00
Jordan Rose
537954fb93 [AST] Rename several DeclContext methods to be clearer and shorter (#18798)
- getAsDeclOrDeclExtensionContext -> getAsDecl

This is basically the same as a dyn_cast, so it should use a 'getAs'
name like TypeBase does.

- getAsNominalTypeOrNominalTypeExtensionContext -> getSelfNominalTypeDecl
- getAsClassOrClassExtensionContext -> getSelfClassDecl
- getAsEnumOrEnumExtensionContext -> getSelfEnumDecl
- getAsStructOrStructExtensionContext -> getSelfStructDecl
- getAsProtocolOrProtocolExtensionContext -> getSelfProtocolDecl
- getAsTypeOrTypeExtensionContext -> getSelfTypeDecl (private)

These do /not/ return some form of 'this'; instead, they get the
extended types when 'this' is an extension. They started off life with
'is' names, which makes sense, but changed to this at some point.  The
names I went with match up with getSelfInterfaceType and
getSelfTypeInContext, even though strictly speaking they're closer to
what getDeclaredInterfaceType does. But it didn't seem right to claim
that an extension "declares" the ClassDecl here.

- getAsProtocolExtensionContext -> getExtendedProtocolDecl

Like the above, this didn't return the ExtensionDecl; it returned its
extended type.

This entire commit is a mechanical change: find-and-replace, followed
by manual reformatted but no code changes.
2018-08-17 14:05:24 -07:00
Ben Cohen
428c580158 Fix unused variable warnings in release builds. (#18755) 2018-08-16 11:46:45 -06:00
Slava Pestov
31a570d53c AST: Replace TypeBase::getRValueInstanceType() with getMetatypeInstanceType()
That is, don't look through InOutType anymore, and update callers to
call getInOutObjectType() as well (or not, where it was obvious to me
that InOutType could not appear).

This surfaces more remaining uses of getInOutObjectType() directly.
2018-08-13 21:13:10 -07:00
Pavel Yaskevich
b9499d71af Merge pull request #18527 from xedin/rdar-42678836
[CSRanking] Fix solution filtering not to erase everything when set i…
2018-08-07 00:22:23 -07:00
Pavel Yaskevich
dc6f86d9b7 [CSRanking] Fix solution filtering not to erase everything when set is completely ambiguous
Since constraint solver has been improved to diagnose more problems
via "fixes", sometimes applying fixes might lead to producing solutions
which are completely ambiguous when compared to each other, and/or are
incomparable, which leads to `findBestSolutions` erasing all of them
while trying to compute best "partial" solution, which is incorrect.

Resolves: rdar://problem/42678836
2018-08-06 17:25:58 -07:00
Mark Lacey
3c9cb97c86 [ConstraintSystem] Add an option to disable the constraint solver perf hacks.
This is helpful in experimenting with constraint solver changes that
might help us remove some of these unsound options. It's not ever mean
to be enabled, but if we're able to remove the things guarded by the
option we can eventually remove the option.
2018-08-06 11:48:19 -07:00
Slava Pestov
e1da265873 Sema: Remove uses of AbstractFunctionDecl::getParameterLists() 2018-07-19 21:21:17 -07:00
Mark Lacey
c0007f8168 Revert "[ConstraintSystem] Fix the ordering of functions with optional parame…" 2018-07-19 00:21:12 -07:00
Mark Lacey
d271f4b057 [ConstraintSystem] Ensure we prefer f(_: Any) to f(_: Any?) when both work.
...unless the argument is an `Any?`, in which case we prefer `f(_: Any?)`.

This change also results in our selecting f<T>(_: T) over f(_:
Any). Coercing with 'as Any' makes it possible to explicitly select
the Any overload. Previously there was no way to select the generic
overload.
2018-07-18 11:44:04 -07:00
Mark Lacey
186c2f251a [ConstraintSystem] Fix the ordering of functions with optional parameters.
Treat non-optional generic parameters as being more specialized than
optional generic parameters, and penalize any solutions that involve
generic arguments that are themselves Optional.

By doing these things, we can remove the special-cased code for the
two overloads of '??' in the stdlib, instead treating the (T?, T)
overload as better than the (T?, T?) overload except where a user
actually passes an optionally-typed value as the second parameter.

Fixes: rdar://problem/19748710
2018-07-18 11:43:44 -07:00
Mark Lacey
efb173857a Merge branch 'master' into remove-swift-3-in-expr-typechecking 2018-07-11 23:49:44 -07:00
Ding Ye
95b91ee953 Apply git-clang-format. 2018-07-11 15:26:56 +10:00
Ding Ye
4a05369b5a Improve interface of InputMatcher with some renaming and rephrasing;
split test cases into different files for different swift versions.
2018-07-11 15:26:56 +10:00
Ding Ye
9648371339 Extract some logic of matching parameters from isDeclAsSpecializedAs,
and reuse it to match parameters and arguments.
2018-07-11 15:26:56 +10:00
Mark Lacey
28f759c8b0 Remove support for -swift-version 3 from the expression type checker.
This is the obvious stuff. There will probably be more fallout.
2018-07-06 16:10:08 -07:00
Rintaro Ishizaki
c622722aaf [CSRanking] Handle EnumElementDecl in IUO parameter checking 2018-06-27 19:07:18 +09:00
Robert Widmann
24abf3ea09 [NFC] Refactor default argument info
Refactor the interface to return a bit vector. Along the way, fix up
the callers and remove some dead usages of the defaults information
that were copied and pasted around Sema.
2018-05-31 17:28:04 -07:00
Pavel Yaskevich
ac083c5da6 [CSRanking] Switch to use AnyFunctionType::getParams() instead of getInput() 2018-04-26 17:33:08 -07:00
Pavel Yaskevich
57ad592844 [Sema] Switch computeDefaultMap to use AnyFunctionType::getParams() 2018-04-26 17:33:08 -07:00
Doug Gregor
a506f602ea Merge branch 'master' into redeclaration-fixes 2018-03-27 21:59:50 -07:00
Mark Lacey
0959f0ce7d Take the IUO-ness of a parameter declaration into account in ranking.
Normally we wouldn't need this because we do not allow overloading by
parameter optionality. In this case, though, we import two Objective-C
methods that end up getting renamed to the same name in Swift, one of
which has a nullability annotation and the other of which doesn't.

Before IUOs were removed from the type system, we would have failed a
subtype test when ranking overloads, but now that subtype test passes
because we're comparing two optionals of the same type. So the change
here is to compare the optionality of the parameters and reject cases
where we are testing whether a plain optional is a subtype of
something declared to be an IUO.

Fixes rdar://problem/38719575.
2018-03-24 11:07:51 -07:00
Hamish
d9401d64dc [Sema] Revise #15412 in response to feedback
- Reuse existing logic to curry the signature type with the 'self' of the context (in addition we no longer use a MetatypeType for the 'self' of a static member as they don't have conflicting signatures with instance members anyway)

- Limit the fix for SR-7251 to Swift 5 mode

- Add tests for generic subscripts
2018-03-23 01:09:14 +00:00
Pavel Yaskevich
d8b355dbae Revert "[CSSolver] Use correct locator when matching function result types re…" 2018-03-07 23:19:30 -08:00
Pavel Yaskevich
20019be9d3 [CSSolver] Use correct locator when matching function result types related to closures
Currently we always use 'FunctionResult' as a path element when matching
function result types, but closure result type is allowed to be implicitly
converted to Void, which means we need to be careful when to use
'FunctionResult' and 'ClosureResult'.

Resolves: rdar://problem/37790062
2018-03-01 11:41:36 -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
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
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
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
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
Pavel Yaskevich
12980335cd [CSRanking] Fix self types to be unrelated when comparing operator decls
Such declarations should already have self bound as one of the parameters
which would enforce subtype relationship.

Resolves: rdar://problem/36333688
2018-01-05 23:24:24 -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
d1bb36425d IUO: Add overload choice kind for IUO decls.
Add a new OverloadChoiceKind for decls that are either IUO-typed, or
function-typed meaning that the function result type is IUO-typed.

Not currently used, so NFC.
2018-01-03 19:37:06 -08:00
Pavel Yaskevich
0bae11be54 [CSRanking] Fix func declaration ranking with default'ed parameters
If default'ed parameters in one decl are intermixed with non-defaulted
ones, skip claiming parameters in other decl at the same position.

Resolves: rdar://problem/36226874.
2018-01-03 12:11:56 -08:00
Doug Gregor
8342f77bc4 [Overloading] Adjust @autoclosure parameter types to their result types.
For the purposes of partial ordering of declarations (e.g., in
overload resolution), adjust `@autoclosure` parameter types to the
result type of the autoclosure, i.e., the type against which the
argument will be matched. This makes overload resoluton agnostic to
`@autoclosure`.

Fixes SR-6472.
2017-11-27 15:32:23 -08:00
Doug Gregor
4f9cb35f6a [Type checker] Fix ranking of _OptionalNilComparisonType overloads of ==/!=.
Always prefer them to Optionals ==/!=, because they are @transparent and
more efficient.
2017-11-21 22:56:08 -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
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
Slava Pestov
3c07c27f84 Revert "[CSRanking] Delete a pointless old ranking hack. NFC"
This reverts commit 199273bcf7.
2017-11-15 13:41:07 -08:00
Doug Gregor
199273bcf7 [CSRanking] Delete a pointless old ranking hack. NFC 2017-11-14 17:04:48 -08:00
Doug Gregor
91fa3aab44 [CSRanking] Remove dead code.
As noted in the comment, we expected this code to be unnecessary,
which it is. Remove it.
2017-11-13 15:14:58 -08:00
Huon Wilson
1339685c5f [CS] Fix conditional conformances in overload ranking.
Resolves rdar://problem/35480952.
2017-11-13 13:14:39 -08:00