diff --git a/src/Compiler/Driver/CompilerDiagnostics.fs b/src/Compiler/Driver/CompilerDiagnostics.fs index 1c50ca26781..83554e74834 100644 --- a/src/Compiler/Driver/CompilerDiagnostics.fs +++ b/src/Compiler/Driver/CompilerDiagnostics.fs @@ -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 [] module OldStyleMessages = @@ -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 diff --git a/src/Compiler/Driver/CompilerDiagnostics.fsi b/src/Compiler/Driver/CompilerDiagnostics.fsi index 6139da434cf..cdf559c301a 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 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 ec91e6dc55c..1d17950a9ac 100644 --- a/src/Compiler/Driver/fsc.fs +++ b/src/Compiler/Driver/fsc.fs @@ -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 @@ -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) = diff --git a/src/Compiler/Interactive/fsi.fs b/src/Compiler/Interactive/fsi.fs index c1c60d6e47d..63e99d1b148 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.AdjustSeverity(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..31ec0536c3e 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.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 @@ -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)