Commit Graph

1513 Commits

Author SHA1 Message Date
Brent Royal-Gordon
760d4be5e0 [ConstraintSystem] Add a KeyPathComponentResult locator path element
We’ll use this instead of the function result, except for subscript components, which are already structured similarly to subscript expressions.
2019-04-17 21:15:16 -07:00
Brent Royal-Gordon
44a472766d [ConstraintSystem] Add KeyPathComponentTypes map
Avoids digging through the long list of type vars to find the ones related to key path components when dumping an expression.
2019-04-17 21:15:16 -07:00
Brent Royal-Gordon
9cb896b828 [ConstraintSolver] Give type vars for keypath components useful locators
Previously, nearly all of the type vars for a key path component were dumped under the KeyPathExpr itself, whcih made them difficult to differentiate when debugging. Attach each one to “key path component #N -> function result” instead.

# Conflicts:
#	lib/Sema/CSGen.cpp
2019-04-17 14:34:45 -07:00
Slava Pestov
f3bb3d92f0 Sema: Don't allow closure return type to contain noescape types 2019-04-16 23:01:39 -04:00
Slava Pestov
eed84abda9 Sema: Add TVO_CanBindToNoEscape
We have a systemic class of issues where noescape types end up bound to
type variables in places that should not. The existing diagnostic for
this is ad-hoc and duplicated in several places but it doesn't actually
address the root cause of the problem.

