Commit Graph

859 Commits

Author SHA1 Message Date
Pavel Yaskevich
60fb2bf94e [CSFix] Try to fix missing member by defining it based on use
If lookup couldn't find anything matching given name, let's try to
fake its presence based on how member is being used. This is going
to help (potentially) type-check whole expression and diagnose the
problem precisely.
2019-01-09 14:35:27 -08:00
Pavel Yaskevich
35202ab5b0 [TypeChecker] Always emit a fallback error if type-check failed without producing one
Sometimes constraint solver fails without producing any diagnostics,
it could happen during different phases e.g. pre-check, constraint
generation, or even while attempting to apply solution. Such behavior
leads to crashes down the line in AST Verifier or SILGen which are
hard to diagnose.

Let's guard against that by tracking if solver produced any diagnostics
upon its failure and if no errors were or are scheduled to be produced,
let's produce a fallback fatal error pointing at affected expression.

Resolves: rdar://problem/38885760
2019-01-07 10:42:00 -08:00
Pavel Yaskevich
637692ccf8 [ConstraintSystem] NFC: Define {activate, deactivate}Constraint to make that logic reusable 2018-12-18 10:24:11 -08:00
Mark Lacey
10ef7347a7 [ConstraintSystem] Better handling of operators with multiple designated types.
For operators with multiple designated types, we attempt to decide the
best order for exploring the types. We were doing this by checking
for matching types. Extend this to also consider conforming types.

Fixes: rdar://problem/46687985
2018-12-14 13:24:23 -08:00
Mark Lacey
e713cbd516 [ConstraintSystem] With designated types enabled, use fewer partitions.
In 119864a0f5 I moved the consideration
of top-level operators sooner when the designated types feature is
enabled, and that fixed some source compatibility issues that show up
in our test suite, but also introduced a new one.

For maximum source compatibility we'll just split the partitions up
into the operators from designated types, followed by operators that
are not deprecated or disabled, followed by the deprecated ones, and
finally the disabled ones.
2018-12-13 15:18:20 -08:00
Mark Lacey
119864a0f5 [ConstraintSystem] Attempt global operators earlier.
When -Xfrontend -solver-enable-operator-designated-types is enabled,
attempt operators defined outside of types before attempting operators
defined within (non-designated) types.

This helps fix some source compatability problems that arise from
attempting some of the operators defined for Optional (e.g. we
would sometimes typecheck successfully with the operator for Optional
and then inject values into Optionals and skip attempting
globally defined operators that also match).

The old behavior resulted in failures in SourceKit and SIL Optimizer
tests when enabling designated types by default in our test suite.
2018-12-11 20:48:09 -08:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Mark Lacey
4d76d308c9 [ConstraintSystem] Move checkTypeOfBinding into CSBindings.cpp.
This is the only place that it's actually used from.
2018-11-29 21:00:32 -08:00
Mark Lacey
a1be08230f [ConstraintSystem] Enable disjunction ordering change when use of designated types are enabled.
We try to work more-or-less bottom up in the expression when we
attempt apply disjunctions first. This is very helpful to get good
results with designated types enabled in the solver. Rather than
forcing tests to specify both `-swift-version 5` (which also enables
this bottom-up behavior) and enabling designated types, allow them to
just enable the later to get the behavior.

We'll eventually have to revisit this when we decide the specific
conditions that we'll enable the use of designated types under.
2018-11-16 22:01:04 -08:00
Mark Lacey
d7cf830842 [ConstraintSystem] Add Array as a designated type for + and +=.
Also add overloads for these operators to an extension of Array.

This allows us to typecheck array concatenation quickly with
designated type support enabled and the remaining type checker hacks
disabled.
2018-11-15 21:42:33 -08: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
Slava Pestov
564695d9bf AST: Remove DeclContext::isExtensionContext()
It's identical in every way to isa<ExtensionDecl>(), so
make code less confusing by only having one way of doing
that check.
2018-11-07 17:34:56 -05:00
Mark Lacey
ffcbe70e90 [ConstraintSystem] Fix crash due to not all literal protocols having default types. 2018-11-07 13:04:59 -08:00
Jordan Rose
dc3ff6c22b Merge pull request #19985 from vinivendra/ast-dump-cleanup
Cleans up calls to print/dump for the AST Dumper
2018-10-24 15:22:41 -07:00
Greg Titus
d20fdf5f82 Merge pull request #19920 from gregomni/8757
[Sema][QoI] Call out missing conformances in protocol witness candidates.
2018-10-22 16:39:51 -07:00
Vinicius Vendramini
b61df445ae Cleans up calls to print/dump for the AST Dumper
The `Stmt` and `Expr` classes had both `dump` and `print` methods that behaved similarly, making it unclear what each method was for. Following a conversation in https://forums.swift.org/t/unifying-printing-logic-in-astdumper/15995/6 the `dump` methods will be used to print the S-Expression-like ASTs, and the `print` methods will be used to print the more textual ASTPrinter-based representations. The `Stmt` and `Expr` classes seem to be where this distinction was more ambiguous. These changes should fix that ambiguity.

