Commit Graph

219 Commits

Author SHA1 Message Date
Hamish Knight
45065f3069 [Diags] Allow multiple in-flight diagnostics
Lift the limitation of a single active diagnostic, which was a pretty
easy-to-hit footgun. We now maintain an array of active in-flight
diagnostics, which gets flushed once all them have ended.
2025-08-17 11:48:21 +01:00
Slava Pestov
4dc0db31f9 Sema: Improve the 'protocol method can't impose new requirements on Self' check
This check had two problems. First, it would assert upon encountering
a layout requirement, due to an unimplemented code path.

A more fundamental issue is that the logic wasn't fully sound, because
it would miss certain cases, for example:

    protocol P {
      associatedtype A

      func run<B: Equatable>(_: B) where B == Self.A
    }

Here, the reduced type of `Self.A` is `B`, and at first glance, the
requirement `B: Equatable` appears to be fine. However, this
is actually a new requirement on `Self`, and the protocol be rejected.

Now that we can change the reduction order by assigning weights to
generic parameters, this check can be implemented in a better way,
by building a new generic signature first, where all generic
parameters introduced by the protocol method, like 'B' above, are
assigned a non-zero weight.

With this reduction order, any type that is equivalent to
a member type of `Self` will have a reduced type rooted in `Self`,
at which point the previous syntactic check becomes sound.

Since this may cause us to reject code we accepted previously,
the type checker now performs the check once: first on the original
signature, which may miss certain cases, and then again on the new
signature built with the weighted reduction order.

If the first check fails, we diagnose an error. If the second
check fails, we only diagnose a warning.

However, this warning will become an error soon, and it really
can cause compiler crashes and miscompiles to have a malformed
protocol like this.

Fixes rdar://116938972.
2025-07-07 12:23:58 -04:00
Hamish Knight
a18f72c29a Constify DiagnosticState::determineBehavior
Split out the state mutation into a new `updateFor`
function that we call for diagnostic emission, allowing
`DiagnosticTransaction::hasErrors` to query the behavior without
mutating any state.
2025-07-02 17:46:10 +01:00
Allan Shortlidge
d258fa783b AST: Simplify the interface of DiagnosticEngine::getBestAddImportFixItLoc().
For clarity, it should just take a `SourceFile`.
2025-05-30 15:34:08 -07:00
Hamish Knight
14bd41159e NFC: Abstract away the use of '7' to represent the next language mode
Introduce `Version::getFutureMajorLanguageVersion` to make it easier
to find clients that will need to be updated once we have a new
language mode.
2025-04-16 19:22:52 +01:00
Anthony Latsis
08d46d2542 Merge pull request #80473 from AnthonyLatsis/diag_type_attr
DiagnosticEngine: Support `TypeAttribute` diagnostic arguments
2025-04-05 12:25:45 +01:00
Anthony Latsis
dafad5b33c [NFC] Factor out DiagnosticArgument into a new file 2025-04-03 00:50:27 +01:00
Anthony Latsis
148c1773a5 DiagnosticEngine: Always favor the category of a wrapped diagnostic 2025-04-02 06:45:15 +01:00
Becca Royal-Gordon
3baba0b262 Type check @abi decls (sans attrs)
This commit compares the decl inside the `@abi` attribute to the decl it’s attached to, diagnosing ABI-incompatible differences. It does not yet cover attributes, which are a large undertaking.
2025-03-26 10:47:13 -07:00
Anthony Latsis
77e673a723 DiagnosticEngine: Print the ID of the wrapped, not wrapper, diagnostic 2025-03-25 02:25:39 +00:00
Anthony Latsis
c49947579a Sema: Implement adoption mode for AsyncCallerExecution 2025-03-18 01:58:31 +00:00
Allan Shortlidge
1409cb1a30 AST: Teach DiagnosticEngine to accept AvailabilityRange arguments. 2025-03-04 19:41:04 -08:00
Gabor Horvath
d0c167762c [cxx-importer] Do not import pointers to non-escapable types
Unfortunately, Unsafe*Pointer types do not support non-escapable
pointees so we do not really have anything to map these types to at the
moment. Previously, importing such code resulted in crashes.

