Skip to content

Commit 4dc1f3a

Browse files
authored
Add ExtendedData to FSharpDiagnostic (#15840)
Add ExtendedData to FSharpDiagnostic
1 parent f983b7c commit 4dc1f3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+885
-180
lines changed

src/Compiler/Checking/SignatureConformance.fs

Lines changed: 43 additions & 42 deletions
Large diffs are not rendered by default.

src/Compiler/Checking/SignatureConformance.fsi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module internal FSharp.Compiler.SignatureConformance
77
open System.Text
88

99
open FSharp.Compiler
10+
open FSharp.Compiler.Syntax
1011
open FSharp.Compiler.Text
1112
open FSharp.Compiler.TypedTree
1213
open FSharp.Compiler.TypedTreeOps
@@ -26,10 +27,19 @@ exception UnionCaseNotContained of DisplayEnv * InfoReader * Tycon * UnionCase *
2627

2728
exception FSharpExceptionNotContained of DisplayEnv * InfoReader * Tycon * Tycon * (string * string -> string)
2829

29-
exception FieldNotContained of DisplayEnv * InfoReader * Tycon * RecdField * RecdField * (string * string -> string)
30+
exception FieldNotContained of
31+
DisplayEnv *
32+
InfoReader *
33+
Tycon *
34+
Tycon *
35+
RecdField *
36+
RecdField *
37+
(string * string -> string)
3038

3139
exception InterfaceNotRevealed of DisplayEnv * TType * range
3240

41+
exception ArgumentsInSigAndImplMismatch of sigArg: Ident * implArg: Ident
42+
3343
type Checker =
3444

3545
new:

src/Compiler/Driver/CompilerDiagnostics.fs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type Exception with
7979

8080
member exn.DiagnosticRange =
8181
match exn with
82+
| ArgumentsInSigAndImplMismatch (_, implArg) -> Some implArg.idRange
8283
| ErrorFromAddingConstraint (_, exn2, _) -> exn2.DiagnosticRange
8384
#if !NO_TYPEPROVIDERS
8485
| TypeProviders.ProvidedTypeResolutionNoRange exn -> exn.DiagnosticRange
@@ -157,7 +158,7 @@ type Exception with
157158
| IndeterminateType m
158159
| TyconBadArgs (_, _, _, m) -> Some m
159160

160-
| FieldNotContained (_, _, _, arf, _, _) -> Some arf.Range
161+
| FieldNotContained (_, _, _, _, arf, _, _) -> Some arf.Range
161162
| ValueNotContained (_, _, _, aval, _, _) -> Some aval.Range
162163
| UnionCaseNotContained (_, _, _, aval, _, _) -> Some aval.Id.idRange
163164
| FSharpExceptionNotContained (_, _, aexnc, _, _) -> Some aexnc.Range
@@ -319,6 +320,7 @@ type Exception with
319320
| BadEventTransformation _ -> 91
320321
| HashLoadedScriptConsideredSource _ -> 92
321322
| UnresolvedConversionOperator _ -> 93
323+
| ArgumentsInSigAndImplMismatch _ -> 3218
322324
// avoid 94-100 for safety
323325
| ObsoleteError _ -> 101
324326
#if !NO_TYPEPROVIDERS
@@ -600,6 +602,7 @@ module OldStyleMessages =
600602
let LoadedSourceNotFoundIgnoringE () = Message("LoadedSourceNotFoundIgnoring", "%s")
601603
let MSBuildReferenceResolutionErrorE () = Message("MSBuildReferenceResolutionError", "%s%s")
602604
let TargetInvocationExceptionWrapperE () = Message("TargetInvocationExceptionWrapper", "%s")
605+
let ArgumentsInSigAndImplMismatchE () = Message("ArgumentsInSigAndImplMismatch", "%s%s")
603606

604607
#if DEBUG
605608
let mutable showParserStackOnParseError = false
@@ -1571,7 +1574,7 @@ type Exception with
15711574
)
15721575
)
15731576

1574-
| FieldNotContained (denv, infoReader, enclosingTycon, v1, v2, f) ->
1577+
| FieldNotContained (denv, infoReader, enclosingTycon, _, v1, v2, f) ->
15751578
let enclosingTcref = mkLocalEntityRef enclosingTycon
15761579

