Commit Graph

950 Commits

Author SHA1 Message Date
Holly Borla
8d52f71561 [NFC][Sema] Rename checkUnsupportedProtocolType to checkExistentialTypes. 2021-12-09 23:15:02 -08:00
Holly Borla
7c7b3ba0da Revert "[NFC, Typechecker] Remove UnsupportedProtocolVisitor and checkUnsupportedProtocolType()"
This reverts commit 15f88e9be3.
2021-12-09 23:15:02 -08:00
Pavel Yaskevich
4cd404d020 [MiscDiagnostics] Introduce a base class for diagnostic walkers
Put some common logic related to local declaration to the base
class and refactor other walkers to use it instead of `ASTWalker`.
2021-12-03 10:57:33 -08:00
Pavel Yaskevich
9bd603bfc9 [Diagnostics] Apply "unhandled throw" diagnostic for for-in loop in closures
Extract diagnostic into a method and use it while type-checking
`for-in` in top-level code and in closures.
2021-12-03 10:56:32 -08:00
Saleem Abdulrasool
349af3707d Merge pull request #40305 from compnerd/semitruck
gardening: make c++98-compat-extra-semi an error
2021-11-30 08:18:36 -08:00
Pavel Yaskevich
bc54bc6bb7 Revert "[TypeChecker] SE-0326: Enable multi-statement closure inference by default" 2021-11-29 17:26:08 -08:00
Saleem Abdulrasool
910fbee14e gardening: make c++98-compat-extra-semi an error
This cleans up 90 instances of this warning and reduces the build spew
when building on Linux.  This helps identify actual issues when
building which can get lost in the stream of warning messages.  It also
helps restore the ability to build the compiler with gcc.
2021-11-27 11:40:17 -08:00
Hamish Knight
ede9302704 [Sema] Fix spurious trailing closure warning
With the argument list refactoring, it's no
longer sufficient to stop walking when we encounter
an explicit tuple or paren. Add an additional
check to stop walking when we encounter an explicit
argument list.

rdar://85343171
2021-11-19 19:26:04 +00:00
Pavel Yaskevich
8b901544ee [MiscDiagnostics] Introduce a base class for diagnostic walkers
Put some common logic related to local declaration to the base
class and refactor other walkers to use it instead of `ASTWalker`.
2021-11-15 16:42:05 -08:00
Pavel Yaskevich
7b0f68c05f [Diagnostics] Apply "unhandled throw" diagnostic for for-in loop in closures
Extract diagnostic into a method and use it while type-checking
`for-in` in top-level code and in closures.
2021-11-15 16:42:05 -08:00
Robert Widmann
ded4dd2103 Suggest Replacement Types for Invalid Placeholders
Now that placeholder types are preserved, we can open them wherever they appear in positions where they are used as contextual types. Use the checks we already run in the primaries to ban placeholders in top-level positions. Only now, use the inferred type of any associated expressions or statements to present the user with a contextual return type.

For parameters in functions and enum cases, we use any default argument expressions to get the contextual type.

For functions, we use a traversal similar to the opaque result type finder to find the type of any return statements in the program and present those as options.
2021-11-03 10:29:15 -07:00
Alexis Laferrière
1a3f28500b [Sema] Don't check availability in assigned properties initializers
For a non-public property where the type is defined by an assignment, like
`internal var internalAssigned = NewStruct()`, type-checking the type's
availability is done via checking the initializer expression.

In -check-api-availaiblity-only mode, pass down a flag to not check
availability in expressions for initializer expressions of such
non-public properties.