rdar://145800679
2025-03-04 12:27:34 +00:00
Allan Shortlidge
272b2b43de AST: Accept AvailabilityDomains as diagnostic arguments.
This simplifies the code to emit availabilty diagnostics and ensures that they
display domain names consistently. While updating existing diagnostics, improve
consistency along other dimensions as well.
2025-02-28 09:18:38 -08:00
Doug Gregor
b9de052ef1 Stop including DiagnosticGroups.h everywhere, so changes are less painful 2025-01-10 09:10:10 -08:00
Anthony Latsis
c4793b6315 DiagnosticEngine: Fix a Diagnostic ctor that skipped group computation 2025-01-08 20:30:09 +00:00
Anthony Latsis
28ec957132 [NFC] Forward-declare DiagGroupID instead of including 2025-01-07 20:29:07 +00:00
Anthony Latsis
901d566e78 DiagnosticEngine: Fix diagnostic groups behavior for wrapped diagnostics 2025-01-07 20:29:07 +00:00
Dmitrii Galimzianov
d56b7df8a9 Add DiagGroupID to Diagnostic
This change addresses the following issue: when an error is being wrapped in a warning, the diagnostic message will use the wrapper's `DiagGroupID` as the warning's name. However, we want to retain the original error's group for use. For example, in Swift 5, async_unavailable_decl is wrapped in error_in_future_swift_version. When we print a diagnostic of this kind, we want to keep the `DiagGroupID` of `async_unavailable_decl`, not that of `error_in_future_swift_version`.
To achieve this, we add `DiagGroupID` to the `Diagnostic` class. When an active diagnostic is wrapped in DiagnosticEngine, we retain the original `DiagGroupID`.

For illustration purposes, this change also introduces a new group: `DeclarationUnavailableFromAsynchronousContext`.

With this change, we produce errors and warnings of this kind with messages like the following:

```
global function 'fNoAsync' is unavailable from asynchronous contexts [DeclarationUnavailableFromAsynchronousContext]
global function 'fNoAsync' is unavailable from asynchronous contexts; this is an error in the Swift 6 language mode [DeclarationUnavailableFromAsynchronousContext]
```
2024-12-03 20:12:11 +01:00
Holly Borla
d2d317a3d7 [Concurrency] Fix preconcurrency downgrade behavior for Sendable closures.
Sendable violations inside `@preconcurrency @Sendable` closures should be
suppressed in minimal checking, and diagnosed as warnings under complete
checking, including the Swift 6 language mode.
2024-11-07 16:02:37 -08:00
Hamish Knight
9d4a78678a [Sema] Add logic to diagnose regex feature availability
Add the necessary compiler-side logic to allow
the regex parsing library to hand back a set of
features for a regex literal, which can then be
diagnosed by ExprAvailabilityWalker if the
availability context isn't sufficient. No tests
as this only adds the necessary infrastructure,
we don't yet hand back the features from the regex
parsing library.
2024-10-28 17:09:47 +00:00
Doug Gregor
5df96a7a6e Turn pretty-printing of a declaration into a request
The diagnostics engine has some code to pretty-print a declaration when
there is no source location for that declaration. The declaration is
pretty-printed into a source buffer, and a source location into that
buffer is synthesizes. This applies to synthesized declarations as well
as those imported from Swift modules (without source code) or from Clang.