15771580
os.AppendString(
@@ -1870,6 +1873,9 @@ type Exception with
18701873

18711874
| MSBuildReferenceResolutionError (code, message, _) -> os.AppendString(MSBuildReferenceResolutionErrorE().Format message code)
18721875

1876+
| ArgumentsInSigAndImplMismatch (sigArg, implArg) ->
1877+
os.AppendString(ArgumentsInSigAndImplMismatchE().Format sigArg.idText implArg.idText)
1878+
18731879
// Strip TargetInvocationException wrappers
18741880
| :? TargetInvocationException as exn -> exn.InnerException.Output(os, suggestNames)
18751881

src/Compiler/FSComp.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,6 @@ notAFunctionButMaybeIndexerWithName2,"This value is not a function and cannot be
14911491
notAFunctionButMaybeIndexer2,"This expression is not a function and cannot be applied. Did you intend to access the indexer via 'expr[index]'?"
14921492
3217,notAFunctionButMaybeIndexerErrorCode,""
14931493
notAFunctionButMaybeDeclaration,"This value is not a function and cannot be applied. Did you forget to terminate a declaration?"
1494-
3218,ArgumentsInSigAndImplMismatch,"The argument names in the signature '%s' and implementation '%s' do not match. The argument name from the signature file will be used. This may cause problems when debugging or profiling."
14951494
3219,pickleUnexpectedNonZero,"An error occurred while reading the F# metadata of assembly '%s'. A reserved construct was utilized. You may need to upgrade your F# compiler or use an earlier version of the assembly that doesn't make use of a specific construct."
14961495
3220,tcTupleMemberNotNormallyUsed,"This method or property is not normally used from F# code, use an explicit tuple pattern for deconstruction instead."
14971496
3221,implicitlyDiscardedInSequenceExpression,"This expression returns a value of type '%s' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield'."

src/Compiler/FSStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,9 @@
11131113
<data name="ErrorFromAddingTypeEquationTuples" xml:space="preserve">
11141114
<value>Type mismatch. Expecting a tuple of length {0} of type\n {1} \nbut given a tuple of length {2} of type\n {3} {4}\n</value>
11151115
</data>
1116+
<data name="ArgumentsInSigAndImplMismatch" xml:space="preserve">
1117+
<value>The argument names in the signature '{0}' and implementation '{1}' do not match. The argument name from the signature file will be used. This may cause problems when debugging or profiling.</value>
1118+
</data>
11161119
<data name="Parser.TOKEN.WHILE.BANG" xml:space="preserve">
11171120
<value>keyword 'while!'</value>
11181121
</data>

src/Compiler/FSharp.Compiler.Service.fsproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,12 @@
440440
<Compile Include="Driver\fsc.fs" />
441441

442442
<!-- the symbol API. -->
443-
<Compile Include="Symbols\FSharpDiagnostic.fsi" />
444-
<Compile Include="Symbols\FSharpDiagnostic.fs" />
445443
<Compile Include="Symbols\SymbolHelpers.fsi" />
446444
<Compile Include="Symbols\SymbolHelpers.fs" />
447445
<Compile Include="Symbols\Symbols.fsi" />
448446
<Compile Include="Symbols\Symbols.fs" />
447+
<Compile Include="Symbols\FSharpDiagnostic.fsi" />
448+
<Compile Include="Symbols\FSharpDiagnostic.fs" />
449449
<Compile Include="Symbols\Exprs.fsi" />
450450
<Compile Include="Symbols\Exprs.fs" />
451451
<Compile Include="Symbols\SymbolPatterns.fsi" />

src/Compiler/Interactive/fsi.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4703,7 +4703,7 @@ type FsiEvaluationSession
47034703
let errs = diagnosticsLogger.GetDiagnostics()
47044704

47054705
let errorInfos =
4706-
DiagnosticHelpers.CreateDiagnostics(errorOptions, true, scriptFile, errs, true, tcConfigB.flatErrors)
4706+
DiagnosticHelpers.CreateDiagnostics(errorOptions, true, scriptFile, errs, true, tcConfigB.flatErrors, None)
47074707

47084708
let userRes =
47094709
match res with

src/Compiler/Service/FSharpCheckerResults.fs

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,21 +2442,16 @@ module internal ParseAndCheckFile =
24422442
let fileInfo = sourceText.GetLastCharacterPosition()
24432443

24442444
let collectOne severity diagnostic =
2445-
for diagnostic in
2446-
DiagnosticHelpers.ReportDiagnostic(
2447-
options,
2448-
false,
2449-
mainInputFileName,
2450-
fileInfo,
2451-
diagnostic,
2452-
severity,
2453-
suggestNamesForErrors,
2454-
flatErrors
2455-
) do
2456-
diagnosticsCollector.Add diagnostic
2445+
// 1. Extended diagnostic data should be created after typechecking because it requires a valid SymbolEnv
2446+
// 2. Diagnostic message should be created during the diagnostic sink, because after typechecking
2447+
// the formatting of types in it may change (for example, 'a to obj)
2448+
//
2449+
// So we'll create a diagnostic later, but cache the FormatCore message now
2450+
diagnostic.Exception.Data[ "CachedFormatCore" ] <- diagnostic.FormatCore(flatErrors, suggestNamesForErrors)
2451+
diagnosticsCollector.Add(struct (diagnostic, severity))
24572452

2458-
if severity = FSharpDiagnosticSeverity.Error then
2459-
errorCount <- errorCount + 1
2453+
if severity = FSharpDiagnosticSeverity.Error then
2454+
errorCount <- errorCount + 1
24602455

24612456
// This function gets called whenever an error happens during parsing or checking
24622457
let diagnosticSink severity (diagnostic: PhasedDiagnostic) =
@@ -2495,15 +2490,30 @@ module internal ParseAndCheckFile =
24952490
// Public members
24962491
member _.DiagnosticsLogger = diagnosticsLogger
24972492

2498-
member _.CollectedDiagnostics = diagnosticsCollector.ToArray()
2499-
25002493
member _.ErrorCount = errorCount
25012494

25022495
member _.DiagnosticOptions
25032496
with set opts = options <- opts
25042497

25052498
member _.AnyErrors = errorCount > 0
25062499

2500+
member _.CollectedDiagnostics(symbolEnv: SymbolEnv option) =
2501+
[|
2502+
for struct (diagnostic, severity) in diagnosticsCollector do
2503+
yield!
2504+
DiagnosticHelpers.ReportDiagnostic(
2505+
options,
2506+
false,
2507+
mainInputFileName,
2508+
fileInfo,
2509+
diagnostic,
2510+
severity,
2511+
suggestNamesForErrors,
2512+
flatErrors,
2513+
symbolEnv
2514+
)
2515+
|]
2516+
25072517
let getLightSyntaxStatus fileName options =
25082518
let indentationAwareSyntaxOnByDefault =
25092519
List.exists (FileSystemUtils.checkSuffix fileName) FSharpIndentationAwareSyntaxFileSuffixes
@@ -2713,7 +2723,7 @@ module internal ParseAndCheckFile =
27132723
errHandler.DiagnosticsLogger.StopProcessingRecovery e range0 // don't re-raise any exceptions, we must return None.
27142724
EmptyParsedInput(fileName, (isLastCompiland, isExe)))
27152725

