Commit Graph

1513 Commits

Author SHA1 Message Date
Pavel Yaskevich
3bbde55052 [ConstraintSystem] Detect and diagnose incorrectly typed weak declarations
All `weak` declarations are supposed to have an optional type.
Detect situations when is not an optional, wrap it as a fix,
and diagnose the problem.
2021-09-08 09:39:54 -07:00
Hamish Knight
b5bc5141e5 Merge pull request #39192 from hamishknight/a-range-of-arguments
[CS] Store argument list mappings on solutions
2021-09-08 12:00:48 +01:00
Pavel Yaskevich
021349f520 Merge pull request #39162 from xedin/dont-increase-weak-let-optionality
[CSGen] Don't increase optionality of `weak var` patterns
2021-09-06 10:30:11 -07:00
Hamish Knight
632bf41768 [CS] Make ArgumentLists a private member
Move associateArgumentList onto ConstraintSystem
and make ArgumentLists private in preparation for
having it roll back at the end of solver scopes.
2021-09-06 12:54:49 +01:00
Hamish Knight
dc60996f89 Merge pull request #38836 from hamishknight/toil-and-tuple 2021-09-04 19:30:04 +01:00
Pavel Yaskevich
b1a79df066 [CSGen] Refactor weak declaration/capture optionality enforcement
- Make sure that `varType` is wrapped into an optional type for `weak`
  declarations.
- Verify that `externalPatternType` is an optional type when one-way
  constraints are requested.
2021-09-03 18:11:53 -07:00
Pavel Yaskevich
e8a89cc1d1 [CSGen] Don't increase optionality of weak var patterns
If the externally-imposed type is already optional,
let's not add optionality to the pattern.
2021-09-03 10:59:30 -07:00
Holly Borla
09f2ca7f25 Merge pull request #39146 from hborla/wrapped-param-diagnostics
[ConstraintSystem] Fix a misleading argument mismatch error when using wrapped parameters.
2021-09-02 22:10:21 -07:00
Holly Borla
947a6c32a5 [ConstraintSystem] In the solver, only generate constraints for wrapped
parameter attributes for closure parameters.

For regular function parameters, these constraints will have already been
generated and solved when computing the backing property wrapper type at
the function declaration. If the solver also generates these constraints
when type checking a function call, it leads to misleading diagnostics
about an argument mismatch that will appear at the function declaration
instead of the call-site.
2021-09-02 17:37:44 -07:00
Holly Borla
138c1170ea [CSGen] Declare generateWrappedPropertyTypeConstraints as a method
on ConstraintSystem.
2021-09-02 17:37:44 -07:00
Pavel Yaskevich
52307608e4 [ConstraintSystem] Get pattern from pattern binding for uninitialized variable targets
Instead of passing pattern directly, let's fetch it from the pattern
binding and remove extra argument from `SolutionApplicationTarget::forUninitializedVar`.
2021-09-02 11:41:43 -07:00
Pavel Yaskevich
20431b9dc6 [ConstraintSystem] Fix incorrect type-check of IUO pattern bindings
Patterns associated with `PatternBindingDecl`s have to be type-checked
in pattern binding context. Not doing so might result in spurious errors.
2021-09-02 10:17:09 -07:00
Hamish Knight
8c2b88abc0 [CS] Adopt ArgumentList
- Explicitly limit favoring logic to only handle
unary args, this seems to have always been the
case, but needs to be handled explicitly now that
argument lists aren't exprs
- Update the ConstraintLocator simplification to
handle argument lists
- Store a mapping of locators to argument lists
in the constraint system
- Abstract more logic into a getArgumentLocator
method which retrieves an argument-to-param locator
from an argument anchor expr
2021-09-01 18:40:25 +01:00
Pavel Yaskevich
9518286ca8 Merge pull request #38912 from xedin/generalize-unitialized-var-target
[ConstraintSystem] Generalize uninitialized wrapped variable targets
2021-08-23 13:20:55 -07:00
Frederick Kellison-Linn
ab7a95c18b [Sema] Delay function representation check until placeholders are bound 2021-08-19 14:53:33 -04:00
Pavel Yaskevich
6ee42ab962 [ConstraintSystem] Make it explicit that default initialized pattern bindings are not supported 2021-08-17 14:24:49 -07:00
Pavel Yaskevich
c87e0e149a [ConstraintSystem] Implement uninitialized pattern binding support
This like `let x: Int` or `let (x, y), ...` are now supported by
the constraint solver.
2021-08-17 14:23:57 -07:00
Pavel Yaskevich
a72b057829 [ConstraintSystem] Generalize solution target to support un-initialized variables
Rename `uninitializedWrappedVar` kind to `uninitializedVar` and
allow storage of patterns and indices into pattern binding entry.
2021-08-17 14:23:57 -07:00
Pavel Yaskevich
56e9fa662e [CSGen] Fail early if pattern binding is incorrect
It's possible for `TypeChecker::typeCheckPattern` to produce
either no type or type containing error. That should be detected
early and immediately fail constraint generation.
2021-08-16 13:53:26 -07:00
Pavel Yaskevich
e583aab8cb [CSGen] Resolve binding pattern before using it
This change makes constraint generation logic consistent with
how `PatternBindingEntryRequest::evaluate` handles pattern binding
entries.
2021-08-16 13:47:51 -07:00
Robert Widmann
592e90af9b Add Sugar for Variadics
We used to represent the interface type of variadic parameters directly
with ArraySliceType. This was awfully convenient for the constraint
solver since it could just canonicalize and open [T] to Array<$T>
wherever it saw a variadic parameter. However, this both destroys the
sugaring of T... and locks the representation to Array<T>. In the
interest of generalizing this in the future, introduce
VariadicSequenceType. For now, it canonicalizes to Array<T> just like
the old representation. But, as you can guess, this is a new staging
point for teaching the solver how to munge variadic generic type bindings.

