-
Notifications
You must be signed in to change notification settings - Fork 831
Improve docs around new diagnostics #15330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,20 @@ From here, you can either simply update the error text, or you can use some of t | |
|
|
||
| If you're including data from user code in an error message, it's important to also write a test that verifies the exact error message for a given string of F# code. | ||
|
|
||
| Note that the .NET SDK generally follows the policy that new diagnostics are introduced only alongside a language version increment, and F# now tries to follow the same policy (though historically this has not always happened). | ||
| This is true even if your diagnostic is off by default. | ||
| So if you create a new diagnostic by adding it to [FsComp.txt](https://github.com/dotnet/fsharp/blob/main/src/Compiler/FSComp.txt), you should add a new language feature at the same time. | ||
|
|
||
| Concretely, consider using the following procedure, which would be the "gold standard" when implementing a new diagnostic. | ||
| (At present, this is more like a best-practice guideline than a requirement.) | ||
|
|
||
| 1. Create a pull request that reserves an error message. (That way, you can avoid other people trampling over your change to FSComp.txt while you're implementing the actual feature.) [Here's a historical example.](https://github.com/dotnet/fsharp/pull/14642) | ||
| 1. In the same pull request, reserve the new feature. [Here's a historical example](https://github.com/dotnet/fsharp/pull/15315/) (although it would have been better practice to combine this into the previous pull request, and also this pull request [should not have registered the feature](https://github.com/dotnet/fsharp/pull/15315/files#r1220330247) as belonging to a particular language version). | ||
| 1. Now you can go ahead and implement the diagnostic. This is the point at which you would register the language feature in some particular language version (likely the preview language version). It is often best practice to introduce it at a lower level than you ultimately intend (e.g. informational rather than warning, or warning rather than error), to permit a more gradual rollout. See the section on "[Enabling a warning or error by default](#enabling-a-warning-or-error-by-default)" for more information. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm from what I understand, the above PR examples are not entirely "canonical" (as in - things could be organized in a better way) hence the whole thing sounds a bit confusing. I'd suggest rather one of the following:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, please don't suggest reserving error numbers or pre-registering features prior to implementing the feature. The checkin for the feature is the first place these should arrive in the codebase. Many features, spend months, some even spend years before being completed, or are closed incompleted. We definitely do not want orphaned partial changes checked in to the codebase. Being able to manage merges and conflicts is a useful skill for developers to have. |
||
| 1. When the next language version comes along, consider creating another language feature which increases the default diagnostic level, remembering not to change the existing level for people who are pinned to the older language version. [Here's an example](https://github.com/dotnet/fsharp/blob/2133c3404186f3ddcafa10c943e9451358412ab3/src/Compiler/Checking/CheckPatterns.fs#L570). | ||
|
|
||
| If you feature is small and uncontroversial, then it might be easier to combine some of these steps. | ||
|
|
||
| ## Formatting Typed Tree items in Diagnostics | ||
|
|
||
| Diagnostics must often format TAST items as user text. When formatting these, you normally use either | ||
|
|
@@ -64,3 +78,12 @@ See [the DEVGUIDE](../DEVGUIDE.md#updating-fscompfs-fscompresx-and-xlf) for more | |
| ## Enabling a warning or error by default | ||
|
|
||
| The file `CompilerDiagnostics.fs` contains the function `IsWarningOrInfoEnabled`, which determines whether a given diagnostic is emitted. | ||
| If you have created a language feature to accompany the diagnostic, and you intend the diagnostic to be on by default in any language version supporting that language feature, you should use `DiagnosticEnabledWithLanguageFeature` to control the diagnostic. | ||
|
|
||
| For example: | ||
|
|
||
| ```fsharp | ||
| // Several contexts give you access to the current language version. | ||
| let supported = g.langVersion.SupportsFeature LanguageFeature.MyLanguageFeature | ||
| informationalWarning(DiagnosticEnabledWithLanguageFeature(FSComp.SR.typrelNeverRefinedAwayFromTop(), m, supported))` | ||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.