Skip to content

Commit cba4aeb

Browse files
authored
Merge pull request #80392 from DougGregor/diagnostic-group-printing
Print diagnostic group names by default
2 parents e132e1c + c93de89 commit cba4aeb

File tree

61 files changed

+545
-701
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+545
-701
lines changed

Diff for: docs/Diagnostics.md

+17-16
Original file line numberDiff line numberDiff line change
@@ -84,34 +84,35 @@ The correct spelling of this feature is "fix-it" rather than "fixit". In [camelc
8484

8585
[camelcased]: https://en.wikipedia.org/wiki/Camel_case
8686

87-
### Educational Notes ###
87+
### Diagnostic Groups ###
8888

89-
Educational notes are short-form documentation attached to a diagnostic which explain relevant language concepts. They are intended to further Swift's goal of progressive disclosure by providing a learning resource at the point of use when encountering an error message for the first time. In very limited circumstances, they also allow the main diagnostic message to use precise terminology (e.g. nominal types) which would otherwise be too unfriendly for beginners.
89+
Diagnostic groups collect some number of diagnostics together under a common group name with short-form documentation to help explain relevant language concepts. For warnings, diagnostic groups also provide a way to control whether the warnings within that group are escalated to errors or not wit compiler flags such as `-Werror <groupname>` and `-Wwarning <groupname>`. They are intended to further Swift's goal of progressive disclosure by providing a learning resource at the point of use when encountering an error message for the first time. In very limited circumstances, they also allow the main diagnostic message to use precise terminology (e.g. nominal types) which would otherwise be too unfriendly for beginners.
9090

91-
When outputting diagnostics on the command line, educational notes will be printed after the main diagnostic body if enabled using the `-print-educational-notes` driver option. When presented in an IDE, it's expected they will be collapsed under a disclosure arrow, info button, or similar to avoid cluttering output.
91+
When outputting diagnostics on the command line, diagnostic group names will be printed as `[#GroupName]` at the end of the main diagnostic, with a footnote that links to the corresponding documentation. When presented in an IDE, it's expected they will be collapsed under a disclosure arrow, info button, or similar to avoid cluttering output.
9292

93-
Educational notes should:
94-
- Explain a single language concept. This makes them easy to reuse across related diagnostics and helps keep them clear, concise, and easy to understand.
93+
Diagnostic group documentation should:
94+
- Cover a single language concept. This helps keep them clear, concise, and easy to understand.
9595
- Be written in unabbreviated English. These are longer-form messages compared to the main diagnostic, so there's no need to omit needless words and punctuation.
96-
- Not generally exceed 3-4 paragraphs. Educational notes should be clear and easily digestible. Messages which are too long also have the potential to create UX issues on the command line.
97-
- Be accessible. Educational notes should be beginner friendly and avoid assuming unnecessary prior knowledge. The goal is not only to help users understand what a diagnostic is telling them, but also to turn errors and warnings into "teachable moments".
96+
- Not generally exceed 3-4 paragraphs. Diagnostic group documentation should be clear and easily digestible. Messages which are too long also have the potential to create UX issues on the command line.
97+
- Be accessible. Diagnostic group documentation should be beginner friendly and avoid assuming unnecessary prior knowledge. The goal is not only to help users understand what a diagnostic is telling them, but also to turn errors and warnings into "teachable moments".
9898
- Include references to relevant chapters of _The Swift Programming Language_.
9999
- Be written in Markdown, but avoid excessive markup which negatively impacts the terminal UX.
100100

101-
### Quick-Start Guide for Contributing New Educational Notes ###
101+
### Quick-Start Guide for Contributing New Diagnostic Groups ###
102102

103-
Adding new educational notes is a great way to get familiar with the process of contributing to Swift, while also making a big impact!
103+
Adding new diagnostic groups is a great way to get familiar with the process of contributing to Swift, while also making a big impact!
104104

105-
To add a new educational note:
105+
To add a new diagnostics group:
106106
1. Follow the [directions in the README](https://github.com/swiftlang/swift#getting-sources-for-swift-and-related-projects) to checkout the Swift sources locally. Being able to build the Swift compiler is recommended, but not required, when contributing a new note.
107-
2. Identify a diagnostic to write an educational note for. To associate an educational note with a diagnostic name, you'll need to know its internal identifier. The easiest way to do this is to write a small program which triggers the diagnostic, and run it using the `-debug-diagnostic-names` compiler flag. This flag will cause the internal diagnostic identifier to be printed after the diagnostic message in square brackets.
107+
2. Identify a diagnostic to collect into a group. To find the diagnostic, you'll need to know its internal identifier. The easiest way to do this is to write a small program which triggers the diagnostic, and run it using the `-debug-diagnostic-names` compiler flag. This flag will cause the internal diagnostic identifier to be printed after the diagnostic message in square brackets.
108108
3. Find any closely related diagnostics. Sometimes, what appears to be one diagnostic from a user's perspective may have multiple variations internally. After determining a diagnostic's internal identifier, run a search for it in the compiler source. You should find:
109109
- An entry in a `Diagnostics*.def` file describing the diagnostic. If there are any closely related diagnostics the note should also be attached to, they can usually be found nearby.
110110
- Each point in the compiler source where the diagnostic is emitted. This can be helpful in determining the exact circumstances which cause it to be emitted.
111-
4. Add a new Markdown file in the `userdocs/diagnostics/` directory in the swift repository containing the contents of the note. When writing a note, keep the writing guidelines from the section above in mind. The existing notes in the directory are another useful guide.
112-
5. Associate the note with the appropriate diagnostics in `EducationalNotes.def`. An entry like `EDUCATIONAL_NOTES(property_wrapper_failable_init, "property-wrapper-requirements.md")` will associate the note with filename `property-wrapper-requirements.md` with the diagnostic having an internal identifier of `property_wrapper_failable_init`.
113-
6. If possible, rebuild the compiler and try recompiling your test program with `-print-educational-notes`. Your new note should appear after the diagnostic in the terminal.
114-
7. That's it! The new note is now ready to be submitted as a pull request on GitHub.
111+
4. Add a new Markdown file in the `userdocs/diagnostics/` directory in the swift repository containing the documentation. When writing a note, keep the writing guidelines from the section above in mind. The existing notes in the directory are another useful guide.
112+
5. Create a new entry in `DiagnosticGroups.def` that provides the name for your new diagnostic group along with the name of the file you added in step (4).
113+
6. Find each diagnostic you want to make part of this group in the various `Diagnostics*.def` files. For each diagnostic, replace the `ERROR` or `WARNING` with `GROUPED_ERROR` or `GROUPED_WARNING`, respectively, and put the diagnostic group name after the string literal for the diagnostic message.
114+
7. If possible, rebuild the compiler and try recompiling your test program. Your new diagnostic group should appear as `[#<group name>]` at the end of the diagnostic, with a link to the diagnostic file.
115+
8. That's it! The new diagnostic group is now ready to be submitted as a pull request on GitHub.
115116

116117
If you run into any issues or have questions while following the steps above, feel free to post a question on the Swift forums or open a work-in-progress pull request on GitHub.
117118

@@ -167,4 +168,4 @@ An expected diagnostic is denoted by a comment which begins with `expected-error
167168

168169
* If two (or more) expected fix-its are juxtaposed with nothing (or whitespace) between them, then both must be present for the verifier to match. If two (or more) expected fix-its have `||` between them, then one of them must be present for the verifier to match. `||` binds more tightly than juxtaposition: `{{1-1=a}} {{2-2=b}} || {{2-2=c}} {{3-3=d}} {{none}}` will only match if there is either a set of three fix-its that insert `a`, `b`, and `d`, or a set of three fix-its that insert `a`, `c`, and `d`. (Without the `{{none}}`, it would also permit all four fix-its, but only because one of the four would be unmatched and ignored.)
169170

170-
- (Optional) Expected educational notes. These appear as a comma separated list after the expected message, enclosed in double curly braces and prefixed by 'educational-notes='. For example, `{{educational-notes=some-note,some-other-note}}` will verify the educational notes with filenames `some-note` and `some-other-note` appear. Do not include the file extension when specifying note names.
171+
- (Optional) Expected documentation file. These is the name of the documentation file, enclosed in double curly braces and prefixed by 'documentation-file='. For example, `{{documentation-file=some-file}}` will verify the documentation group with filename `some-note` appears. Do not include the file extension when specifying the name.

Diff for: docs/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ provide greater clarity to contributors wanting to add new documentation.
334334
It might also be valuable to introduce the tips in context, and have the
335335
explanation link to all the different tips.
336336
- [Diagnostics.md](/docs/Diagnostics.md):
337-
Describes how to write diagnostic messages and associated educational notes.
337+
Describes how to write diagnostic messages and associated documentation.
338338
TODO: Consider splitting into how-tos and recommended practices.
339339
For example, we could have a how-to guide on adding a new diagnostic,
340340
and have a recommended practices page which explains the writing style
341-
for diagnostics and educational notes.
341+
for diagnostics and diagnostic groups.
342342
- [HowSwiftImportsCAPIs.md](/docs/HowSwiftImportsCAPIs.md):
343343
Contains a thorough description of the mapping between C/ObjC entities and
344344
Swift entities.

Diff for: docs/ReferenceGuides/UnderscoredAttributes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ func baz() {
733733
Additionally, if they are of a tuple or struct type, their stored members
734734
without observers may also be passed inout as non-ephemeral pointers.
735735

736-
For more details, see the educational note on
736+
For more details, see the diagnostic group
737737
[temporary pointer usage](/userdocs/diagnostics/temporary-pointers.md).
738738

739739
## `@_nonoverride`

Diff for: include/swift/AST/DiagnosticConsumer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ struct DiagnosticInfo {
5959
/// DiagnosticInfo of notes which are children of this diagnostic, if any
6060
ArrayRef<DiagnosticInfo *> ChildDiagnosticInfo;
6161

62-
/// Paths to "educational note" diagnostic documentation in the toolchain.
63-
ArrayRef<std::string> EducationalNotePaths;
62+
/// Path for category documentation.
63+
std::string CategoryDocumentationURL;
6464

6565
/// Represents a fix-it, a replacement of one range of text with another.
6666
class FixIt {

Diff for: include/swift/AST/DiagnosticGroups.def

+21-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,30 @@
2222

2323
GROUP(no_group, "")
2424

25+
GROUP(ActorIsolatedCall, "actor-isolated-call.md")
26+
GROUP(AsyncCallerExecution, "async-caller-execution.md")
27+
GROUP(ConformanceIsolation, "conformance-isolation.md")
2528
GROUP(DeprecatedDeclaration, "deprecated-declaration.md")
26-
GROUP(StrictMemorySafety, "strict-memory-safety.md")
27-
GROUP(UnknownWarningGroup, "unknown-warning-group.md")
29+
GROUP(DynamicCallable, "dynamic-callable-requirements.md")
30+
GROUP(ErrorInFutureSwiftVersion, "error-in-future-swift-version.md")
31+
GROUP(ExistentialAny, "existential-any.md")
32+
GROUP(ExistentialMemberAccess, "existential-member-access-limitations.md")
33+
GROUP(MultipleInheritance, "multiple-inheritance.md")
34+
GROUP(MutableGlobalVariable, "mutable-global-variable.md")
35+
GROUP(NominalTypes, "nominal-types.md")
36+
GROUP(OpaqueTypeInference, "opaque-type-inference.md")
2837
GROUP(PreconcurrencyImport, "preconcurrency-import.md")
29-
GROUP(AsyncCallerExecution, "async-caller-execution.md")
38+
GROUP(PropertyWrappers, "property-wrapper-requirements.md")
39+
GROUP(ProtocolTypeNonConformance, "protocol-type-non-conformance.md")
40+
GROUP(ResultBuilderMethods, "result-builder-methods.md")
41+
GROUP(SendableClosureCaptures, "sendable-closure-captures.md")
42+
GROUP(SendingRisksDataRace, "sending-risks-data-race.md")
3043
GROUP(StrictLanguageFeatures, "strict-language-features.md")
31-
GROUP(ExistentialAny, "existential-any.md")
44+
GROUP(StrictMemorySafety, "strict-memory-safety.md")
45+
GROUP(StringInterpolationConformance, "string-interpolation-conformance.md")
46+
GROUP(TemporaryPointers, "temporary-pointers.md")
47+
GROUP(TrailingClosureMatching, "trailing-closure-matching.md")
48+
GROUP(UnknownWarningGroup, "unknown-warning-group.md")
3249

3350
#define UNDEFINE_DIAGNOSTIC_GROUPS_MACROS
3451
#include "swift/AST/DefineDiagnosticGroupsMacros.h"

Diff for: include/swift/AST/DiagnosticsCommon.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ NOTE(brace_stmt_suggest_do,none,
4949

5050
// This does not have a group because warnings of this kind inherit the group
5151
// from the wrapped error.
52-
WARNING(error_in_swift_lang_mode,none,
52+
GROUPED_WARNING(error_in_swift_lang_mode,ErrorInFutureSwiftVersion,none,
5353
"%0; this is an error in the Swift %1 language mode",
5454
(DiagnosticInfo *, unsigned))
5555

Diff for: include/swift/AST/DiagnosticsSIL.def

+3-2
Original file line numberDiff line numberDiff line change
@@ -970,10 +970,11 @@ NOTE(regionbasedisolation_type_is_non_sendable, none,
970970
//===
971971
// Use After Send Emitter
972972

973-
ERROR(regionbasedisolation_named_send_yields_race, none,
973+
GROUPED_ERROR(regionbasedisolation_named_send_yields_race,
974+
SendingRisksDataRace, none,
974975
"sending %0 risks causing data races",
975976
(Identifier))
976-
ERROR(regionbasedisolation_type_send_yields_race, none,
977+
GROUPED_ERROR(regionbasedisolation_type_send_yields_race, SendingRisksDataRace, none,
977978
"sending value of non-Sendable type %0 risks causing data races",
978979
(Type))
979980
NOTE(regionbasedisolation_type_use_after_send, none,

0 commit comments

Comments
 (0)