Commit Graph

315 Commits

Author SHA1 Message Date
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
Doug Gregor
cd3c63cbfd [AST] Stop including GenericSignature.h in other headers.
Except GenericEnvironment.h, because you can't meaningfully use a
GenericEnvironment without its signature. Lots less depends on
GenericSignature.h now. NFC
2017-10-12 14:23:46 -07:00
Huon Wilson
5ede600a6b [Sema/CS] Thread conditional requirements through specialization checking. 2017-10-10 20:17:41 -07:00
Robert Widmann
9bd12bc20c [NFC] Remove Existential Ranking Hack
This hack was originally intended to distinguish between functions
of single arguments that were overloaded on AnyObject and AnyObject?.
Nowadays, CSRanking is capable of handling that itself, and the check
this function was using to detect AnyObject no longer works.
2017-09-21 01:15:58 -04:00
Slava Pestov
d35cacd45a Sema: Remove witness/requirement ranking
This doesn't appear to be necessary when name lookup
does the right thing.

We don't *want* Sema to consider overload sets
containing both a protocol requirement and a witness,
because this could lead to exponential behavior.
2017-07-28 01:19:24 -07:00
Pavel Yaskevich
96c4bcd411 [ConstraintSolver] Prefer same name class properties found on subclass over superclass
If class property with the same name has been found on both subclass and superclass,
let's always prefer subclass with all else equal, because subclass property
could only be found if requested directly.

Resolves: rdar://problem/32973206
2017-07-24 15:38:58 -07:00
Mark Lacey
84209778bd [CS] Dump the number of viable solutions with -debug-constraints. 2017-07-19 15:20:59 -07:00
Pavel Yaskevich
6231f85be9 [ConstraintSolver] Use a flag to prevent solver from removing possible solutions
In some situations e.g. while trying to shrink domains of the type
variables before attempting search, use a flag to tell constraint
system to retain all of the viable solutions otherwise solver could
loose some of the information required to produce complete solution.

Resolves: rdar://problem/32726044
2017-06-22 00:11:32 -07:00
Robert Widmann
9fa8341540 Only decompose function types for info about default arguments
AnyFunctionType::Param carries around information about decomposed
parameters now.  Information about default arguments must be computed
separately with swift::computeDefaultMap.
2017-06-19 23:05:44 -07:00
Pavel Yaskevich
3559047340 [ConstraintSolver] Unify value-to-pointer conversion scoring
Instead of having three different scores for - string, array, inout,
let's unify them under a single value-to-pointer score and use
it in appropriate places when simplifying restricted constraints.
2017-06-02 23:42:32 -07:00
Pavel Yaskevich
5998cd645f [ConstraintSolver] Penalize conversions from String to UnsafePointer
There are possible situations when we find solutions with String
and String -> UnsafePointer conversions at the same time for
expressions with default string literals. In order to disambiguite
such situations let's prefer solutions without String -> UnsafePointer
conversions if possible.
2017-06-01 14:43:43 -07:00
Jordan Rose
4468ad2028 [Sema] Preparations for removal of getName on ValueDecl (#9972)
With the introduction of special decl names, `Identifier getName()` on
`ValueDecl` will be removed and pushed down to nominal declarations
whose name is guaranteed not to be special. Prepare for this by calling
to `DeclBaseName getBaseName()` instead where appropriate.
2017-05-28 22:36:39 -07:00
swift-ci
97aa981887 Merge pull request #9889 from rudkx/isdeclasspecialized-output 2017-05-23 18:31:44 -07:00
Mark Lacey
7d06ae0d68 [Constraint Solver] Add some debugging output to isDeclAsSpecializedAs. 2017-05-23 18:00:12 -07:00
Slava Pestov
fe41900873 Sema: Split off ConstraintSystem::openUnboundGenericType() from openType()
The openType() function did two things:

- Replace unbound generic types like 'G' with fresh type variables
  applied to bound generic types, like 'G<$T0>'.

- Replace generic parameters with type variables from the replacement
  map.

The two behaviors were mutually exclusive and never used from the
same call site, so split them up into two independent functions.

Also, eliminate ConstraintSystem::openBindingType() since it was only
used in one place and could be expressed in terms of existing functions.
2017-05-23 02:10:03 -07:00
Slava Pestov
a6dfcd52cf Sema: Remove OverloadChoiceKind::TypeDecl, which was never used 2017-05-23 02:10:02 -07:00
Robert Widmann
872bb002ea Merge pull request #9829 from CodaFi/argumentative-discourse
[Gardening][NFC] Use `hasAssociatedValues` where possible
2017-05-22 12:16:20 -06:00
Robert Widmann
3b202c18d8 Use 'hasAssociatedValues'
Use 'hasAssociatedValues' instead of computing and discarding the
interface type of an enum element decl.  This change has specifically not
been made in conditions that use the presence or absence of the
interface type, only conditions that depend on the presence or absence
of associated values in the enum element decl.
2017-05-22 09:54:47 -07:00
Slava Pestov
ca6e4e6138 Sema: Clean up openGeneric() for nested generic types
If we had an unbound generic type whose parent was a bound
generic type, and the outer type's generic signature
placed generic constraints on outer parameters, we would
create type variables that weren't ever bound to anything.

Previously, this would never come up, but it will once
preCheckExpression() is folding more MemberRefExprs down
to TypeExprs.
2017-05-21 18:11:52 -07:00
Jordan Rose
27b4070f5f When ranking overloads, always match up final closure parameters.
(in Swift 4 mode)

There are a lot of other things that overload ranking does *not* take
into account, and I intend to file a more general bug about that, but
this should resolve some of the most egregious ambiguities with Swift
3's import rules (SE-0005, particularly "omit needless words" and
adding default arguments).

Finishes rdar://problem/25607552.
2017-04-26 19:33:07 -07:00
Joe Groff
3d178be169 Merge pull request #8875 from jckarter/keypaths
Keypaths
2017-04-21 17:51:17 -07:00
Slava Pestov
f4b91cd118 AST: Remove unused 'resolver' argument from TypeBase::getSuperclass() 2017-04-20 00:37:38 -07:00
Joe Groff
595e0e4ede Merge branch 'master' into keypaths 2017-04-19 18:38:24 -07:00
Doug Gregor
8584e97408 [AST] Sink ProtocolConformance::getWitness() down into NormalProtocolConformance.
NormalProtocolConformance has the only correct implementation of this
functionality. Instead, providing a safer getWitnessDecl() that
doesn't promise substitutions that are incorrect (and not actually
used by any clients).
2017-04-18 23:35:24 -07:00
Slava Pestov
819dfd5211 Sema: Better type safety for opened types map
Now that we no longer map dependent member types to fresh type
variables, the keys in the replacement map can just be
GenericTypeParamTypes.
2017-04-10 17:04:36 -07:00