Skip to content

Commit 1112c72

Browse files
authored
FSharpDiagnostic: add default severity (#19152)
1 parent efe3c9d commit 1112c72

29 files changed

+210
-159
lines changed

docs/release-notes/.FSharp.Compiler.Service/10.0.200.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
* Type relations cache: optimize key generation ([Issue #19116](https://github.com/dotnet/fsharp/issues/18767)) ([PR #19120](https://github.com/dotnet/fsharp/pull/19120))
44

5+
### Added
6+
7+
* FSharpDiagnostic: add default severity ([#19152](https://github.com/dotnet/fsharp/pull/19152))
8+
59
### Breaking Changes
610

711
* `SynExpr.LetOrUse` holds `SynLetOrUse`. ([PR #19090](https://github.com/dotnet/fsharp/pull/19090))

docs/release-notes/.FSharp.Compiler.Service/11.0.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Add FSharpCodeCompletionOptions ([PR #19030](https://github.com/dotnet/fsharp/pull/19030))
2121
* Type checker: recover on checking binding parameter constraints ([#19046](https://github.com/dotnet/fsharp/pull/19046))
2222
* Debugger: provide breakpoint ranges for short lambdas ([#19067](https://github.com/dotnet/fsharp/pull/19067))
23+
* FSharpDiagnostic: add default severity ([#19152](https://github.com/dotnet/fsharp/pull/19152))
2324

2425
### Changed
2526

src/Compiler/Driver/CompilerDiagnostics.fs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ type PhasedDiagnostic with
405405
(severity = FSharpDiagnosticSeverity.Info && level > 0)
406406
|| (severity = FSharpDiagnosticSeverity.Warning && level >= x.WarningLevel)
407407

408-
member x.AdjustSeverity(options, severity) =
408+
member x.AdjustSeverity(options) =
409+
let severity = x.Severity
409410
let n = x.Number
410411

411412
let localWarnon () = WarnScopes.IsWarnon options n x.Range
@@ -2007,7 +2008,7 @@ type PhasedDiagnostic with
20072008
x.Exception.Output(buf, suggestNames)
20082009
let message = buf.ToString()
20092010
let exn = DiagnosticWithText(x.Number, message, m)
2010-
{ Exception = exn; Phase = x.Phase }
2011+
{ x with Exception = exn }
20112012
| None -> x
20122013

20132014
let SanitizeFileName fileName implicitIncludeDir =
@@ -2313,15 +2314,15 @@ type DiagnosticsLoggerFilteringByScopedNowarn(diagnosticOptions: FSharpDiagnosti
23132314

23142315
let mutable realErrorPresent = false
23152316

2316-
override _.DiagnosticSink(diagnostic: PhasedDiagnostic, severity) =
2317+
override _.DiagnosticSink(diagnostic: PhasedDiagnostic) =
23172318

2318-
if severity = FSharpDiagnosticSeverity.Error then
2319+
if diagnostic.Severity = FSharpDiagnosticSeverity.Error then
23192320
realErrorPresent <- true
2320-
diagnosticsLogger.DiagnosticSink(diagnostic, severity)
2321+
diagnosticsLogger.DiagnosticSink(diagnostic)
23212322
else
2322-
match diagnostic.AdjustSeverity(diagnosticOptions, severity) with
2323+
match diagnostic.AdjustSeverity(diagnosticOptions) with
23232324
| FSharpDiagnosticSeverity.Hidden -> ()
2324-
| s -> diagnosticsLogger.DiagnosticSink(diagnostic, s)
2325+
| s -> diagnosticsLogger.DiagnosticSink({ diagnostic with Severity = s })
23252326

23262327
override _.ErrorCount = diagnosticsLogger.ErrorCount
23272328

src/Compiler/Driver/CompilerDiagnostics.fsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type PhasedDiagnostic with
6161
member FormatCore: flattenErrors: bool * suggestNames: bool -> string
6262

6363
/// Compute new severity according to the various diagnostics options
64-
member AdjustSeverity: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> FSharpDiagnosticSeverity
64+
member AdjustSeverity: FSharpDiagnosticOptions -> FSharpDiagnosticSeverity
6565

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

src/Compiler/Driver/ScriptClosure.fs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ type LoadClosureInput =
2929
{
3030
FileName: string
3131
SyntaxTree: ParsedInput option
32-
ParseDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
33-
MetaCommandDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
32+
ParseDiagnostics: PhasedDiagnostic list
33+
MetaCommandDiagnostics: PhasedDiagnostic list
3434
}
3535

3636
[<RequireQualifiedAccess>]
@@ -64,13 +64,13 @@ type LoadClosure =
6464
OriginalLoadReferences: (range * string * string) list
6565

6666
/// Diagnostics seen while processing resolutions
67-
ResolutionDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
67+
ResolutionDiagnostics: PhasedDiagnostic list
6868

6969
/// Diagnostics seen while parsing root of closure
70-
AllRootFileDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
70+
AllRootFileDiagnostics: PhasedDiagnostic list
7171

7272
/// Diagnostics seen while processing the compiler options implied root of closure
73-
LoadClosureRootFileDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
73+
LoadClosureRootFileDiagnostics: PhasedDiagnostic list
7474
}
7575

7676
[<RequireQualifiedAccess>]
@@ -91,8 +91,8 @@ module ScriptPreprocessClosure =
9191
fileName: string *
9292
range: range *
9393
parsedInput: ParsedInput option *
94-
parseDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list *
95-
metaDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
94+
parseDiagnostics: PhasedDiagnostic list *
95+
metaDiagnostics: PhasedDiagnostic list
9696

9797
type Observed() =
9898
let seen = Dictionary<_, bool>()
@@ -594,7 +594,7 @@ module ScriptPreprocessClosure =
594594
| None -> true
595595

596596
// Filter out non-root errors and warnings
597-
let allRootDiagnostics = allRootDiagnostics |> List.filter (fst >> isRootRange)
597+
let allRootDiagnostics = allRootDiagnostics |> List.filter isRootRange
598598

599599
{
600600
SourceFiles = List.groupBy fst sourceFiles |> List.map (map2Of2 (List.map snd))

src/Compiler/Driver/ScriptClosure.fsi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ type LoadClosureInput =
2626

2727
SyntaxTree: ParsedInput option
2828

29-
ParseDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
29+
ParseDiagnostics: PhasedDiagnostic list
3030

31-
MetaCommandDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list }
31+
MetaCommandDiagnostics: PhasedDiagnostic list }
3232

3333
[<RequireQualifiedAccess>]
3434
type LoadClosure =
@@ -61,13 +61,13 @@ type LoadClosure =
6161
OriginalLoadReferences: (range * string * string) list
6262

6363
/// Diagnostics seen while processing resolutions
64-
ResolutionDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
64+
ResolutionDiagnostics: PhasedDiagnostic list
6565

6666
/// Diagnostics to show for root of closure (used by fsc.fs)
67-
AllRootFileDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
67+
AllRootFileDiagnostics: PhasedDiagnostic list
6868

6969
/// Diagnostics seen while processing the compiler options implied root of closure
70-
LoadClosureRootFileDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
70+
LoadClosureRootFileDiagnostics: PhasedDiagnostic list
7171
}
7272

7373
/// Analyze a script text and find the closure of its references.

src/Compiler/Driver/fsc.fs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ type DiagnosticsLoggerUpToMaxErrors(tcConfigB: TcConfigBuilder, exiter: Exiter,
7474

7575
override _.ErrorCount = errors
7676

77-
override x.DiagnosticSink(diagnostic, severity) =
77+
override x.DiagnosticSink(diagnostic) =
7878
let tcConfig = TcConfig.Create(tcConfigB, validate = false)
7979

80-
match diagnostic.AdjustSeverity(tcConfig.diagnosticsOptions, severity) with
80+
match diagnostic.AdjustSeverity(tcConfig.diagnosticsOptions) with
8181
| FSharpDiagnosticSeverity.Error ->
8282
if errors >= tcConfig.maxErrors then
8383
x.HandleTooManyErrors(FSComp.SR.fscTooManyErrors ())
@@ -89,9 +89,8 @@ type DiagnosticsLoggerUpToMaxErrors(tcConfigB: TcConfigBuilder, exiter: Exiter,
8989

9090
match diagnostic.Exception, tcConfigB.simulateException with
9191
| InternalError(msg, _), None
92-
| Failure msg, None -> Debug.Assert(false, sprintf "Bug in compiler: %s\n%s" msg (diagnostic.Exception.ToString()))
93-
| :? KeyNotFoundException, None ->
94-
Debug.Assert(false, sprintf "Lookup exception in compiler: %s" (diagnostic.Exception.ToString()))
92+
| Failure msg, None -> Debug.Assert(false, sprintf "Bug in compiler: %s\n%s" msg (diagnostic.ToString()))
93+
| :? KeyNotFoundException, None -> Debug.Assert(false, sprintf "Lookup exception in compiler: %s" (diagnostic.ToString()))
9594
| _ -> ()
9695

9796
| FSharpDiagnosticSeverity.Hidden -> ()

src/Compiler/Driver/fsc.fsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type DiagnosticsLoggerUpToMaxErrors =
3939

4040
override ErrorCount: int
4141

42-
override DiagnosticSink: diagnostic: PhasedDiagnostic * severity: FSharpDiagnosticSeverity -> unit
42+
override DiagnosticSink: diagnostic: PhasedDiagnostic -> unit
4343

4444
/// The main (non-incremental) compilation entry point used by fsc.exe
4545
val CompileFromCommandLineArguments:

src/Compiler/Facilities/DiagnosticsLogger.fs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,18 @@ type PhasedDiagnostic =
286286
{
287287
Exception: exn
288288
Phase: BuildPhase
289+
Severity: FSharpDiagnosticSeverity
290+
DefaultSeverity: FSharpDiagnosticSeverity
289291
}
290292

291293
/// Construct a phased error
292-
static member Create(exn: exn, phase: BuildPhase) : PhasedDiagnostic = { Exception = exn; Phase = phase }
294+
static member Create(exn: exn, phase: BuildPhase, severity: FSharpDiagnosticSeverity) : PhasedDiagnostic =
295+
{
296+
Exception = exn
297+
Phase = phase
298+
Severity = severity
299+
DefaultSeverity = severity
300+
}
293301

294302
member this.DebugDisplay() =
295303
sprintf "%s: %s" (this.Subcategory()) this.Exception.Message
@@ -355,7 +363,7 @@ type DiagnosticsLogger(nameForDebugging: string) =
355363

356364
// The 'Impl' factoring enables a developer to place a breakpoint at the non-Impl
357365
// code just below and get a breakpoint for all error logger implementations.
358-
abstract DiagnosticSink: diagnostic: PhasedDiagnostic * severity: FSharpDiagnosticSeverity -> unit
366+
abstract DiagnosticSink: diagnostic: PhasedDiagnostic -> unit
359367

360368
member x.CheckForErrors() = (x.ErrorCount > 0)
361369

@@ -367,14 +375,14 @@ type DiagnosticsLogger(nameForDebugging: string) =
367375

368376
let DiscardErrorsLogger =
369377
{ new DiagnosticsLogger("DiscardErrorsLogger") with
370-
member _.DiagnosticSink(diagnostic, severity) = ()
378+
member _.DiagnosticSink(diagnostic) = ()
371379
member _.ErrorCount = 0
372380
}
373381

374382
let AssertFalseDiagnosticsLogger =
375383
{ new DiagnosticsLogger("AssertFalseDiagnosticsLogger") with
376384
// TODO: reenable these asserts in the compiler service
377-
member _.DiagnosticSink(diagnostic, severity) = (* assert false; *) ()
385+
member _.DiagnosticSink(diagnostic) = (* assert false; *) ()
378386
member _.ErrorCount = (* assert false; *) 0
379387
}
380388

@@ -383,16 +391,16 @@ type CapturingDiagnosticsLogger(nm, ?eagerFormat) =
383391
let mutable errorCount = 0
384392
let diagnostics = ResizeArray()
385393

386-
override _.DiagnosticSink(diagnostic, severity) =
394+
override _.DiagnosticSink(diagnostic) =
387395
let diagnostic =
388396
match eagerFormat with
389397
| None -> diagnostic
390398
| Some f -> f diagnostic
391399

392-
if severity = FSharpDiagnosticSeverity.Error then
400+
if diagnostic.Severity = FSharpDiagnosticSeverity.Error then
393401
errorCount <- errorCount + 1
394402

395-
diagnostics.Add(diagnostic, severity)
403+
diagnostics.Add(diagnostic)
396404

397405
override _.ErrorCount = errorCount
398406

@@ -457,7 +465,7 @@ module DiagnosticsLoggerExtensions =
457465
| ReportedError _ ->
458466
PreserveStackTrace exn
459467
raise exn
460-
| _ -> x.DiagnosticSink(PhasedDiagnostic.Create(exn, DiagnosticsThreadStatics.BuildPhase), severity)
468+
| _ -> x.DiagnosticSink(PhasedDiagnostic.Create(exn, DiagnosticsThreadStatics.BuildPhase, severity))
461469

462470
member x.ErrorR exn =
463471
x.EmitDiagnostic(exn, FSharpDiagnosticSeverity.Error)
@@ -472,8 +480,8 @@ module DiagnosticsLoggerExtensions =
472480
x.ErrorR exn
473481
raise (ReportedError(Some exn))
474482

475-
member x.SimulateError diagnostic =
476-
x.DiagnosticSink(diagnostic, FSharpDiagnosticSeverity.Error)
483+
member x.SimulateError(diagnostic) =
484+
x.DiagnosticSink(diagnostic)
477485
raise (ReportedError(Some diagnostic.Exception))
478486

479487
member x.ErrorRecovery (exn: exn) (m: range) =
@@ -580,17 +588,11 @@ let error exn =
580588
DiagnosticsThreadStatics.DiagnosticsLogger.Error exn
581589

582590
/// Simulates an error. For test purposes only.
583-
let simulateError (diagnostic: PhasedDiagnostic) =
584-
DiagnosticsThreadStatics.DiagnosticsLogger.SimulateError diagnostic
585-
586-
let diagnosticSink (diagnostic, severity) =
587-
DiagnosticsThreadStatics.DiagnosticsLogger.DiagnosticSink(diagnostic, severity)
588-
589-
let errorSink diagnostic =
590-
diagnosticSink (diagnostic, FSharpDiagnosticSeverity.Error)
591+
let simulateError diagnostic =
592+
DiagnosticsThreadStatics.DiagnosticsLogger.SimulateError(diagnostic)
591593

592-
let warnSink diagnostic =
593-
diagnosticSink (diagnostic, FSharpDiagnosticSeverity.Warning)
594+
let diagnosticSink diagnostic =
595+
DiagnosticsThreadStatics.DiagnosticsLogger.DiagnosticSink(diagnostic)
594596

595597
let errorRecovery exn m =
596598
DiagnosticsThreadStatics.DiagnosticsLogger.ErrorRecovery exn m
@@ -623,7 +625,7 @@ let suppressErrorReporting f =
623625
try
624626
let diagnosticsLogger =
625627
{ new DiagnosticsLogger("suppressErrorReporting") with
626-
member _.DiagnosticSink(_phasedError, _isError) = ()
628+
member _.DiagnosticSink(_diagnostic) = ()
627629
member _.ErrorCount = 0
628630
}
629631

src/Compiler/Facilities/DiagnosticsLogger.fsi

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,12 @@ module BuildPhaseSubcategory =
178178

179179
type PhasedDiagnostic =
180180
{ Exception: exn
181-
Phase: BuildPhase }
181+
Phase: BuildPhase
182+
Severity: FSharpDiagnosticSeverity
183+
DefaultSeverity: FSharpDiagnosticSeverity }
182184

183185
/// Construct a phased error
184-
static member Create: exn: exn * phase: BuildPhase -> PhasedDiagnostic
186+
static member Create: exn: exn * phase: BuildPhase * severity: FSharpDiagnosticSeverity -> PhasedDiagnostic
185187

186188
/// Return true if the textual phase given is from the compile part of the build process.
187189
/// This set needs to be equal to the set of subcategories that the language service can produce.
@@ -208,7 +210,7 @@ type DiagnosticsLogger =
208210
member DebugDisplay: unit -> string
209211

210212
/// Emit a diagnostic to the logger
211-
abstract DiagnosticSink: diagnostic: PhasedDiagnostic * severity: FSharpDiagnosticSeverity -> unit
213+
abstract DiagnosticSink: diagnostic: PhasedDiagnostic -> unit
212214

213215
/// Get the number of error diagnostics reported
214216
abstract ErrorCount: int
@@ -235,9 +237,9 @@ type CapturingDiagnosticsLogger =
235237

236238
member CommitDelayedDiagnostics: diagnosticsLogger: DiagnosticsLogger -> unit
237239

238-
override DiagnosticSink: diagnostic: PhasedDiagnostic * severity: FSharpDiagnosticSeverity -> unit
240+
override DiagnosticSink: diagnostic: PhasedDiagnostic -> unit
239241

240-
member Diagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
242+
member Diagnostics: PhasedDiagnostic list
241243

242244
override ErrorCount: int
243245

@@ -313,11 +315,7 @@ val informationalWarning: exn: exn -> unit
313315

314316
val simulateError: diagnostic: PhasedDiagnostic -> 'T
315317

316-
val diagnosticSink: diagnostic: PhasedDiagnostic * severity: FSharpDiagnosticSeverity -> unit
317-
318-
val errorSink: diagnostic: PhasedDiagnostic -> unit
319-
320-
val warnSink: diagnostic: PhasedDiagnostic -> unit
318+
val diagnosticSink: diagnostic: PhasedDiagnostic -> unit
321319

322320
val errorRecovery: exn: exn -> m: range -> unit
323321

0 commit comments

Comments
 (0)