2716-
errHandler.CollectedDiagnostics, parseResult, errHandler.AnyErrors
2726+
errHandler.CollectedDiagnostics(None), parseResult, errHandler.AnyErrors
27172727

27182728
let ApplyLoadClosure
27192729
(
@@ -2915,28 +2925,29 @@ module internal ParseAndCheckFile =
29152925
return ((tcState.TcEnvFromSignatures, EmptyTopAttrs, [], [ mty ]), tcState)
29162926
}
29172927

2918-
let errors = errHandler.CollectedDiagnostics
2928+
let (tcEnvAtEnd, _, implFiles, ccuSigsForFiles), tcState = resOpt
2929+
2930+
let symbolEnv = SymbolEnv(tcGlobals, tcState.Ccu, Some tcState.CcuSig, tcImports)
2931+
let errors = errHandler.CollectedDiagnostics(Some symbolEnv)
29192932

29202933
let res =
2921-
match resOpt with
2922-
| (tcEnvAtEnd, _, implFiles, ccuSigsForFiles), tcState ->
2923-
TypeCheckInfo(
2924-
tcConfig,
2925-
tcGlobals,
2926-
List.head ccuSigsForFiles,
2927-
tcState.Ccu,
2928-
tcImports,
2929-
tcEnvAtEnd.AccessRights,
2930-
projectFileName,
2931-
mainInputFileName,
2932-
projectOptions,
2933-
sink.GetResolutions(),
2934-
sink.GetSymbolUses(),
2935-
tcEnvAtEnd.NameEnv,
2936-
loadClosure,
2937-
List.tryHead implFiles,
2938-
sink.GetOpenDeclarations()
2939-
)
2934+
TypeCheckInfo(
2935+
tcConfig,
2936+
tcGlobals,
2937+
List.head ccuSigsForFiles,
2938+
tcState.Ccu,
2939+
tcImports,
2940+
tcEnvAtEnd.AccessRights,
2941+
projectFileName,
2942+
mainInputFileName,
2943+
projectOptions,
2944+
sink.GetResolutions(),
2945+
sink.GetSymbolUses(),
2946+
tcEnvAtEnd.NameEnv,
2947+
loadClosure,
2948+
List.tryHead implFiles,
2949+
sink.GetOpenDeclarations()
2950+
)
29402951

