Skip to content

Commit dfa696f

Browse files
authored
Internal: simplify FSharpDiagnostics.CreateFromException (#18610)
1 parent 20ff6a9 commit dfa696f

File tree

10 files changed

+21
-83
lines changed

10 files changed

+21
-83
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
### Breaking Changes
2121

2222
* Scoped Nowarn: Add the #warnon compiler directive ([Language suggestion #278](https://github.com/fsharp/fslang-suggestions/issues/278), [RFC FS-1146 PR](https://github.com/fsharp/fslang-design/pull/782), [PR #18049](https://github.com/dotnet/fsharp/pull/18049) and [PR #18637](https://github.com/dotnet/fsharp/pull/18637))
23+
* Simplify creation of `FSharpDiagnostics`. In a few cases, errors without ranges were assigned to the currently checked file, while in other cases they carried an empty range. The latter is now true in all cases. In a few cases, ranges at eof were corrected, while in others they were not. They are now always left uncorrected. This is a prerequisit for [#18553](https://github.com/dotnet/fsharp/issues/18553). ([PR #18610](https://github.com/dotnet/fsharp/pull/18610)).

src/Compiler/Service/BackgroundCompiler.fs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,16 +1370,10 @@ type internal BackgroundCompiler
13701370
scriptClosureCache.Set(AnyCallerThread, options, loadClosure) // Save the full load closure for later correlation.
13711371

13721372
let diags =
1373+
let flatErrors = options.OtherOptions |> Array.contains "--flaterrors"
1374+
13731375
loadClosure.LoadClosureRootFileDiagnostics
1374-
|> List.map (fun (exn, isError) ->
1375-
FSharpDiagnostic.CreateFromException(
1376-
exn,
1377-
isError,
1378-
range.Zero,
1379-
false,
1380-
options.OtherOptions |> Array.contains "--flaterrors",
1381-
None
1382-
))
1376+
|> List.map (fun (exn, isError) -> FSharpDiagnostic.CreateFromException(exn, isError, false, flatErrors, None))
13831377

13841378
return options, (diags @ diagnostics.Diagnostics)
13851379
}

src/Compiler/Service/FSharpCheckerResults.fs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,21 +2797,11 @@ module internal ParseAndCheckFile =
27972797

27982798
/// Diagnostics handler for parsing & type checking while processing a single file
27992799
type DiagnosticsHandler
2800-
(
2801-
reportErrors,
2802-
mainInputFileName,
2803-
diagnosticsOptions: FSharpDiagnosticOptions,
2804-
sourceText: ISourceText,
2805-
suggestNamesForErrors: bool,
2806-
flatErrors: bool
2807-
) =
2800+
(reportErrors, mainInputFileName, diagnosticsOptions: FSharpDiagnosticOptions, suggestNamesForErrors: bool, flatErrors: bool) =
28082801
let mutable options = diagnosticsOptions
28092802
let diagnosticsCollector = ResizeArray<_>()
28102803
let mutable errorCount = 0
28112804

2812-
// We'll need number of lines for adjusting error messages at EOF
2813-
let fileInfo = sourceText.GetLastCharacterPosition()
2814-
28152805
let collectOne severity diagnostic =
28162806
// 1. Extended diagnostic data should be created after typechecking because it requires a valid SymbolEnv
28172807
// 2. Diagnostic message should be created during the diagnostic sink, because after typechecking
@@ -2881,7 +2871,6 @@ module internal ParseAndCheckFile =
28812871
options,
28822872
false,
28832873
mainInputFileName,
2884-
fileInfo,
28852874
diagnostic,
28862875
severity,
28872876
suggestNamesForErrors,
@@ -2960,7 +2949,7 @@ module internal ParseAndCheckFile =
29602949

29612950
usingLexbufForParsing (createLexbuf options.LangVersionText options.StrictIndentation sourceText, fileName) (fun lexbuf ->
29622951
let errHandler =
2963-
DiagnosticsHandler(false, fileName, options.DiagnosticOptions, sourceText, suggestNamesForErrors, false)
2952+
DiagnosticsHandler(false, fileName, options.DiagnosticOptions, suggestNamesForErrors, false)
29642953

29652954
let lexfun = createLexerFunction fileName options lexbuf errHandler ct
29662955

@@ -3064,7 +3053,7 @@ module internal ParseAndCheckFile =
30643053
Activity.start "ParseAndCheckFile.parseFile" [| Activity.Tags.fileName, fileName |]
30653054

30663055
let errHandler =
3067-
DiagnosticsHandler(true, fileName, options.DiagnosticOptions, sourceText, suggestNamesForErrors, flatErrors)
3056+
DiagnosticsHandler(true, fileName, options.DiagnosticOptions, suggestNamesForErrors, flatErrors)
30683057

30693058
use _ = UseDiagnosticsLogger errHandler.DiagnosticsLogger
30703059

@@ -3233,14 +3222,7 @@ module internal ParseAndCheckFile =
32333222

32343223
// Initialize the error handler
32353224
let errHandler =
3236-
DiagnosticsHandler(
3237-
true,
3238-
mainInputFileName,
3239-
tcConfig.diagnosticsOptions,
3240-
sourceText,
3241-
suggestNamesForErrors,
3242-
tcConfig.flatErrors
3243-
)
3225+
DiagnosticsHandler(true, mainInputFileName, tcConfig.diagnosticsOptions, suggestNamesForErrors, tcConfig.flatErrors)
32443226

32453227
use _ = UseDiagnosticsLogger errHandler.DiagnosticsLogger
32463228

src/Compiler/Service/FSharpCheckerResults.fsi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,6 @@ module internal ParseAndCheckFile =
593593
reportErrors: bool *
594594
mainInputFileName: string *
595595
diagnosticsOptions: FSharpDiagnosticOptions *
596-
sourceText: ISourceText *
597596
suggestNamesForErrors: bool *
598597
flatErrors: bool ->
599598
DiagnosticsHandler

src/Compiler/Service/IncrementalBuild.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ type IncrementalBuilder(initialState: IncrementalBuilderInitialState, state: Inc
16661666
Array.ofList delayedLogger.Diagnostics, false
16671667
diagnostics
16681668
|> Array.map (fun (diagnostic, severity) ->
1669-
FSharpDiagnostic.CreateFromException(diagnostic, severity, range.Zero, suggestNamesForErrors, flatErrors, None))
1669+
FSharpDiagnostic.CreateFromException(diagnostic, severity, suggestNamesForErrors, flatErrors, None))
16701670

16711671
return builderOpt, diagnostics
16721672
}

src/Compiler/Service/TransparentCompiler.fs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ type internal TransparentCompiler
11281128
|> Option.map (fun bootstrapInfo -> bootstrapInfo.TcConfig.flatErrors)
11291129
|> Option.defaultValue false // TODO: do we need to figure this out?
11301130

1131-
FSharpDiagnostic.CreateFromException(diagnostic, severity, range.Zero, suggestNamesForErrors, flatErrors, None))
1131+
FSharpDiagnostic.CreateFromException(diagnostic, severity, suggestNamesForErrors, flatErrors, None))
11321132