Reimplement this pretty-printing for declarations as a request. In
doing so, change the manner in which we do the printing: the
diagnostics engine printed the entire enclosing type into a buffer
whose name was the module + that type. This meant that the buffer was
shared by every member of that type, but also meant that we would end
up deserializing a lot of declarations just for printing and
potentially doing a lot more work for these diagnostics.
2024-10-01 15:49:15 -07:00
Doug Gregor
b272a05ea9 Merge pull request #76363 from DmT021/wp/print-diagnostic-groups
[Diagnostics] Add -print-diagnostic-groups flag
2024-09-11 13:04:07 -07:00
Dmitrii Galimzianov
a8b71ea97f Add -print-diagnostic-groups flag
This change adds the `-print-diagnostic-groups` flag as described by SE-0443.
2024-09-11 13:34:42 +02:00
Dmitrii Galimzianov
9e64b873c3 [Diagnostics] Fix typos and flag names 2024-09-10 21:11:36 +02:00
Doug Gregor
08e339b7b4 Merge pull request #74466 from DmT021/wp/no-warning-as-error
[Diagnostics] Add -no-warning-as-error to except a specific warning from being treated as an error
2024-09-09 09:35:05 -07:00
Dmitrii Galimzianov
28883b6654 [Diagnostics] Add -[no-]warning-as-error flags for precise control over warning behavior
This commit adds new compiler options -no-warning-as-error/-warning-as-error which allows users to specify behavior for exact warnings and warning groups.
2024-09-07 01:14:43 +02:00
Michael Gottesman
f075e4eb28 Change DiagnosticBehavior into a struct enum so we can put methods on it.
The reason why I am making this change is because I want to put a merge
operation on DiagnosticBehavior. This merge operation allows for
DiagnosticBehavior to work like a lattice. When one merges, one moves
potentially from fatal, error to things like note, ignore.
2024-08-22 15:59:04 -04:00
Egor Zhdan
bfe72b4be9 Merge pull request #75589 from swiftlang/egorzhdan/linux-libcxx-interop
[cxx-interop] Allow compiling with libc++ on Linux
2024-08-09 13:42:29 +01:00
Egor Zhdan
059f0f97d1 [cxx-interop] Allow compiling with libc++ on Linux
This makes sure that Swift respects `-Xcc -stdlib=libc++` flags.

Clang already has existing logic to discover the system-wide libc++ installation on Linux. We rely on that logic here.

Importing a Swift module that was built with a different C++ stdlib is not supported and emits an error.

The Cxx module can be imported when compiling with any C++ stdlib. The synthesized conformances, e.g. to CxxRandomAccessCollection also work. However, CxxStdlib currently cannot be imported when compiling with libc++, since on Linux it refers to symbols from libstdc++ which have different mangled names in libc++.

rdar://118357548 / https://github.com/swiftlang/swift/issues/69825
2024-08-08 16:24:58 +01:00
Holly Borla
5fa35b5c3e [Concurrency] Compute the source of actor isolation in ActorIsolationRequest. 2024-08-04 18:48:57 -07:00
Steven Wu
30737eb396 [Caching] Support swift style diagnostics for swift caching
Add support for swift style diagnostics for swift caching. This includes
pre-populate the GeneratedSourceInfo with macro name so it doesn't need
to infer from an ASTNode, which the caching mechanism cannot preserve.

Still leave the default diagnostic style to LLVM style because replaying
swift style diagnostics is still very slow and including parsing source
file using swift-syntax.