rdar://81628287
2021-08-06 12:51:39 -07:00
Pavel Yaskevich
1509accca2 [ConstraintSystem] Open only directly declared opaque types related to return statements (#38762)
* [ConstraintSystem] NFC: Rename `openOpaqueTypeRec` and make `openOpaqueType` private for individual types

* [ConstraintSystem] Require a contextual purpose for `openOpaqueType(Type, ...)`

Some of the contexts require special handling e.g. return type of a function
can only open directly declared opaque result type(s), this would be
implemented in a follow-up commit.

* [ConstraintSystem] Open only directly declared opaque types related to return statements

Re-establish a check which would only open opaque types directly
declared in the return type of a function/accessor declaration,
otherwise constraint system would end up with type variables it
has no context for if the type had generic parameters.

Resolves: rdar://81531010
2021-08-05 20:45:46 -04:00
willtunnels
319b3e64aa Add support for opaque result types in structural positions (#38392)
* [TypeResolver][TypeChecker] Add support for structural opaque result types

* [TypeResolver][TypeChecker] Clean up changes that add structural opaque result types
2021-08-03 23:45:02 -04:00
Hamish Knight
8f9d30bbaa Preserve trailing closure index in OriginalArgumentList 2021-08-02 12:59:42 +01:00
Hamish Knight
df566e3147 [AST] Rename decomposeInput/composeInput
Explicitly call out the fact we're dealing with
tuples.
2021-07-28 23:14:45 +01:00
Hamish Knight
2d951efd8b [CS] Don't bail out of CollectVarRefs early
Upon encountering an ErrorExpr, we were previously
bailing from the walk. However, for multi-statement
closures, that could result in us missing some
variable references required to connect to the
closure to its enclosing context. Make sure to
continue walking to catch those cases.

SR-14709
rdar://78781677
2021-07-21 23:04:06 +01:00
Pavel Yaskevich
befe4bd2ab Revert "[CSGen] Don't apply one-way requirement to typed patterns" 2021-07-20 14:35:49 -07:00
Pavel Yaskevich
8a59c1e038 Merge pull request #38320 from xedin/rdar-80271666
[CSGen] Don't apply one-way requirement to typed patterns
2021-07-09 22:52:21 -07:00
Pavel Yaskevich
66b1149abd [CSGen] Don't apply one-way requirement to typed patterns
Typed patterns are represented by a name and a fixed contextual
type, let's not use intermediary type variable and one-way constraint
as its type because that variable would be bound right away to
contextual type. Also setting type of a variable declaration
to a type variable when contextual type is IUO doesn't play well
with overload resolution because it expects an optional type for
declarations with IUO attribute.

Resolves: rdar://80271666
2021-07-09 14:50:17 -07:00
Alex Hoppen
78a7fe8438 [Sema] Record type of identity key paths in the constraint system
If we are completing e.g. `\.self.#^COMPLETE^#` we use the type of `self` as the base type for the completion. In order to retrieve that, we need to record the type.

Fixes rdar://80271346 [SR-14887]
2021-07-07 23:47:09 +02:00
Alex Hoppen
d64b8ecea6 [CodeCompletion] Migrate key path completion to be solver based
This commit essentially consistes of the following steps:
- Add a new code completion key path component that represents the code completion token inside a key path. Previously, the key path would have an invalid component at the end if it contained a code completion token.
- When type checking the key path, model the code completion token’s result type by a new type variable that is unrelated to the previous components (because the code completion token might resolve to anything).
- Since the code completion token is now properly modelled in the constraint system, we can use the solver based code completion implementation and inspect any solution determined by the constraint solver. The base type for code completion is now the result type of the key path component that preceeds the code completion component.

This resolves bugs where code completion was not working correctly if the key path’s type had a generic base or result type. It’s also nice to have moved another completion type over to the solver-based implementation.

Resolves rdar://78779234 [SR-14685] and rdar://78779335 [SR-14703]
2021-06-25 23:19:35 +02:00
Hamish Knight
c1b1feb8e7 [AST] Clean up CaptureListEntry
Don't store a VarDecl separately, expose a `getVar`
accessor that forwards onto `getSingleVar`, and
rename `Init` to `PBD`.
2021-06-08 22:56:06 +01:00
Pavel Yaskevich
b7a7024e0a [ConstraintLocator] Add information about expected value type to key path locator
Record a "value" type associated with this location to be able to
reference during inference.
2021-05-28 17:51:58 -07:00
Slava Pestov
131d3f4bce Sema: Pass down a ModuleDecl instead of a DeclContext to conformsToProtocol()
... and a bunch of follow-up simplifications pushing ModuleDecls further
up, since I couldn't resist the yak shave.
2021-05-17 16:34:18 -04:00
Slava Pestov
ee33f12df7 IDE: Refactor IsDeclApplicableRequest to use Requirement::isSatisfied() 2021-05-17 16:34:17 -04:00
Luciano Almeida
c069e86f83 [NFC][Sema] Changing API to facilitate recording pontential holes recursively 2021-05-01 23:06:25 -03:00
Pavel Yaskevich
51ff12d06e [ConstraintLocator] Augment ContextualType element to carry its purpose
Having purpose attached to the contextual type element makes it much
easier to diagnose contextual mismatches without involving constraint
system state.
2021-04-26 09:51:21 -07:00
Strieker
4e2b67cbae [NFC] Resolved merge conflict in CSSimplify.cpp after making changes to improve error handling for composed wrapped value mismatches 2021-04-22 11:49:21 -07:00
Strieker
b3f0442faa [ConstraintSystem] Modified CSGen.cpp in order to resolve a failing test in property_wrappers.swift 2021-04-22 11:31:42 -07:00
Strieker
c9b53ec66f [ConstraintSystem] Created a locator in CSGen to store the location where the wrappedValue type of a property wrapper was determined and added necessary logic so that a constraint locator can recognize a composed property wrapper’s wrapped value type in CSFix.cpp, CSFix.h, and CSSimplify.cpp. 2021-04-22 11:31:41 -07:00
Azoy
9ed732f0ab Introduce isDecl and getDeclType
fix enum logic issue

fix tests

guard against null types
2021-04-20 02:22:16 -04:00
Nathan Hawes
f75f5fe78d Merge pull request #36879 from nathawes/track-match-call-result
[ConstraintSystem] Record parameter bindings in solutions (NFC)
2021-04-18 06:10:23 +10:00
Nathan Hawes
c57c403ffa [ConstraintSystem] Record parameter bindings in solutions (NFC)
This saves us from needing to re-match args to params in CSApply and is also
useful for a forthcoming change migrating code completion in argument position
to use the solver-based typeCheckForCodeCompletion api.

rdar://76581093
2021-04-16 18:32:06 +10:00
Pavel Yaskevich
e507bcfc44 Merge pull request #36792 from LucianoPAlmeida/failure-diagnostics-left
[CSGen] Removing code that was only necessary for failure diagnostics
2021-04-12 11:04:34 -07:00
Luciano Almeida
a028a0dc2c [CSGen] Removing code that was only necessary for failure diagnostics 2021-04-06 23:40:23 -03:00
Alex Hoppen
fd8e34913a Merge pull request #36551 from ahoppen/pr/internal-labels-in-closures
[CodeComplete] Default parameter names of completed closure to internal names
2021-04-06 15:30:26 +02:00
Alex Hoppen
865e80f9c4 [CodeComplete] Default parameter names of completed closure to internal names
If have a function that takes a trailing closure as follows
```
func sort(callback: (_ left: Int, _ right: Int) -> Bool) {}
```
completing a call to `sort` and expanding the trailing closure results in
```
sort { <#Int#>, <#Int#> in
  <#code#>
}
```

We should be doing a better job here and defaulting the trailing closure's to the internal names specified in the function signature. I.e. the final result should be
```
sort { left, right in
  <#code#>
}
```

This commit does exactly that.

Firstly, it keeps track of the closure's internal names (as specified in the declaration of `sort`) in the closure's type through a new `InternalLabel` property in `AnyFunctionType::Param`. Once the type containing the parameter gets canonicalized, the internal label is dropped.

Secondly, it adds a new option to `ASTPrinter` to always try and print parameter labels. With this option set to true, it will always print external paramter labels and, if they are present, print the internal parameter label as `_ <internalLabel>`.

Finally, we can use this new printing mode to print the trailing closure’s type as
```
<#T##callback: (Int, Int) -> Bool##(_ left: Int, _ right: Int) -> Bool#>
```

This is already correctly expanded by code-expand to the desired result. I also added a test case for that behaviour.
2021-04-01 19:14:19 +02:00
Holly Borla
ad3568e52e Merge pull request #36344 from hborla/property-wrapper-parameter-revision
[SE-0293] Implement revision #3 of property wrapper parameters
2021-03-31 15:49:01 -07:00
Pavel Yaskevich
f00c578761 Merge pull request #34401 from xedin/implicit-cgfloat-conversion
[DNM][TypeChecker] Implement Double <-> CGFloat implicit conversion
2021-03-31 10:20:28 -07:00
Holly Borla
a15f4233a2 [Property Wrappers] Implement implementation-detail property wrappers for
parameters.
2021-03-31 09:29:46 -07:00