Skip to content

Commit da7e1ca

Browse files
committed
[Diagnostics] Add a section to Diagnostics.md on educational notes
1 parent ac05a66 commit da7e1ca

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

docs/Diagnostics.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ The Swift compiler has a setting (under LangOptions) called `DiagnosticsEditorMo
9090

9191
Most diagnostics have no reason to change behavior under editor mode. An example of an exception is the "protocol requirements not satisfied diagnostic"; on the command line, it may be better to show all unsatisfied requirements, while in an IDE a single multi-line fix-it would be preferred.
9292

93+
### Educational Notes ###
94+
95+
**Note**: This feature is currently experimental. It can be enabled by passing the `-Xfrontend -enable-descriptive-diagnostics` flag.
96+
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.
98+
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.
105+
- 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.
108+
109+
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.
93112

94113
### Format Specifiers ###
95114

lib/Frontend/PrintingDiagnosticConsumer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
7070

7171
printDiagnostic(SM, Info);
7272
for (auto path : Info.EducationalNotePaths) {
73-
auto buffer = llvm::MemoryBuffer::getFile(path);
74-
if (buffer) {
73+
if (auto buffer = SM.getFileSystem()->getBufferForFile(path))
7574
Stream << buffer->get()->getBuffer() << "\n";
76-
}
7775
}
7876

7977
for (auto ChildInfo : Info.ChildDiagnosticInfo) {

0 commit comments

Comments
 (0)