Commit Graph

1162 Commits

Author SHA1 Message Date
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
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
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
Holly Borla
142e9f6289 [Diagnostics] Tweak some diagnostics code to allow diagnosing a fix
anchored at a TypeRepr rather than an Expr.
2020-07-02 14:47:45 -07:00
Holly Borla
2ac8d7ea17 [ConstraintSystem] Add support for type checking an out-of-line
initialized wrapped property via SolutionApplicationTarget.
2020-07-01 18:02:58 -07:00
Pavel Yaskevich
a8d44bc9ce Merge pull request #32558 from xedin/fix-req-conformace-assessment
[ConstraintSystem] Adjust recording of "fixed" requirements to avoid conflicts
2020-06-26 15:28:13 -07:00
Pavel Yaskevich
f7ef6d411c [Diagnostics] Augment conformance failure to with a note about enum with associated values
If the problem is related to an operator and argument is an enum
with associated values mention that conformances to `Equatable`
and `Comparable` are not synthesized in such cases.
2020-06-25 14:44:28 -07:00
Pavel Yaskevich
0ea0b8e27b [ConstraintSystem] Adjust recording of "fixed" requirements to avoid conflicts
Currently it's possible to have a type conflict between different
requirements deduced as the same type which leads to incorrect
diagnostics. To mitigate that let's adjust how "fixed" requirements
are stored - instead of using resolved type for the left-hand side,
let's use originating generic parameter type.
2020-06-25 13:40:15 -07:00
Saleem Abdulrasool
87d2cdcc9e Sema: silence some uncovered warnings on MSVC (NFC)
This adds an unreachable to indicate to MSVC that the switch is covered.
2020-06-25 10:38:54 -07:00
Pavel Yaskevich
6c85c7f729 [TypeChecker] NFC: Revise comments for {typeCheck, solve}ForCodeCompletion 2020-06-17 00:06:03 -07:00
Pavel Yaskevich
ccd7167371 [TypeChecker] Add a entry point to be used for code completion
As part of the code completion redesign this new entry point is going
to replace use of:

- `typeCheckExpression`
- `getTypeOfExpressionWithoutApplying` (which could be removed)

and possibly other methods currently used to retrieve information
for code completion purposes.

Advantages of a new approach:

- Avoids mutating AST;
- Allows to avoid sub-expression type-checking;
- Allows code completion access to multiple solutions in ambiguous cases;
- Provides all possible solutions - valid and invalid (with holes);
- Allows code completion to easily access not only types but
  overload choices and other supplimentary information associated
  with each solution.
2020-06-15 13:55:00 -07:00
Pavel Yaskevich
77d0919966 Merge pull request #32365 from gregomni/solution-node-map
[Sema] Solution nodeTypes from MapVector to DenseMap.
2020-06-15 01:28:44 -07:00
Greg Titus
148436035b CS node->type map requires ordering because it can be partially undone,
but Solution node->type map is all or nothing, so can be cheaper.
2020-06-13 12:47:34 -07:00
Anthony Latsis
f4f2101e9c [NFC] Sema: Internalize the use of UnboundGenericType in applyUnboundGenericArguments 2020-06-12 18:04:55 +03:00
Anthony Latsis
86b7e989e7 [NFC] CS: Give the type-transforming openUnboundGenericType method a more accurate name 2020-06-11 23:58:01 +03:00
Anthony Latsis
bd23534db6 [NFC] CS: Inline a parameter in openUnboundGenericType 2020-06-11 04:54:44 +03:00
Robert Widmann
ca728917e1 Merge pull request #32138 from AnthonyLatsis/eliminate-typeloc
Sema: Remove TypeLoc from ExplicitCastExpr (via TypeExpr)
2020-06-10 11:28:50 -07:00
Anthony Latsis
4b4634141f Sema: Remove TypeLoc from ExplicitCast 2020-06-09 15:20:25 +03:00
Luciano Almeida
3e280b8e83 Merge pull request #32107 from LucianoPAlmeida/nfc-move-to-cpp
[NFC][ConstraintSystem] Move some inlined header methods to implementation file
2020-06-06 16:37:19 -03:00
Doug Gregor
f1febc2d52 [Constraint system] Lift PatternBindingDecl into SolutionApplicationTarget.
Generalize the code used to generate constraints and apply solutions to
PatternBindingDecls so that it is handled directly by the constaint
system and solution, respectively, rather than as part of the function
builder transform. No functionality change, but this is a cleaner
abstraction.
2020-06-05 22:51:18 -07:00
Doug Gregor
b1493413ab [Constraint system] Generalize recording of pattern binding entry targets.
Rather than storing the record of each pattern binding entry's solution
application targets as part of an applied function builder, store them
within the constraint system and solution using a newly-generalized
form of SolutionApplicationTargetsKey.
2020-06-05 22:51:18 -07:00
Doug Gregor
b8440359ad Fix a typo in a comment 2020-06-05 14:05:17 -07:00
Doug Gregor
836bc57fe5 [AST Walker] Stop visiting the bodies of closures as expressions.
Single-expression closures have always been traversed differently
from multi-statement closures. The former were traversed as if the
expression was their only child, skipping the BraceStmt and implicit
return, while the later was traversed as a normal BraceStmt.
Unify on the latter treatment, so that traversal

There are a few places where we unintentionally relied on this
expression-as-child behavior. Clean those up to work with arbitrary
closures, which is an overall simplification in the logic.
2020-06-04 23:06:32 -07:00