Commit Graph

341 Commits

Author SHA1 Message Date
Hamish Knight
7fb532e4e6 Merge pull request #83776 from hamishknight/the-diags-never-stop-no
[Diags] Allow multiple in-flight diagnostics
2025-09-03 19:59:44 +01:00
Allan Shortlidge
9de88624b2 Sema: Diagnose availability of availability domains in if #available queries.
When emitting statement diagnostics for `if #available` queries, diagnose the
availability of the decls representing the referenced availability domains.
Among other things, this checks that the domains are sufficiently visible to be
used in the containing function body context.
2025-08-26 08:20:12 -07:00
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
Hamish Knight
92e6a00056 [Diags] Avoid infinite recursion on decl printing request cycle
The ASTPrinter may kick requests that may emit diagnostics, make sure
we don't attempt to recursively print the decl since that would lead
to infinite recursion when diagnosing the cycle.
2025-08-17 11:48:21 +01:00
Anthony Latsis
fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01: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
Allan Shortlidge
6ea2dd8fac AST/Sema: Remove code adding @_spi to suggested import fix-its.
The was never invoked because inaccessibility due to SPI protection level is
always diagnosed before missing imports are diagnosed. The functionality could
therefore not be tested and should be removed.
2025-05-30 15:34:08 -07:00
Anthony Latsis
eedc774fca InFlightDiagnostic: Minor improvement to fixItAddAttribute
Do not follow `in` keyword with a space unless the first token inside
the body begins with the next char after `{`.
2025-05-21 12:11:19 +01:00
Anthony Latsis
d90c7cb57a DiagnosticEngine: Fix attribute insertion location for closures with capture lists 2025-05-15 20:17:42 +01: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
d93b6a4e4d DiagnosticEngine: Do not describe an extension's nominal for %kindonly
This is the desired behavior is most cases. In the future, we should
consider adding format specifiers for short/detailed descriptions.
2025-04-05 12:31:19 +01:00
Anthony Latsis
96be6cf6a6 DiagnosticEngine: Do not describe an accessor's storage for %kindonly
This is the desired behavior is most cases. In the future, we should
consider adding format specifiers for short/detailed descriptions.
2025-04-05 12:31:19 +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
99f63ed933 DiagnosticEngine: Support TypeAttribute diagnostic arguments 2025-04-03 01:52:38 +01:00
Anthony Latsis
148c1773a5 DiagnosticEngine: Always favor the category of a wrapped diagnostic 2025-04-02 06:45:15 +01:00
Doug Gregor
b182c96bd7 Print diagnostic group names by default
Print diagnostic groups as part of the LLVM printer in the same manner as the
Swift one does, always. Make `-print-diagnostic-groups` an inert option, since we
always print diagnostic group names with the `[#GroupName]` syntax.

As part of this, we no longer render the diagnostic group name as part
of the diagnostic *text*, instead leaving it up to the diagnostic
renderer to handle the category appropriately. Update all of the tests
that were depending on `-print-diagnostic-groups` putting it into the
text to instead use the `{{documentation-file=<file name>}}`
diagnostic verification syntax.
2025-03-29 15:40:56 -07:00
Doug Gregor
e88f8995e1 [Diagnostics] Eliminate educational notes in favor of diagnostic groups
We've been converging the implementations of educational notes and
diagnostic groups, where both provide category information in
diagnostics (e.g., `[#StrictMemorySafety]`) and corresponding
short-form documentation files. The diagnostic group model is more
useful in a few ways:

* It provides warnings-as-errors control for warnings in the group
* It is easier to associate a diagnostic with a group with
GROUPED_ERROR/GROUPED_WARNING than it is to have a separate diagnostic
ID -> mapping.
* It is easier to see our progress on diagnostic-group coverage
* It provides an easy name to use for diagnostic purposes.

