Skip to content

Commit ccac85f

Browse files
committed
wip
1 parent 6ef4403 commit ccac85f

23 files changed

+218
-154
lines changed

src/Compiler/Driver/CompilerDiagnostics.fs

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

408-
member x.AdjustSeverity(options, severity) =
408+
type PhasedDiagnosticWithSeverity with
409+
member x.AdjustSeverity(options) =
410+
let {
411+
PhasedDiagnostic = x
412+
Severity = severity
413+
} =
414+
x
415+
409416
let n = x.Number
410417

411418
let localWarnon () = WarnScopes.IsWarnon options n x.Range
@@ -2313,15 +2320,15 @@ type DiagnosticsLoggerFilteringByScopedNowarn(diagnosticOptions: FSharpDiagnosti
23132320

23142321
let mutable realErrorPresent = false
23152322

2316-
override _.DiagnosticSink(diagnostic: PhasedDiagnostic, severity) =
2323+
override _.DiagnosticSink(diagnostic: PhasedDiagnosticWithSeverity) =
23172324

2318-
if severity = FSharpDiagnosticSeverity.Error then
2325+
if diagnostic.Severity = FSharpDiagnosticSeverity.Error then
23192326
realErrorPresent <- true
2320-
diagnosticsLogger.DiagnosticSink(diagnostic, severity)
2327+
diagnosticsLogger.DiagnosticSink(diagnostic)
23212328
else
2322-
match diagnostic.AdjustSeverity(diagnosticOptions, severity) with
2329+
match diagnostic.AdjustSeverity(diagnosticOptions) with
23232330
| FSharpDiagnosticSeverity.Hidden -> ()
2324-
| s -> diagnosticsLogger.DiagnosticSink(diagnostic, s)
2331+
| s -> diagnosticsLogger.DiagnosticSink({ diagnostic with Severity = s })
23252332

23262333
override _.ErrorCount = diagnosticsLogger.ErrorCount
23272334

src/Compiler/Driver/CompilerDiagnostics.fsi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ type PhasedDiagnostic with
6060
/// Format the core of the diagnostic as a string. Doesn't include the range information.
6161
member FormatCore: flattenErrors: bool * suggestNames: bool -> string
6262

63-
/// Compute new severity according to the various diagnostics options
64-
member AdjustSeverity: FSharpDiagnosticOptions * FSharpDiagnosticSeverity -> FSharpDiagnosticSeverity
65-
6663
/// Output all of a diagnostic to a buffer, including range
6764
member Output: buf: StringBuilder * tcConfig: TcConfig * severity: FSharpDiagnosticSeverity -> unit
6865

@@ -75,6 +72,10 @@ type PhasedDiagnostic with
7572
severity: FSharpDiagnosticSeverity ->
7673
unit
7774

75+
type PhasedDiagnosticWithSeverity with
76+
/// Compute new severity according to the various diagnostics options
77+
member AdjustSeverity: FSharpDiagnosticOptions -> FSharpDiagnosticSeverity
78+
7879
/// Get a diagnostics logger that filters the reporting of warnings based on scoped pragma information
7980
val GetDiagnosticsLoggerFilteringByScopedNowarn:
8081
diagnosticOptions: FSharpDiagnosticOptions * diagnosticsLogger: DiagnosticsLogger -> DiagnosticsLogger

src/Compiler/Driver/ScriptClosure.fs

Lines changed: 9 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: PhasedDiagnosticWithSeverity list
33+
MetaCommandDiagnostics: PhasedDiagnosticWithSeverity 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: PhasedDiagnosticWithSeverity list
6868

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

7272
/// Diagnostics seen while processing the compiler options implied root of closure
73-
LoadClosureRootFileDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
73+
LoadClosureRootFileDiagnostics: PhasedDiagnosticWithSeverity 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: PhasedDiagnosticWithSeverity list *
95+
metaDiagnostics: PhasedDiagnosticWithSeverity list
9696

9797
type Observed() =
9898
let seen = Dictionary<_, bool>()
@@ -594,7 +594,8 @@ 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 =
598+
allRootDiagnostics |> List.filter (isRootRange << _.PhasedDiagnostic)
598599

599600
{
600601
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: PhasedDiagnosticWithSeverity list
3030

31-
MetaCommandDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list }
31+
MetaCommandDiagnostics: PhasedDiagnosticWithSeverity 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: PhasedDiagnosticWithSeverity list
6565

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

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

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

src/Compiler/Driver/fsc.fs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,28 @@ 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)
79+
let phasedDiagnostic = diagnostic.PhasedDiagnostic
7980

