Commit Graph

950 Commits

Author SHA1 Message Date
John McCall
3918fb0e42 Merge pull request #35898 from rjmccall/diagnose-implicit-nested-self
Diagnose the implicit use of self in nested closures
2021-02-12 18:06:50 -05:00
John McCall
f29074d8b6 Diagnose the implicit use of self in nested closures.
Fixes SE-14120.
2021-02-10 18:44:42 -05:00
Becca Royal-Gordon
899d1a47e5 [NFC] Centralize ObjCSelector helper 2021-02-09 14:17:35 -08:00
Slava Pestov
e675bee26c AST: Split off DependencyCollector.h from EvaluatorDependencies.h
Also remove some unnecessary #includes from DependencyCollector.h,
which necessitated adding #includes in various other files.
2020-12-23 00:00:25 -05:00
Richard Wei
de2dbe57ed [AutoDiff] Bump-pointer allocate pullback structs in loops. (#34886)
In derivatives of loops, no longer allocate boxes for indirect case payloads. Instead, use a custom pullback context in the runtime which contains a bump-pointer allocator.

When a function contains a differentiated loop, the closure context is a `Builtin.NativeObject`, which contains a `swift::AutoDiffLinearMapContext` and a tail-allocated top-level linear map struct (which represents the linear map struct that was previously directly partial-applied into the pullback). In branching trace enums, the payloads of previously indirect cases will be allocated by `swift::AutoDiffLinearMapContext::allocate` and stored as a `Builtin.RawPointer`.
2020-11-30 15:49:38 -08:00
Doug Gregor
810f1cdec0 [Sema] Perform actor isolation checking across the whole function at once.
Rather than performing actor isolation checking as part of
"miscellaneous" diagnostics on an expression, do it on the whole
function at once. This should not change behavior by itself.
2020-11-16 21:58:39 -08:00
Doug Gregor
33cfbbdd72 [Concurrency] Don't diagnose missing "self." in async let autoclosures. 2020-11-04 23:18:51 -08:00
Slava Pestov
a072655599 Sema: Rename some availability checking entry points 2020-10-28 00:30:07 -04:00
Doug Gregor
16104d8045 [Concurrency] Don't lose name information from completion-handler arguments.
When a completion handler parameter has a selector piece that ends with
"WithCompletion(Handler)", prepend the text before that suffix to the
base name or previous argument label, as appropriate. This ensures that
we don't lose information from the name, particularly with delegate names.
2020-10-22 13:32:03 -07:00
Doug Gregor
24ea8becc8 [Concurrency] Move removal of leading "get" for async imports.
Name adjustments should be performed by omitNeedlessWords(), not the
early classifcation of async imports.
2020-10-21 21:48:52 -07:00
Slava Pestov
94e999a1b5 Sema: Pull availability checking out of resolveType()
We used to diagnose references to unavailable declarations in
two places:

- inside Exprs, right after type checking the expression
- inside TypeReprs, from resolveType()

In broad terms, resolveType() is called with TypeReprs
stored inside both Stmts and Decls.

To handle the first case, I added a new overload of
diagAvailability() that takes a Stmt, to be called from
typeCheckStmt(). This doesn't actually walk into any Exprs
stored inside the statement; this means it only walks
Patterns and such.

For the second case, a new DeclAvailabilityChecker is
now defined in TypeCheckAccess.cpp. It's structure is
analogous to the other three walkers there:

- AccessControlChecker
- UsableFromInlineChecker
- ExportabilityChecker

The new implementation of availability checking for types
introduces a lot more code than the old online logic
it replaces. However, I hope to consolidate some of the
code duplication among the four checkers that are defined
in TypeCheckAccess.cpp, and do some other cleanups that
will make the benefit of the new approach apparent.
2020-10-14 23:42:02 -04:00
Pavel Yaskevich
461eafff54 [ConstraintSystem] NFC: Move ConstraintSystem.h to include/swift/Sema 2020-10-08 10:45:47 -07:00
Slava Pestov
fe9d1ed595 Sema: Use ASTScope::lookupSingleLocalDecl() in VarDeclUsageChecker::handleIfConfig() 2020-09-28 17:50:52 -04:00
Robert Widmann
ee35a4fe18 Remove NameLookupFlags::KnownPrivate 2020-09-21 10:37:41 -06:00
Robert Widmann
259e1a94c9 Remove UnqualifiedLookupFlags::KnownPrivate 2020-09-21 10:37:41 -06:00
Slava Pestov
d7f4b1a1bd AST: Capture list bindings now point back to their parent CaptureListExpr
We'll need this to get the right 'selfDC' when name lookup
finds a 'self' declaration in a capture list, eg

class C {
  func bar() {}
  func foo() {
    _ = { [self] in bar() }
  }
}
2020-09-18 02:59:15 -04:00
Doug Gregor
cb6429530c [Concurrency] Actor-isolated members cannot satisfy protocol requirements.
Enforce the actor-isolation constraint that an actor-isolated member
(e.g., a non-async instance method or an instance property) cannot be
used to conform to a protocol requirement.
2020-09-17 13:36:57 -07:00
Doug Gregor
3c49e46f4f Merge pull request #33906 from DougGregor/concurrency-actors
[Concurrency] Basic support for actor classes and actor isolation
2020-09-11 15:10:45 -07:00
Doug Gregor
e00ba43b9a [Concurrency] Implement basic actor isolation rules.
Implement a basic set of isolation rules for actors, which includes:

* Asynchronous actor methods can be invoked on any actor instance.
* All mutable instance properties and synchronous instance methods are
only accessible via "self" or "super" within the context of the actor.
* Closures defined within an actor method are considered to be a
context distinct from the actor itself, are therefore are subject to
the same restrictions as above.

Together with a scheduler that ensures that asynchronous invocations
for a particular actor are serialized (at least up until they hit a
suspension point or return), this serves to isolate the actor's
instance state in a shallow manner: references to shared mutable state
can still be passed among actors, code can access global variables,
and one can still form and pass around unsafe pointers.

Pair this with some basic checking within actor contexts that
identifies a few obvious potential kinds of race conditions:

* References to captured mutable local variables from within closures.
* References to mutable global and static/class variables.

The latter are warnings, for which we currently don't have a great
suppression mechanism. That, and a lot of tuning, are still to come.
2020-09-10 21:22:00 -07:00
Suyash Srijan
49e7c6126c [Diagnostics] Diagnose comparisons with '.nan' and suggest using '.isNan' instead (#33860)
* [AST] Add 'FloatingPoint' known protocol kind

* [Sema] Emit a diagnostic for comparisons with '.nan' instead of using '.isNan'

* [Sema] Update '.nan' comparison diagnostic wording

* [Sema] Explicitly check for either arguments to be '.nan' and tweak a comment

* [Test] Tweak some comments

* [Diagnostic] Change 'isNan' to 'isNaN'

* [Sema] Fix a bug where firstArg was checked twice for FloatingPoint conformance and update some comments

* [Test] Fix comments in test file

* [NFC] Add a new 'isStandardComparisonOperator' method to 'Identifier' and use it in ConstraintSystem

* [NFC] Reuse argument decl extraction code and switch over to the new 'isStandardComparisonOperator' method

* [NFC] Update conformsToKnownProtocol to accept DeclContext and use it to check for FloatingPoint conformance
2020-09-09 22:08:42 +01:00
Anthony Latsis
492156c10f Remove SubscriptDecl::getBodyResultTypeLoc 2020-08-19 00:09:10 +03:00
Anthony Latsis
21faa48298 Remove FuncDecl::getBodyResultTypeLoc 2020-08-19 00:09:10 +03:00
Slava Pestov
397e36a6f0 Sema: Remove TypeChecker::lookupConstructor() 2020-08-18 02:00:20 -04:00
Slava Pestov
45fc0bc4db Sema: Replace some calls to getDeclaredType() with getDeclaredInterfaceType() 2020-07-31 13:39:02 -04: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
Doug Gregor
73f07a6f0f Merge pull request #33092 from DougGregor/forward-trailing-closure-matching-sourcecompat-backward-bias
[SE-0286] Forward matching of trailing closure arguments
2020-07-28 14:11:15 -07:00
Suyash Srijan
d3b5996b05 [Sema] Diagnose explicit access to a lazy variable's underlying storage (#33144) 2020-07-28 19:12:53 +01:00
Doug Gregor
05c4cee9e2 [Constraint solver] Type check captures as part of the constraint system
Rather than type-checking captures as separate declarations during
pre-check, generate constraints and apply solutions to captures in
the same manner as other pattern bindings within a constraint
system.

Fixes SR-3186 / rdar://problem/64647232.
2020-07-25 00:20:05 -07:00
Rintaro Ishizaki
b1eec26653 [AST] Add a enum for type checking state of ClosureExpr's body 2020-07-24 10:46:03 -07:00
Doug Gregor
5d1ade5784 Make the argument-label diagnostics slightly more robust.
If we're matching the trailing closure at the end but haven't seen any
mismatches yet, consider the trailing closure to be a mismatch.
This function probably needs to be rewritten in terms of
matchCallArguments(), because it is performing an incorrect
approximation of label matching that doesn't work for multiple
trailing closures.
2020-07-24 08:10:00 -07:00
Robert Widmann
8a7a1d98f5 Teach the VarDeclUsageChecker About Variables Bound in Patterns
The VDUC was missing a class of AST nodes that can bind variables:
patterns in switch statements. For these, it was falling back to
requesting a simple replacement of the bound variable name with _. But
for patterns, this means there's a two-step dance the user has to go
through where the first fixit does this:

.pattern(let x) -> .pattern(let _)

Then a second round of compilation would emit a fixit to do this:

.pattern(let _) -> .pattern(_)

Instead, detect "simple" variable bindings - for now, variable patterns
that are immediately preceded by a `let` or `var` binding pattern - and
collapse two steps into one.

Resolves rdar://47240768
2020-07-21 12:33:14 -07:00
Michael Gottesman
d2f986739c Merge pull request #32940 from gottesmm/pr-8414ddbc369ed5bb8c0622162de920b962426cf1
[ast] Rename VarPattern -> BindingPattern.
2020-07-18 20:47:21 -07:00
Michael Gottesman
092edd6621 [ast] Rename VarPattern -> BindingPattern.
VarPattern is today used to implement both 'let' and 'var' pattern bindings, so
today is already misleading. The reason why the name Var was chosen was done b/c
it is meant to represent a pattern that performs 'variable binding'. Given that
I am going to add a new 'inout' pattern binding to this, it makes sense to
give it now a better fitting name before I make things more confusing.
2020-07-16 18:56:01 -07:00
Brent Royal-Gordon
25ff82fb7d Loosen default argument mismatch diagnostics
This change makes:

* #file compatible with #fileID in “Swift 6 mode”
* #file compatible with #filePath and #fileID in Swift 5 mode
* #file in Swift 5 mode code compatible with #file in “Swift 6 mode” code

This should keep anyone from seeing XCTAssert-wrapping noise until they adopt “Swift 6 mode” (whatever version that ends up actually being).
2020-07-13 14:06:55 -07:00
Brent Royal-Gordon
acc01aea03 [NFC] Add MagicIdentifierKinds.def
Extracts the list of magic identifier literal kinds into a separate file and updates a lot of code to use macro metaprogramming instead of naming half a dozen cases manually. This is a complicated change, but it should be NFC.
2020-07-13 14:05:13 -07:00
Robert Widmann
e1015761fb [NFC] Reduce usage of TypedPattern::getTypeLoc 2020-06-10 16:51:10 -07:00
Arnold Schwaighofer
825a2a259b Mark non-foreign entry points of @objc dynamic methods in generic classes dynamically_replaceable
```
class Generic<T> {
  @objc dynamic func method() {}
}

extension Generic {
  @_dynamicReplacement(for:method())
  func replacement() {}
}
```

The standard mechanism of using Objective-C categories for dynamically
replacing @objc methods in generic classes does not work.

Instead we mark the native entry point as replaceable.

Because this affects all @objc methods in generic classes (whether there
is a replacement or not) by making the native entry point
`[dynamically_replaceable]` (regardless of optimization mode) we guard this by
the -enable-implicit-dynamic flag because we are late in the release cycle.

* Replace isNativeDynamic and isObjcDynamic by calls to shouldUse*Dispatch and
  shouldUse*Replacement
  This disambiguates between which dispatch method we should use at call
  sites and how these methods should  implement dynamic function
  replacement.

* Don't emit the method entry for @_dynamicReplacement(for:) of generic class
  methods
  There is not way to call this entry point since we can't generate an
  objective-c category for generic classes.

rdar://63679357
2020-06-09 09:23:29 -07:00
Doug Gregor
a115ad9f5c [AST] Record "separately checked" in ClosureExpr.
Reverse the polarity of the "checked in context" bit for ClosureExpr
to "separately checked", which simplifies the AST walker logic (to
"should we walk separately type-checked closure bodies?") and
eliminates single-expression closures as a separate case to consider.
2020-06-05 00:05:11 -07:00
Doug Gregor
a71203d2f4 [Sema] Switch to wasTypeCheckedInEnclosingContext() for walks 2020-06-04 23:15:07 -07:00
Doug Gregor
690d6c446b [AST] Record whether a closure was type-checked in its enclosing expression
Rather than using various "applied function builder" and "is single
expression body" checks to determine whether a closure was
type-checked in its enclosing expression, record in the closure
expression whether it actually *was* type-checked as part of its
enclosing expression.
2020-06-03 22:26:20 -07:00
Anthony Latsis
9fd1aa5d59 [NFC] Pre- increment and decrement where possible 2020-06-01 15:39:29 +03:00
Owen Voorhees
dac96989a4 Fix comparisons between presumed and buffer line numbers 2020-05-26 08:02:33 -07:00
Owen Voorhees
45bc578ae5 [SourceManager] Rename line and column APIs for clarity 2020-05-21 12:54:07 -05:00
Robert Widmann
31242bc3da Remove The Parser Hack For If-Let
The parser used to rewrite

if let x: T

into

if let x: T?

This transformation is correct at face value, but relied on being able
to construct TypeReprs with bogus source locations. Instead of having
the parser kick semantic analysis into shape, let's perform this
reinterpretation when we resolve if-let patterns in statement
conditions.
2020-05-13 12:34:24 -07:00
Hamish Knight
12b9426312 [Sema] Don't diagnose DynamicSubscriptExpr's arg as tuple
Register DynamicSubscriptExpr's index expression
as a call argument in MiscDiagnostics to avoid it
being diagnosed as a single element labelled tuple.

Resolves SR-12799.
2020-05-12 21:23:33 -07:00
Anthony Latsis
74252028ca AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl 2020-04-23 05:16:55 +03:00
Michael Forster
fae87c96d7 Move interleave(...) to the llvm namespace
This simplifies fixing the master-next build. Upstream LLVM already
has a copy of this function, so on master-next we only need to delete
the Swift copy, reducing the potential for merge conflicts.
2020-04-17 11:20:50 +02:00
Ravi Kandhadai
4264b39b23 Merge pull request #26969 from ravikandhadai/oslog-user-model
[Sema Diagnostics] Add Miscellaneous Sema diagnostics to enforce that the new os log APIs and atomics are passed constants for certain arguments
2020-04-07 13:23:10 -07:00
Ravi Kandhadai
b57a1d7c0e [Sema] Add miscellaneous sema diagnostics to check that the new os log
APIs and atomic operations are passed compile-time constants for
certain arguments.
2020-04-06 18:41:13 -07:00
Owen Voorhees
43e2d107e1 [SE-0276] Implement multi-pattern catch clauses
Like switch cases, a catch clause may now include a comma-
separated list of patterns. The body will be executed if any
one of those patterns is matched.

This patch replaces `CatchStmt` with `CaseStmt` as the children
of `DoCatchStmt` in the AST. This necessitates a number of changes
throughout the compiler, including:
- Parser & libsyntax support for the new syntax and AST structure
- Typechecking of multi-pattern catches, including those which
  contain bindings.
- SILGen support
- Code completion updates
- Profiler updates
- Name lookup changes
2020-04-04 09:28:26 -07:00