Skip to content
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

Diagnostics: Clearer information style #17488

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions src/Compiler/Driver/CompilerDiagnostics.fs
psfinaki marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2044,14 +2044,15 @@ type FormattedDiagnosticDetailedInfo =
Location: FormattedDiagnosticLocation option
Canonical: FormattedDiagnosticCanonicalInformation
Message: string
Context: string option
}

[<RequireQualifiedAccess>]
type FormattedDiagnostic =
| Short of FSharpDiagnosticSeverity * string
| Long of FSharpDiagnosticSeverity * FormattedDiagnosticDetailedInfo

let FormatDiagnosticLocation (tcConfig: TcConfig) m : FormattedDiagnosticLocation =
let FormatDiagnosticLocation (tcConfig: TcConfig) (m: Range) : FormattedDiagnosticLocation =
if equals m rangeStartup || equals m rangeCmdArgs then
{
Range = m
Expand Down Expand Up @@ -2114,6 +2115,10 @@ let FormatDiagnosticLocation (tcConfig: TcConfig) m : FormattedDiagnosticLocatio
sprintf "%s(%d,%d,%d,%d): " file m.StartLine m.StartColumn m.EndLine m.EndColumn, m, file
else
"", m, file
| DiagnosticStyle.Rich ->
let file = file.Replace('/', Path.DirectorySeparatorChar)
let m = withStart (mkPos m.StartLine (m.StartColumn + 1)) m
(sprintf "◦→ %s (%d,%d)" file m.StartLine m.StartColumn), m, file
vzarytovskii marked this conversation as resolved.
Show resolved Hide resolved

{
Range = m
Expand Down Expand Up @@ -2154,8 +2159,12 @@ let CollectFormattedDiagnostics (tcConfig: TcConfig, severity: FSharpDiagnosticS
let text =
match tcConfig.diagnosticStyle with
// Show the subcategory for --vserrors so that we can fish it out in Visual Studio and use it to determine error stickiness.
| DiagnosticStyle.Emacs
| DiagnosticStyle.Gcc
| DiagnosticStyle.Default
| DiagnosticStyle.Test -> sprintf "%s FS%04d: " message errorNumber
| DiagnosticStyle.VisualStudio -> sprintf "%s %s FS%04d: " subcategory message errorNumber
| _ -> sprintf "%s FS%04d: " message errorNumber
| DiagnosticStyle.Rich -> sprintf "\n◦→ %s FS%04d: " message errorNumber

let canonical: FormattedDiagnosticCanonicalInformation =
{
Expand All @@ -2164,11 +2173,51 @@ let CollectFormattedDiagnostics (tcConfig: TcConfig, severity: FSharpDiagnosticS
TextRepresentation = text
}

let message = diagnostic.FormatCore(tcConfig.flatErrors, suggestNames)
let message =
match tcConfig.diagnosticStyle with
| DiagnosticStyle.Emacs
| DiagnosticStyle.Gcc
| DiagnosticStyle.Default
| DiagnosticStyle.Test
| DiagnosticStyle.VisualStudio -> diagnostic.FormatCore(tcConfig.flatErrors, suggestNames)
| DiagnosticStyle.Rich ->
diagnostic.FormatCore(tcConfig.flatErrors, suggestNames).Split([| '\n' |])
|> Array.map (fun msg -> "\n◦ " + msg)
|> String.Concat

let context =
match tcConfig.diagnosticStyle with
| DiagnosticStyle.Emacs
| DiagnosticStyle.Gcc
| DiagnosticStyle.Default
| DiagnosticStyle.Test
| DiagnosticStyle.VisualStudio -> None
| DiagnosticStyle.Rich ->
match diagnostic.Range with
| Some m ->
let content =
m.FileName
|> FileSystem.GetFullFilePathInDirectoryShim tcConfig.implicitIncludeDir
|> System.IO.File.ReadAllLines

if m.StartLine = m.EndLine then
$"\n◦ {m.StartLine} |{content[m.StartLine - 1]}\n"
+ $"""◦ {String.init (m.StartColumn + 3) (fun _ -> " ")}^{String.init (m.EndColumn - m.StartColumn) (fun _ -> "~")}"""
|> Some
else
content
|> fun lines -> Array.sub lines (m.StartLine - 1) (m.EndLine - m.StartLine - 1)
|> Array.fold
(fun (context, lineNumber) line -> (context + $"\n◦ {lineNumber} |{line}", lineNumber + 1))
("", (m.StartLine))
|> fst
|> Some
| None -> None

let entry: FormattedDiagnosticDetailedInfo =
{
Location = where
Context = context
Canonical = canonical
Message = message
}
Expand Down Expand Up @@ -2199,7 +2248,11 @@ type PhasedDiagnostic with
| FormattedDiagnostic.Short(_, txt) -> buf.AppendString txt
| FormattedDiagnostic.Long(_, details) ->
match details.Location with
| Some l when not l.IsEmpty -> buf.AppendString l.TextRepresentation
| Some l when not l.IsEmpty ->
buf.AppendString l.TextRepresentation
// Because details.Context depends on the value of details.Location, if details.Location is not None, details.Context can be accessed directly.
if details.Context.IsSome then
buf.AppendString details.Context.Value
| _ -> ()

buf.AppendString details.Canonical.TextRepresentation
Expand Down
3 changes: 2 additions & 1 deletion src/Compiler/Driver/CompilerDiagnostics.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ type FormattedDiagnosticCanonicalInformation =
type FormattedDiagnosticDetailedInfo =
{ Location: FormattedDiagnosticLocation option
Canonical: FormattedDiagnosticCanonicalInformation
Message: string }
Message: string
Context: string option }

/// Used internally and in LegacyHostedCompilerForTesting
[<RequireQualifiedAccess>]
Expand Down
3 changes: 2 additions & 1 deletion src/Compiler/Facilities/DiagnosticsLogger.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ open Internal.Utilities.Library.Extras
open System.Threading.Tasks

/// Represents the style being used to format errors
[<RequireQualifiedAccess>]
[<RequireQualifiedAccess; NoComparison; NoEquality>]
type DiagnosticStyle =
| Default
| Emacs
| Test
| VisualStudio
| Gcc
| Rich

/// Thrown when we want to add some range information to a .NET exception
exception WrappedError of exn * range with
Expand Down
3 changes: 2 additions & 1 deletion src/Compiler/Facilities/DiagnosticsLogger.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ open System.Runtime.CompilerServices
open System.Runtime.InteropServices

/// Represents the style being used to format errors
[<RequireQualifiedAccess>]
[<RequireQualifiedAccess; NoComparison; NoEquality>]
type DiagnosticStyle =
| Default
| Emacs
| Test
| VisualStudio
| Gcc
| Rich

/// Thrown when we want to add some range information to a .NET exception
exception WrappedError of exn * range
Expand Down
Loading