80-
match diagnostic.AdjustSeverity(tcConfig.diagnosticsOptions, severity) with
81+
match diagnostic.AdjustSeverity(tcConfig.diagnosticsOptions) with
8182
| FSharpDiagnosticSeverity.Error ->
8283
if errors >= tcConfig.maxErrors then
8384
x.HandleTooManyErrors(FSComp.SR.fscTooManyErrors ())
8485
exiter.Exit 1
8586

86-
x.HandleIssue(tcConfig, diagnostic, FSharpDiagnosticSeverity.Error)
87+
x.HandleIssue(tcConfig, phasedDiagnostic, FSharpDiagnosticSeverity.Error)
8788

8889
errors <- errors + 1
8990

90-
match diagnostic.Exception, tcConfigB.simulateException with
91+
match phasedDiagnostic.Exception, tcConfigB.simulateException with
9192
| 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()))
93+
| Failure msg, None -> Debug.Assert(false, sprintf "Bug in compiler: %s\n%s" msg (phasedDiagnostic.ToString()))
94+
| :? KeyNotFoundException, None -> Debug.Assert(false, sprintf "Lookup exception in compiler: %s" (phasedDiagnostic.ToString()))
9595
| _ -> ()
9696

9797
| FSharpDiagnosticSeverity.Hidden -> ()
98-
| s -> x.HandleIssue(tcConfig, diagnostic, s)
98+
| s -> x.HandleIssue(tcConfig, phasedDiagnostic, s)
9999

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

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: PhasedDiagnosticWithSeverity -> unit
4343

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

src/Compiler/Facilities/DiagnosticsLogger.fs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,28 @@ type PhasedDiagnostic =
348348
| BuildPhase.TypeCheck -> true
349349
| _ -> false
350350

351+
type PhasedDiagnosticWithSeverity =
352+
{
353+
PhasedDiagnostic: PhasedDiagnostic
354+
Severity: FSharpDiagnosticSeverity
355+
DefaultSeverity: FSharpDiagnosticSeverity
356+
}
357+
358+
static member Create(phasedDiagnostic, severity) =
359+
{
360+
PhasedDiagnostic = phasedDiagnostic
361+
Severity = severity
362+
DefaultSeverity = severity
363+
}
364+
351365
[<AbstractClass>]
352366
[<DebuggerDisplay("{DebugDisplay()}")>]
353367
type DiagnosticsLogger(nameForDebugging: string) =
354368
abstract ErrorCount: int
355369

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

360374
member x.CheckForErrors() = (x.ErrorCount > 0)
361375

@@ -367,14 +381,14 @@ type DiagnosticsLogger(nameForDebugging: string) =
367381

368382
let DiscardErrorsLogger =
369383
{ new DiagnosticsLogger("DiscardErrorsLogger") with
370-
member _.DiagnosticSink(diagnostic, severity) = ()
384+
member _.DiagnosticSink(diagnostic) = ()
371385
member _.ErrorCount = 0
372386
}
373387

374388
let AssertFalseDiagnosticsLogger =
375389
{ new DiagnosticsLogger("AssertFalseDiagnosticsLogger") with
376390
// TODO: reenable these asserts in the compiler service
377-
member _.DiagnosticSink(diagnostic, severity) = (* assert false; *) ()
391+
member _.DiagnosticSink(diagnostic) = (* assert false; *) ()
378392
member _.ErrorCount = (* assert false; *) 0
379393
}
380394

@@ -383,16 +397,19 @@ type CapturingDiagnosticsLogger(nm, ?eagerFormat) =
383397
let mutable errorCount = 0
384398
let diagnostics = ResizeArray()
385399