11331133
return bootstrapInfoOpt, diagnostics
11341134
}
@@ -1382,15 +1382,13 @@ type internal TransparentCompiler
13821382
let tcImports = bootstrapInfo.TcImports
13831383

13841384
let mainInputFileName = file.FileName
1385-
let sourceText = file.SourceText
13861385

13871386
// Initialize the error handler
13881387
let errHandler =
13891388
ParseAndCheckFile.DiagnosticsHandler(
13901389
true,
13911390
mainInputFileName,
13921391
tcConfig.diagnosticsOptions,
1393-
sourceText,
13941392
suggestNamesForErrors,
13951393
tcConfig.flatErrors
13961394
)
@@ -2466,16 +2464,10 @@ type internal TransparentCompiler
24662464
)
24672465

24682466
let diags =
2467+
let flaterrors = otherFlags |> List.contains "--flaterrors"
2468+
24692469
loadClosure.LoadClosureRootFileDiagnostics
2470-
|> List.map (fun (exn, isError) ->
2471-
FSharpDiagnostic.CreateFromException(
2472-
exn,
2473-
isError,
2474-
range.Zero,
2475-
false,
2476-
otherFlags |> List.contains "--flaterrors",
2477-
None
2478-
))
2470+
|> List.map (fun (exn, isError) -> FSharpDiagnostic.CreateFromException(exn, isError, false, flaterrors, None))
24792471