rdar://84389825
2021-10-25 13:46:53 -07:00
Hamish Knight
dc60996f89 Merge pull request #38836 from hamishknight/toil-and-tuple 2021-09-04 19:30:04 +01:00
Jack March
7e0a7f322a fix SR-4559 (unapplied reference to method named "self" can be used without qualification)
https://bugs.swift.org/browse/SR-4559
2021-09-02 21:17:42 +01:00
John McCall
a00ed9f2dd Merge pull request #39118 from rjmccall/implicit-self-capture-capture-warning
Fix the condition for warning about implicit capture of self captures.
2021-09-02 01:09:21 -04:00
Konrad `ktoso` Malawski
b73050e49e Merge pull request #39095 from AnthonyLatsis/se-309
SE-0309: Unlock existential types for all protocols
2021-09-02 11:27:39 +09:00
Hamish Knight
3e28bbbd2c Update for review feedback
- Remove OriginalArguments in favor of storing the
pre-rewritten argument list, which simplifies things
nicely
- Adopt llvm::indexed_accessor_iterator
2021-09-01 18:40:29 +01:00
Hamish Knight
ad55c0976b [MiscDiagnostics] Adopt ArgumentList 2021-09-01 18:40:25 +01:00
John McCall
1711df4ce4 Fix the condition for warning about implicit capture of self captures.
We've always emitted an error if we saw an implicit use of a self
parameter of class type from an escaping closure.  In PR #35898, I fixed
this to also emit an error if the reference was to an explicit capture
of self that wasn't made in the current closure.  That was causing
some source incompatibilities that we decided were too severe, so in
PR #38947 I weakened that to a warning when the diagnostic walk was
within multiple levels of closures, because I have always thought of
this as a fix to nested closures.  However, this was the wrong condition
in two ways.

First, the diagnostic walk does not always start from the outermost
function declaration; it can also start from a multi-statement closure.
In that case, we'll still end up emitting an error when we see uses
of explicit captures from the closure when we walk it, and so we still
have a source incompatibility.  That is rdar://82545600.

Second, the old diagnostic did actually fire correctly in nested
closures as long as the code was directly referring to the original
self parameter and not any intervening captures.  Therefore, #38947
actually turned some things into warnings that had always been errors.

The fix is to produce a warning exactly when the referenced declaration
was an explicit capture.
2021-09-01 02:29:51 -04:00
Suyash Srijan
15f88e9be3 [NFC, Typechecker] Remove UnsupportedProtocolVisitor and checkUnsupportedProtocolType() 2021-08-31 19:21:26 +03:00
Rintaro Ishizaki
857490e3cb Merge pull request #39078 from rintaro/isa_and_nonnull 2021-08-27 21:46:42 -07:00
Rintaro Ishizaki
49547a5378 [NFC][Basic] Import llvm::isa_and_nonnull to 'swift' namespace
Just for convenicence.

* Replace `llvm::isa_and_nonnull` with imported `isa_and_nonnull`
* Repalce some `EXPR && isa<T>(EXPR)` with `isa_and_nonnull<T>(EXPR)`
2021-08-27 11:36:21 -07:00
Hamish Knight
8be85c09b9 [Sema] Have isValidTypeExprParent consider its child
With the introduction of the ArgumentList type,
argument expressions will be direct decedents of
call expressions, without an intermediate TupleExpr.
As such, we need to tweak isValidTypeExprParent to
consider the child expression such that `T(x)` is
permissible, but `x(T)` is not.

Change isValidTypeExprParent to take a child expr
parameter, and update its use in MiscDiagnostics
and PreCheckExpr. For PreCheckExpr, switch from a
stack to walking the parent directly, as a
stack-based approach would be a bit more fiddly in
this case, and walking the parents shouldn't be
expensive.

This should be NFC, I'm splitting it off from the
ArgumentList refactoring to make the rebasing there
a little more straightforward.
2021-08-27 12:38:33 +01:00
Kavon Farvardin
c0607b345f Revert "Merge pull request #33767 from theblixguy/chore/remove-self-or-associated-type-diagnostic"
The following regression test added for this feature is not passing:

Swift(linux-x86_64) :: decl/protocol/protocols_with_self_or_assoc_reqs_executable.swift

with a compiler crash happening during SILFunctionTransform "Devirtualizer".

Reverting to unblock CI.

