Skip to content

Commit 791312f

Browse files
authored
Turn Educational Notes On-By-Default (swiftlang#30583)
* [Diagnostics] Turn educational notes on-by-default * [Diagnostics] Only include educational notes in printed output if -print-educational-notes is passed * Make -print-educational-notes a driver option * [Diagnostics] Issue a printed remark if educational notes are available, but disabled * [docs] Update educational notes documentation and add a contributing guide * [Diagnostics] Cleanup PrintingDiagnosticConsumer handling of edu notes * Revert "[Diagnostics] Issue a printed remark if educational notes are available, but disabled" For now, don't notify users if edu notes are available but disabled. This decision can be reevaluated later.
1 parent c83e166 commit 791312f

File tree

14 files changed

+61
-52
lines changed

14 files changed

+61
-52
lines changed

docs/Diagnostics.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,34 @@ Most diagnostics have no reason to change behavior under editor mode. An example
9292

9393
### Educational Notes ###
9494

95-
**Note**: This feature is currently experimental. It can be enabled by passing the `-Xfrontend -enable-descriptive-diagnostics` flag.
95+
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.
9696

97-
Educational notes are small snippets of 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 for users encountering a new error message for the first time. In very limited circumstances, they also allow the main diagnostic message to use more precise and correct terminology (e.g. nominal types) which would otherwise be too unfriendly for beginners.
97+
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.
9898

99-
When outputting diagnostics on the command line, educational notes will be printed after the main diagnostic body if descriptive diagnostics are enabled. When presented in an IDE, it's expected they will be collapsed under a disclosure arrow, info button, or similar to avoid cluttering output.
100-
101-
Generally speaking, a diagnostic should try to provide educational notes for any concepts/terminology which is difficult to understand from context or is especially subtle. Educational notes should:
102-
- Explain a single language concept. This makes them easy to reuse across diagnostics and helps keep them clear, concise, and easy to understand.
103-
- Be written in unabbreviated English. These are longer form messages compared to the main diagnostic, so there is no need to omit needless words and punctuation.
104-
- 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 diagnostics UX issues in some contexts.
99+
Educational notes should:
100+
- Explain a single language concept. This makes them easy to reuse across related diagnostics and helps keep them clear, concise, and easy to understand.
101+
- 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.
102+
- 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.
105103
- Be accessible. Educational notes should be beginner friendly and avoid assuming unnecesary 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".
106-
- Include references to relevant chapters of _The Swift Programming Language_ if applicable.
107-
- Be written in Markdown, but avoid excessive markup to avoid impacting the terminal UX.
104+
- Include references to relevant chapters of _The Swift Programming Language_.
105+
- Be written in Markdown, but avoid excessive markup which negatively impacts the terminal UX.
106+
107+
### Quick-Start Guide for Contributing New Educational Notes ###
108+
109+
Adding new educational notes is a great way to get familiar with the process of contributing to Swift, while also making a big impact!
108110

109111
To add a new educational note:
110-
1. Add a new Markdown file in the `userdocs/diagnostics/` directory containing the contents of the note.
111-
2. Associate the note with one or more diagnostics in EducationalNotes.def.
112+
1. Follow the [directions in the README](https://github.com/apple/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.
113+
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.
114+
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:
115+
- 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.
116+
- 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.
117+
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.
118+
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`.
119+
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.
120+
7. That's it! The new note is now ready to be submitted as a pull request on GitHub.
121+
122+
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.
112123

113124
### Format Specifiers ###
114125

include/swift/AST/DiagnosticEngine.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,6 @@ namespace swift {
676676
/// Print diagnostic names after their messages
677677
bool printDiagnosticNames = false;
678678

679-
/// Use educational notes when available.
680-
bool useEducationalNotes = false;
681-
682679
/// Path to diagnostic documentation directory.
683680
std::string diagnosticDocumentationPath = "";
684681

@@ -730,9 +727,6 @@ namespace swift {
730727
return printDiagnosticNames;
731728
}
732729

733-
void setUseEducationalNotes(bool val) { useEducationalNotes = val; }
734-
bool getUseEducationalNotes() const { return useEducationalNotes; }
735-
736730
void setDiagnosticDocumentationPath(std::string path) {
737731
diagnosticDocumentationPath = path;
738732
}

include/swift/Basic/DiagnosticOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ class DiagnosticOptions {
5555
// When printing diagnostics, include the diagnostic name at the end
5656
bool PrintDiagnosticNames = false;
5757

58-
/// If set to true, display educational note content to the user if available.
58+
/// If set to true, include educational notes in printed output if available.
5959
/// Educational notes are documentation which supplement diagnostics.
60-
bool EnableEducationalNotes = false;
60+
bool PrintEducationalNotes = false;
6161

6262
// If set to true, use the more descriptive experimental formatting style for
6363
// diagnostics.

include/swift/Frontend/PrintingDiagnosticConsumer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class AnnotatedSourceSnippet;
3131
class PrintingDiagnosticConsumer : public DiagnosticConsumer {
3232
llvm::raw_ostream &Stream;
3333
bool ForceColors = false;
34+
bool PrintEducationalNotes = false;
3435
bool DidErrorOccur = false;
3536
bool ExperimentalFormattingEnabled = false;
3637
// The current snippet used to display an error/warning/remark and the notes
@@ -59,6 +60,10 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
5960
llvm::sys::Process::UseANSIEscapeCodes(true);
6061
}
6162

63+
void setPrintEducationalNotes(bool ShouldPrint) {
64+
PrintEducationalNotes = ShouldPrint;
65+
}
66+
6267
void enableExperimentalFormatting() { ExperimentalFormattingEnabled = true; }
6368

6469
bool didErrorOccur() {

include/swift/Option/FrontendOptions.td

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ def enable_cross_import_overlays : Flag<["-"], "enable-cross-import-overlays">,
130130
HelpText<"Automatically import declared cross-import overlays.">;
131131
def disable_cross_import_overlays : Flag<["-"], "disable-cross-import-overlays">,
132132
HelpText<"Do not automatically import declared cross-import overlays.">;
133-
134-
def enable_educational_notes : Flag<["-"], "enable-educational-notes">,
135-
HelpText<"Show educational notes, if available.">;
136133

137134
def enable_experimental_diagnostic_formatting :
138135
Flag<["-"], "enable-experimental-diagnostic-formatting">,

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ def no_color_diagnostics : Flag<["-"], "no-color-diagnostics">,
372372
def debug_diagnostic_names : Flag<["-"], "debug-diagnostic-names">,
373373
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, HelpHidden]>,
374374
HelpText<"Include diagnostic names when printing">;
375+
def print_educational_notes : Flag<["-"], "print-educational-notes">,
376+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
377+
HelpText<"Include educational notes in printed diagnostic output, if available">;
375378

376379
def module_cache_path : Separate<["-"], "module-cache-path">,
377380
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,

lib/AST/DiagnosticEngine.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -984,16 +984,15 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
984984
info->ChildDiagnosticInfo = childInfoPtrs;
985985

986986
SmallVector<std::string, 1> educationalNotePaths;
987-
if (useEducationalNotes) {
988-
auto associatedNotes = educationalNotes[(uint32_t)diagnostic.getID()];
989-
while (associatedNotes && *associatedNotes) {
990-
SmallString<128> notePath(getDiagnosticDocumentationPath());
991-
llvm::sys::path::append(notePath, *associatedNotes);
992-
educationalNotePaths.push_back(notePath.str().str());
993-
associatedNotes++;
994-
}
995-
info->EducationalNotePaths = educationalNotePaths;
987+
988+
auto associatedNotes = educationalNotes[(uint32_t)diagnostic.getID()];
989+
while (associatedNotes && *associatedNotes) {
990+
SmallString<128> notePath(getDiagnosticDocumentationPath());
991+
llvm::sys::path::append(notePath, *associatedNotes);
992+
educationalNotePaths.push_back(notePath.str().str());
993+
associatedNotes++;
996994
}
995+
info->EducationalNotePaths = educationalNotePaths;
997996

998997
for (auto &consumer : Consumers) {
999998
consumer->handleDiagnostic(SourceMgr, *info);

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ static void addCommonFrontendArgs(const ToolChain &TC, const OutputInfo &OI,
249249
inputArgs.AddLastArg(arguments, options::OPT_package_description_version);
250250
inputArgs.AddLastArg(arguments, options::OPT_serialize_diagnostics_path);
251251
inputArgs.AddLastArg(arguments, options::OPT_debug_diagnostic_names);
252+
inputArgs.AddLastArg(arguments, options::OPT_print_educational_notes);
252253
inputArgs.AddLastArg(arguments, options::OPT_enable_astscope_lookup);
253254
inputArgs.AddLastArg(arguments, options::OPT_disable_astscope_lookup);
254255
inputArgs.AddLastArg(arguments, options::OPT_disable_parser_lookup);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
821821
Opts.SuppressWarnings |= Args.hasArg(OPT_suppress_warnings);
822822
Opts.WarningsAsErrors |= Args.hasArg(OPT_warnings_as_errors);
823823
Opts.PrintDiagnosticNames |= Args.hasArg(OPT_debug_diagnostic_names);
824-
Opts.EnableEducationalNotes |= Args.hasArg(OPT_enable_educational_notes);
824+
Opts.PrintEducationalNotes |= Args.hasArg(OPT_print_educational_notes);
825825
Opts.EnableExperimentalFormatting |=
826826
Args.hasArg(OPT_enable_experimental_diagnostic_formatting);
827827
if (Arg *A = Args.getLastArg(OPT_diagnostic_documentation_path)) {

lib/Frontend/Frontend.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,6 @@ void CompilerInstance::setUpDiagnosticOptions() {
410410
if (Invocation.getDiagnosticOptions().PrintDiagnosticNames) {
411411
Diagnostics.setPrintDiagnosticNames(true);
412412
}
413-
if (Invocation.getDiagnosticOptions().EnableEducationalNotes) {
414-
Diagnostics.setUseEducationalNotes(true);
415-
}
416413
Diagnostics.setDiagnosticDocumentationPath(
417414
Invocation.getDiagnosticOptions().DiagnosticDocumentationPath);
418415
}

0 commit comments

Comments
 (0)