A few other classes also have `print` methods used to print straightforward representations that are neither the S-Expressions nor ASTPrinters. These were left as they are, as they don't cause the same ambiguity.

It should be noted that the ASTPrinter implementations themselves haven't yet been finished and aren't a part of these changes.
2018-10-22 16:04:02 -03:00
Mark Lacey
5b4a332b0e [ConstraintSystem] Sort the designated types based on actual argument types.
In cases where we have multiple designated types, sort the types that
were designated for this operator based on any information we can
gather about actual argument types at usage sites.

We can consider extending this further in a future commit to ignore
designated types when we have concrete type information that we are
confident of.
2018-10-21 13:14:27 -07:00
Mark Lacey
5cfa61cbf8 [ConstraintSystem] Address review feedback from @xedin. 2018-10-19 19:02:07 -07:00
Mark Lacey
f6710a5216 [ConstraintSystem] Further refactoring of disjunction partitioning.
Unnest code by splitting it out into lambdas.
2018-10-19 16:46:05 -07:00
Mark Lacey
fc47edc89b [ConstraintSystem] Hoist code for creating partitions for designated types.
Move this into a separate function.
2018-10-19 15:36:11 -07:00
Mark Lacey
f66a8527fd [ConstraintSystem] Minor refactor to disjunction partitioning.
Change the order of statements but nothing functional.
2018-10-19 12:57:10 -07:00
Mark Lacey
b892e6bc6b Merge pull request #19930 from rudkx/extend-operator-designated-type
[ConstraintSystem] Add a new stat to be used for expression type chec…
2018-10-18 18:53:14 -07:00
Mark Lacey
cc0386b461 [ConstraintSystem] Add a new stat to be used for expression type checker performance testing.
This counts the number of leaf scopes we reach while solving the
constraint sytem, and is a much better measure of the growth of
unnecessary work than the total number of scopes opened.

There were two tests where I had a difficult time getting scale-test
to fit the curve even after adjusting some of the parameters, so I've
left those to use the old stat for now.
2018-10-17 07:26:18 -07:00
Pavel Yaskevich
3e68e96ebe [CSSolver] NFC: Replace erase with splice while returning constraints to active list 2018-10-16 17:06:48 -07:00
gregomni
f2a80c4a9d Let CS::solveSingle() optionally allow fixes in the solution. Use this in protocol inference to give better candidate failure notes. 2018-10-16 15:27:09 -07:00
Mark Lacey
3d6c93f3bd Merge pull request #19909 from rudkx/extend-operator-designated-type
[ConstraintSystem] Extend solver support for designated types for ope…
2018-10-16 10:42:17 -07:00
Mark Lacey
b158651119 [ConstraintSystem] Extend solver support for designated types for operators.
Have the constraint solver consider multiple designated types for an
operator. We currently consider the overloads from each in turn,
stopping as soon as we have a solution. As a result, we can still end
up with exponential type checking in some cases if an operator has
more than a single designated type. This still allows us to reduce the
base of that exponent, though, which makes it possible to increase the
number of expressions we can type check successfully in practice.
2018-10-15 23:51:43 -07:00
Pavel Yaskevich
59899a7911 [ConstraintSystem] Make sure that system is returned into its original state after solving
Currently (with or w/o failures) constraint system is not returned
back to its original state after solving, because constraints from
initial "active" list are not returned to the system. To fix that
let's allocate "initial" scope which captures state right before
solving begins, and add "active" list to the solver state to capture
information about "active" constraints at the time of its creation.

This is follow-up to https://github.com/apple/swift/pull/19873
2018-10-15 16:33:57 -07:00
Mark Lacey
703341239b Add support for multiple designated types for an operator declaration.
Add parsing, type checking, serialization, and deserialization support
for specifying multiple types as "designated" for operator lookup for
a given operator declaration.

The constraint solver still considers only the first type when
deciding the order to attempt the elements of a disjunction, so this
doesn't really change behavior yet.
2018-10-09 23:54:01 -07:00
Mark Lacey
36284ba377 Extend operator decls to allow any designated nominal type for lookup.
Rather than limiting this to protocols, allow any nominal type.