rdar://128615572
2024-07-08 16:15:44 -07:00
Konrad `ktoso` Malawski
54229549b3 [Distributed] Offer fixit for import Distributed when it is required (#72948) 2024-04-12 18:05:32 -07:00
John McCall
976f1494f5 [NFC] Introduce DiagRef and use it throughout the parser
The goal is to have a lightweight way to pass an unapplied
diagnostic to general routines.  Constructing a Diagnostic
is quite expensive as something we're potentially doing in
hot paths, as opposed to just when we're actually emitting
the diagnostic.  This design allows the expense to be delayed
until we need it.

I've also optimized the Diagnostic constructor to avoid
copying arguments unnecessarily; this is a relatively small
expense, since arguments are POD, but there's really no good
reason not to do it.
2024-03-01 22:09:47 -05: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
d9c82b1b04 Sema: Clean up printing of generic signatures in missing witness diagnostics
Instead of stripping out requirements, let's pass the right PrintOptions.
2024-02-05 17:13:02 -05:00
Rintaro Ishizaki
b839718351 [AST] Use scoped enum for attribute kinds
Align with other kind enum e.g. DeclKind.
2024-02-02 09:36:48 -08:00
Holly Borla
11cb94faa5 [Concurrency] Use .limitBehaviorUntilSwiftVersion for concurrency diagnostics
that are suppressed or downgraded until Swift 6.

There are a few benefits to using a `UntilSwiftVersion` diagnostic engine API,
including the diagnostic wrapping to communicate that the mistake will be an
error in Swift 6, and to include the mistake in the frontend statistic for
Swift 6 errors.
2024-01-31 17:08:01 -08:00
Holly Borla
50945efe6d [Diagnostics] Add InFlightDiagnostic::limitBehaviorUntilSwiftVersion to
downgrade or suppress errors until a specified language version.
2024-01-30 19:50:32 -08:00
Holly Borla
32038d9261 [Statistics] Add a frontend counter for Swift 6 errors diagnosed via
`warnUntilSwiftVersion(6)`.
2024-01-29 19:58:48 -08:00
Sophia Poirier
e8945ac193 [Concurrency] @preconcurrency import downgrades globals concurrency errors to warnings 2024-01-05 16:35:02 -08:00
Kavon Farvardin
b55c4f1fc9 [nfc] allow ErrorTypeRepr to store a ZeroArgDiagnostic
There are sometimes parsing stuations where we don't want to
emit a parsing error, because of feature guarding. For
example, if a Feature involves new syntax for a type, we
must be able to parse both the true and false sides of an
ifdef guarding that new syntax based on a Feature flag.
2023-11-13 15:49:46 -08:00
Becca Royal-Gordon
fe6753485f [NFC] Adopt new diagnostic features across Sema 2023-07-20 15:23:47 -07:00
Becca Royal-Gordon
325ab9118e [NFC] Adopt new features in availability diagnostics
Allows the removal of a helper function.
2023-07-19 13:08:12 -07:00
Becca Royal-Gordon
2ab1e05c68 [NFC] Add new DiagnosticEngine Decl features
Implements several enhancements to DiagnosticEngine’s handling of Decl arguments:

• All Decl classes, not just ValueDecls, are now valid to use as arguments.
• There is built-in logic to handle accessors by printing a phrase like `getter for 'foo'`, so this no longer has to be special-cased for each diagnostic.
• `%kind` can be used to insert the descriptive kind before a name; `%kindonly` inserts only the descriptive kind. This can eliminate kind/name argument pairs.
• `%base` can insert only the base name, leaving out any argument labels; `%kindbase` combines `%kind` and `%base`.

This PR is marked NFC because there are no intentional uses of these changes yet.
2023-07-19 13:06:51 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Hamish Knight
8e66001174 Merge pull request #66676 from hamishknight/interface-error 2023-06-16 10:24:20 +01:00
Hamish Knight
a9f9bd1394 [SourceKit] Record module loading errors when generating interfaces
Record up to two errors emitted when we fail to
load a module for interface generation, and include
these errors in the message we pass back to the
editor. This should help us better pin down the
reason why interface generation failed.

rdar://109511099
2023-06-15 23:55:07 +01:00
Allan Shortlidge
a971a0c980 AST: Add convenience for limiting diags to warnings in swiftinterfaces.
Sometimes it's useful to be more lenient when type checking swiftinterfaces
since restrictions that could be dropped in the future will manifest in
resilient libraries being incompatible with older compilers otherwise.
2023-06-15 11:17:34 -07:00
Alexis Laferrière
2f03c952c3 [Serialization] ModularizationError keeps full objects
Preserve more information about the context of modularization errors by
replacing the module names with pointers to ModuleDecl and ModuleFile
objects.
2023-06-07 13:01:37 -07:00