This reverts commit f96057e260, reversing
changes made to 3fc18f3603.
2021-08-26 16:46:42 -07:00
Konrad `ktoso` Malawski
f96057e260 Merge pull request #33767 from theblixguy/chore/remove-self-or-associated-type-diagnostic
SE-0309: Unlock existential types for all protocols
2021-08-27 06:09:47 +09:00
Frederick Kellison-Linn
42b04279f0 [Sema] Rework TypeExpr folding for placeholders 2021-08-19 14:53:33 -04:00
John McCall
5eecc6ac07 Only warn about self capture in nested closures until Swift 6.
Swift has diagnosed implicit uses of class-reference `self` in
escaping closures for a long time as potentially leading to reference
cycles.  PR #35898 fixed a bug where we failed to diagnose this
condition in nested closures.  Unfortunately, treating this as an
error has proven problematic because there's a lot of code doing
it.  Requiring that code to be thoroughly stamped out before we
ship a compiler is just too much to ask.  Stage the fix in by
treating it as a warning in Swift versions prior to 6.

As with the non-nested case, this warning can be suppressed by
explicitly either capturing `self` or spelling out `self.` in the
access.

Fixes rdar://80847025.
2021-08-18 21:44:23 -04:00
Suyash Srijan
23c1d66eaf [NFC, Typechecker] Remove UnsupportedProtocolVisitor and checkUnsupportedProtocolType() 2021-08-16 18:30:26 +03:00
willtunnels
319b3e64aa Add support for opaque result types in structural positions (#38392)
* [TypeResolver][TypeChecker] Add support for structural opaque result types

* [TypeResolver][TypeChecker] Clean up changes that add structural opaque result types
2021-08-03 23:45:02 -04:00
Ben Barham
7e7b521abd [Diagnostics] Allow matching accessors and functions for a renamed decl
A lookup on the name provided by `renamed` in `@available` returns the
VarDecl. If the name specified a getter or setter, make sure to grab the
corresponding accessor when comparing candidates.

We currently ignore the basename and parameters specified in the name
for accessors. Checking them would only cause a getter/setter not to
match, there can never be a conflict. Since this is a best effort match
anyway, this seems fine.
2021-08-03 08:46:44 +10:00
Hamish Knight
7d6dac6855 [Sema] Correct label fix-its for multiple trailing closures
Update the logic to account for multiple trailing
closures.

rdar://81278194
2021-08-02 12:59:43 +01:00
Pavel Yaskevich
66815f9d25 [MiscDiagnostics] Downgrade plain type use error to a warning for certain positions
In Swift < 6 warn about plain type name passed as an
argument to a subscript, dynamic subscript, or ObjC
literal since it used to be accepted.

Resolves: rdar://80270126
2021-07-07 14:36:29 -07:00
Pavel Yaskevich
333fdbcca4 [MiscDiagnostics] Store a reference to a call node together with its arguments 2021-07-07 12:22:17 -07:00
Pavel Yaskevich
9c54d7916a Merge pull request #37897 from mininny/control-flow-suggestion-for-optional
[Diagnostics] Offer 'is' replacement for unused 'if let' expression with optional operand
2021-06-22 10:24:59 -07:00
Minhyuk Kim
f85cb50af2 [Sema] Add diagnostics handling for nested expressions 2021-06-17 11:25:51 +09:00
Minhyuk Kim
c3ab99a405 [Sema] Offer 'is' replacement for unused 'if let' expression when the operand is optional 2021-06-14 00:40:03 +09:00
Pavel Yaskevich
299da73ec4 [MiscDiagnostics] NFC: Add an assert that arguments a valid to .nan comparison diagnostic
Make sure that both arguments are valid before checking conformance
to `FloatingPoint`. This helps us to debug reports of crashes in
`conformsToKnownProtocol` referencing arguments (rdar://78920375).

Since this diagnostic should only be run on type-checked AST,
it's unclear what caused one of the arguments to have null type.
2021-06-10 12:23:24 -07:00
Hamish Knight
cc62c116f8 [ASTWalker] Don't visit capture list vars twice
Previously we were walking them once when visiting
the capture list, and then again as a part of the
pattern binding decl. Change the logic to only
visit them as a part of their pattern binding decl.
2021-06-08 22:56:05 +01:00
Slava Pestov
5bdf189a5f Sema: Remove a usage of hasComputedGenericSignature() from expression checker diagnostics
Removing this exposed a re-entrant diagnostic emission, so let's fix that too.
2021-05-26 18:48:29 -04:00
Hamish Knight
46fa6e5721 [AST] Improve BinaryExpr
Abstract away the TupleExpr gunk and expose
`getLHS` and `getRHS` accessors. This is in
preparation for completely expunging the use
of TupleExpr as an argument list.
2021-05-19 14:48:01 +01:00
Slava Pestov
131d3f4bce Sema: Pass down a ModuleDecl instead of a DeclContext to conformsToProtocol()
... and a bunch of follow-up simplifications pushing ModuleDecls further
up, since I couldn't resist the yak shave.
2021-05-17 16:34:18 -04:00
Doug Gregor
abfc9bcdc4 Add @_implicitSelfCapture attribute to disable "self." requirement.
Add a new parameter attribute `@_implicitSelfCapture` that disables the
requirement to explicitly use `self.` to refer to a member of `self`
in an escaping closure.

Part of rdar://76927008.
2021-04-20 17:26:07 -07:00
Azoy
9ed732f0ab Introduce isDecl and getDeclType
fix enum logic issue

fix tests

guard against null types
2021-04-20 02:22:16 -04:00
Ben Barham
3de83aca66 [ClangImporter] Add @completionHandlerAsync for imported decls
Implicitly add the @completionHandlerAsync attribute for ObjCMethodDecl
that have a completion handler. Adds a link from the non-async to the
async function for use in diagnostics and refactorings.

Resolves rdar://74665226
2021-03-31 08:39:12 +10:00
Kavon Farvardin
efbb0f1a14 implement typechecking for effects specifiers on 'get' accessors
The basic rule is that the protocol requirement
describes the maximal set of effects that the
property is allowed to have. Thus, witnesses
must have the same or fewer effects specifiers
on the getter.

For class inheritance overrides, you can remove
effects freely, as long as you stay within
the bounds of the normal override restrictions.
But, you cannot override with more effects than
the base member has. Same goes for protocol
member overrides.

Furthermore, we disallow key paths to effectful
properties/subscripts, which also cannot be @objc.
2021-03-26 07:54:53 -07:00
Evan Wilde
8516c0c5aa Fix crash with failed typecheck
The walker was crashing if something failed to typecheck since the type
wasn't available. I've added a little test to ensure that not only do we
not crash, but continue checking within the closure.
2021-03-24 22:54:15 -07:00
Evan Wilde
0fbbd896e6 Migrate checker to MiscDiagnostics 2021-03-24 17:51:07 -07:00
Alex Hoppen
fa9f3f3337 [Sema] Correctly diagnose passing tuple as only argument to function with multiple parameters
If a tuple is passed as the only argument to a function that takes two unnamed arguments, we try to diagnose the missing argument label. However, in the old diagnose code we didn’t distinguish between non-existing arguments and arguments without labels; both behaved the same.

Thus, the missing second argument in the function call would line up with the empty second argument label, not producing a diagnostic, but hitting an assertion later on.

Distinguish between the two to fix the issue.

Fixes rdar://64319340
2021-03-19 12:58:17 +01:00
Doug Gregor
85611dbf25 Allow marker protocols as existentials (but still ban casts)
There is no problem with having an existential value that involves a
marker protocol. However, since there is no runtime checking for
marker protocols, ban "as?" and "is" casts to them.
2021-02-18 23:44:39 -08:00
Erik Eckstein
a17f8c2f3f SILOptimizer: add a diagnostics pass to warn about lifetime issues with weak references.
The DiagnoseLifetimeIssuesPass pass prints a warning if an object is stored to a weak property (or is weakly captured) and destroyed before the property (or captured reference) is ever used again.
This can happen if the programmer relies on the lexical scope to keep an object alive, but copy-propagation can shrink the object's lifetime to its last use.
For example:

  func test() {
    let k = Klass()
      // k is deallocated immediately after the closure capture (a store_weak).
      functionWithClosure({ [weak k] in
                            // crash!
                            k!.foo()
                          })
    }

Unfortunately this pass can only catch simple cases, but it's better than nothing.

rdar://73910632
2021-02-15 11:11:35 +01:00