Commit Graph

1182 Commits

Author SHA1 Message Date
Pavel Yaskevich
d039107bdb [ConstraintSystem] Replace use of UnresolvedType with specialized HoleType 2020-08-26 18:43:10 -07:00
Pavel Yaskevich
bc319779d5 [TypeChecker] Replace invalid refs with ErrorExpr only when explicitly allowed
Add a flag to `ConstraintSystem::preCheckExpression` and subsequently
to `TypeChecker::resolveDeclRefExpr` to indicate whether it's allowed
to replace invalid member refs with `ErrorExpr`.

It is useful for diagnostics and code completion to preserve AST
in it's original state otherwise it's impossible to diagnose errors
post factum or extract `CodeCompletionExpr` when it's a child of an
invalid reference.
2020-08-17 12:39:29 -07:00
Pavel Yaskevich
df4d89c58d Merge pull request #33164 from xedin/refactor-getpotentialbindings
[CSBindings] Refactor `getPotentialBindings` into `inferBindingsFor` + `infer`
2020-08-13 12:37:29 -07:00
Pavel Yaskevich
17e5ec4fac Merge pull request #33406 from xedin/adjuments-for-code-completion
[ConstraintSystem] A couple of adjustments to make interaction between solution and code completion simpler
2020-08-11 15:08:39 -07:00
Pavel Yaskevich
73262ae2a9 [ConstraintSystem] Add Solution::getResolvedType to get a concrete type for a given ASTNode 2020-08-11 12:19:21 -07:00
Pavel Yaskevich
54d2581886 [ConstraintSystem] Add isStaticallyDerivedMetatype to Solution
This is useful for a new style code completion which doesn't apply
solutions back to neither constraint system nor AST.
2020-08-11 12:10:34 -07:00
Holly Borla
89c499ec61 [ConstraintSystem] Remove default argument for updateWorkList from
ConstraintSystem::mergeEquivalenceClasses.

Most callers don't use the default, and it's important to consider
the value of this argument for each call to mergeEquivalenceClasses.
2020-08-11 09:35:45 -07:00
Holly Borla
4096a62b2a [ConstraintSystem] Don't update the work list when merging type variables
in addJoinConstraint.
2020-08-10 19:56:00 -07:00
Pavel Yaskevich
08771ab5e9 Merge pull request #33364 from xedin/trailing-closure-warning
[Diagnostics] Extract backward scan deprecation warning into a fix/diagnostic
2020-08-07 15:20:33 -07:00
Holly Borla
9e818233f0 Merge pull request #33296 from hborla/merge-joined-literal-typevars
[ConstraintSystem] Add a type variable merging heuristic to addJoinConstraint
2020-08-07 09:07:19 -07:00
Pavel Yaskevich
f292eb6b6a [Diagnostics] Introduce a warning diagnostic for backward trailing closure matches
Diagnose situations when trailing closure has been matched to a specific
parameter via a deprecated backward scan.

```swift
func multiple_trailing_with_defaults(
  duration: Int,
  animations: (() -> Void)? = nil,
  completion: (() -> Void)? = nil) {}

multiple_trailing_with_defaults(duration: 42) {} // picks `completion:`
```
2020-08-07 00:30:55 -07:00
Holly Borla
b26554147e [NFC] Document the new type variable merging heuristic in addJoinConstraint 2020-08-06 10:14:57 -07:00
Holly Borla
cd44ca87a9 [ConstraintSystem] Move getAtomicLiteralKind to TypeVariableType::Implementation 2020-08-06 07:36:46 -07:00
Holly Borla
0efb86a7fb [ConstraintSystem] Add an overload of addJoinConstraint that takes in
an iterator range and a callback to get the type.

