Commit Graph

933 Commits

Author SHA1 Message Date
Rintaro Ishizaki
d4db99ce9d [Parse] Remove unnecessary dependencies to Parser.h
C++ swift::Parser is going to be replaced with SwiftParser+ASTGen.
Direct dependencies to it should be removed. Before that, remove
unnecessary '#include "swift/Parse/Parser.h"' to clarify what actually
depends on 'swift::Parser'.

Split 'swift::parseDeclName()' et al. into the dedicated files.
2024-11-02 01:23:59 -07:00
Hamish Knight
2d7500eda6 [AST] Remove ParenType
Today ParenType is used:

1. As the type of ParenExpr
2. As the payload type of an unlabeled single
   associated value enum case (and the type of
   ParenPattern).
3. As the type for an `(X)` TypeRepr

For 1, this leads to some odd behavior, e.g the
type of `(5.0 * 5).squareRoot()` is `(Double)`. For
2, we should be checking the arity of the enum case
constructor parameters and the presence of
ParenPattern respectively. Eventually we ought to
consider replacing Paren/TuplePattern with a
PatternList node, similar to ArgumentList.

3 is one case where it could be argued that there's
some utility in preserving the sugar of the type
that the user wrote. However it's really not clear
to me that this is particularly desirable since a
bunch of diagnostic logic is already stripping
ParenTypes. In cases where we care about how the
type was written in source, we really ought to be
consulting the TypeRepr.
2024-10-31 11:32:40 +00:00
Nate Chandler
091368ba21 [CoroutineAccessors] Added read.
The name is a placeholder for the mutating single-yield coroutine
accessor.
2024-09-26 18:10:39 -07:00
Nate Chandler
98a2e6a7df [CoroutineAccessors] Added modify.
The name is a placeholder for the mutating single-yield coroutine
accessor.
2024-09-26 18:10:39 -07:00
Kavon Farvardin
df73eecdeb Sema: generalize findSyntacticErrorForConsume
Since this function is being called from the constraint solver now, we
need to generalize the way it obtains the Type of an Expression, as the
expression itself may not know its own type, only the solver does.

resolves rdar://134371893 / https://github.com/swiftlang/swift/issues/75999
2024-09-24 13:59:29 -07:00
Hamish Knight
e8ef77d307 [Sema] NFC: Elaborate on a comment 2024-09-17 12:35:11 +01:00
Allan Shortlidge
c868378d96 ConstraintSystem: Use scoring to implement MemberImportVisibility.
Previously, the constraint solver would first attempt member lookup that
excluded members from transitively imported modules. If there were no viable
candidates, it would perform a second lookup that included the previously
excluded members, treating any candidates as unviable. This meant that if the
member reference did resolve to one of the unviable candidates the resulting
AST would be broken, which could cause unwanted knock-on diagnostics.

Now, members from transitively imported modules are always returned in the set
of viable candidates. However, scoring will always prioritize candidates from
directly imported modules over members from transitive imports. This solves the
ambiguities that `MemberImportVisibility` is designed to prevent. If the only
viable candidates are from transitively imported modules, though, then the
reference will be resolved successfully and diagnosed later in
`MiscDiagnostics.cpp`. The resulting AST will not contain any errors, which
ensures that necessary access levels can be computed correctly for the imports
suggested by `MemberImportVisibility` fix-its.

Resolves rdar://126637855.
2024-09-10 09:47:42 -07:00
Doug Gregor
42809af51e Merge pull request #76327 from DougGregor/suppress-warnings-in-inactive-regions-without-ifconfigdecl
Suppress certain warnings in inactive `#if` regions without relying on `IfConfigDecl`
2024-09-07 10:16:20 -07:00
Doug Gregor
4a4c0f68f1 Teach the "unused variable / value" diagnostics to use SwiftIfConfig
Rather than walking into the inactive regions of IfConfigDecls looking for
references to a declaration before we diagnose it, go to the syntax
tree and look through inactive *and unparsed* regions for identifier
tokens that match. If we find one, suppress the diagnostic.

This reduces our dependency on IfConfigDecl in the AST, and also makes
the same suppression work with code in unparsed regions that had no
representation in IfConfigDecl.
2024-09-06 22:14:52 -07:00
Hamish Knight
1caaeeb83e Merge pull request #76295 from hamishknight/capturent
Remove `shouldWalkCaptureInitializerExpressions`
2024-09-06 21:39:51 +01:00
Alejandro Alonso
45d7ea39a5 Merge pull request #75518 from Azoy/integer-generics
Implement Value generics
2024-09-05 15:33:46 -07:00
Hamish Knight
f5557c4c10 Remove shouldWalkCaptureInitializerExpressions
There doesn't seem to be any reason we can't
walk the PatternBindingDecls directly here.
2024-09-05 22:27:15 +01:00
Slava Pestov
1ff1b9479a AST: Pick off some usages of GenericTypeParamType::getDecl() 2024-09-04 15:13:46 -07:00
Alejandro Alonso
75c2cbf593 Implement value generics
Some requirement machine work

Rename requirement to Value

Rename more things to Value

Fix integer checking for requirement

some docs and parser changes

Minor fixes
2024-09-04 15:13:25 -07:00
Hamish Knight
9bd0d9b7e4 [Sema] Walk separately-checked closures normally in MiscDiagnostics
We no longer need to make any special affordances
for separately-checked closures, walk them normally.
2024-08-31 12:47:46 +01:00
Hamish Knight
cbeb4bfbc8 [Sema] Walk patterns for syntactic diagnostics
Use `SyntacticElementTarget::walk`, ensuring we
walk to any ExprPatterns.
2024-08-31 12:47:46 +01:00
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