Rename -enable-operator-designated-protocols to
-enable-operator-designated-types to reflect the change.
2018-10-06 17:02:31 -07:00
Mark Lacey
b08060b4f1 [ConstraintSystem] Fix mistake involving comparison to extension.
We need to compare the Decl for the nominal we're extending, not the
extension decl itself.
2018-10-06 01:27:40 -07:00
Mark Lacey
985bcba9c3 [ConstraintSystem] Address review feedback. 2018-10-03 23:27:19 -07:00
Mark Lacey
1f517d328d [ConstraintSystem] Distinguish between overloads from a type vs. an extension.
For operators with default implementations in an extension, we don't
want to typecheck it both with overloads from the protocol type and
the ones from the extension.
2018-10-03 18:51:43 -07:00
Mark Lacey
0955aa4677 [ConstraintSystem] Replace argument walk with a more robust utility.
For now this is only used for the same purpose as the previous type
constraint walking utility function.

Eventually we can incorporate this into our selection of overload
choices.
2018-10-03 18:08:00 -07:00
Mark Lacey
aad2295d9c [ConstraintSystem] Only look for overloads in the designated type if
one was designated.
2018-10-03 17:19:59 -07:00
Mark Lacey
86a6dc0e0e Fix small formatting problem. 2018-10-03 17:04:37 -07:00
Mark Lacey
93b553cf4d [ConstraintSystem] Use operator designated protocols to speed type checking.
Use the type designated in the operator declaration as a mechanism to
limit the exploration we do in constraint solving. If we can find a
solution with an overload defined within that type or an extension of
that type, we do not look at any of the other options in the
disjunction.

This is disabled by default, and enabled with
> `-swift-version 5 -Xfrontend -solver-enable-operator-designated-protocols`
2018-10-03 14:41:59 -07:00
Mark Lacey
9cc817db1b Merge branch 'master' into fix-ordering 2018-09-26 17:43:35 -07:00
Mark Lacey
61b96d039c Merge pull request #19469 from rudkx/disjunction-partitioning
[ConstraintSystem] Add the notion of disjunction partitioning.
2018-09-26 17:26:42 -07:00
Mark Lacey
1d227731c1 [ConstraintSystem] Only use the new disjunction ordering under -swift-version 5.
This will minimize the chance of breaking code. Currently SwiftLint
has one "too complex" expression with this change. Further changes to
the solver may improve that situation, and potentially allow us to
move this out from -swift-version 5 if we're willing to take the risk
of breaking some code as a result.
2018-09-26 14:55:07 -07:00
Mark Lacey
7e0c7040c0 [ConstraintSystem] Use shouldStopAt to limit disjunction choices.
Rather than using shouldStopAfter, use shouldStopAt, and check if
we're at the beginning of a disjunction (with an already successful
solution).
2018-09-26 13:42:40 -07:00
Pavel Yaskevich
bd527c114e [CSBindings] Don't consider dependent member types even if they are wrapped in optionals
Because binding producer is going to attempt to unwrap optionals
and try the type, which would lead to infinite recursion because
dependent member types aren't bindable.

Resolves: rdar://problem/44770297
2018-09-25 20:59:58 -07:00
Mark Lacey
d7cf84ab51 [ConstraintSystem] Use Param::getPlainType() rather than Param::getType(). 2018-09-20 15:26:13 -07:00
Mark Lacey
732b98fb05 [ConstraintSystem] Fold some logic into the gatherConstraints predicate. 2018-09-20 15:25:20 -07:00
Mark Lacey
dd6f57759b [ConstraintSystem] Use llvm::all_of. 2018-09-20 15:25:01 -07:00
Mark Lacey
948ee3c9bf [ConstraintSystem] Remove lambda that returns true since this argument
is defualted in the same way.
2018-09-20 14:24:34 -07:00
Mark Lacey
c6c66f6873 [ConstraintSystem] Change the order in which we visit disjunctions.
Attempt to visit disjunctions that are associated with applies where
we have at least some useful information about the types of all of the
arguments before visiting other disjunctions.

Two tests here got faster, and one slightly slower. One of the
faster tests is actually moving from test/ to the slow/ directory in
validation-test because despite going from 16s to less than 1s, it was
still borderline for what we consider the slow threshold, so I made
the test more complex. The one that got a little slower is
rdar22022980, which I also made more complex so that it is clearly
"slow" by the way we are testing it.

slower:
  rdar22022980.swift

faster:
  rdar33688063.swift
  expression_too_complex_4.swift
2018-09-20 13:15:59 -07:00
Pavel Yaskevich
ed9c219492 [CSStep] Add state transition verification
Instead of asserting in the solver loop, let's move all of that
logic into `SolverStep::transitionTo(StepState)` and verify state
transition validity there.
2018-09-19 16:08:01 -07:00
Pavel Yaskevich
8daaf905d6 [CSStep] Switch to use std::unique_ptr for work list
Instead of manually managing lifetime of the steps, let's just
use `std::unique_ptr` instead.
2018-09-19 11:19:15 -07:00
Pavel Yaskevich
82d07ad88d [ConstraintSystem] NFC: TypeBinding abstraction is no longer necessary 2018-09-17 15:28:37 -07:00