From 474a874784efe0ca98f2d981dc511ae30cbe6754 Mon Sep 17 00:00:00 2001 From: Martin521 <29605222+Martin521@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:16:08 +0000 Subject: [PATCH 1/4] simplify diagnostics severity adjustment --- src/Compiler/Driver/CompilerDiagnostics.fs | 60 +++++++-------------- src/Compiler/Driver/CompilerDiagnostics.fsi | 10 +--- src/Compiler/Driver/fsc.fs | 10 ++-- src/Compiler/Interactive/fsi.fs | 15 ++---- src/Compiler/Symbols/FSharpDiagnostic.fs | 26 ++++----- 5 files changed, 39 insertions(+), 82 deletions(-) diff --git a/src/Compiler/Driver/CompilerDiagnostics.fs b/src/Compiler/Driver/CompilerDiagnostics.fs index 1c50ca26781..c77ea7d4952 100644 --- a/src/Compiler/Driver/CompilerDiagnostics.fs +++ b/src/Compiler/Driver/CompilerDiagnostics.fs @@ -409,47 +409,26 @@ 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.AdjustedSeverity(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 [] module OldStyleMessages = @@ -2333,12 +2312,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.AdjustedSeverity(diagnosticOptions, severity) with + | FSharpDiagnosticSeverity.Hidden -> () + | s -> diagnosticsLogger.DiagnosticSink(diagnostic, s) override _.ErrorCount = diagnosticsLogger.ErrorCount diff --git a/src/Compiler/Driver/CompilerDiagnostics.fsi b/src/Compiler/Driver/CompilerDiagnostics.fsi index 6139da434cf..22262822669 100644 --- a/src/Compiler/Driver/CompilerDiagnostics.fsi +++ b/src/Compiler/Driver/CompilerDiagnostics.fsi @@ -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 AdjustedSeverity: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> FSharpDiagnosticSeverity /// Output all of a diagnostic to a buffer, including range member Output: buf: StringBuilder * tcConfig: TcConfig * severity: FSharpDiagnosticSeverity -> unit diff --git a/src/Compiler/Driver/fsc.fs b/src/Compiler/Driver/fsc.fs index ac4ee179538..d59396a6667 100644 --- a/src/Compiler/Driver/fsc.fs +++ b/src/Compiler/Driver/fsc.fs @@ -76,7 +76,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.AdjustedSeverity(tcConfigB.diagnosticsOptions, severity) with + | FSharpDiagnosticSeverity.Error -> if errors >= tcConfig.maxErrors then x.HandleTooManyErrors(FSComp.SR.fscTooManyErrors ()) exiter.Exit 1 @@ -92,11 +93,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) = diff --git a/src/Compiler/Interactive/fsi.fs b/src/Compiler/Interactive/fsi.fs index 74fcc37340c..8851fcf15f7 100644 --- a/src/Compiler/Interactive/fsi.fs +++ b/src/Compiler/Interactive/fsi.fs @@ -898,7 +898,8 @@ type internal DiagnosticsLoggerThatStopsOnFirstError override _.DiagnosticSink(diagnostic, severity) = let tcConfig = TcConfig.Create(tcConfigB, validate = false) - if diagnostic.ReportAsError(tcConfig.diagnosticsOptions, severity) then + match diagnostic.AdjustedSeverity(tcConfig.diagnosticsOptions, severity) with + | FSharpDiagnosticSeverity.Error -> fsiStdinSyphon.PrintDiagnostic(tcConfig, diagnostic) errorCount <- errorCount + 1 @@ -906,20 +907,14 @@ type internal DiagnosticsLoggerThatStopsOnFirstError 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 diff --git a/src/Compiler/Symbols/FSharpDiagnostic.fs b/src/Compiler/Symbols/FSharpDiagnostic.fs index 46566d61bbf..03fc76e9c35 100644 --- a/src/Compiler/Symbols/FSharpDiagnostic.fs +++ b/src/Compiler/Symbols/FSharpDiagnostic.fs @@ -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.AdjustedSeverity(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 @@ -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.AdjustedSeverity(options, severity) with + | FSharpDiagnosticSeverity.Hidden -> [] + | sev -> // 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, sev, 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) From 04c17ad0de72b0943ef158342521b517786681c0 Mon Sep 17 00:00:00 2001 From: Martin521 <29605222+Martin521@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:18:27 +0000 Subject: [PATCH 2/4] formatting --- src/Compiler/Driver/CompilerDiagnostics.fs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Compiler/Driver/CompilerDiagnostics.fs b/src/Compiler/Driver/CompilerDiagnostics.fs index c77ea7d4952..d32e875efe3 100644 --- a/src/Compiler/Driver/CompilerDiagnostics.fs +++ b/src/Compiler/Driver/CompilerDiagnostics.fs @@ -412,8 +412,7 @@ type PhasedDiagnostic with member x.AdjustedSeverity(options, severity) = let n = x.Number - let warnOff () = - List.contains n options.WarnOff + let warnOff () = List.contains n options.WarnOff match severity with | FSharpDiagnosticSeverity.Error -> FSharpDiagnosticSeverity.Error From 67e90636eadab2351f1883eabc6a10e2be1abbfe Mon Sep 17 00:00:00 2001 From: Martin521 <29605222+Martin521@users.noreply.github.com> Date: Sat, 19 Oct 2024 09:17:46 +0000 Subject: [PATCH 3/4] name fix --- src/Compiler/Symbols/FSharpDiagnostic.fs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compiler/Symbols/FSharpDiagnostic.fs b/src/Compiler/Symbols/FSharpDiagnostic.fs index 03fc76e9c35..8d6396d9b24 100644 --- a/src/Compiler/Symbols/FSharpDiagnostic.fs +++ b/src/Compiler/Symbols/FSharpDiagnostic.fs @@ -320,12 +320,12 @@ module DiagnosticHelpers = let ReportDiagnostic (options: FSharpDiagnosticOptions, allErrors, mainInputFileName, fileInfo, diagnostic: PhasedDiagnostic, severity, suggestNames, flatErrors, symbolEnv) = match diagnostic.AdjustedSeverity(options, severity) with | FSharpDiagnosticSeverity.Hidden -> [] - | sev -> + | 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, sev, 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 [diagnostic] From a2454213e6d74b0e830a11ed308e5612b0cc398a Mon Sep 17 00:00:00 2001 From: Martin521 <29605222+Martin521@users.noreply.github.com> Date: Sun, 20 Oct 2024 09:57:28 +0000 Subject: [PATCH 4/4] member name changed --- src/Compiler/Driver/CompilerDiagnostics.fs | 4 ++-- src/Compiler/Driver/CompilerDiagnostics.fsi | 2 +- src/Compiler/Driver/fsc.fs | 2 +- src/Compiler/Interactive/fsi.fs | 2 +- src/Compiler/Symbols/FSharpDiagnostic.fs | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Compiler/Driver/CompilerDiagnostics.fs b/src/Compiler/Driver/CompilerDiagnostics.fs index d32e875efe3..83554e74834 100644 --- a/src/Compiler/Driver/CompilerDiagnostics.fs +++ b/src/Compiler/Driver/CompilerDiagnostics.fs @@ -409,7 +409,7 @@ type PhasedDiagnostic with (severity = FSharpDiagnosticSeverity.Info && level > 0) || (severity = FSharpDiagnosticSeverity.Warning && level >= x.WarningLevel) - member x.AdjustedSeverity(options, severity) = + member x.AdjustSeverity(options, severity) = let n = x.Number let warnOff () = List.contains n options.WarnOff @@ -2311,7 +2311,7 @@ type DiagnosticsLoggerFilteringByScopedPragmas | None -> true if report then - match diagnostic.AdjustedSeverity(diagnosticOptions, severity) with + match diagnostic.AdjustSeverity(diagnosticOptions, severity) with | FSharpDiagnosticSeverity.Hidden -> () | s -> diagnosticsLogger.DiagnosticSink(diagnostic, s) diff --git a/src/Compiler/Driver/CompilerDiagnostics.fsi b/src/Compiler/Driver/CompilerDiagnostics.fsi index 22262822669..cdf559c301a 100644 --- a/src/Compiler/Driver/CompilerDiagnostics.fsi +++ b/src/Compiler/Driver/CompilerDiagnostics.fsi @@ -62,7 +62,7 @@ type PhasedDiagnostic with member FormatCore: flattenErrors: bool * suggestNames: bool -> string /// Compute new severity according to the various diagnostics options - member AdjustedSeverity: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> FSharpDiagnosticSeverity + member AdjustSeverity: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> FSharpDiagnosticSeverity /// Output all of a diagnostic to a buffer, including range member Output: buf: StringBuilder * tcConfig: TcConfig * severity: FSharpDiagnosticSeverity -> unit diff --git a/src/Compiler/Driver/fsc.fs b/src/Compiler/Driver/fsc.fs index d59396a6667..68299534adf 100644 --- a/src/Compiler/Driver/fsc.fs +++ b/src/Compiler/Driver/fsc.fs @@ -76,7 +76,7 @@ type DiagnosticsLoggerUpToMaxErrors(tcConfigB: TcConfigBuilder, exiter: Exiter, override x.DiagnosticSink(diagnostic, severity) = let tcConfig = TcConfig.Create(tcConfigB, validate = false) - match diagnostic.AdjustedSeverity(tcConfigB.diagnosticsOptions, severity) with + match diagnostic.AdjustSeverity(tcConfigB.diagnosticsOptions, severity) with | FSharpDiagnosticSeverity.Error -> if errors >= tcConfig.maxErrors then x.HandleTooManyErrors(FSComp.SR.fscTooManyErrors ()) diff --git a/src/Compiler/Interactive/fsi.fs b/src/Compiler/Interactive/fsi.fs index 8851fcf15f7..a279e945041 100644 --- a/src/Compiler/Interactive/fsi.fs +++ b/src/Compiler/Interactive/fsi.fs @@ -898,7 +898,7 @@ type internal DiagnosticsLoggerThatStopsOnFirstError override _.DiagnosticSink(diagnostic, severity) = let tcConfig = TcConfig.Create(tcConfigB, validate = false) - match diagnostic.AdjustedSeverity(tcConfig.diagnosticsOptions, severity) with + match diagnostic.AdjustSeverity(tcConfig.diagnosticsOptions, severity) with | FSharpDiagnosticSeverity.Error -> fsiStdinSyphon.PrintDiagnostic(tcConfig, diagnostic) errorCount <- errorCount + 1 diff --git a/src/Compiler/Symbols/FSharpDiagnostic.fs b/src/Compiler/Symbols/FSharpDiagnostic.fs index 8d6396d9b24..31ec0536c3e 100644 --- a/src/Compiler/Symbols/FSharpDiagnostic.fs +++ b/src/Compiler/Symbols/FSharpDiagnostic.fs @@ -304,7 +304,7 @@ type internal CompilationDiagnosticLogger (debugName: string, options: FSharpDia | Some f -> f diagnostic | None -> diagnostic - match diagnostic.AdjustedSeverity(options, severity) with + match diagnostic.AdjustSeverity(options, severity) with | FSharpDiagnosticSeverity.Error -> diagnostics.Add(diagnostic, FSharpDiagnosticSeverity.Error) errorCount <- errorCount + 1 @@ -318,7 +318,7 @@ type internal CompilationDiagnosticLogger (debugName: string, options: FSharpDia module DiagnosticHelpers = let ReportDiagnostic (options: FSharpDiagnosticOptions, allErrors, mainInputFileName, fileInfo, diagnostic: PhasedDiagnostic, severity, suggestNames, flatErrors, symbolEnv) = - match diagnostic.AdjustedSeverity(options, severity) with + match diagnostic.AdjustSeverity(options, severity) with | FSharpDiagnosticSeverity.Hidden -> [] | adjustedSeverity ->