Skip to content

Commit 658b245

Browse files
authored
simplify the code for diagnostics severity adjustment (#17896)
1 parent b34e992 commit 658b245

File tree

5 files changed

+38
-82
lines changed

5 files changed

+38
-82
lines changed

src/Compiler/Driver/CompilerDiagnostics.fs

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -409,47 +409,25 @@ type PhasedDiagnostic with
409409
(severity = FSharpDiagnosticSeverity.Info && level > 0)
410410
|| (severity = FSharpDiagnosticSeverity.Warning && level >= x.WarningLevel)
411411

412-
/// Indicates if a diagnostic should be reported as an informational
413-
member x.ReportAsInfo(options, severity) =
414-
match severity with
415-
| FSharpDiagnosticSeverity.Error -> false
416-
| FSharpDiagnosticSeverity.Warning -> false
417-
| FSharpDiagnosticSeverity.Info -> x.IsEnabled(severity, options) && not (List.contains x.Number options.WarnOff)
418-
| FSharpDiagnosticSeverity.Hidden -> false
419-
420-
/// Indicates if a diagnostic should be reported as a warning
421-
member x.ReportAsWarning(options, severity) =
422-
match severity with
423-
| FSharpDiagnosticSeverity.Error -> false
424-
425-
| FSharpDiagnosticSeverity.Warning -> x.IsEnabled(severity, options) && not (List.contains x.Number options.WarnOff)
426-
427-
// Informational become warning if explicitly on and not explicitly off
428-
| FSharpDiagnosticSeverity.Info ->
429-
let n = x.Number
430-
List.contains n options.WarnOn && not (List.contains n options.WarnOff)
431-
432-
| FSharpDiagnosticSeverity.Hidden -> false
412+
member x.AdjustSeverity(options, severity) =
413+
let n = x.Number
433414

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

437417
match severity with
438-
| FSharpDiagnosticSeverity.Error -> true
439-
440-
// Warnings become errors in some situations
441-
| FSharpDiagnosticSeverity.Warning ->
442-
let n = x.Number
443-
418+
| FSharpDiagnosticSeverity.Error -> FSharpDiagnosticSeverity.Error
419+
| FSharpDiagnosticSeverity.Warning when
444420
x.IsEnabled(severity, options)
445-
&& not (List.contains n options.WarnAsWarn)
446-
&& ((options.GlobalWarnAsError && not (List.contains n options.WarnOff))
421+
&& ((options.GlobalWarnAsError && not (warnOff ()))
447422
|| List.contains n options.WarnAsError)
448-
449-
// Informational become errors if explicitly WarnAsError
450-
| FSharpDiagnosticSeverity.Info -> List.contains x.Number options.WarnAsError
451-
452-
| FSharpDiagnosticSeverity.Hidden -> false
423+
&& not (List.contains n options.WarnAsWarn)
424+
->
425+
FSharpDiagnosticSeverity.Error
426+
| FSharpDiagnosticSeverity.Warning when x.IsEnabled(severity, options) && not (warnOff ()) -> FSharpDiagnosticSeverity.Warning
427+
| FSharpDiagnosticSeverity.Info when List.contains n options.WarnAsError -> FSharpDiagnosticSeverity.Error
428+
| FSharpDiagnosticSeverity.Info when List.contains n options.WarnOn && not (warnOff ()) -> FSharpDiagnosticSeverity.Warning
429+
| FSharpDiagnosticSeverity.Info when x.IsEnabled(severity, options) && not (warnOff ()) -> FSharpDiagnosticSeverity.Info
430+
| _ -> FSharpDiagnosticSeverity.Hidden
453431

454432
[<AutoOpen>]
455433
module OldStyleMessages =
@@ -2333,12 +2311,9 @@ type DiagnosticsLoggerFilteringByScopedPragmas
23332311
| None -> true
23342312

23352313
if report then
2336-
if diagnostic.ReportAsError(diagnosticOptions, severity) then
2337-
diagnosticsLogger.DiagnosticSink(diagnostic, FSharpDiagnosticSeverity.Error)
2338-
elif diagnostic.ReportAsWarning(diagnosticOptions, severity) then
2339-
diagnosticsLogger.DiagnosticSink(diagnostic, FSharpDiagnosticSeverity.Warning)
2340-
elif diagnostic.ReportAsInfo(diagnosticOptions, severity) then
2341-
diagnosticsLogger.DiagnosticSink(diagnostic, severity)
2314+
match diagnostic.AdjustSeverity(diagnosticOptions, severity) with
2315+
| FSharpDiagnosticSeverity.Hidden -> ()
2316+
| s -> diagnosticsLogger.DiagnosticSink(diagnostic, s)
23422317

23432318
override _.ErrorCount = diagnosticsLogger.ErrorCount
23442319

src/Compiler/Driver/CompilerDiagnostics.fsi

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,8 @@ type PhasedDiagnostic with
6161
/// Format the core of the diagnostic as a string. Doesn't include the range information.
6262
member FormatCore: flattenErrors: bool * suggestNames: bool -> string
6363

64-
/// Indicates if a diagnostic should be reported as an informational
65-
member ReportAsInfo: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> bool
66-
67-
/// Indicates if a diagnostic should be reported as a warning
68-
member ReportAsWarning: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> bool
69-
70-
/// Indicates if a diagnostic should be reported as an error
71-
member ReportAsError: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> bool
64+
/// Compute new severity according to the various diagnostics options
65+
member AdjustSeverity: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> FSharpDiagnosticSeverity
7266

7367
/// Output all of a diagnostic to a buffer, including range
7468
member Output: buf: StringBuilder * tcConfig: TcConfig * severity: FSharpDiagnosticSeverity -> unit

src/Compiler/Driver/fsc.fs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ type DiagnosticsLoggerUpToMaxErrors(tcConfigB: TcConfigBuilder, exiter: Exiter,
7777
override x.DiagnosticSink(diagnostic, severity) =
7878
let tcConfig = TcConfig.Create(tcConfigB, validate = false)
7979

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

96-
elif diagnostic.ReportAsWarning(tcConfig.diagnosticsOptions, severity) then
97-
x.HandleIssue(tcConfig, diagnostic, FSharpDiagnosticSeverity.Warning)
98-
99-
elif diagnostic.ReportAsInfo(tcConfig.diagnosticsOptions, severity) then
100-
x.HandleIssue(tcConfig, diagnostic, severity)
97+
| FSharpDiagnosticSeverity.Hidden -> ()
98+
| s -> x.HandleIssue(tcConfig, diagnostic, s)
10199

102100
/// Create an error logger that counts and prints errors
103101
let ConsoleDiagnosticsLogger (tcConfigB: TcConfigBuilder, exiter: Exiter) =

src/Compiler/Interactive/fsi.fs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -898,28 +898,23 @@ type internal DiagnosticsLoggerThatStopsOnFirstError
898898
override _.DiagnosticSink(diagnostic, severity) =
899899
let tcConfig = TcConfig.Create(tcConfigB, validate = false)
900900

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

905906
if tcConfigB.abortOnError then
906907
exit 1 (* non-zero exit code *)
907908
// STOP ON FIRST ERROR (AVOIDS PARSER ERROR RECOVERY)
908909
raise StopProcessing
909-
elif diagnostic.ReportAsWarning(tcConfig.diagnosticsOptions, severity) then
910-
DoWithDiagnosticColor FSharpDiagnosticSeverity.Warning (fun () ->
911-
fsiConsoleOutput.Error.WriteLine()
912-
diagnostic.WriteWithContext(fsiConsoleOutput.Error, " ", fsiStdinSyphon.GetLine, tcConfig, severity)
913-
fsiConsoleOutput.Error.WriteLine()
914-
fsiConsoleOutput.Error.WriteLine()
915-
fsiConsoleOutput.Error.Flush())
916-
elif diagnostic.ReportAsInfo(tcConfig.diagnosticsOptions, severity) then
917-
DoWithDiagnosticColor FSharpDiagnosticSeverity.Info (fun () ->
910+
| (FSharpDiagnosticSeverity.Warning | FSharpDiagnosticSeverity.Info) as adjustedSeverity ->
911+
DoWithDiagnosticColor adjustedSeverity (fun () ->
918912
fsiConsoleOutput.Error.WriteLine()
919913
diagnostic.WriteWithContext(fsiConsoleOutput.Error, " ", fsiStdinSyphon.GetLine, tcConfig, severity)
920914
fsiConsoleOutput.Error.WriteLine()
921915
fsiConsoleOutput.Error.WriteLine()
922916
fsiConsoleOutput.Error.Flush())
917+
| FSharpDiagnosticSeverity.Hidden -> ()
923918

924919
override _.ErrorCount = errorCount
925920

src/Compiler/Symbols/FSharpDiagnostic.fs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,12 @@ type internal CompilationDiagnosticLogger (debugName: string, options: FSharpDia
304304
| Some f -> f diagnostic
305305
| None -> diagnostic
306306

307-
if diagnostic.ReportAsError (options, severity) then
307+
match diagnostic.AdjustSeverity(options, severity) with
308+
| FSharpDiagnosticSeverity.Error ->
308309
diagnostics.Add(diagnostic, FSharpDiagnosticSeverity.Error)
309310
errorCount <- errorCount + 1
310-
elif diagnostic.ReportAsWarning (options, severity) then
311-
diagnostics.Add(diagnostic, FSharpDiagnosticSeverity.Warning)
312-
elif diagnostic.ReportAsInfo (options, severity) then
313-
diagnostics.Add(diagnostic, severity)
311+
| FSharpDiagnosticSeverity.Hidden -> ()
312+
| sev -> diagnostics.Add(diagnostic, sev)
314313

315314
override _.ErrorCount = errorCount
316315

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

321320
let ReportDiagnostic (options: FSharpDiagnosticOptions, allErrors, mainInputFileName, fileInfo, diagnostic: PhasedDiagnostic, severity, suggestNames, flatErrors, symbolEnv) =
322-
[ let severity =
323-
if diagnostic.ReportAsError (options, severity) then
324-
FSharpDiagnosticSeverity.Error
325-
else
326-
severity
327-
328-
if severity = FSharpDiagnosticSeverity.Error ||
329-
diagnostic.ReportAsWarning (options, severity) ||
330-
diagnostic.ReportAsInfo (options, severity) then
321+
match diagnostic.AdjustSeverity(options, severity) with
322+
| FSharpDiagnosticSeverity.Hidden -> []
323+
| adjustedSeverity ->
331324

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

340334
let CreateDiagnostics (options, allErrors, mainInputFileName, diagnostics, suggestNames, flatErrors, symbolEnv) =
341335
let fileInfo = (Int32.MaxValue, Int32.MaxValue)

0 commit comments

Comments
 (0)