Commit Graph

264 Commits

Author SHA1 Message Date
Pavel Yaskevich
f95d9b092e [TypeChecker] Add new type of overload choice to support keypath dynamic lookup 2019-04-01 12:40:39 -07:00
Pavel Yaskevich
be99082598 [Constraint] Disable "fixed" overload choice by default 2019-03-14 13:18:42 -07:00
Pavel Yaskevich
179a04446c [Constraint] Add an ability to attach a fix to BindOverload constraint 2019-03-12 15:11:32 -07:00
Slava Pestov
1026968616 Sema: Remove ConversionRestrictionKind::TupleToTuple 2019-01-10 08:44:18 -05:00
Slava Pestov
28343c9480 Sema: Remove ConversionRestrictionKind::LValueToRValue 2019-01-10 08:44:18 -05:00
Dan Zheng
2a4e1b83fd Implement @dynamicCallable. (#20305)
* Implement dynamically callable types (`@dynamicCallable`).

- Implement dynamically callable types as proposed in SE-0216.
  - Dynamic calls are resolved based on call-site syntax.
  - Use the `withArguments:` method if it's defined and there are no
    keyword arguments.
  - Otherwise, use the `withKeywordArguments:` method.
- Support multiple `dynamicallyCall` methods.
  - This enables two scenarios:
    - Overloaded `dynamicallyCall` methods on a single
      `@dynamicCallable` type.
    - Multiple `dynamicallyCall` methods from a `@dynamicCallable`
      superclass or from `@dynamicCallable` protocols.
  - Add `DynamicCallableApplicableFunction` constraint. This, used with
    an overload set, is necessary to support multiple `dynamicallyCall`
    methods.
2018-11-09 09:49:14 -08:00
Pavel Yaskevich
3be5c203e3 [ConstraintSystem] NFC: Move isExplicitConversion to Constraint
Since this logic is tightly coupled to constraint, it makes sense
to move just there, also it's easier to re-use it elsewhere since
it doesn't have to be `private` anymore.
2018-08-29 00:01:36 -07:00
Slava Pestov
2975f145a1 Sema: Remove ArgumentTupleConversion constraint now that its no longer used 2018-08-28 14:40:56 -07:00
Slava Pestov
65edce7591 Sema: Add FunctionInput and FunctionResult constraints
These are temporary.

FunctionInput is conditional on fixing some ranking behavior to not
depend on type variables having argument tuples bound to them.
Hopefully we can replace TVO_PreferSubtypeBinding with a better
mechanism that compares overload types instead.

FunctionResult is used in CSDiag's contextual type diagnostics.
This is also on the chopping block.
2018-08-28 14:37:57 -07:00
Pavel Yaskevich
fa45b3b675 [Diagnostics] NFC: ConstraintFix::{print, dump} no longer need SourceManager passed-in
Since `ConstraintFix` references `ConstraintSystem` directly now,
we can get `SourceManager` from `ASTContext` associated with that
`ConstraintSystem` instead of passing it in every time.
2018-08-22 00:15:24 -07:00
Pavel Yaskevich
e81ce930ca [CSSolver] Refactor selectBestBindingDisjunction
Add an assert which verifies that all of the disjunction
choices has the same type variable on the left-hand side.

Based on that fact, refactor `selectBestBindingDisjunction`
to only check first choice to find suitable "bind" disjunctions
to favor.
2018-08-17 17:46:58 -07:00
Pavel Yaskevich
dd9c28b456 [ConstraintSystem] Add proper printing (name + locator) for fixes 2018-08-13 18:10:50 -07:00
Pavel Yaskevich
ebb9f86bee [ConstraintSystem] NFC: Remove obsolete Fix struct
Also clean up `ConstraintSystem` interface from the storage
supporing `Fix` such as missing types, decl names, conformances etc.
2018-08-13 18:10:40 -07:00
Pavel Yaskevich
e631a37ef6 [ConstraintSystem] Replace Fix with ConstraintFix throughout solver 2018-08-13 18:10:27 -07:00
Slava Pestov
8d4b8e31aa Sema: Remove ScalarToTuple conversion
This either became dead shortly after the removal of Swift 3
compatibility mode from the constraint solver, or even earlier.

Note that the code completion test change is actually correct
because (Any) -> () is not convertible to () -> () in the
language.
2018-08-08 10:18:52 -07:00
Mark Lacey
6ccc2acb06 [ConstraintSystem] Remove disjunction number as we're not currently
using it.

It was added to use for selecting the order we visit disjunctions, but
this order turned out to not be great (and I don't think there are
simple variations on the creation order, like reverse order), that
will work well either.
2018-08-06 16:00:26 -07:00
Pavel Yaskevich
9d50122e75 [ConstraintSystem] NFC: Fix typo in FixKind::AddConformance name 2018-08-03 14:33:19 -07:00
Pavel Yaskevich
29e34e66e7 [ConstraintSystem] Diagnose missing conformance requirements via "fixes"
If fixes are allowed let solver record missing protocol conformance
requirements and assume that `conformsTo` constraint is successfully
solved, this helps to diagnose such errors without involving
heavy-weight expression based diagnostics.

Resolves: rdar://problem/40537858
2018-08-02 16:40:34 -07:00
gregomni
5faa8bf4d1 Don't offer force-unwrap of the base as a possible fixit for optional
member access if optional chaining is sure to be valid.
2018-07-27 13:56:24 -07:00
Pavel Yaskevich
62eccd53ad [ConstraintSystem] Use fixes to diagnose missing argument labels
Let the solver disregard missing argument labels and record correct
ones, so such problem could be diagnosed later on iff there were no
other more serious failures.
2018-07-24 22:11:56 -07:00
Mark Lacey
6fa403dc7d [ConstraintSystem] Change the order in which we attempt disjunctions
to be stable.

We currently will stop visiting the elements of a disjunction under
certain circumstances once we have found a solution. The result we get
is inherently dependent on the order in which we determine to visit
the disjunctions themselves (in addition to the elements of the
disjunction).

This change makes the order in which we visit disjunctions
stable. Future commits will create a stable ordering for the elements
of disjunctions. Once we also have that stable ordering in place we
can in theory short circuit more often as part of changing the way in
which we decide what the "best" solution is to a system.

This results in an expression in
validation-test/stdlib/AnyHashable.swift.gyb no longer being able to
typecheck in a reasonable amount of time, so I had to tweak that
expression.
2018-07-16 09:32:14 -07:00
Doug Gregor
e7eac0af22 [Type checker] Extend the diagnostics for unwrapping the base of a member access.
Introduce a new fix kind into the constraint solver to cover unwrapping the base
of a member access so we can refer to the a member of the unwrapped base.
Wire this fix kind to the just-added diagnostic that suggests either the
chaining ‘?’ or the force-unwrap ‘!’ via separate, descriptive Fix-Its.

Example:

error: value of optional type 'X?' must be unwrapped to refer to member 'f' of wrapped base type 'X'
  let _: Int = x.f()
               ^
note: chain the optional using '?' to access member 'f' only for non-'nil' base values
  let _: Int = x.f()
               ^
                ?
note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
  let _: Int = x.f()
               ^
                !

Before this, we would sometimes get a Fix-It for just ‘?’ and sometimes get a Fix-It for the
coalescing ‘??’, neither of which is likely to be right.

More work on rdar://problem/42081852.
2018-07-13 16:26:03 -07:00
Lily Vulcano
1377ab7d7e Turn on ‘as’ bridging on Darwin. 2018-05-30 15:07:22 -07:00
Ben Langmuir
628b6a1fc7 Revert "Turn on ‘as’ bridging on Linux." 2018-05-17 14:54:35 -07:00
Lily Vulcano
b9455930ee Turn on ‘as’ bridging on Darwin. 2018-05-17 09:59:39 -07:00
Huon Wilson
285c2a469b [CS] Add decl-ref outer alternatives as non-favored options.
Given something like `max(1, 2)` inside a type that conforms to Sequence, this
allows the compiler to consider Swift.max as well as the two methods with that
name on Sequence.
2018-04-20 00:55:11 +10:00
Pavel Yaskevich
79b14c8916 [Sema] Improve diagnostics for non-escaping function types
Allow certain bindings and conversions involving non-escaping
function types to succeed in "diagnostic" mode to gather fixes
and diagnose such problems better, expecially related to
conversions to 'Any' and generic parameters.

Resolves: rdar://problem/38648760
2018-03-20 17:57:03 -07:00
Mark Lacey
2b1ad9c4fe Remove the ForceUnchecked ConversionRestrictionKind.
More post-IUO-rework cleanup.
2018-03-15 01:11:00 -07: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
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
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
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
Slava Pestov
a6dfcd52cf Sema: Remove OverloadChoiceKind::TypeDecl, which was never used 2017-05-23 02:10:02 -07:00
Joe Groff
517c45aa3d Followups from merging master 2017-04-19 20:58:52 -07:00
Joe Groff
595e0e4ede Merge branch 'master' into keypaths 2017-04-19 18:38:24 -07:00
Slava Pestov
db58e02cb2 Sema: Hook up layout constraints to the solver
There were various problems with layout constraints either
being ignored or handled incorrectly. Now that I've exercised
this support with an upcoming patch, there are some fixes
here.

Also, introduce a new ExistentialLayout::getLayoutConstriant()
which returns a value for existentials which are class-constrained
but don't have a superclass or any class-constrained protocols;
an example would be AnyObject, or AnyObject & P for some
non-class protocol P.

NFC for now, since these layout-constrained existentials cannot
be constructed yet.
2017-04-13 21:17:05 -07:00
Slava Pestov
2e5b3b6dfc Sema: Update constraint solver for subclass existentials 2017-04-10 17:05:45 -07:00
Joe Groff
7eebb27153 Sema: Infer the specific type of a key path literal from the mutability of the resolved components. 2017-04-10 16:06:40 -07:00
Mark Lacey
8465cf36df [Constraint system] Fix debug output for constraint dumping.
I updated this incorrectly to test the disabled bit on the disjunction
rather than the members.
2017-04-09 16:48:02 -07:00
Joe Groff
964dc0e174 Sema: (wip) Overload resolution for keypath subscripts.
TODO: Some diagnostic regressions:
test-macosx-x86_64/Compatibility/tuple_arguments.swift
test-macosx-x86_64/Constraints/diagnostics.swift
test-macosx-x86_64/Constraints/tuple_arguments.swift
test-macosx-x86_64/expr/unary/keypath/keypath.swift
test-macosx-x86_64/expr/unary/selector/selector.swift
2017-04-09 16:38:02 -07:00
Mark Lacey
0cd57bd481 [Constraint solver] Constraint propagation, disabled by default.
This is an initial implementation of the constraint propagation pass,
disabled by default at the moment as it does not currently pass all
tests.

I have other changes that I'll hold off on for now because they are
incomplete. Without those changes, this pass doesn't really achieve
much, so this is really just a checkpoint on some progress and not
something really worth trying out at this point.
2017-04-03 19:05:27 -07:00
swift-ci
2ba4f03d8a Merge pull request #8023 from huonw/tweak 2017-03-10 17:06:39 -08:00
Huon Wilson
a9299802bb Factor out first = true; ... if (first) first = false; else ....
There's an `interleave` function for this: let's use it.
2017-03-10 15:44:12 -08:00
Joe Groff
eb40d4303b Sema: Add a stdlib-internal _openExistential helper.
Leverage the "special type-checking semantics" hack to pass an opened existential down to an arbitrary subexpression. Please don't use this.
2017-03-10 13:41:00 -08:00
Joe Groff
f662ed86b3 Sema: Assert that bridging constraints aren't formed when ObjC interop is disabled. 2017-03-03 13:18:08 -08:00
Hugh Bellamy
f001b7562b Use relatively new LLVM_FALLLTHROUGH instead of our own SWIFT_FALLTHROUGH 2017-02-12 10:47:03 +07:00
John McCall
39b65f49a0 Track the actual DC of a member access in the constraint system.
Without this, CSGen/CSSimplify and CSApply may have differing
opinions about whether e.g. a let property is settable, which
can lead to invalid ASTs.

Arguably, a better fix would be to remove the dependency on the
exact nested DC.  For example, we could treat lets as settable
in all contexts and then just complain later about invalid
attempts to set them.  Or we could change CSApply to directly
use the information it already has about how an l-value is used,
rather than trying to figure out whether it *might* be getting set.
But somehow, tracking a new piece of information through the
entire constraint system seems to be the more minimal change.

Fixes rdar://29810997.
2017-01-26 00:14:21 -05:00
Hugh Bellamy
201ad5d594 Prefix COMPILER_IS_MSVC with SWIFT_ 2017-01-16 22:09:53 +00:00
Hugh Bellamy
cf777d04f5 Introduce and use COMPILER_IS_MSVC 2017-01-15 15:17:18 +00:00
Doug Gregor
d79ba781c6 [Constraint solver] Collapse the three optional-to-optional restriction kinds.
All three of these conversion restriction kinds were handled in
exactly the same way, so just treat them as one.
2017-01-12 17:11:13 -08:00