Skip to content

Commit 36d09cb

Browse files
committed
Diagnostics: add extended data for 'No constructors' error
1 parent 9425e4d commit 36d09cb

File tree

9 files changed

+97
-45
lines changed

9 files changed

+97
-45
lines changed

src/Compiler/Checking/CheckDeclarations.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,8 @@ module MutRecBindingChecking =
13701370
try
13711371
let baseTy, tpenv = TcType cenv NoNewTypars CheckCxs ItemOccurrence.Use WarnOnIWSAM.Yes envInstance tpenv synBaseTy
13721372
let baseTy = baseTy |> convertToTypeWithMetadataIfPossible g
1373-
TcNewExpr cenv envInstance tpenv baseTy (Some synBaseTy.Range) true arg m
1373+
let mTcNew = unionRanges synBaseTy.Range arg.Range
1374+
TcNewExpr cenv envInstance tpenv baseTy (Some synBaseTy.Range) true arg mTcNew
13741375
with RecoverableException e ->
13751376
errorRecovery e m
13761377
mkUnit g m, tpenv

src/Compiler/Checking/NameResolution.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ open FSharp.Compiler.TypeHierarchy
3838
open FSharp.Compiler.TypeProviders
3939
#endif
4040

41+
exception NoConstructorsAvailableForType of TType * DisplayEnv * range
42+
4143
/// An object that captures the logical context for name resolution.
4244
type NameResolver(g: TcGlobals,
4345
amap: Import.ImportMap,
@@ -2554,7 +2556,7 @@ let private ResolveObjectConstructorPrim (ncenv: NameResolver) edenv resInfo m a
25542556
[DefaultStructCtor(g, ty)]
25552557
else []
25562558
if (isNil defaultStructCtorInfo && isNil ctorInfos) || (not (isAppTy g ty) && not (isAnyTupleTy g ty)) then
2557-
raze (Error(FSComp.SR.nrNoConstructorsAvailableForType(NicePrint.minimalStringOfType edenv ty), m))
2559+
raze (error(NoConstructorsAvailableForType(ty, edenv, m)))
25582560
else
25592561
let ctorInfos = ctorInfos |> List.filter (IsMethInfoAccessible amap m ad)
25602562
let metadataTy = convertToTypeWithMetadataIfPossible g ty

src/Compiler/Checking/NameResolution.fsi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ open FSharp.Compiler.TypedTree
1414
open FSharp.Compiler.TypedTreeOps
1515
open FSharp.Compiler.TcGlobals
1616

17+
exception NoConstructorsAvailableForType of TType * DisplayEnv * range
18+
1719
/// A NameResolver is a context for name resolution. It primarily holds an InfoReader.
1820
type NameResolver =
1921

src/Compiler/Driver/CompilerDiagnostics.fs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,24 @@ open FSharp.Compiler.TypedTreeOps
4444
let showAssertForUnexpectedException = ref true
4545
#endif
4646

47-
/// This exception is an old-style way of reporting a diagnostic
4847
exception HashIncludeNotAllowedInNonScript of range
4948

50-
/// This exception is an old-style way of reporting a diagnostic
5149
exception HashReferenceNotAllowedInNonScript of range
5250

53-
/// This exception is an old-style way of reporting a diagnostic
5451
exception HashLoadedSourceHasIssues of informationals: exn list * warnings: exn list * errors: exn list * range
5552

56-
/// This exception is an old-style way of reporting a diagnostic
5753
exception HashLoadedScriptConsideredSource of range
5854

59-
/// This exception is an old-style way of reporting a diagnostic
6055
exception HashDirectiveNotAllowedInNonScript of range
6156

62-
/// This exception is an old-style way of reporting a diagnostic
6357
exception DeprecatedCommandLineOptionFull of string * range
6458

65-
/// This exception is an old-style way of reporting a diagnostic
6659
exception DeprecatedCommandLineOptionForHtmlDoc of string * range
6760

68-
/// This exception is an old-style way of reporting a diagnostic
6961
exception DeprecatedCommandLineOptionSuggestAlternative of string * string * range
7062

71-
/// This exception is an old-style way of reporting a diagnostic
7263
exception DeprecatedCommandLineOptionNoDescription of string * range
7364

74-
/// This exception is an old-style way of reporting a diagnostic
7565
exception InternalCommandLineOption of string * range
7666

7767
type Exception with
@@ -207,7 +197,9 @@ type Exception with
207197
| MSBuildReferenceResolutionError(_, _, m)
208198
| AssemblyNotResolved(_, m)
209199
| HashLoadedSourceHasIssues(_, _, _, m)
210-
| HashLoadedScriptConsideredSource m -> Some m
200+
| HashLoadedScriptConsideredSource m
201+
| NoConstructorsAvailableForType(_, _, m) -> Some m
202+
211203
// Strip TargetInvocationException wrappers
212204
| :? System.Reflection.TargetInvocationException as e when isNotNull e.InnerException -> (!!e.InnerException).DiagnosticRange
213205
#if !NO_TYPEPROVIDERS
@@ -336,6 +328,7 @@ type Exception with
336328
| PatternMatchCompilation.EnumMatchIncomplete _ -> 104
337329
| Failure _ -> 192
338330
| DefinitionsInSigAndImplNotCompatibleAbbreviationsDiffer _ -> 318
331+
| NoConstructorsAvailableForType _ -> 1133
339332
| ArgumentsInSigAndImplMismatch _ -> 3218
340333

341334
// Strip TargetInvocationException wrappers
@@ -616,6 +609,8 @@ module OldStyleMessages =
616609
let InvalidAttributeTargetForLanguageElement1E () = Message("InvalidAttributeTargetForLanguageElement1", "%s%s")
617610
let InvalidAttributeTargetForLanguageElement2E () = Message("InvalidAttributeTargetForLanguageElement2", "")
618611

612+
let NoConstructorsAvailableForTypeE () = Message("NoConstructorsAvailableForType", "%s")
613+
619614
#if DEBUG
620615
let mutable showParserStackOnParseError = false
621616
#endif
@@ -1945,6 +1940,9 @@ type Exception with
19451940
let allowedTargets = allowedTargets |> String.concat ", "
19461941
os.AppendString(InvalidAttributeTargetForLanguageElement1E().Format elementTargets allowedTargets)
19471942

1943+
| NoConstructorsAvailableForType(t, denv, _) ->
1944+
os.AppendString(NoConstructorsAvailableForTypeE().Format(NicePrint.minimalStringOfType denv t))
1945+
19481946
// Strip TargetInvocationException wrappers
19491947
| :? TargetInvocationException as e when isNotNull e.InnerException -> (!!e.InnerException).Output(os, suggestNames)
19501948

src/Compiler/FSStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,4 +1152,7 @@
11521152
<data name="InvalidAttributeTargetForLanguageElement2" xml:space="preserve">
11531153
<value>This attribute is not valid for use on this language element</value>
11541154
</data>
1155+
<data name="NoConstructorsAvailableForType" xml:space="preserve">
1156+
<value>No constructors are available for the type '{0}'</value>
1157+
</data>
11551158
</root>

src/Compiler/Symbols/FSharpDiagnostic.fs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ open System
1111

1212
open FSharp.Compiler.CheckExpressions
1313
open FSharp.Compiler.ConstraintSolver
14+
open FSharp.Compiler.NameResolution
1415
open FSharp.Compiler.SignatureConformance
1516
open FSharp.Compiler.Symbols
1617
open FSharp.Compiler.Syntax
@@ -86,6 +87,12 @@ module ExtendedData =
8687
member x.ContextInfo = context
8788
member x.DisplayContext = FSharpDisplayContext(fun _ -> dispEnv)
8889

90+
type TypeExtendedData internal (symbolEnv: SymbolEnv, displayEnv: DisplayEnv, actualType: TType) =
91+
interface IFSharpDiagnosticExtendedData
92+
93+
member x.Type = FSharpType(symbolEnv, actualType)
94+
member x.DisplayContext = FSharpDisplayContext(fun _ -> displayEnv)
95+
8996
type ExpressionIsAFunctionExtendedData internal (symbolEnv: SymbolEnv, actualType: TType) =
9097
interface IFSharpDiagnosticExtendedData
9198

@@ -203,11 +210,15 @@ type FSharpDiagnostic(m: range, severity: FSharpDiagnosticSeverity, message: str
203210
| DefinitionsInSigAndImplNotCompatibleAbbreviationsDiffer(implTycon = implTycon; sigTycon = sigTycon) ->
204211
Some(DefinitionsInSigAndImplNotCompatibleAbbreviationsDifferExtendedData(sigTycon, implTycon))
205212

206-
| ObsoleteDiagnostic(diagnosticId= diagnosticId; urlFormat= urlFormat) ->
213+
| ObsoleteDiagnostic(diagnosticId = diagnosticId; urlFormat = urlFormat) ->
207214
Some(ObsoleteDiagnosticExtendedData(diagnosticId, urlFormat))
208215

209-
| Experimental(diagnosticId= diagnosticId; urlFormat= urlFormat) ->
216+
| Experimental(diagnosticId = diagnosticId; urlFormat = urlFormat) ->
210217
Some(ExperimentalExtendedData(diagnosticId, urlFormat))
218+
219+
| NoConstructorsAvailableForType(ttype, displayEnv, _) ->
220+
Some(TypeExtendedData(symbolEnv, displayEnv, ttype))
221+
211222
| _ -> None
212223

213224
let msg =

src/Compiler/Symbols/FSharpDiagnostic.fsi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ module ExtendedData =
9999
/// Represents the information needed to format types
100100
member DisplayContext: FSharpDisplayContext
101101

102+
[<Class>]
103+
type TypeExtendedData =
104+
interface IFSharpDiagnosticExtendedData
105+
106+
member Type: FSharpType
107+
member DisplayContext: FSharpDisplayContext
108+
102109
/// Additional data for 'This expression is a function value, i.e. is missing arguments' diagnostic
103110
[<Class>]
104111
type ExpressionIsAFunctionExtendedData =

0 commit comments

Comments
 (0)