This allows callers to lazily compute the input types to join rather
than constructing an array first. The old addJoinConstraint is now
a wrapper for this new overload.
2020-08-06 07:36:46 -07:00
Holly Borla
1724debda4 [ConstraintSystem] Add a type variable merging heuristic to addJoinConstraint.
This heuristic merges type variables for literal expressions of the same
kind. This is valid because such type variables will have the same
set of constraints on them, and must be bound to the same type.
2020-08-03 19:53:34 -07:00
Holly Borla
d376f295a1 [Property Wrappers] Don't inject the wrapped value placeholder
in CSApply if the apply expression is a nullptr.

The can happen for subscript calls because coerceCallArguments is
called with a nullptr ApplyExpr.
2020-07-31 14:14:07 -07:00
Holly Borla
a8f363478e Merge pull request #33149 from hborla/function-builder-body-result-diags
[Function Builders] Teach diagnostics about function builder body result types...
2020-07-29 09:04:14 -07:00
Rintaro Ishizaki
59196975b7 Merge pull request #32696 from rintaro/ide-completion-typecheckatloc
[CodeCompletion] Only type check target statement and related decls in function body
2020-07-28 19:12:31 -07:00
Pavel Yaskevich
6dd859e92f [CSBindings] Remove remaining flags from inference 2020-07-28 12:48:33 -07:00
Holly Borla
9baca13088 [Function Builders] Teach diagnostics about function builder body
result types in order to properly diagnose requirement failures that
aren't anchored at an expression.
2020-07-27 18:28:27 -07:00
Pavel Yaskevich
de390584cd [CSBindings] Refactor getPotentialBindings into inferBindingsFor + infer
Split `getPotentialBindings` in two:

- `inferBindingsFor` should take responsibility for binding constraints
  to be evaluated as potential binding sources;
- `PotentialBindings::infer` would process constraints provided by
  `inferBindingsFor` and "extract" bindings from them.

This is done to separate "sourcing" logic from "inference" and
make the former pluggable, so bindings could be computed based
on changes in constraint graph in the future.
2020-07-27 16:33:30 -07:00
Pavel Yaskevich
fa55358730 [CSBindings] const qualify ConstraintSystem passed to inference methods 2020-07-27 16:33:30 -07:00
Pavel Yaskevich
de645d0744 [CSBindings] Make infer* methods private to PotentialBindings 2020-07-27 16:33:30 -07:00
Rintaro Ishizaki
01c4df8e98 [CodeCompletion] Propagate "skipping body" flag per request basis
Instead of using a global type checker flag.
2020-07-24 10:48:51 -07:00
Doug Gregor
25d40125e9 [Trailing closures] Bias toward the backward scan for ambiguities.
This approach, suggested by Xiaodi Wu, provides better source
compatibility for existing Swift code, by breaking ties in favor of the
existing Swift semantics. Each time the backward-scan rule is needed
(and differs from the forward-scan result), we will produce a warning
+ Fix-It to prepare for Swift 6 where the backward rule can be
removed.
2020-07-24 08:47:51 -07:00
Doug Gregor
17669d7d5d [Trailing closures] Attempt both forward and backward scans.
To better preserve source compatibility, teach the constraint
solver to try both the new forward scanning rule as well as the
backward scanning rule when matching a single, unlabeled trailing
closure. In the extreme case, where the unlabeled trailing closure
matches different parameters with the different rules, and yet both
produce a potential match, introduce a disjunction to explore both
possibilities.

Prefer solutions that involve forward scans to those that involve
backward scans, so we only use the backward scan as a fallback.
2020-07-24 08:11:25 -07:00
Doug Gregor
c6670250e1 [Trailing closures] Diagnose the change in behavior with SE-0268.
SE-0248 changes the backward-scan matching behavior for the unlabeled
trailing closure into a forward scan. In circumstances where this
could silently change the meaning of a call to a particular
function, i.e., when there are two defaulted closure parameters such
that a given closure to match either one of them, produce an warning
that describes the change in behavior. For example:

    t4.swift:2:24: warning: since Swift 5.3, unlabeled trailing
closure argument matches parameter 'x' rather than parameter 'z'
    trailingClosureSingle2 { $0 }
                           ^
    t4.swift:2:24: note: label the argument with 'z' to retain the
