Commit Graph

917 Commits

Author SHA1 Message Date
Hamish Knight
89a3390aa1 [Sema] Move SingleValueStmtUsageChecker into pre-checking
These diagnostics are better suited for pre-checking
since we ought to be emitting them even if there's
some other error with the expression.
2024-08-30 18:55:48 +01:00
Slava Pestov
ae77d6f0c1 AST: Replace one-off predicates with SubstitutionMap::getRecursiveProperties() 2024-08-21 13:19:10 -04:00
Hamish Knight
769805e714 [Sema] Fully allow fallthrough in switch expressions
Allow `fallthrough` to appear as the last statement
in the case of a `switch` expression. We already
allowed it in other positions, this was just an
oversight.

rdar://127670432
2024-08-14 19:59:05 +01:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Kavon Farvardin
e04b35d709 Consume: warn about no-op consumes to be fixed
As of now, SE-366 is not correctly implemented with respect to concrete,
bitwise-copyable types like `Int`. Writing `consume someInt` doesn't
actually consume the variable binding as it should, meaning code that
should be flagged as having a use-after-consume is being silently
permitted:

```swift
let someInt = 10
let y = consume someInt
print(someInt) // no error!
```

This has been a problem since Swift 5.9. Eventually we plan to fix this
issue, which means code previously doing the above would become an
error. To help people get ready for the fix, start warning people that
these consumes are actually no-ops and suggest removing them until the
intended behavior is actually enforced in the future.

resolves rdar://127081103
2024-07-24 16:05:47 -07:00
Kavon Farvardin
6e35711bb4 Noncopyable: prevent keypaths from accessing
We don't yet have keypaths working correctly to allow access to
noncopyable fields, so we should raise a friendly error in Sema rather
than an error-out elsewhere vaguely.

resolves rdar://109287447
2024-07-22 21:22:05 -07:00
Slava Pestov
ab0ffe395b Merge pull request #74998 from slavapestov/global-conformance-lookup
AST: Stop pretending that conformance lookup takes the module into account
2024-07-06 17:26:46 -04:00
Slava Pestov
3fcda140bb AST: ModuleDecl::checkConformance() is a static method 2024-07-06 12:05:46 -04:00
Hamish Knight
ee5df0041a [Sema] Remove DisableExprAvailabilityChecking 2024-07-06 16:20:21 +01:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Hamish Knight
fe272e6cdd [Sema] Restore 5.10 implicit self behavior prior to Swift 6 mode
Unfortunately we've encountered another source
breaking case here:

```
class C {
  func method() {}

  func foo() {
    Task { [weak self] in
      Task {
        method()
      }
    }
  }
}
```

In 5.10 we'd only do the unqualified lookup for
`self` when directly in a `weak self` closure,
but with the implicit self rework, we'd start
using the `weak self` here, leading to a
type-checker error.

At this point, adding more edge cases to the
existing logic is going to make things much more
complicated. Instead, reinstate the 5.10 implicit
self lookup behavior and diagnostic logic,
switching over to the new logic only under Swift 6
mode.

rdar://129475277
2024-06-10 20:29:16 +01:00
Hamish Knight
4a49e65368 [Sema] NFC: Replace a couple of closure parameters with ASTContext
Make it a bit clearer which function actually cares
about the closure it's handed.
2024-06-10 20:29:16 +01:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Kavon Farvardin
02e0770834 Merge pull request #73916 from kavon/ncgenerics-implicit-wrapping-127450418
ConstraintSystem: clarify consuming conversions
2024-06-05 13:16:49 -07:00
Kavon Farvardin
1f830177ac NFC: refactor checkConsumeExpr
I want to expose the syntactic checking to other parts of Sema, so that
we can determine whether an expression is a candidate for having a
`consume` added to it.
2024-06-03 10:47:15 -07:00
Hamish Knight
21c0247eb2 [Sema] Preserve compatibility for weak self with @_implicitSelfCapture
In 5.10 we warned on this:

```swift
func bar(@_implicitSelfCapture _ fn: @escaping () -> Void) {}

class C {
  func foo() {
    bar { [weak self] in
      foo()
    }
  }
}
```

But with the implicit self rework, this accidentally
became an error. Fix it to ensure we continue to
warn until Swift 6 mode.

rdar://128941797
2024-05-29 16:11:38 +01:00
Doug Gregor
d4ce618e2f Provide more Fix-It guidance for concurrency-unsafe global variables (SE-0412)
When diagnosing a concurrency-unsafe global or static variable, provide
Fix-Its with specific guidance and advice. This is intended to aid the
workflow for folks enabling strict concurrency checking or Swift 6.
There are up to three Fix-Its attached to a diagnostic about
concurrency-unsafe global/static variables:

* convert 'global' to a 'let' constant to make the shared state
immutable, which replaces `var` with `let`
* restrict 'global' to the main actor if it will only be accessed from the
main thread, which adds `@MainActor`
* unsafely mark %0 as concurrency-safe if all accesses are protected
by an external synchronization mechanism, which adds `nonisolated(unsafe)`

I fretted over two things before deciding on this path:

1. For the second note, the reality is that any global actor will
suffice, but `@MainActor` is orders of magnitude more common than any
other global actor, so "common case convenience" wins over "precise
but less useful.
2. For the third note, `nonisolated(unsafe)` should only be used
sparingly, and surfacing it via Fix-It could cause overuse. However,
developers need to know about it, and this is how we do that. It comes
last in the list of notes (after the better options) and says "unsafe"
in not one but two places.
2024-05-19 15:05:39 -07:00
Joe Groff
10f9289ef8 Enable 'BorrowingSwitch' feature. 2024-05-14 07:31:59 -07:00
Cal Stephens
da144f71ad Fix outdated usage of isKnownNoEscape 2024-05-04 14:08:43 -07:00
Cal Stephens
6b36f78f67 Remove unused validateOutermostCapture param 2024-05-04 12:37:43 -07:00
Cal Stephens
b525b9c7da Merge tag 'swift-DEVELOPMENT-SNAPSHOT-2024-05-01-a' of github.com:apple/swift into cal--fix-70089
Tag build swift-DEVELOPMENT-SNAPSHOT-2024-05-01-a
2024-05-04 12:19:16 -07:00
Cal Stephens
7d897fd383 Fix validation for async let thunks 2024-05-04 11:25:24 -07:00
Cal Stephens
d17bc35a93 Refactor and simplify shouldOnlyWarn logic 2024-05-04 10:10:53 -07:00
Cal Stephens
7e95f9b15a Fix unexpected error by simplifying outermost capture validation 2024-05-04 09:25:24 -07:00
Slava Pestov
c11beb5d2f Sema: Make wonky logic explicit in diagnoseImplicitSelfUseInClosure()
We walk up the DeclContext hierarchy looking for closures, but they might
not have had their types set yet because they're currently being type
checked.
2024-04-30 16:10:36 -04:00
Cal Stephens
e4e80787ac Simplify autoclosure check in shouldOnlyWarn 2024-04-25 07:36:32 -07:00
Cal Stephens
985e8d9941 Update DeclContext::getInnermostClosureForSelfCapture to return a ClosureExpr 2024-04-25 06:44:54 -07:00
Cal Stephens
74a33eb420 Improve 'shouldOnlyWarn' logic 2024-04-25 06:26:50 -07:00
Nate Chandler
23e7c36c5f [NoncopyablePartialConsumption] Ungate feature.
Remove all checks for the feature flag.  It's on all the time.
2024-04-19 12:37:34 -07:00
Cal Stephens
3fcc5a8f46 Always validate self capture 2024-03-31 19:50:44 -07:00
Pavel Yaskevich
6a5bd1042a Merge pull request #72527 from xedin/rdar-119593407-alternative
[TypeChecker] InferSendableFromCaptures: Infer `@Sendable` on adjusted types
2024-03-29 09:54:50 -07:00
Konrad `ktoso` Malawski
6132386371 [Distributed] Complete handling of protocol calls and witnesses using adjusted mangling scheme (#72416) 2024-03-23 23:54:23 +09:00
Pavel Yaskevich
64a200ab07 [MiscDiagnostics] Make sure that diagnostics look through function conversions
None of the diagnostics in MiscDiagnostics make a distinction between
sendable or actor-isolated function references which means that we can
skip conversions while reaching for a declaration used in an application.
2024-03-21 15:35:03 -07:00
Cal Stephens
3315cab336 Merge branch 'main' into cal--fix-70089 2024-03-18 16:44:09 -07:00
Kavon Farvardin
149c052ec5 use new noncopyable types infrastructure
The infrastructure underpinning the new feature NoncopyableGenerics is
mature enough to be used.
2024-03-14 23:10:44 -07:00
Cal Stephens
97178b2494 Look up the outer self decl via the capture's parent initializer instead of ASTScope::lookupSingleLocalDecl 2024-03-13 20:18:48 -07:00
Pavel Yaskevich
93fabf05ea Merge pull request #72239 from xedin/infersendablefromcaptures-fixes
[Sema] A few minor fixes for `InferSendableFromCaptures` feature
2024-03-13 14:12:01 -07:00
Kavon Farvardin
01864d9af4 Sema: replace legacy Copyable containment check
We can use part of the new infrastructure if we simply handle abstract
conformances to Copyable, which is what we'd get upon lookup for a
nominal in the old world. This means that we can merge diagnostics for
the containment test together, and fix differences with deinit
diagnostics.
2024-03-12 11:05:25 -07:00
Cal Stephens
f21aee7baa Improve quality of 'capture self explicitly to enable implicit self in this closure' and 'variable other than self captured here under the name self does not enable implicit self' diagnostics 2024-03-12 10:45:17 -07:00
Pavel Yaskevich
3376d03079 [MiscDiagnostics] KeyPath KVO diagnostics should handle existential opening
Sendable key path literals are represented as an existential
protocol composition with `Sendable` protocol which has to be
opened in certain scenarios i.e. to pass it to non-Sendable version.
2024-03-11 13:48:32 -07:00
Cal Stephens
94dcf9bc70 Fix edge cases related to nested autoclosures, invalid weak self unwrapping 2024-03-11 07:42:44 -07:00
Cal Stephens
8c5f783f5b [SE-0365] Fix issue where implicit self was unexpectedly disallowed in nested closures 2024-03-10 07:57:51 -07:00
Nate Chandler
74ab0f616c [NoncopyablePartialConsumption] Hone diagnostics.
Only noncopyable fields of noncopyable aggregates may be partially
consumed with the `consume` keyword.
2024-03-07 13:27:40 -08:00
Nate Chandler
fb0ae6ecbb [NoncopyablePartialConsumption] Allow consume.
Allow a noncopyable value to be partially consumed via the `consume`
keyword.
2024-03-06 18:58:38 -08:00
Anthony Latsis
5b7a8b6705 [NFC] AST: Rename MemberTypeReprQualifiedIdentTypeRepr 2024-03-02 14:59:37 +03:00
Anthony Latsis
68746a0579 [NFC] AST: Rename IdentTypeReprUnqualifiedIdentTypeRepr 2024-03-02 08:28:47 +03:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Slava Pestov
1c24b880ec Merge pull request #70467 from AnthonyLatsis/recursive-member-typerepr
AST: Remodel `MemberTypeRepr` to be recursive
2024-02-16 11:36:31 -05:00
Doug Gregor
81ffafdc6a Merge pull request #70602 from ApolloZhu/macro/expression-as-default-argument
[Macros] Expression macro as caller-side default argument
2024-02-14 16:10:11 -08:00
Anthony Latsis
d6fd424560 [NFC] MemberTypeRepr: getBaseComponent()getRoot() 2024-02-09 17:22:05 +03:00