Collapse the educational-notes infrastructure into diagnostic groups,
migrating all of the existing educational notes into new groups.
Simplify the code paths that dealt with multiple educational notes to
have a single, possibly-missing "category documentation URL", which is
how we're treating this.
2025-03-29 15:40:35 -07:00
Anthony Latsis
72c0d4cc75 Diag: Handle CustomAttr in formatDiagnosticArgument 2025-03-28 02:03:38 +00: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
Doug Gregor
0997a7e016 Merge pull request #79688 from DougGregor/diagnostic-ref-footnotes
[Diagnostics] Reference markdown files for educational notes and diagnostic group documentation
2025-03-05 12:41:16 -08:00
Allan Shortlidge
1409cb1a30 AST: Teach DiagnosticEngine to accept AvailabilityRange arguments. 2025-03-04 19:41:04 -08:00
Doug Gregor
db2fa7bcda [Diagnostics] Use the educational note name for the category name 2025-03-04 09:14:48 -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
fc41265d4d Merge pull request #79711 from tshortli/availability-diagnostics
AST/Sema: Adopt `AvailabilityDomain` arguments in diagnostics
2025-02-28 22:30:09 -08: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
aafd24bfed Prefer diagnostic group names for diagnostic categories over the ad hoc ones 2025-02-27 19:40:23 -08:00
Doug Gregor
779acb3d15 [Diagnostics] Record diagnostic groups in serialized diagnostics
The serialized diagnostic format has some extra fields that we can
adopt for diagnostic groups. Specifically:
* Category: store the diagnostic group name here
* Flags: extend the hack used by educational notes of placing Markdown file paths here
2025-02-27 17:26:48 -08:00
Pavel Yaskevich
664c119e30 [DiagnosticEngine] Don't account ignored diagnostics in Sema.NumSwift6Errors statistic
Since the diagnostic is not going to be emitted counting it in
`Swift6Errors` statistics is going to be confusing to the users.

Resolves: https://github.com/swiftlang/swift/issues/79291
Resolves: rdar://145341605
2025-02-24 18:04:55 -08:00
Allan Shortlidge
e019a32122 AST: Introduce and adopt DeclContext::isInSwiftinterface().
Checking whether a declaration is in a `.swiftinterface` is a very common query
that is made somewhat awkward because declarations are not always in source
files. To make these checks more ergonomic, expose a convenience on
DeclContext.
2025-01-27 19:25:41 -08:00
Anthony Latsis
29626c4427 [NFC] Diag: Rename error_in_future_swift_version wrappers
Be clear that these are about language mode.
2025-01-15 23:14:08 +00:00
Doug Gregor
63c9882852 Introduce the notion of a warning that is ignored by default, but enabled by -Wwarning/-Werror
As an example, use this for the "`@preconcurrency` on import has no
effect" warning, which is not yet working correctly. This disables it
by default but leaves it in place for our testing.
2025-01-10 09:10:56 -08:00
Anthony Latsis
64e8976261 [NFC] AST: Define NumDiagIDs instead of redefining the enum 2025-01-09 14:09:46 +00:00
Anthony Latsis
901d566e78 DiagnosticEngine: Fix diagnostic groups behavior for wrapped diagnostics 2025-01-07 20:29:07 +00:00
Slava Pestov
ed5e22ddab AST: Make -Xllvm -swift-diagnostics-assert-on-error and -Xllvm -swift-diagnostics-assert-on-warning work in noassert builds 2024-12-10 17:36:30 -05: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
Doug Gregor
24a12ebc34 Renable GeneratedSourceInfo::Attribute to GeneratedSourceInfo::AttributeFromClang 2024-11-15 09:02:49 -08:00
Doug Gregor
aa4c548c0f Clang importer: switch swift_attr attribute text cache over to be module-sensitive
We need different buffers for each imported module that has swift_attr attributes,
so cache them appropriately.
2024-11-13 21:19:39 -08:00
Doug Gregor
989c73d014 Ensure that buffers containing Clang swift_attr attributes are parsed as attributes
Previously, they were being parsed as top-level code, which would cause
errors because there are no definitions. Introduce a new
GeneratedSourceInfo kind to mark the purpose of these buffers so the
parser can handle them appropriately.
2024-11-13 21:19:37 -08: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
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
Rintaro Ishizaki
c57025af67 Avoid #include AST headers in Basic headers
Resolve a layering violation. `DiagnosticBehavior` was used in
`LangOptions`. Introduce a dedicated 'enum' for the possible values.
2024-09-24 15:18:26 -07:00
Doug Gregor
49aa0e966f Ensure that SourceFiles always have a backing buffer in the SourceManager
The "buffer ID" in a SourceFile, which is used to find the source file's
contents in the SourceManager, has always been optional. However, the
effectively every SourceFile actually does have a buffer ID, and the
vast majority of accesses to this information dereference the optional
without checking.

Update the handful of call sites that provided `nullopt` as the buffer
ID to provide a proper buffer instead. These were mostly unit tests
and testing programs, with a few places that passed a never-empty
optional through to the SourceFile constructor.

Then, remove optionality from the representation and accessors. It is
now the case that every SourceFile has a buffer ID, simplying a bunch
of code.
2024-09-16 21:46:42 -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
Alexis Laferrière
8d28ed4fa9 Merge pull request #76269 from xymus/public-module-name
Diagnostics: Intro the public module name concept to hide support modules from clients
2024-09-09 16:57:12 -07: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