pre-Swift 5.3 behavior
    trailingClosureSingle2 { $0 }
                           ^
                          (z:    )
    t4.swift:2:24: note: label the argument with 'x' to silence this
warning for Swift 5.3 and newer
    trailingClosureSingle2 { $0 }
                           ^
                          (x:    )
    t4.swift:1:6: note: 'trailingClosureSingle2(x:y:z:)' contains
defaulted closure parameters 'x' and 'z'
    func trailingClosureSingle2(x: (Int) -> Int = { $0 } , y: (Int) ->
Int = { $0 }, z: (Int) -> Int = { $0 }) {}
         ^                      ~

This explains the (rare) case where SE-0286 silently changes the
meaning of a program, offering Fix-Its to either restore the
pre-SE-0286 behavior or silence the warning, as appropriate.
2020-07-24 08:10:55 -07:00
Doug Gregor
7f2d4e00fc [Diagnostics] Remove the now-unused AllowInvalidUseOfTrailingClosure
Diagnosis for invalid uses of trailing closures has been folded in
with argument-matching diagnostics, so remove all of the machinery
around the syntactic "mismatched trailing closure" logic.
2020-07-24 08:10:54 -07:00
Doug Gregor
2395225df7 [Type checker] Improve diagnostics for trailing closures.
The change to the forward-scanning rule regressed some diagnostics,
because we no longer generated the special "trailing closure mismatch"
diagnostic. Reinstate the special-case "trailing closure mismatch"
diagnostic, but this time do so as part of the normal argument
mismatch diagnostics so it is based on type information.

While here, clean up the handling of missing-argument diagnostics to
deal with (multiple) trailing closures properly, so that we can (e.g)
suggest adding a new labeled trailing closure at the end, rather than
producing nonsensical Fix-Its.

And, note that SR-12291 is broken (again) by the forward-scan matching
rules.
2020-07-24 08:10:54 -07:00
Hamish Knight
be607d15b3 [CS] Introduce performSyntacticDiagnosticsForTarget
Add a function that deals with invoking syntactic
diagnostics for all the expressions involved in
a SolutionApplicationTarget.

Resolves SR-13260
Resolves rdar://65903005
2020-07-21 15:51:07 -07:00
Hamish Knight
1907330996 [CS] Add SolutionApplicationTarget::isForEachStmt 2020-07-21 15:51:06 -07:00
Hamish Knight
3437ec92d7 Merge pull request #33005 from hamishknight/connect-four-builder 2020-07-20 17:04:06 -07:00
Hamish Knight
b2813e1f8a [CS] Connect arg-to-param constraint to function builder
Previously we could inadvertently split the
constraint system without realizing that a function
builder with a generic argument may allow the
closure body to reference a type variable that
connects it to the enclosing expression.

Fix this issue by checking for an unresolved
closure argument and forming an unresolved argument
conversion constraint that includes any type
variables from the function builder type.

