Commit Graph

1522 Commits

Author SHA1 Message Date
Luciano Almeida
81afffee72 [CSSimplify] Handle generic argument for unsatisfied generic requirement bind constraint in repairFailures 2020-07-21 00:23:21 -03: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
a49353b97b [Diagnostics] Add a tailored fix when ternary is associated with return
If it has been detected that there is an attempt to return a value
from a function without result type, let's diagnose that specifically
in cases when ternary operator is involved.
2020-07-15 20:50:50 -07:00
Luciano Almeida
11905055d8 [CSSimplify] Attempt unwrap optional base fix before root base mismatch on keypath application 2020-07-15 08:56:22 -03: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
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
f555bfefc9 [Property Wrappers] Add new contextual type purposes for property wrappers
to produce better diagnostics when there's a 'wrappedValue' type mismatch.
2020-07-02 14:47:45 -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
Luciano Almeida
43fd786ae0 [SR-5688] [Sema] Handle key path component base type on MemberAccessOnOptionalBaseFailure (#32376)
* [CSDiagnostics] Adjusting MemberAccessOnOptionalBaseFailure to be able to handle key path component member base types

* [tests] Adding regression tests for SR-5688

* [CSDiagnostics] Adjusting source range to diagnose/insert the fixes in correct location

* [tests] Adjusting regression tests to handle the fixits

* [AST] Creating an helper getSourceRange function for KeyPathExpr::Component

* [ConstraintSystem] Store member base type when recording UnwrapOptionalBase fix

* [AST] Creating a new diagnostic note for removing optional from written type

* [CSDiagnostics] Adjusting logic around MemberAccessOnOptionalBaseFailure to emit the correct diagnostics and fixes

* [tests] Adjusting regression tests to add subscript and key path root cases with respective diagnostics

* [Diagnostics] Adjusting message to mention base type

* [CSDiagnostics] Better naming for method/variable that represents base source range

* [CSDiagnostics] Adjusting to use the stored base member only when member is a key path component.

* [Diagnostics] Adjusting minor typos and code

* [AST] Adjusting keypath root diagnostic note message for use unwrapped type

* [CSDiagnostics] Adjusments in MemberAccessOnOptionalBaseFailure diagnostics as per suggestion

* [tests] Adding more test cases for SR-5688

* [CSDiagnostics] Adjusting fixits for key path root and range for diagnostics

* [CSSimplify] Attempt to diagnose InsertExplicitCall for optional function return types when possible on simplifyOptionalObjectConstraint

* [tests] Adding TODO to improve the diagnostics refering to key path root infered as optional types

* [CSDiagnostics] Adjusting comments

* [CSSimplify] Adjusting logic on simplifyOptionalObjectConstraint to attempt InsertCall fix before remove unwrap

* [CSDiagnostics] Adjust the logic to use resolveType on MemberAccessOnOptionalBaseFailure construction
2020-06-25 20:07:03 -03: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
Pavel Yaskevich
92271e9ce5 [ConstraintSystem] Rework handling of object literal expressions
Instead of special casing argument-to-parameter matching for
object literal expressions, let's allow constraint system to
lookup a witness initializer and apply it to the given set
of arguments.

This also simplifies constraint application because
`coerceCallArguments` could be used to form type-checked
argument expression.
2020-06-22 10:15:08 -07:00
Pavel Yaskevich
16862e50a5 Merge pull request #31713 from xedin/no-ranking-with-ambiguous-with-fixes
[ConstraintSystem] Overhaul ambiguity diagnostics
2020-06-16 12:55:14 -07:00
Luciano Almeida
d22821e18b Merge pull request #31814 from LucianoPAlmeida/SR-12723-conventions
[SR-12723][Sema] Validate function type param representations thick-to-thin conversions
2020-06-15 21:31:04 -03:00
Luciano Almeida
da53129beb [CSSimplify] Adjusting naming and comments of method that validates representation conversions. 2020-06-15 06:40:57 -03:00
Pavel Yaskevich
3ebf633e66 [ConstraintSystem] Increase impact of conformance failure associated with a known foundation type
Just like we already do for stdlib types, extend impact of missing
conformances on _known_ foundation types because it's unlikely to
be a valid suggestion to extend a foundation type with a new conformance.
2020-06-12 11:47:04 -07:00
Pavel Yaskevich
b407f7cd68 [ConstraintSystem] Avoid recording duplicate fixes for contextual type mismatch 2020-06-12 11:47:04 -07:00
Pavel Yaskevich
e2e57ada1b [ConstraintSystem] Turn 'omit ' into a contextual mismatch it is 2020-06-12 11:47:04 -07:00
Pavel Yaskevich
ac2305e7f1 [ConstraintSystem] Increase impact of fixes for some conditional requirements
If there is a conditional requirement failure associated with
member/function reference used in a call let's increase a score
of a fix for such failure because it renders member/function
unreachable in current context or with a given set of arguments.
2020-06-12 11:47:04 -07:00
Robert Widmann
080bea5ef9 Merge pull request #32321 from CodaFi/type-check
[NFC] TypeCheckType No Longer Returns Null
2020-06-11 20:37:33 -07:00
Robert Widmann
399a5510d2 [NFC] Inline TypeChecker::getStringType 2020-06-11 12:21:35 -07:00
Robert Widmann
09395ba345 [NFC] Inline TypeChecker::getInt8Type and TypeChecker::getUInt8Type 2020-06-11 12:21:35 -07:00
Robert Widmann
d81f1482c3 [NFC] Fixup TypeChecker::getOptionalType To Not Return Type() 2020-06-11 12:21:35 -07:00
Doug Gregor
f60d17bf3d [Function builders] Use one-way constraints for closure parameters.
Introduce one-way constraints for the parameters of closures to which a
function builder is being applied. This was an intended part of the
model when one-way constraints were introduced, but somehow got
missed. This should further break up large constraints systems for
faster solving, and *most likely* won't break much source code in
practice.

Fixes rdar://problem/64231116.
2020-06-10 16:58:03 -07:00
Luciano Almeida
adba283a00 [CSSimplify] Abstract function conversion validation into static method 2020-06-09 21:37:10 -03:00
Hamish Knight
a48776705d [CS] Add a missing null check in repairFailures
If we fail to simplify the locator, such as in the
case where we have synthesized a missing call
argument, bail without trying to add a missing
call fix.

Resolves SR-12964.
Resolves rdar://64168162.
2020-06-09 13:52:09 -07:00
Greg Titus
f7fd83de3a Merge pull request #32220 from gregomni/unintended_generic_param
[QoI] Detect unintended generic params and provide a note and fixit.
2020-06-09 12:49:19 -07:00
Greg Titus
c73b144ad8 Perform the unintended generic detection in simplifyMemberConstraint and define a ConstraintFix to produce the new note. 2020-06-09 09:02:42 -07:00
Luciano Almeida
d6bf34e65c [CSSimplify] Thin to thin is also allowed 2020-06-08 20:36:09 -03:00
Luciano Almeida
baccbde0d1 [CSSimplify] Simplify logic for valiting to thick conversion on subtype 2020-06-08 20:36:09 -03:00
Luciano Almeida
8f2f14bbad [CSSimplify] Modify logic on match representations to allow ONLY thick to thin and thick to thick representations on subtype context 2020-06-08 20:36:09 -03:00
Doug Gregor
64f903fe2a [Type checker] Experimental support for one-way parameter constraints.
Introduce an experimental mode (behind the flag
`experimental-one-way-closure-params`) that places one-way
constraints between closure parameter types and references to those
parameters within the body of the closure. The intent here is to
break up constraint systems further, potentially improving type
checking performance and making way for larger closure bodies to be
supported.

This is a source-breaking change when the body of a single-expression
closure is used to determine the parameter types. One obvious example
is when there is no contextual type, e.g.,

    let _ = { $0 + 1 }

this type-checks today because `1` becomes `Int`, which matches the
`+` overload with the type `(Int, Int) -> Int`, determining the
parameter type `Int` for the closure. Such code would not type-check
with one-way constraints.
2020-06-05 22:47:21 -07:00
Doug Gregor
7d4da10843 Merge pull request #32200 from DougGregor/single-expression-closure-cleanup
[AST] Clean up handling of single-expression closures
2020-06-05 21:02:58 -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
Doug Gregor
8e6840ddd5 Merge pull request #32178 from DougGregor/single-expression-closure-cleanup
[AST] Clean up handling of single-expression closures
2020-06-04 10:20:59 -07:00
Doug Gregor
f55e7643fa [Constaint system] Add a predicate for when to check closures in enclosing expr
Introduce a new predicate, shouldTypeCheckInEnclosingExpression(), to
determine when the body of a closure should be checked as part of the
enclosing expression rather than separately, and use it in the various
places where "hasSingleExpressionBody()" was used for that purpose.
2020-06-03 22:48:58 -07:00
Pavel Yaskevich
651503b147 Merge pull request #32128 from xedin/adjust-raw-representable-diag
[Diagnostics] Refactor diagnostics related to raw representable types
2020-06-03 11:26:22 -07:00
Pavel Yaskevich
b938f960df [Diagnostics] NFC: Rename a fix/diagnostic for missing .rawValue reference 2020-06-03 08:48:33 -07:00
Pavel Yaskevich
734f5b660c [Diagnostics] NFC: Fix typo(s) raw representative -> representable 2020-06-02 12:24:42 -07:00
Pavel Yaskevich
9805b0ab89 [ConstraintSystem] Do more checking before suggesting to use of .rawValue
Introduce `repairByUsingRawValueOfRawRepresentableType` which is used for
assignment, argument-to-parameter conversions and contextual mismatches.

It checks whether `from` side is a raw representable type and tries
to match `to` side to its `rawValue`.

Also extract logic common for `repairByUsingRawValueOfRawRepresentableType`
and `repairByExplicitRawRepresentativeUse` into `isValueOfRawRepresentable`.
2020-06-01 14:10:24 -07:00
Doug Gregor
6def0303a9 [Constraint system] Separate out constraint generation for closures. 2020-05-29 20:51:17 -07:00
Pavel Yaskevich
f048652a2b [ConstraintSystem] Do more checking before suggesting to explicitly contruct raw representable
Introduce `repairByExplicitRawRepresentativeUse` which is used for
assignment, argument-to-parameter conversions and contextual mismatches.

It checks whether `to` side is a raw representable type and tries
to match `from` side to its `rawValue`.
2020-05-29 12:14:07 -07:00
Pavel Yaskevich
c884068bfa [ConstraintSystem] Tally implicit optional wraps into impact of pointer mismatch
Impact of cases where pointer types mismatch after implict optional
wrap(s) should be higher than mismatch alone.

Distinguishing that helps to disambiguate following case (and other similar ones)
without using ranking:

```swift
func foo(_: UnsafePointer<Int>) {}
func foo(_: UnsafePointer<Int>?) {}

func test(_ ptr: UnsafePointer<Float>) {
  foo(ptr)
}
```
2020-05-26 12:46:34 -07:00
Luciano Almeida
2e1609e533 [tests] Adjusting SR-12869 test cases for optional mismatches 2020-05-24 16:13:23 -03:00
Luciano Almeida
ab0e8a8e8e [CSSimplify] Do not record IgnoreAssignmentDestinationType or contextual if we already record a tuple mismatch 2020-05-24 16:08:58 -03:00
Robert Widmann
afe8f2b63f Drop TypeCheckerDebugConsumer 2020-05-18 22:49:55 -07:00
Hamish Knight
aa0ad55706 [CS] Don't leave key path with holes unsolved
We currently leave a key path constraint unsolved
if one of its components hasn't yet had its
overload resolved. However, for e.g a missing
member component, the overload type variable will
be bound to a hole and an overload will never be
resolved.

Tweak the logic to consider the key path constraint
trivially solved if one of its components has been
marked as a hole, which will allow the key path
type itself to be marked as a hole.

Resolves SR-12437 & SR-12823.
Resolves rdar://62201037.
2020-05-16 16:52:35 -07:00
Pavel Yaskevich
d111f119d8 [ConstraintSystem] Detect and diagnose inability to infer type of closure parameter(s)
Detect situation when it's impossible to determine types for
closure parameters used in the body from the context. E.g.
when a call closure is associated with refers to a missing
member.

```swift
struct S {
}

S.foo { a, b in } // `S` doesn't have static member `foo`

let _ = { v in } // not enough context to infer type of `v`

_ = .foo { v in } // base type for `.foo` couldn't be determined
```

Resolves: [SR-12815](https://bugs.swift.org/browse/SR-12815)
Resolves: rdar://problem/63230293
2020-05-15 01:14:30 -07:00
Robert Widmann
2bca013457 Move "isDebugMode" into ConstraintSystem
This eliminates the final source of mutation of the TypeCheckerFlags on the ASTContext.
2020-05-13 09:13:44 -07:00