The key types are:
ErrorLogger
FSharpDiagnosticSeverity
FSharpDiagnostic
DiagnosticWithText
and functions
warning
- emit a warningerrorR
- emit an error and continueerror
- emit an error and throw an exceptionerrorRecovery
- recover from an exceptionFor the compiler, a key file is https://github.com/dotnet/fsharp/blob/main/src/Compiler/FSComp.txt
holding most of the messages. There are also a few other similar files including some old error messages in FSStrings.resx
.
Adding or adjusting diagnostics emitted by the compiler is usually straightforward (though it can sometimes imply deeper compiler work). Here's the general process:
FS0020
) in the message.FSComp.fs
with a title, such as parsMissingTypeArgs
.From here, you can either simply update the error text, or you can use some of the information at the point in the source code you identified to see if there is more information to include in the error message. For example, if the error message doesn't contain information about the identifier the user is using incorrectly, you may be able to include the name of the identifier based on data the compiler has available at that stage of compilation.
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.
Diagnostics must often format TAST items as user text. When formatting these, you normally use either
The functions in the NicePrint
module such as NicePrint.outputTyconRef
. These take a DisplayEnv
that records the context in which a type was referenced, for example, the open namespaces. Opened namespaces are not shown in the displayed output.
The DisplayName
properties on the relevant object. This drops the 'n
text that .NET adds to the compiled name of a type, and uses the F#-facing name for a type rather than the compiled name for a type (for example, the name given in a CompiledName
attribute).
When formatting "info" objects, see the functions in the NicePrint
module.
Diagnostics must often format types.
When displaying a type, you will normally want to "prettify" the type first. This converts any remaining type inference variables to new, better user-friendly type variables with names like 'a
. Various functions prettify types prior to display, for example, NicePrint.layoutPrettifiedTypes
and others.
When displaying multiple types in a comparative way, for example, two types that didn't match, you will want to display the minimal amount of infomation to convey the fact that the two types are different, for example, NicePrint.minimalStringsOfTwoTypes
.
When displaying a type, you have the option of displaying the constraints implied by any type variables mentioned in the types, appended as when ...
. For example, NicePrint.layoutPrettifiedTypeAndConstraints
.
The file FSComp.txt
contains the canonical listing of diagnostic messages, but there are also .xlf
localization files for various languages.
See the DEVGUIDE for more details.
The file CompilerDiagnostics.fs
contains the function IsWarningOrInfoEnabled
, which determines whether a given diagnostic is emitted.