Resolves SR-13183
Resolves rdar://problem/65695054
2020-07-20 11:47:04 -07:00
Suyash Srijan
24b9ec78b2 [Sema] Remove a leftover CSDiag hack (#32990) 2020-07-20 12:45:00 +01:00
Holly Borla
61cb9a5f4f Merge pull request #32866 from hborla/property-wrapper-autoclosure
[Property Wrappers] Use autoclosure information from CSApply to compute `VarDecl::getPropertyWrapperInitValueInterfaceType`
2020-07-17 09:16:05 -07:00
Pavel Yaskevich
a907a15df9 Merge pull request #32821 from xedin/improve-typevar-bindings
[CSBindings] Split type variable binding inference into phases
2020-07-16 15:51:38 -07:00
Pavel Yaskevich
7622993083 [ConstraintSystem] Add a new endpoint to infer bindings for a given type variable
Since bindings now require finalization we need a new endpoint
which perform all of the required actions before returning complete
`PotentialBindings` object when they are requested for a particular
type variable without any other context.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
e5e54e703c [CSBindings] Infer transitive defaults and add them at finalization 2020-07-15 20:50:50 -07:00
Pavel Yaskevich
248341a4f0 [CSBindings] Adjust optionality of the compute bindings as a final step
If type variable is expected to conform to `ExpressibleByNilLiteral`
adjust optionality of the inferred bindings only after all of the
bindings have been collected otherwise transitive supertype bindings
are going to stay non-optional which is incorrect.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
4568e6e27b [CSBindings] Add finalize method to PotentialBindings
This is used to infer transitive information and adjust existing
bindings before solver can consider them.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
8df19d2c87 [ConstraintSystem] NFC: Associate transitive binding inference with PotentialBindings 2020-07-15 20:50:50 -07:00
Pavel Yaskevich
fa553ab83d [CSBindings] Record conformance requirements associated with type variable
Include direct conformnace requirements as well as transitive ones.
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
cb545e7202 [ConstraintSystem] Preserve l-valueness of the result after implicit IUO unwrap
Detect that result type of the overload choice is l-value and preserve
that information through the forced unwrap operation so it's possible
to load the value implicitly during solution application.

Resolves: rdar://problem/61337704
2020-07-15 13:03:28 -07:00
Holly Borla
28a817b271 [Property Wrappers] Treat default wrapped values that are noescape
autoclosures the same way we treat default arguments that are
noescape autoclosures.
2020-07-15 10:02:35 -07:00
Holly Borla
a6c33a9611 [Property Wrappers] Use autoclosure information from CSApply to compute
`VarDecl::getPropertyWrapperInitValueInterfaceType`.
2020-07-13 17:22:29 -07:00
Joe Groff
88e17a6d9a Merge pull request #32819 from jckarter/keypath-writable-availability
Availability-related fixes with keypaths, _modify, and/or property wrappers
2020-07-10 16:16:07 -07:00
Joe Groff
2e1b6e7070 Sema: KeyPath literals should be read-only when referring to unavailable setters.
Part of rdar://problem/65152582, where key paths were generating references to setters in contexts where they weren't
available.
2020-07-10 10:33:55 -07:00
Anthony Latsis
2133fe27df Sema: Have TypeResolution accept an unbound generic type opener function 2020-07-08 13:39:34 +03:00
Holly Borla
063d420e50 Merge pull request #32672 from hborla/property-wrapper-diagnostics
[Property Wrappers] Improve diagnostics for property wrappers initialized out-of-line
2020-07-07 19:07:55 -07:00
Luciano Almeida
28fb66cbaf [SR-13088] Fix false positive downcast unrelated of types that cannot be statically known (#32592)
* [TypeCheckConstraints] Adjusting cases where checked casts that cannot be determined statically were producing misleading warnings

* [tests] Adding regression tests for SR-13088

* [TypeCheckConstraints] Adjusting comment and adding an extra test case for SR13035

* [TypeCheckConstraints] Fixing typos in comments

* [AST] Moving implementation of isCollection from ConstraintSystem to AST TypeBase

* [TypeCheckConstraints] Adjusting logic to verify specific conformance to stdlib collection type before emit an downcast warning

* [TypeCheckConstraints] Creating new CheckedCastContextKind::CollectionElement to be able to verify special cases within typeCheckCheckedCast for collection elements

* [TypeCheckConstraints] Adjusting logic around generic substitution to check both subtype and supertype

* [Sema] Adding isKnownStdlibCollectionType and replacing all usages contraint system method

* [TypeChecker] Reverting fixes around array element types

* [TypeChecker] Abstract logic of check for conditional requirements on TypeChecker::couldDynamicallyConformToProtocol

* [TypeChecker] Ajdustinc can conformDynamically conform and adjust review comments

* [TypeChecker] Ajusting comments and fixing typos

* [TypeChecker] Adjusting existential and archetype logic to check inside couldDynamicConform

* [TypeChecker] Adjusting minor and adding existential check into couldDynamically conform.

* [TypeChecker] Adjusting comments
2020-07-02 22:06:29 -03:00