386-
override _.DiagnosticSink(diagnostic, severity) =
400+
override _.DiagnosticSink(diagnostic) =
387401
let diagnostic =
388402
match eagerFormat with
389403
| None -> diagnostic
390-
| Some f -> f diagnostic
404+
| Some f ->
405+
{ diagnostic with
406+
PhasedDiagnostic = f diagnostic.PhasedDiagnostic
407+
}
391408

392-
if severity = FSharpDiagnosticSeverity.Error then
409+
if diagnostic.Severity = FSharpDiagnosticSeverity.Error then
393410
errorCount <- errorCount + 1
394411

395-
diagnostics.Add(diagnostic, severity)
412+
diagnostics.Add(diagnostic)
396413

397414
override _.ErrorCount = errorCount
398415

@@ -457,7 +474,14 @@ module DiagnosticsLoggerExtensions =
457474
| ReportedError _ ->
458475
PreserveStackTrace exn
459476
raise exn
460-
| _ -> x.DiagnosticSink(PhasedDiagnostic.Create(exn, DiagnosticsThreadStatics.BuildPhase), severity)
477+
| _ ->
478+
let phasedDiagnostic =
479+
PhasedDiagnostic.Create(exn, DiagnosticsThreadStatics.BuildPhase)
480+
481+
let diagnosticWithSeverity =
482+
PhasedDiagnosticWithSeverity.Create(phasedDiagnostic, severity)
483+
484+
x.DiagnosticSink(diagnosticWithSeverity)
461485

462486
member x.ErrorR exn =
463487
x.EmitDiagnostic(exn, FSharpDiagnosticSeverity.Error)
@@ -472,8 +496,11 @@ module DiagnosticsLoggerExtensions =
472496
x.ErrorR exn
473497
raise (ReportedError(Some exn))
474498

475-
member x.SimulateError diagnostic =
476-
x.DiagnosticSink(diagnostic, FSharpDiagnosticSeverity.Error)
499+
member x.SimulateError(diagnostic) =
500+
let diagnosticWithSeverity =
501+
PhasedDiagnosticWithSeverity.Create(diagnostic, FSharpDiagnosticSeverity.Error)
502+
503+
x.DiagnosticSink(diagnosticWithSeverity)
477504
raise (ReportedError(Some diagnostic.Exception))
478505

479506
member x.ErrorRecovery (exn: exn) (m: range) =
@@ -580,17 +607,17 @@ let error exn =
580607
DiagnosticsThreadStatics.DiagnosticsLogger.Error exn
581608

582609
/// Simulates an error. For test purposes only.
583-
let simulateError (diagnostic: PhasedDiagnostic) =
584-
DiagnosticsThreadStatics.DiagnosticsLogger.SimulateError diagnostic
610+
let simulateError diagnostic =
611+
DiagnosticsThreadStatics.DiagnosticsLogger.SimulateError(diagnostic)
585612

586-
let diagnosticSink (diagnostic, severity) =
587-
DiagnosticsThreadStatics.DiagnosticsLogger.DiagnosticSink(diagnostic, severity)
613+
let diagnosticSink diagnostic =
614+
DiagnosticsThreadStatics.DiagnosticsLogger.DiagnosticSink(diagnostic)
588615

589616
let errorSink diagnostic =
590-
diagnosticSink (diagnostic, FSharpDiagnosticSeverity.Error)
617+
diagnosticSink (PhasedDiagnosticWithSeverity.Create(diagnostic, FSharpDiagnosticSeverity.Error))
591618

592619
let warnSink diagnostic =
593-
diagnosticSink (diagnostic, FSharpDiagnosticSeverity.Warning)
620+
diagnosticSink (PhasedDiagnosticWithSeverity.Create(diagnostic, FSharpDiagnosticSeverity.Warning))
594621

595622
let errorRecovery exn m =
596623
DiagnosticsThreadStatics.DiagnosticsLogger.ErrorRecovery exn m
@@ -623,7 +650,7 @@ let suppressErrorReporting f =
623650
try
624651
let diagnosticsLogger =
625652
{ new DiagnosticsLogger("suppressErrorReporting") with
626-
member _.DiagnosticSink(_phasedError, _isError) = ()
653+
member _.DiagnosticSink(_diagnosticWithSeverity) = ()
627654
member _.ErrorCount = 0
628655
}
629656

