Commit Graph

428 Commits

Author SHA1 Message Date
Pavel Yaskevich
c17bdaf715 [AST] Adjust Diagnostic transaction to only track pending errors 2020-09-01 13:57:03 -07:00
Pavel Yaskevich
1bfb0b0747 [ConstraintSystem] Extend invalid function body fix to cover constraint generation failures
Ignore function builder body if it emits at least one diagnostic
during constraint generation.

Resolves: rdar://problem/65983237
2020-09-01 12:23:50 -07:00
Pavel Yaskevich
da6bb3657f [CSFix] Adjust IgnoreInvalidFunctionBuilderBody to return true only if errors are emitted 2020-08-17 12:39:29 -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
3ad8ea86bb [CSFix] Add a fix to ignore function builder body which fails pre-check
Fix is anchored on a `BraceStmt` associated with an invalid closure.
It's diagnosing the problem by calling `ConstraintSystem::preCheckExpression`
for all expressions in the body without suppressing errors.
2020-08-17 12:39:29 -07:00
Luciano Almeida
b1eccb594d [Sema] Do not diagnose contextual type mismatches for malformed key path expressions (#33230)
* [AST] Adding hasSingleInvalidComponent to key path expression

* [Sema] Adding a new fix and failure to diagnose missing key path component

* [Sema] Recording new fix for key path missing components and remove diagnose from pre-check

* [tests] Adjusting key path missing component contextual tests

* [Sema] Renaming missing component key path fix and failure

* [Sema] Correcting comments typos
2020-08-16 12:27:21 -03:00
Pavel Yaskevich
44d686f71d [ConstraintSystem] Use new "specify label" fix/diagnostic to warn about backward scan 2020-08-07 11:50:29 -07:00
Pavel Yaskevich
adbbc00181 [CSFix] Add a warning fix for situations when trailing closure is matched via backward scan 2020-08-06 16:55:37 -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
Luciano Almeida
9924719ced [CSFix] Creating fix for diagnose missing unwrap of optional base type on key path application 2020-07-15 08:55:31 -03:00
Luciano Almeida
efc46bd635 [TypeChecker] Not warning element cast to unrelated for known collection types (Set,Array and Dictionary) 2020-07-05 22:54:51 -03: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
73667cdc7e [Diagnostics] Use Id_MatchOperator to check whether given operator is pattern match 2020-06-15 11:57:48 -07:00
Pavel Yaskevich
2c0b8ca76a [Diagnostics] Add a tailored diagnostic for function mismatch in argument positions
If parameter has a function type and no argument overload choices
match let's diagnose that specifically.

Resolves: [SR-12689](https://bugs.swift.org/browse/SR-12689)
Resolves: rdar://problem/62481592
2020-06-12 15:02:53 -07:00
Pavel Yaskevich
d23b938331 [Diagnostics] Implement IgnoreAssignmentDestinationType::diagnoseForAmbiguity
For cases like this:

```swift
struct X {}
struct Y {}

func overloaded<T>(_ value: T) -> T { value }
func overloaded<T>(_ value: T) -> Int { 0 }

func test(x: inout X, y: Y) {
  x = overloaded(y)
}
```

Solver would record a `IgnoreAssignmentDestinationType` fix per overload,
`diagnoseForAmbiguity` could be used to properly diagnose ambiguity cases
like that.
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
b0070f5739 [Diagnostics] Check whether all contextual mismatches has the same type before diagnosing ambiguity
Instead of requiring sub-classes of `ContextualMismatch` to implement
`diagnoseForAmbiguity` let's implement it directly on `ContextualMismatch`
itself and check whether all of the aggregated fixes have same types on
both sides and if so, diagnose as-if it was a single fix.
2020-06-12 11:47:04 -07: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
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
Anthony Latsis
4b4634141f Sema: Remove TypeLoc from ExplicitCast 2020-06-09 15:20:25 +03: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
Pavel Yaskevich
80c4aa6e80 [Diagnostics] NFC: Rename ValueType to ExpectedType in raw representable fix/diagnostics 2020-06-01 12:18:03 -07:00
Pavel Yaskevich
942d967297 [CSFix] Move type verification out of UseValueTypeOfRawRepresentative 2020-05-29 12:36:34 -07:00
Pavel Yaskevich
959c6d5606 [CSFix] Move type verification out of ExplicitlyConstructRawRepresentable
Do the verification during constraint repair and provide fix itself
with raw representative type and its raw value to be used for diagnostics.
2020-05-27 17:52:52 -07:00
Pavel Yaskevich
b90e579927 Merge pull request #32001 from LucianoPAlmeida/SR-12869-tuple-mismatch-crash
[SR-12869] [Diagnostics] Fixes assertion hit on TupleContextualFailure involving optional tuple type
2020-05-26 09:36:24 -07:00
Luciano Almeida
e43cd9d784 [CSFix] Ajusting contextual type purpose for subscript destination for assign expr in getStructuralTypeContext 2020-05-25 20:18:12 -03:00
Luciano Almeida
f13b00fc6d [CSFix] Adjusting tuple mismatch to handle optionals 2020-05-24 10:39:48 -03:00
Suyash Srijan
f026c61dce [ConstraintSystem] Disallow use of enum case as a key path component (#31969) 2020-05-22 20:41:45 +01:00
Pavel Yaskevich
5db1c70cd0 [CSFix] Extend locator with ArgumentAttribute for escaping/inout fixes
If either `MarkExplicitlyEscaping` or `TreatRValueAsLValue` fix
points to an argument-to-parameter conversion, let's add more
more clarifying argument to the locator to pinpoint the problem.
2020-05-18 13:14:01 -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
Pavel Yaskevich
bf4c513f98 [CSFix] Add a fix to use when type of closure parameter can't be inferred 2020-05-15 01:13:06 -07:00
Luciano Almeida
02c454c976 [Diagnostics] Diagnose that we cannot infer the key path type when binding to a hole 2020-05-11 18:08:40 -03:00
Artem Chikin
7aed042817 Add fix on Keypath -> Function with multiple arguments type mismatch.
When simplifying a keypath constraint with a function type binding, single-parameter functions have the parameter type and the return type matched against the keypath root and value; whereas multiple-parameter functions cause an ambiguous failure (in `simplifyKeyPathConstraint`).

Resolves rdar://problem/57930643
2020-05-06 19:15:08 -07:00
Pavel Yaskevich
bd09d0c776 [ConstraintSystem] Switch ConstraintFix to use ASTNode instead of TypedNode 2020-04-29 17:03:45 -07:00
Joe Groff
b4c7a7e85d Merge pull request #31224 from AnthonyLatsis/rename-getfullname-2
AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl
2020-04-24 12:51:28 -07:00
Pavel Yaskevich
12a63ce907 [ConstraintSystem] Switch getContextualType and its variants to use TypedNode 2020-04-23 01:13:13 -07:00
Pavel Yaskevich
7669e0d059 [CSFix] Switch ConstraintFix::getAnchor to return TypedNode 2020-04-23 01:13:13 -07:00
Anthony Latsis
74252028ca AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl 2020-04-23 05:16:55 +03:00
Luciano Almeida
f18fe0960f [CSFix] Create AllowKeyPathRootTypeMismatch to diagnose key path root fail on key path application 2020-04-21 17:41:50 -03:00
Hamish Knight
d69a42d656 [CS] Preserve compatibility for collection coercions
Previously we could allow some invalid coercions to
sneak past Sema. In most cases these would either
cause crashes later down the pipeline or
miscompiles. However, for coercions between
collections, we emitted somewhat reasonable code
that performed a force cast.

This commit aims to preserve compatibility with
those collection coercions that previously
compiled, and emits a warning telling the user to
use either 'as?' or 'as!' instead.
2020-04-10 10:16:07 -07:00
Holly Borla
ece84b176c [Diagnostics] Adjust ConstraintFix::diagnoseForAmbiguity to take an array
ref of pairs rather than two parallel arrays.
2020-04-07 13:38:26 -07:00
Holly Borla
6375481ea4 [Diagnostics] In DefineMemberBasedOnUse::diagnoseForAmbiguity, use
the base type from each solution instead of only the base type from
the first solution.
2020-04-06 17:35:31 -07:00
Luciano Almeida
faba29a55f [Diagnostics] Diagnose key path reference to initializer methods. 2020-03-28 16:32:08 -03:00
Pavel Yaskevich
7800a04de3 [Diagnostics] Adjust "missing arguments" diagnostic to store parameter indices directly
This helps to avoid allocating new type variables (which shouldn't be done regardless)
to store parameter indices when `missing arguments` diagnostic is used by other diagnostics.
2020-03-13 16:55:37 -07:00
Pavel Yaskevich
3ab8710329 [Diagnostics] Cleanup use of constraint system by ConstraintFix/FailureDiagnostic 2020-03-13 15:02:10 -07:00
Pavel Yaskevich
0fc9d1b84a [Diagnostics] Move getFunctionArgApplyInfo to Solution
Instead of relying on constraint system having solution applied
let's pass a solution directly to `getFunctionArgApplyInfo`.
2020-03-13 12:46:25 -07:00
Pavel Yaskevich
1fc0ea6b0e [CSFix] Allow diagnose to accept a solution to diagnose
Instead of always applying solution back to constraint system
before diagnostics, let's pass solution directly to `ConstaintFix::diagnose`
method to be used by `FailureDiagnostic` which paves a way for
stateless diagnostics.
2020-03-12 11:41:36 -07:00
Pavel Yaskevich
bb656e120d [CSFix] Teach getStructuralTypeContext about tuple initialization calls 2020-02-17 16:09:11 -08:00