Commit Graph

1177 Commits

Author SHA1 Message Date
Pavel Yaskevich
45634bd262 Merge pull request #17947 from xedin/rdar-42056741
[Diagnostics] Transfer previously resolved types directly from expressions
2018-07-14 12:14:42 -07:00
Doug Gregor
5db1901d57 [Type checker] Emit coalesce-or-force-unwrap Fix-Its for necessary unwraps more consistently.
Replace the last (and most obscure) use of the poor “use ‘?’ or ‘!’” diagnostic with the
new, more explanatory version that provides separate notes with Fix-Its for coalescing or
force-unwrapping the value.

Finishes rdar://problem/42081852.
2018-07-13 16:48:15 -07:00
Pavel Yaskevich
d3249695a9 [Diagnostics] Transfer previously resolved types directly from expressions
While trying to diagnose the problem with previously type-checked
sub-expression, use its type-checked variant as a source of type
information, instead of transferring from its original constraint system,
because if expression was type-checked successfully it would
have all of the required information in AST, and that doesn't
rely on associated constraint system being present.

Resolves: rdar://problem/42056741
2018-07-13 16:38:48 -07:00
Doug Gregor
e7eac0af22 [Type checker] Extend the diagnostics for unwrapping the base of a member access.
Introduce a new fix kind into the constraint solver to cover unwrapping the base
of a member access so we can refer to the a member of the unwrapped base.
Wire this fix kind to the just-added diagnostic that suggests either the
chaining ‘?’ or the force-unwrap ‘!’ via separate, descriptive Fix-Its.

Example:

error: value of optional type 'X?' must be unwrapped to refer to member 'f' of wrapped base type 'X'
  let _: Int = x.f()
               ^
note: chain the optional using '?' to access member 'f' only for non-'nil' base values
  let _: Int = x.f()
               ^
                ?
note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
  let _: Int = x.f()
               ^
                !

Before this, we would sometimes get a Fix-It for just ‘?’ and sometimes get a Fix-It for the
coalescing ‘??’, neither of which is likely to be right.

More work on rdar://problem/42081852.
2018-07-13 16:26:03 -07:00
Doug Gregor
9ec3b00ea4 [Type Checker] Improve diagnostic when referencing a member of an optional base.
Improve diagnostics when referencing a member of an optional base, where the
Optional type does not have the member but the wrapped type does. Specifically,
suggest both the chaining ‘?’ and the force-unwrapping ‘!’ Fix-Its via explanatory
notes, e.g.:

  error: value of optional type '[Int]?' must be unwrapped to refer to member 'subscript' of wrapped base type '[Int]'
    return foo.array[0]
               ^
  note: chain the optional using '?' to access member 'subscript' only for non-'nil' base values
    return foo.array[0]
               ^
                    ?
  note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
    return foo.array[0]
               ^
                    !

More of rdar://problem/42081852.
2018-07-13 15:22:56 -07:00
Pavel Yaskevich
6dd7bcff59 [Diagnostics] Remove misplaced LoadExpr workaround for RawRepresentable
Since verifier is now in place to make sure that ParenExpr/ForceValueExpr
always preceed LoadExpr there is need to special case against incorrect
ordering in diagnostics.
2018-07-12 10:13:39 -07:00
Pavel Yaskevich
597698091e Merge pull request #17093 from dingobye/SR-7918
[Sema] Diagnostic: improve diagnostics for ApplyExpr.
2018-07-11 17:38:00 -07:00
Brent Royal-Gordon
70f788ed7f Merge pull request #17656 from brentdax/thats-what-a-diagnostics-all-about
[Sema] Improve error for inout on conversion
2018-07-11 16:19:34 -07:00
Ding Ye
95b91ee953 Apply git-clang-format. 2018-07-11 15:26:56 +10:00
Ding Ye
4a05369b5a Improve interface of InputMatcher with some renaming and rephrasing;
split test cases into different files for different swift versions.
2018-07-11 15:26:56 +10:00
Ding Ye
9648371339 Extract some logic of matching parameters from isDeclAsSpecializedAs,
and reuse it to match parameters and arguments.
2018-07-11 15:26:56 +10:00
Ding Ye
3916452f18 Do not consider number of arguments when it comes to
instance method as curried member on type.
2018-07-11 15:26:55 +10:00
Ding Ye
64274a9b17 Handle parameters with default values and variadics. 2018-07-11 15:26:55 +10:00
Ding Ye
c4c730f8d1 [Sema] Diagnostic: improve diagnostics for ApplyExpr.
When diagnosing ApplyExpr, the existing implementation
tries to resolve the function subexpression independently
in the first place, without considering the argument
information. As a result, such resolved function type
may not produce the best diagnostic message.
This patch adds condideration of the number of arguments
to decide the better function subexpression for diagnostic
purpose.