For now, I've changed all call sites of createTypeVariable() to set the
new flag. I plan on removing enough occurrences of the flag to replicate
the old diagnostics. Then we can continue to refine this over time.
2019-04-16 23:01:33 -04:00
Suyash Srijan
072e84acd6 [CSSimplify] Reject key path if root type is AnyObject (#23820)
Detect situations where `AnyObject` is attempted to be used as a root type of the key path
early and diagnose via new diagnostics framework.
2019-04-14 12:18:35 -07:00
Pavel Yaskevich
16b65018b4 Merge pull request #23436 from xedin/keypath-dynamic-lookup
[SE-0252][TypeChecker] Keypath Dynamic Member Lookup
2019-04-05 00:10:53 -07:00
Anthony Latsis
13e805ebf8 Merge the «declared here» diagnostics for Identifier and DeclName (#23703) 2019-04-01 14:53:30 -07:00
Pavel Yaskevich
589283b2c0 [ConstraintSystem] Teach sanitizer about dynamic member lookup expressions
Type-check based diagnostics could introduce keypath dynamic member
expressions into AST, which have to be converted back to their
original member or subscript reference form before attempting
re-typecheck.
2019-04-01 12:41:55 -07:00
Saleem Abdulrasool
bebde5dd5d Sema: avoid a use-after-free in ResolvedMemberResult
The use of the reference to a private implementation caused a silent
use-after-free which would normally not trigger a problem as the use was
pretty close by.  The reference would copy the pointer and the
destructor for the implementation would free the backing memory.  We
would then continue to use the free'd memory to query the information.

The Windows heap allocator kindly scribbles over the memory which caused
an invalid memory access, helping isolate the use-after-free.
2019-03-31 13:09:42 -07:00
Slava Pestov
e212d4567f Sema: Collect varargs into an ArrayExpr and use DefaultArgumentExpr
Instead of building ArgumentShuffleExprs, lets just build a TupleExpr,
with explicit representation of collected varargs and default
arguments.

This isn't quite as elegant as it should be, because when re-typechecking,
SanitizeExpr needs to restore the 'old' parameter list by stripping out
the nodes inserted by type checking. However that hackery is all isolated
in one place and will go away soon.

Note that there's a minor change the generated SIL. Caller default
arguments (#file, #line, etc) are no longer delayed and are instead
evaluated in their usual argument position. I don't believe this actually
results in an observable change in behavior, but if it turns out to be
a problem, we can pretty easily change it back to the old behavior with a
bit of extra work.
2019-03-31 01:36:19 -04:00
Slava Pestov
b9ef5708e2 Sema: Simplify representation of vararg forwarding
VarargExpansionExpr shows up in call argument lists in synthesized
initializers and modify accessors when we need to forward arguments
to a call taking varargs.

Previously we would say that the type of VarargExpansionExpr is
$T when its subexpression type is [$T]. matchCallArguments() would
then 'collect' the single VarargExpansionExpr into a variadic
argument list with a single element, and build an ArgumentShuffleExpr
for the argument list.

In turn, SILGen would peephole vararg emission of a variadic
argument list with a single entry that happens to be a
VarargExpansionExpr, by returning the subexpression's value,
which happened to be an array of the right element type,
instead of building a new array containing the elements of the
variadic argument list.

This was all too complicated. Instead, let's say that the type of
a VarargExpansionExpr is [$T], except that when it appears in a
TupleExpr, the variadic bit of the corresponding element is set.

Then, matchCallArguments() needs to support a case where both
the parameter and argument list have a matching vararg element.
In this case, instead of collecting multiple arguments into a
single variadic argument list, we treat the variadic argument like
an ordinary parameter, bypassing construction of the
ArgumentShuffleExpr altogether.

Finally, SILGen now needs to be able to emit a VarargExpansionExpr
in ordinary rvalue position, since it now appears as a child of a
TupleExpr; it can do this by simply emitting the sub-expression
to produce an array value.
2019-03-28 23:23:58 -04:00
Pavel Yaskevich
53a9b1f9b9 Merge pull request #23312 from xedin/form-complete-member-overload-sets
[ConstraintSystem] Include unviable-by-fixable choices into member overload sets
2019-03-18 13:43:52 -07:00
Pavel Yaskevich
852169a5f5 [ConstraintSystem] Split unviable lookup result storage into candidates & reasons
That makes it easy to process unviable choices the same way as viable
ones and request rejection reasons only when necessary.
2019-03-14 22:20:20 -07:00
Rintaro Ishizaki
b006c7c9b8 [AST] Don't return inapplicable decls in lookupVisibleDecls
rdar://problem/45340583 / https://bugs.swift.org/browse/SR-9027
rdar://problem/36594731
2019-03-14 12:57:37 -07:00
Doug Gregor
4c16107365 [Constraint solver] Unify overload favoring on getEffectiveOverloadType().
The overload-favoring code had a couple of different ways in which it
tried to figure out the parameter lists of a given declaration. Have
those all depend on ConstraintSystem::getEffectiveOverloadType(),
which deals with the various member/non-member cases uniformly.
2019-03-04 20:48:48 -08:00
Doug Gregor
daf4610be1 [Constraint solver] Don't remove/reintroduce disjunction when favoring.
When favoring particular constraints in a disjunction, don't remove
the old disjunction and create a new one---it's just churn in the
constraint system. Instead, favor the constraints that need it.

This technically flips the SR-139 test case from "too complex" to
being fast enough, but it's still fairly slow.
2019-03-04 20:31:06 -08:00
Doug Gregor
9086b2beef [Constraint solver] Flatten the disjunction created by call favoring.
The call-favoring code was creating a two-level disjunction, when would
then immediately get flattened into a single level. Instead, create a
single-level disjunction directly.
2019-03-04 16:21:24 -08:00
Doug Gregor
ae4949d27c [Constraint solver] Remove a hand-rolled clone of getUnboundBindOverloadDisjunction
Use the real one instead.
2019-03-04 15:59:59 -08:00
Doug Gregor
69ef7895a8 [Constraint solver] Match argument labels for unresolved member expressions. 2019-03-01 23:11:34 -08:00
Doug Gregor
603d5d6f20 [Constraint solver] Synchronize argument label setting/retrievable.
The walker that was setting argument labels was looking through ! and ?
postfix expressions, but the lookup code itself was not, leading to missed
opportunities for filtering based on argument labels. Generalize the
transformation that maps from the function expression down to the locator
for which we will retrieve argument labels.
2019-03-01 21:45:39 -08:00
Doug Gregor
c6631ec83d [Constraint solver] Match call arguments for subscripts.
Record the argument labels provided to a subscript in the solver, and
use that to filter out subscript declarations with non-matching
argument labels during function application. This reduces the number of
overloaded subscript declarations that will be considered in later
steps in the solver, reducing the solution space.

*Disable* this optimization in the normal member-lookup path, which is
intended to go away in the near future. This limits the scope of the
change somewhat, so we can separately tackle the diagnostics issue.
The one diagnostics change here is probably an improvement, because
the user explicitly stated the argument labels, and is more likely
missing a conversion on the argument than having typed the wrong
label.
2019-03-01 09:18:53 -08:00
Doug Gregor
202d52135d [Constraint system] Generalize effective-overload-type computation.
Extend the computation of effective overload types, used in common result
type and common type computations, to also handle subscripts and variables.
This allows the optimization to also apply to subscripts, which did not
previously have a peephole in constraint generation.
2019-02-27 23:30:03 -08:00
Doug Gregor
5f99d91ea8 [Constraint solver] Compute common apply result type in the solver.
Constraint generation for function application expressions contains a simple
hack to try to find the common result type for an overload set containing
callable things. Instead, perform this “common result type” computation
when simplifying an applicable function constraint, so it is more
widely applicable.
2019-02-27 23:30:03 -08:00
Doug Gregor
86d14a8be8 Merge pull request #22858 from DougGregor/constraint-system-common-type-overloads
[Constraint system] Compute and use a common type among overloads.
2019-02-26 15:39:18 -08:00
Joe Groff
bb67cf815c Merge pull request #21355 from technicated/tuple-keypaths-2
Tuple KeyPaths
2019-02-25 12:56:05 -08:00
Doug Gregor
90e5957787 [Constraint solver] Use type variable constraints to find literal types.
Rather than looking directly at the subexpressions of a function
application to determine whether they are literal expressions, look
instead at the type variables for the argument types: if they have a
literal-conforms-to constraint on them, use the named literal protocol
to determine favored declarations.

This refactoring is intended to broaden the applicability of the
existing "favored declaration" machinery to also consider the literal
constraints of other type variables that are equivalent to the type
variable named by the argument.
2019-02-23 22:50:17 -10:00
Doug Gregor
1ab417cd8e [Constraint system] Compute and use a common type among overloads.
Given an overload set, attempt to compute a "common type" that
abstracts over all entries in the overload set, providing more
structure for the constraint solver.
2019-02-22 21:53:34 -10:00
technicated
d7324b977e Added more tests
Testing SILGen & IRGen tuple keypath generation
Added tuple element type check in SILVerifier
2019-02-18 10:19:42 +01:00
Andrea Tomarelli
ede47cafbd Partial AST & Sema implementation of TKP 2019-02-18 09:04:42 +01:00
Azoy
e9f4217f03 remove lookupBoolType
diagnose broken bool

fix
2019-02-09 00:13:51 -06:00
Pavel Yaskevich
7f262a7003 [Sema] Extract @autoclosure diagnostics from type resolver
Since @autoclosure attribute is associated with declarations
it makes more sense to move diagnostics to where type of the
parameter has been completely resolved. This also helps to support
parameters with typealiases pointing to function types without
any extra logic in the resolver.
2019-02-05 10:57:05 -08:00
Pavel Yaskevich
7d830cee8b [ConstraintSystem] Add a fix to detect invalid partial application
Currently supported only partial applications of instance methods
marked as `mutating`.
2019-01-25 13:29:11 -08:00
Slava Pestov
e99607c421 Sema: Use ConstraintKind::Bind where possible instead of ::Equal
Solving Bind is a little easier than Equal. The only remaining uses of Equal
are in the .member syntax and keypaths; if we can refactor those, we might be
able to simplify LValue handling in the type checker in general.
2019-01-14 14:55:16 -05:00
Slava Pestov
33871548b3 Clean up TypeVariableType::Implementation::mustBeMaterializable(), etc 2019-01-12 14:10:11 -05:00
Slava Pestov
bb34620bf8 Sema: Validate extension if needed in isExtensionApplied()
We're looking at the generic signature of the extension, so it must
have been validated at this point.

I don't have a test case for this and maybe it never came up, but
nothing forces it to be validated here so let's make it more robust
by doing it explicitly.
2019-01-08 22:58:12 -05:00
Pavel Yaskevich
91724c9918 [TypeChecker] Address review comments
* Incomplete conformance is not necessarily going to produce a diagnostic
* Use `userFacingName` to avoid assert related to special names.
2019-01-07 10:53:50 -08:00
Pavel Yaskevich
64fa0ee729 [TypeChecker] Add Builtin operation to trigger/test fallback diagnostic 2019-01-07 10:42:00 -08:00
Robert Widmann
2efbeb3912 Merge pull request #21451 from CodaFi/logicd
[SR-8272] Drop the last remnants of LogicValue
2018-12-20 23:33:20 -05:00
Robert Widmann
426fe886dc [SR-8272] Drop the last remnants of LogicValue
Removes the _getBuiltinLogicValue intrinsic in favor of an open-coded
struct_extract in SIL.  This removes Sema's last non-literal use of builtin
integer types and unblocks a bunch of cleanup.

This patch would be NFC, but it improves line information for conditional expression codegen.
2018-12-19 23:14:59 -05:00
Pavel Yaskevich
20b7c2a4ba [CodeSynthesis] Make sure that LazyInitializerExpr has a type
Currently when `LazyInitializerExpr` is added to the AST it's not
given an explicit type. Because of that constraint solver silently
fails in contraint generator without diagnostic. But since
sub-expression associated with `LazyInitializerExpr` is already
type-checked it makes sense to set its type explicitly.
2018-12-19 14:04:32 -08:00
Pavel Yaskevich
56089d8782 [CSGen] Sanitizer should preserve type of re-introduced member references (if any) 2018-12-13 19:21:17 -08:00
Joe Groff
89979137fc Push ArchetypeType's API down to subclasses.
And clean up code that conditionally works only with certain kinds of archetype along the way.
2018-12-12 19:45:40 -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
Rintaro Ishizaki
5df5711b67 [CodeCompletion] Rework getOperatorCompletions() (#20632)
In postfix completion, for operator completion, we do:

  1. Type check the operand without applying it, but set the resolved
     type to the root of the expression.
  2. For each possible operators:
      i. Build temporary binary/postfix expression
      ii. Perform type checking to see whether the operator is applicable 

This could be very slow especially if the operand is complex.

* Introduce `ReusePrecheckedType` option to constraint system. With
  this option, CSGen respects pre-stored types in expressions and doesn't
  take its sub-expressions into account.
  * Improve type checking performance because type variables aren't
     generated for sub-expressions of LHS (45511835)
  * Guarantee that the operand is not modified by the type checker because
     expression walkers in `CSGen` doesn't walk into the operand.

* Introduce `TypeChecker::findLHS()` to find LHS for a infix operator from
  pre-folded expression. We used to `foldSequence()` temporary
  `SequenceExpr` and find 'CodeCompletionExpr' for each attempt.
  * No need to flatten folded expression after initial type-checking.
  * Save memory of temporary `BinaryExpr` which used to be allocated by
    `foldSequence()`.
  * Improve accuracy of the completion. `foldSequence()` recovers invalid
    combination of operators by `left` associative manner (with
    diagnostics). This used to cause false-positive results. For instance,
    `a == b <HERE>` used to suggest `==` operator. `findLHS()` returns
    `nullptr` for such invalid combination.

rdar://problem/45511835
https://bugs.swift.org/browse/SR-9061
2018-11-22 21:20:51 +09:00
Mark Lacey
018498fb2f Merge pull request #16942 from bjhomer/bjhomer/optional-try-flattening
Flatten nested optionals resulting from try? and optional sub-expressions
2018-11-16 11:14:05 -08:00
Michael Gottesman
e379ab9beb Merge pull request #20557 from brentdax/this-is-why-swift-is-a-memory-safe-language
Fix lookupDirect() use-after-scope bugs
2018-11-14 00:01:10 -08:00
Brent Royal-Gordon
8c969b8eb8 [Sema] Assert that tap vars are in the current DeclContext (#20303)
This assertion would have caught the last-minute bug in the string interpolation rework.
2018-11-13 17:19:23 -08:00
Brent Royal-Gordon
270ad33a72 Fix lookupDirect() use-after-scope bugs
In #7530, NominalTypeDecl::lookupDirect() started returning TinyPtrVector instead of ArrayRef so that it wouldn’t be returning a pointer into a mutable data structure. Unfortunately, some callees assigned its return value into an ArrayRef; C++ happily converted the TinyPtrVector to an ArrayRef and then treated the TinyPtrVector as out-of-scope, so the ArrayRef would now point to an out-of-scope object. Oops.
2018-11-13 16:31:59 -08:00
BJ Homer
df6be36457 Add backwards-compatible behavior for swift-version < 5 2018-11-06 23:31:02 -07:00