src/Compiler/Facilities/DiagnosticsLogger.fsi

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ type PhasedDiagnostic =
199199
///
200200
member Subcategory: unit -> string
201201

202+
type PhasedDiagnosticWithSeverity =
203+
{ PhasedDiagnostic: PhasedDiagnostic
204+
Severity: FSharpDiagnosticSeverity
205+
DefaultSeverity: FSharpDiagnosticSeverity }
206+
207+
static member Create:
208+
phasedDiagnostic: PhasedDiagnostic * severity: FSharpDiagnosticSeverity -> PhasedDiagnosticWithSeverity
209+
202210
/// Represents a capability to log diagnostics
203211
[<AbstractClass>]
204212
type DiagnosticsLogger =
@@ -208,7 +216,7 @@ type DiagnosticsLogger =
208216
member DebugDisplay: unit -> string
209217

210218
/// Emit a diagnostic to the logger
211-
abstract DiagnosticSink: diagnostic: PhasedDiagnostic * severity: FSharpDiagnosticSeverity -> unit
219+
abstract DiagnosticSink: diagnostic: PhasedDiagnosticWithSeverity -> unit
212220

213221
/// Get the number of error diagnostics reported
214222
abstract ErrorCount: int
@@ -235,9 +243,9 @@ type CapturingDiagnosticsLogger =
235243

236244
member CommitDelayedDiagnostics: diagnosticsLogger: DiagnosticsLogger -> unit
237245

238-
override DiagnosticSink: diagnostic: PhasedDiagnostic * severity: FSharpDiagnosticSeverity -> unit
246+
override DiagnosticSink: diagnostic: PhasedDiagnosticWithSeverity -> unit
239247

240-
member Diagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
248+
member Diagnostics: PhasedDiagnosticWithSeverity list
241249

242250
override ErrorCount: int
243251

@@ -313,7 +321,7 @@ val informationalWarning: exn: exn -> unit
313321

314322
val simulateError: diagnostic: PhasedDiagnostic -> 'T
315323

316-
val diagnosticSink: diagnostic: PhasedDiagnostic * severity: FSharpDiagnosticSeverity -> unit
324+
val diagnosticSink: diagnostic: PhasedDiagnosticWithSeverity -> unit
317325

318326
val errorSink: diagnostic: PhasedDiagnostic -> unit
319327

src/Compiler/Interactive/fsi.fs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,18 @@ type internal DiagnosticsLoggerThatStopsOnFirstError
897897

898898
member _.ResetErrorCount() = errorCount <- 0
899899

900-
override _.DiagnosticSink(diagnostic, severity) =
900+
override _.DiagnosticSink(diagnostic) =
901+
let {
902+
PhasedDiagnostic = phasedDiagnostic
903+
Severity = severity
904+
} =
905+
diagnostic
906+
901907
let tcConfig = TcConfig.Create(tcConfigB, validate = false)
902908

903-
match diagnostic.AdjustSeverity(tcConfig.diagnosticsOptions, severity) with
909+
match diagnostic.AdjustSeverity(tcConfig.diagnosticsOptions) with
904910
| FSharpDiagnosticSeverity.Error ->
905-
fsiStdinSyphon.PrintDiagnostic(tcConfig, diagnostic)
911+
fsiStdinSyphon.PrintDiagnostic(tcConfig, phasedDiagnostic)
906912
errorCount <- errorCount + 1
907913

908914
if tcConfigB.abortOnError then
@@ -913,7 +919,7 @@ type internal DiagnosticsLoggerThatStopsOnFirstError
913919
| FSharpDiagnosticSeverity.Info as adjustedSeverity ->
914920
DoWithDiagnosticColor adjustedSeverity (fun () ->
915921
fsiConsoleOutput.Error.WriteLine()
916-
diagnostic.WriteWithContext(fsiConsoleOutput.Error, " ", fsiStdinSyphon.GetLine, tcConfig, severity)
922+
phasedDiagnostic.WriteWithContext(fsiConsoleOutput.Error, " ", fsiStdinSyphon.GetLine, tcConfig, severity)
917923
fsiConsoleOutput.Error.WriteLine()
918924
fsiConsoleOutput.Error.WriteLine()
919925
fsiConsoleOutput.Error.Flush())

0 commit comments

Comments
 (0)