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

simplify the code for diagnostics severity adjustment #17896

Merged
merged 14 commits into from
Oct 31, 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
59 changes: 17 additions & 42 deletions src/Compiler/Driver/CompilerDiagnostics.fs
Original file line number Diff line number Diff line change
Expand Up @@ -409,47 +409,25 @@ type PhasedDiagnostic with
(severity = FSharpDiagnosticSeverity.Info && level > 0)
|| (severity = FSharpDiagnosticSeverity.Warning && level >= x.WarningLevel)

/// Indicates if a diagnostic should be reported as an informational
member x.ReportAsInfo(options, severity) =
match severity with
| FSharpDiagnosticSeverity.Error -> false
| FSharpDiagnosticSeverity.Warning -> false
| FSharpDiagnosticSeverity.Info -> x.IsEnabled(severity, options) && not (List.contains x.Number options.WarnOff)
| FSharpDiagnosticSeverity.Hidden -> false

/// Indicates if a diagnostic should be reported as a warning
member x.ReportAsWarning(options, severity) =
match severity with
| FSharpDiagnosticSeverity.Error -> false

| FSharpDiagnosticSeverity.Warning -> x.IsEnabled(severity, options) && not (List.contains x.Number options.WarnOff)

// Informational become warning if explicitly on and not explicitly off
| FSharpDiagnosticSeverity.Info ->
let n = x.Number
List.contains n options.WarnOn && not (List.contains n options.WarnOff)

| FSharpDiagnosticSeverity.Hidden -> false
member x.AdjustSeverity(options, severity) =
let n = x.Number

/// Indicates if a diagnostic should be reported as an error
member x.ReportAsError(options, severity) =
let warnOff () = List.contains n options.WarnOff

match severity with
| FSharpDiagnosticSeverity.Error -> true

// Warnings become errors in some situations
| FSharpDiagnosticSeverity.Warning ->
let n = x.Number