Resolves: SR-7918, SR-7786, SR-7440, SR-7295, SR-5154.
2018-07-11 15:26:55 +10:00
Brent Royal-Gordon
7545b1945f [Sema] Improve error for inout on conversion
The diagnostic when a user attempts to pass an argument requiring an implicit conversion (e.g. declared as a subclass, type conforming to protocol, etc.) to an inout parameter seems to be confusing for users—see e.g. SR-8155, SR-8148. This change replaces it with a more specific diagnostic which clearly suggests a solution. It also includes a fix-it suggesting the user change the type of the original variable if it’s a local with a simple type signature.
2018-07-10 12:31:36 -07:00
Jordan Rose
c468ca1e48 Handle stray LoadExprs in RawRepresentable fix-its (#17804)
When there are multiple possible overloads for a call, the partially-
type-checked argument expression might end up with a LoadExpr outside
of the call ParenExpr instead of inside it. Account for this in a one-
off way for the 'rawValue' / 'init(rawValue:)' fix-its.

Yet more https://bugs.swift.org/browse/SR-8150
2018-07-09 15:22:18 -07:00
Slava Pestov
9f055beb55 Merge pull request #17783 from slavapestov/kill-lvalue-access-kind
Kill lvalue access kind
2018-07-06 00:39:26 -07:00
Slava Pestov
381483bd74 AST: Remove Expr's LValueAccessKind 2018-07-05 23:54:13 -07:00
Slava Pestov
cf9b0d6466 Sema: Fix some keypath diagnostic crashers
We would crash in these cases:

- If all lookup results were subscripts
- If all results were unavailable
2018-07-05 23:01:14 -07:00
Jordan Rose
9116f95826 Merge pull request #17714 from jrose-apple/raw-deal
Prefer RawRepresentable fix-its that don't require an extra conversion

https://bugs.swift.org/browse/SR-8150
2018-07-03 16:03:53 -07:00
Jordan Rose
b318ab125d Prefer RawRepresentable fix-its that don't require an extra conversion
That is, if a type has a raw value of Int, and the function being
called has overloads for both Int and UInt, we always want to offer a
fix-it that leads to the 'Int' overload, not insert a conversion to
UInt that might be incorrect.

More https://bugs.swift.org/browse/SR-8150.
2018-07-03 13:24:36 -07:00
Jordan Rose
cc82ae00d9 Fix RawRepresentable fix-its for single unlabeled arguments
They were being put outside the call parentheses instead of inside.
We even had tests for this; I just got them wrong.

https://bugs.swift.org/browse/SR-8150
2018-07-03 13:24:22 -07:00
Doug Gregor
80688d0176 [Type Checker] Remove the TypeChecker from most of the “override” checking.
A type checker instance isn’t needed for most of the “override” checking,
so remove the TypeChecker parameters and instead get the ASTContext or
DiagnosticEngine from whatever decl or type is available. Another step
toward reducing dependency on a TypeChecker instance.
2018-07-02 23:00:39 -07:00
Doug Gregor
d098b3e571 [Sema] Remove the IterativeTypeChecker. 2018-06-15 22:46:55 -07:00
Doug Gregor
18569e5f77 Merge pull request #16963 from DougGregor/evaluator-type-checker
[Type checker] Use the request-evaluator in the type checker.
2018-06-15 22:41:31 -07:00
Matt Diephouse
87aaf10e6b [Diagnostics] Improve error when type parameters aren't equal 2018-06-15 15:08:05 -04:00
Doug Gregor
b7fbe57bf9 [AST] Remove “validated” bit from TypeLoc.
TypeLocs have been “validated” when they have a non-null Type, so eliminate
the separate “validated” bit that was out-of-sync with the Type field.
2018-06-14 15:29:57 -07:00
Matt Diephouse
863d7d36f2 [Diagnostics]: Rewrite "no subscript members" error
• Change name to match names of member diagnostics.

• Explicitly call out that this is a "value of type". This matches the error from non-existent methods and properties.

• Don't call them "subscript members". That term is never used in documentation and "member" doesn't add anything besides confusion.
2018-06-13 13:35:24 -04:00
Slava Pestov
51319ee171 Merge pull request #17106 from AnthonyLatsis/clean_subscript_fixits_logic
[CSDiag] Remove nested type and use TC.isConvertible instead
2018-06-11 21:43:00 -07:00
fischertony
88928a8eba [CSDiag] Remove nested type and use TC.isConvertible instead 2018-06-11 14:10:44 +03:00
Robert Widmann
24abf3ea09 [NFC] Refactor default argument info
Refactor the interface to return a bit vector. Along the way, fix up
the callers and remove some dead usages of the defaults information
that were copied and pasted around Sema.
2018-05-31 17:28:04 -07:00
Robert Widmann
7e8aa9fcc6 [NFC] Rename @lvalue Locator Path Element
8271c1a removed the last hacky usage of ArrayElementType,
leaving behind just the @lvalue-to-inout conversions.  Rename
the locator path element to reflect this and do a bit of cleanup on the
unrelated-but-near-enough other hack RValueAdjustment.
2018-05-26 16:48:41 -07:00
fischertony
dcf75de693 [#16431][Amendments] Note emitting amendment & use generic note
Show the note always if there is a single candidate
Switch to using the generic 'declared here' diag introduced in #16766
2018-05-24 17:24:39 +03:00
Anthony Latsis
7139152132 [Diagnostics|SR-5789] Fixits for applying subscripts by keyword (#16431)
* [Diagnostics|SR-5789] Added fixits for referring to subscripts by keyword

If there is a subscript member, the error message changes to [type -bash has no member property or method named 'subscript']
The fix-it replaces parentheses with brackets, removes '.subscript'
If the apply expression is incomplete, e.g. subscript(..., the fix-it adds a bracket at the end.

* tests updated & logic for compatible arg types (except generics)

* ignore generic types & switch to returning

* avoid explicitly using sting literals

* handle implicit conversion of tuples & encapsulate it

* isolate subscript misusage

* return bool instead of void

* move function to FailureDiagnosis, diagnose independently & update error message

* Update CSDiag.cpp
2018-05-22 11:18:27 -07:00
Mark Lacey
43775e817d Disallow some implicit pointer conversions in autoclosures.
Disallow implicit conversion or arguments from Array, String, and
InOut (formed by &) to pointer types if the argument is for an
@autoclosure parameter.

These conversions were really only intended to be used for C/ObjC
interop, and in some contexts like autoclosures they are not safe.

Fixes: rdar://problem/31538995
2018-05-16 12:13:25 -07:00
Pavel Yaskevich
934351a8e3 [Diagnostics] Hint contextual type diagnostics with expression type if known
Avoid a re-typecheck in `diagnoseContextualConversionError` if the
expression type is already known and only return `true` if we know
that at least one error diagnostic has been emitted otherwise there
is a risk that type-check is going to return without any and fail
in verifier.

Resolves: rdar://problem/40002266
2018-05-12 13:56:53 -07:00
Pavel Yaskevich
7500c6eced [Diagnostics] Switch from vector to set to avoid storing duplicate solution types
This allows callees of `getPossibleTypesOfExpressionWithoutApplying`
to get only unique types from the re-run of the solver.
2018-05-12 11:28:34 -07:00
Huon Wilson
8e0bba14a0 Revert "[Diagnostics] Hint contextual type diagnostics with expression type if known" 2018-05-13 00:28:52 +10:00
Pavel Yaskevich
d102f7d590 [Diagnostics] Hint contextual type diagnostics with expression type if known
Avoid a re-typecheck in `diagnoseContextualConversionError` if the
expression type is already known and only return `true` if we know
that at least one error diagnostic has been emitted otherwise there
is a risk that type-check is going to return without any and fail
in verifier.

Resolves: rdar://problem/40002266
2018-05-11 13:50:36 -07:00
Pavel Yaskevich
52571eeffd [Diagnostics] Switch from vector to set to avoid storing duplicate solution types
This allows callees of `getPossibleTypesOfExpressionWithoutApplying`
to get only unique types from the re-run of the solver.
2018-05-11 13:50:11 -07:00
Anthony Latsis
d4fc337a9b Left string initialized empty
(for the further condition to work properly)
2018-05-08 16:54:01 +03:00
fischertony
1ec733cabb [Diagnostics] Provide fixits for cases with up to 10 missing closure args 2018-05-08 04:45:24 +03:00
Huon Wilson
d4fbca1183 [Sema/CS] std::function -> llvm::function_ref for some non-escaping params. 2018-05-01 08:29:08 +10:00
Anthony Latsis
96c0c13a2b [Diagnostics] SR-7445 Improve diagnostics for assignment failures (#16179)
* [Diagnostics] SR-7445 Improve diagnostics for assignment failures

* modified messages for assignments to function calls,
   modified messages for assignments to methods.
   removed comment for resolved radar.

* removed extra line and braces

* added tests for assignment_lhs_is_apply_expression
   eliminated redundant literal check which is always invoked before call
   reverted 'cannot assign to value' for literal assignments in subexpressions

* Complemented assigning to literal tests & reverted to 'cannot asign to value' for methods (was 'cannot assign to member')

* removed extra tabs

* eliminated one more accidental spacing

* Update CSDiag.cpp

* added highlighting, fixed & rechecked tests

* added highlighting for complex expressions involving assigning to literals

Resolves: [SR-7445](https://bugs.swift.org/browse/SR-7445)
2018-04-28 15:49:10 -07:00
Pavel Yaskevich
aa2b25b3dd Merge pull request #15868 from xedin/solver-get-params
[Sema] Attempt to replace usages of `getInput()` with `getParams()`
2018-04-26 23:32:15 -07:00
Pavel Yaskevich
3c64680a91 [Diagnostics] Switch CalleeCandidateInfo to use FunctionType::getParams() 2018-04-26 17:33:08 -07:00
Pavel Yaskevich
57ad592844 [Sema] Switch computeDefaultMap to use AnyFunctionType::getParams() 2018-04-26 17:33:08 -07:00
Slava Pestov
ff61b9e039 Sema: Fix another type variable leak with closures
When we type check a closure expression with inferred parameter
types, we assign type variables to them. If type checking fails,
we end up in FailureDiagnosis::diagnoseClosureExpr().

If there was no contextual type, we would skip the code path
which nukes type variables in the closure's ParamDecls before
proceeding to type check the body. Type checking of the body
creates a new constraint system which would pick up the outer
constraint system's type variables and blow up.

Fixes <rdar://problem/39489003>.
2018-04-25 20:52:34 -07:00
Pavel Yaskevich
0168fa43a4 Merge pull request #16056 from xedin/rdar-39514009
[Diagnostics] Check member name kind before trying to get its identifier
2018-04-20 18:03:21 -07:00
Huon Wilson
18683f305d Merge pull request #15587 from huonw/minmax
[Sema] Perform name lookup in outer scopes in some cases involving conditional conformances
2018-04-20 22:33:07 +10:00