29412952
return errors, res
29422953
}

src/Compiler/Service/IncrementalBuild.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ type IncrementalBuilder(initialState: IncrementalBuilderInitialState, state: Inc
16421642
Array.ofList delayedLogger.Diagnostics, false
16431643
diagnostics
16441644
|> Array.map (fun (diagnostic, severity) ->
1645-
FSharpDiagnostic.CreateFromException(diagnostic, severity, range.Zero, suggestNamesForErrors, flatErrors))
1645+
FSharpDiagnostic.CreateFromException(diagnostic, severity, range.Zero, suggestNamesForErrors, flatErrors, None))
16461646

16471647
return builderOpt, diagnostics
1648-
}
1648+
}

src/Compiler/Service/service.fs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ module CompileHelpers =
106106
{ new DiagnosticsLogger("CompileAPI") with
107107

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

111111
member _.ErrorCount =
112112
diagnostics
@@ -556,7 +556,8 @@ type BackgroundCompiler
556556
fileName,
557557
parseDiagnostics,
558558
suggestNamesForErrors,
559-
builder.TcConfig.flatErrors
559+
builder.TcConfig.flatErrors,
560+
None
560561
)
561562

562563
let diagnostics = [| yield! creationDiags; yield! parseDiagnostics |]
@@ -857,14 +858,19 @@ type BackgroundCompiler
857858
let tcDiagnostics = tcInfo.TcDiagnostics
858859
let diagnosticsOptions = builder.TcConfig.diagnosticsOptions
859860

861+
let symbolEnv =
862+
SymbolEnv(tcProj.TcGlobals, tcInfo.tcState.Ccu, Some tcInfo.tcState.CcuSig, tcProj.TcImports)
863+
|> Some
864+
860865
let parseDiagnostics =
861866
DiagnosticHelpers.CreateDiagnostics(
862867
diagnosticsOptions,
863868
false,
864869
fileName,
865870
parseDiagnostics,
866871
suggestNamesForErrors,
867-
builder.TcConfig.flatErrors
872+
builder.TcConfig.flatErrors,
873+
None
868874
)
869875

870876
let parseDiagnostics = [| yield! creationDiags; yield! parseDiagnostics |]
@@ -876,7 +882,8 @@ type BackgroundCompiler
876882
fileName,
877883
tcDiagnostics,
878884
suggestNamesForErrors,
879-
builder.TcConfig.flatErrors
885+
builder.TcConfig.flatErrors,
886+
symbolEnv
880887
)
881888

882889
let tcDiagnostics = [| yield! creationDiags; yield! tcDiagnostics |]
@@ -1030,14 +1037,19 @@ type BackgroundCompiler
10301037
let tcDiagnostics = tcInfo.TcDiagnostics
10311038
let tcDependencyFiles = tcInfo.tcDependencyFiles
10321039

1040+
let symbolEnv =
1041+
SymbolEnv(tcProj.TcGlobals, tcInfo.tcState.Ccu, Some tcInfo.tcState.CcuSig, tcProj.TcImports)
1042+
|> Some
1043+
10331044
let tcDiagnostics =
10341045
DiagnosticHelpers.CreateDiagnostics(
10351046
diagnosticsOptions,
10361047
true,
10371048
fileName,
10381049
tcDiagnostics,
10391050
suggestNamesForErrors,
1040-
builder.TcConfig.flatErrors
1051+
builder.TcConfig.flatErrors,
1052+
symbolEnv
10411053
)
10421054

10431055
let diagnostics = [| yield! creationDiags; yield! tcDiagnostics |]
@@ -1204,7 +1216,7 @@ type BackgroundCompiler
12041216
let diags =
12051217
loadClosure.LoadClosureRootFileDiagnostics
12061218
|> List.map (fun (exn, isError) ->
1207-
FSharpDiagnostic.CreateFromException(exn, isError, range.Zero, false, options.OtherOptions |> Array.contains "--flaterrors"))
1219+
FSharpDiagnostic.CreateFromException(exn, isError, range.Zero, false, options.OtherOptions |> Array.contains "--flaterrors", None))
12081220

12091221
return options, (diags @ diagnostics.Diagnostics)
12101222
}

0 commit comments

Comments
 (0)