| FSharpDiagnosticSeverity.Error -> FSharpDiagnosticSeverity.Error
| FSharpDiagnosticSeverity.Warning when
x.IsEnabled(severity, options)
&& not (List.contains n options.WarnAsWarn)
&& ((options.GlobalWarnAsError && not (List.contains n options.WarnOff))
&& ((options.GlobalWarnAsError && not (warnOff ()))
|| List.contains n options.WarnAsError)

// Informational become errors if explicitly WarnAsError
| FSharpDiagnosticSeverity.Info -> List.contains x.Number options.WarnAsError

| FSharpDiagnosticSeverity.Hidden -> false
&& not (List.contains n options.WarnAsWarn)
->
FSharpDiagnosticSeverity.Error
| FSharpDiagnosticSeverity.Warning when x.IsEnabled(severity, options) && not (warnOff ()) -> FSharpDiagnosticSeverity.Warning
| FSharpDiagnosticSeverity.Info when List.contains n options.WarnAsError -> FSharpDiagnosticSeverity.Error
| FSharpDiagnosticSeverity.Info when List.contains n options.WarnOn && not (warnOff ()) -> FSharpDiagnosticSeverity.Warning
| FSharpDiagnosticSeverity.Info when x.IsEnabled(severity, options) && not (warnOff ()) -> FSharpDiagnosticSeverity.Info
| _ -> FSharpDiagnosticSeverity.Hidden

[<AutoOpen>]
module OldStyleMessages =
Expand Down Expand Up @@ -2333,12 +2311,9 @@ type DiagnosticsLoggerFilteringByScopedPragmas
| None -> true

if report then
if diagnostic.ReportAsError(diagnosticOptions, severity) then
diagnosticsLogger.DiagnosticSink(diagnostic, FSharpDiagnosticSeverity.Error)
elif diagnostic.ReportAsWarning(diagnosticOptions, severity) then
diagnosticsLogger.DiagnosticSink(diagnostic, FSharpDiagnosticSeverity.Warning)
elif diagnostic.ReportAsInfo(diagnosticOptions, severity) then
diagnosticsLogger.DiagnosticSink(diagnostic, severity)
match diagnostic.AdjustSeverity(diagnosticOptions, severity) with
| FSharpDiagnosticSeverity.Hidden -> ()
| s -> diagnosticsLogger.DiagnosticSink(diagnostic, s)

override _.ErrorCount = diagnosticsLogger.ErrorCount

Expand Down
10 changes: 2 additions & 8 deletions src/Compiler/Driver/CompilerDiagnostics.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,8 @@ type PhasedDiagnostic with
/// Format the core of the diagnostic as a string. Doesn't include the range information.
member FormatCore: flattenErrors: bool * suggestNames: bool -> string

/// Indicates if a diagnostic should be reported as an informational
member ReportAsInfo: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> bool

/// Indicates if a diagnostic should be reported as a warning
member ReportAsWarning: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> bool

/// Indicates if a diagnostic should be reported as an error
member ReportAsError: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> bool
/// Compute new severity according to the various diagnostics options
member AdjustSeverity: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> FSharpDiagnosticSeverity

/// Output all of a diagnostic to a buffer, including range
member Output: buf: StringBuilder * tcConfig: TcConfig * severity: FSharpDiagnosticSeverity -> unit
Expand Down
10 changes: 4 additions & 6 deletions src/Compiler/Driver/fsc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ type DiagnosticsLoggerUpToMaxErrors(tcConfigB: TcConfigBuilder, exiter: Exiter,
override x.DiagnosticSink(diagnostic, severity) =
let tcConfig = TcConfig.Create(tcConfigB, validate = false)

if diagnostic.ReportAsError(tcConfig.diagnosticsOptions, severity) then
match diagnostic.AdjustSeverity(tcConfigB.diagnosticsOptions, severity) with
| FSharpDiagnosticSeverity.Error ->
if errors >= tcConfig.maxErrors then
x.HandleTooManyErrors(FSComp.SR.fscTooManyErrors ())
exiter.Exit 1
Expand All @@ -93,11 +94,8 @@ type DiagnosticsLoggerUpToMaxErrors(tcConfigB: TcConfigBuilder, exiter: Exiter,
Debug.Assert(false, sprintf "Lookup exception in compiler: %s" (diagnostic.Exception.ToString()))
| _ -> ()

elif diagnostic.ReportAsWarning(tcConfig.diagnosticsOptions, severity) then
x.HandleIssue(tcConfig, diagnostic, FSharpDiagnosticSeverity.Warning)

elif diagnostic.ReportAsInfo(tcConfig.diagnosticsOptions, severity) then
x.HandleIssue(tcConfig, diagnostic, severity)
| FSharpDiagnosticSeverity.Hidden -> ()
| s -> x.HandleIssue(tcConfig, diagnostic, s)

/// Create an error logger that counts and prints errors
let ConsoleDiagnosticsLogger (tcConfigB: TcConfigBuilder, exiter: Exiter) =
Expand Down
15 changes: 5 additions & 10 deletions src/Compiler/Interactive/fsi.fs
Original file line number Diff line number Diff line change
Expand Up @@ -898,28 +898,23 @@ type internal DiagnosticsLoggerThatStopsOnFirstError
override _.DiagnosticSink(diagnostic, severity) =
let tcConfig = TcConfig.Create(tcConfigB, validate = false)

if diagnostic.ReportAsError(tcConfig.diagnosticsOptions, severity) then
match diagnostic.AdjustSeverity(tcConfig.diagnosticsOptions, severity) with
| FSharpDiagnosticSeverity.Error ->
fsiStdinSyphon.PrintDiagnostic(tcConfig, diagnostic)
errorCount <- errorCount + 1

if tcConfigB.abortOnError then
exit 1 (* non-zero exit code *)
// STOP ON FIRST ERROR (AVOIDS PARSER ERROR RECOVERY)
raise StopProcessing
elif diagnostic.ReportAsWarning(tcConfig.diagnosticsOptions, severity) then
DoWithDiagnosticColor FSharpDiagnosticSeverity.Warning (fun () ->
fsiConsoleOutput.Error.WriteLine()
diagnostic.WriteWithContext(fsiConsoleOutput.Error, " ", fsiStdinSyphon.GetLine, tcConfig, severity)
fsiConsoleOutput.Error.WriteLine()
fsiConsoleOutput.Error.WriteLine()
fsiConsoleOutput.Error.Flush())
elif diagnostic.ReportAsInfo(tcConfig.diagnosticsOptions, severity) then
DoWithDiagnosticColor FSharpDiagnosticSeverity.Info (fun () ->
| (FSharpDiagnosticSeverity.Warning | FSharpDiagnosticSeverity.Info) as adjustedSeverity ->
DoWithDiagnosticColor adjustedSeverity (fun () ->
fsiConsoleOutput.Error.WriteLine()
diagnostic.WriteWithContext(fsiConsoleOutput.Error, " ", fsiStdinSyphon.GetLine, tcConfig, severity)
fsiConsoleOutput.Error.WriteLine()
fsiConsoleOutput.Error.WriteLine()
fsiConsoleOutput.Error.Flush())
| FSharpDiagnosticSeverity.Hidden -> ()

override _.ErrorCount = errorCount

Expand Down
26 changes: 10 additions & 16 deletions src/Compiler/Symbols/FSharpDiagnostic.fs
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,12 @@ type internal CompilationDiagnosticLogger (debugName: string, options: FSharpDia
| Some f -> f diagnostic
| None -> diagnostic

if diagnostic.ReportAsError (options, severity) then
match diagnostic.AdjustSeverity(options, severity) with
| FSharpDiagnosticSeverity.Error ->
diagnostics.Add(diagnostic, FSharpDiagnosticSeverity.Error)
errorCount <- errorCount + 1
elif diagnostic.ReportAsWarning (options, severity) then
diagnostics.Add(diagnostic, FSharpDiagnosticSeverity.Warning)
elif diagnostic.ReportAsInfo (options, severity) then
diagnostics.Add(diagnostic, severity)
| FSharpDiagnosticSeverity.Hidden -> ()
| sev -> diagnostics.Add(diagnostic, sev)

override _.ErrorCount = errorCount

Expand All @@ -319,23 +318,18 @@ type internal CompilationDiagnosticLogger (debugName: string, options: FSharpDia
module DiagnosticHelpers =

let ReportDiagnostic (options: FSharpDiagnosticOptions, allErrors, mainInputFileName, fileInfo, diagnostic: PhasedDiagnostic, severity, suggestNames, flatErrors, symbolEnv) =
[ let severity =
if diagnostic.ReportAsError (options, severity) then
FSharpDiagnosticSeverity.Error
else
severity

if severity = FSharpDiagnosticSeverity.Error ||
diagnostic.ReportAsWarning (options, severity) ||
diagnostic.ReportAsInfo (options, severity) then
match diagnostic.AdjustSeverity(options, severity) with
| FSharpDiagnosticSeverity.Hidden -> []
| adjustedSeverity ->

// We use the first line of the file as a fallbackRange for reporting unexpected errors.
// Not ideal, but it's hard to see what else to do.
let fallbackRange = rangeN mainInputFileName 1
let diagnostic = FSharpDiagnostic.CreateFromExceptionAndAdjustEof (diagnostic, severity, fallbackRange, fileInfo, suggestNames, flatErrors, symbolEnv)
let diagnostic = FSharpDiagnostic.CreateFromExceptionAndAdjustEof (diagnostic, adjustedSeverity, fallbackRange, fileInfo, suggestNames, flatErrors, symbolEnv)
let fileName = diagnostic.Range.FileName
if allErrors || fileName = mainInputFileName || fileName = TcGlobals.DummyFileNameForRangesWithoutASpecificLocation then
yield diagnostic ]
[diagnostic]
else []

let CreateDiagnostics (options, allErrors, mainInputFileName, diagnostics, suggestNames, flatErrors, symbolEnv) =
let fileInfo = (Int32.MaxValue, Int32.MaxValue)
Expand Down
Loading