24802472
return snapshot, (diags @ diagnostics.Diagnostics)
24812473
}

src/Compiler/Service/service.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module CompileHelpers =
4949
{ new DiagnosticsLogger("CompileAPI") with
5050

5151
member _.DiagnosticSink(diag, isError) =
52-
diagnostics.Add(FSharpDiagnostic.CreateFromException(diag, isError, range0, true, flatErrors, None)) // Suggest names for errors
52+
diagnostics.Add(FSharpDiagnostic.CreateFromException(diag, isError, true, flatErrors, None)) // Suggest names for errors
5353

5454
member _.ErrorCount =
5555
diagnostics

src/Compiler/Symbols/FSharpDiagnostic.fs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ type FSharpDiagnostic(m: range, severity: FSharpDiagnosticSeverity, message: str
191191
sprintf "%s (%d,%d)-(%d,%d) %s %s %s" fileName s.Line (s.Column + 1) e.Line (e.Column + 1) subcategory severity message
192192

193193
/// Decompose a warning or error into parts: position, severity, message, error number
194-
static member CreateFromException(diagnostic: PhasedDiagnostic, severity, fallbackRange: range, suggestNames: bool, flatErrors: bool, symbolEnv: SymbolEnv option) =
195-
let m = match diagnostic.Range with Some m -> m | None -> fallbackRange
194+
static member CreateFromException(diagnostic: PhasedDiagnostic, severity, suggestNames: bool, flatErrors: bool, symbolEnv: SymbolEnv option) =
196195
let extendedData: IFSharpDiagnosticExtendedData option =
197196
match symbolEnv with
198197
| None -> None
@@ -236,22 +235,9 @@ type FSharpDiagnostic(m: range, severity: FSharpDiagnosticSeverity, message: str
236235
| _ -> diagnostic.FormatCore(flatErrors, suggestNames)
237236

238237
let errorNum = diagnostic.Number
238+
let m = match diagnostic.Range with Some m -> m | None -> range.Zero
239239
FSharpDiagnostic(m, severity, msg, diagnostic.Subcategory(), errorNum, "FS", extendedData)
240240

241-
/// Decompose a warning or error into parts: position, severity, message, error number
242-
static member CreateFromExceptionAndAdjustEof(diagnostic, severity, fallbackRange: range, (linesCount: int, lastLength: int), suggestNames: bool, flatErrors: bool, symbolEnv: SymbolEnv option) =
243-
let diagnostic = FSharpDiagnostic.CreateFromException(diagnostic, severity, fallbackRange, suggestNames, flatErrors, symbolEnv)
244-
245-
// Adjust to make sure that diagnostics reported at Eof are shown at the linesCount
246-
let startLine, startChanged = min (Line.toZ diagnostic.Range.StartLine, false) (linesCount, true)
247-
let endLine, endChanged = min (Line.toZ diagnostic.Range.EndLine, false) (linesCount, true)
248-
249-
if not (startChanged || endChanged) then
250-
diagnostic
251-
else
252-
let r = if startChanged then diagnostic.WithStart(mkPos startLine lastLength) else diagnostic
253-
if endChanged then r.WithEnd(mkPos endLine (1 + lastLength)) else r
254-
255241
static member NewlineifyErrorString(message) = NewlineifyErrorString(message)
256242

257243
static member NormalizeErrorString(text) = NormalizeErrorString(text)
@@ -271,7 +257,7 @@ type DiagnosticsScope(flatErrors: bool) =
271257
{ new DiagnosticsLogger("DiagnosticsScope") with
272258

273259
member _.DiagnosticSink(diagnostic, severity) =
274-
let diagnostic = FSharpDiagnostic.CreateFromException(diagnostic, severity, range.Zero, false, flatErrors, None)
260+
let diagnostic = FSharpDiagnostic.CreateFromException(diagnostic, severity, false, flatErrors, None)
275261
diags <- diagnostic :: diags
276262

277263
member _.ErrorCount = diags.Length }
@@ -344,21 +330,17 @@ type internal CompilationDiagnosticLogger (debugName: string, options: FSharpDia
344330

345331
module DiagnosticHelpers =
346332

347-
let ReportDiagnostic (options: FSharpDiagnosticOptions, allErrors, mainInputFileName, fileInfo, diagnostic: PhasedDiagnostic, severity, suggestNames, flatErrors, symbolEnv) =
333+
let ReportDiagnostic (options: FSharpDiagnosticOptions, allErrors, mainInputFileName, diagnostic: PhasedDiagnostic, severity, suggestNames, flatErrors, symbolEnv) =
348334
match diagnostic.AdjustSeverity(options, severity) with
349335
| FSharpDiagnosticSeverity.Hidden -> []
350336
| adjustedSeverity ->
351337

352-
// We use the first line of the file as a fallbackRange for reporting unexpected errors.
353-
// Not ideal, but it's hard to see what else to do.
354-
let fallbackRange = rangeN mainInputFileName 1
355-
let diagnostic = FSharpDiagnostic.CreateFromExceptionAndAdjustEof (diagnostic, adjustedSeverity, fallbackRange, fileInfo, suggestNames, flatErrors, symbolEnv)
338+
let diagnostic = FSharpDiagnostic.CreateFromException (diagnostic, adjustedSeverity, suggestNames, flatErrors, symbolEnv)
356339
let fileName = diagnostic.Range.FileName
357340
if allErrors || fileName = mainInputFileName || fileName = TcGlobals.DummyFileNameForRangesWithoutASpecificLocation then
358341
[diagnostic]
359342
else []
360343

361344
let CreateDiagnostics (options, allErrors, mainInputFileName, diagnostics, suggestNames, flatErrors, symbolEnv) =
362-
let fileInfo = (Int32.MaxValue, Int32.MaxValue)
363345
[| for diagnostic, severity in diagnostics do
364-
yield! ReportDiagnostic (options, allErrors, mainInputFileName, fileInfo, diagnostic, severity, suggestNames, flatErrors, symbolEnv) |]
346+
yield! ReportDiagnostic (options, allErrors, mainInputFileName, diagnostic, severity, suggestNames, flatErrors, symbolEnv) |]

src/Compiler/Symbols/FSharpDiagnostic.fsi

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,9 @@ type public FSharpDiagnostic =
191191
?subcategory: string ->
192192
FSharpDiagnostic
193193

194-
static member internal CreateFromExceptionAndAdjustEof:
195-
diagnostic: PhasedDiagnostic *
196-
severity: FSharpDiagnosticSeverity *
197-
range *
198-
lastPosInFile: (int * int) *
199-
suggestNames: bool *
200-
flatErrors: bool *
201-
symbolEnv: SymbolEnv option ->
202-
FSharpDiagnostic
203-
204194
static member internal CreateFromException:
205195
diagnostic: PhasedDiagnostic *
206196
severity: FSharpDiagnosticSeverity *
207-
range *
208197
suggestNames: bool *
209198
flatErrors: bool *
210199
symbolEnv: SymbolEnv option ->
@@ -251,7 +240,6 @@ module internal DiagnosticHelpers =
251240
FSharpDiagnosticOptions *
252241
allErrors: bool *
253242
mainInputFileName: string *
254-
fileInfo: (int * int) *
255243
diagnostic: PhasedDiagnostic *
256244
severity: FSharpDiagnosticSeverity *
257245
suggestNames: bool *

tests/FSharp.Compiler.ComponentTests/CompilerOptions/fsc/codepage/codepage.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module Codepage =
6464
|> compile
6565
|> shouldFail
6666
|> withDiagnostics [
67-
(Error 193, Line 1, Col 1, Line 1, Col 1, "No data is available for encoding 65535. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.")
67+
(Error 193, Line 0, Col 1, Line 0, Col 1, "No data is available for encoding 65535. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.")
6868
]
6969

7070
//# Boundary case

0 commit comments

Comments
 (0)