Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit a683f01

Browse files
tbobynosami
authored andcommitted
Improve error reporting: Missing = on type declaration (dotnet#9642)
1 parent 14634e6 commit a683f01

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/fsharp/FSComp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,7 @@ notAFunctionButMaybeDeclaration,"This value is not a function and cannot be appl
14981498
3351,chkFeatureNotRuntimeSupported,"Feature '%s' is not supported by target runtime."
14991499
3352,typrelInterfaceMemberNoMostSpecificImplementation,"Interface member '%s' does not have a most specific implementation."
15001500
3353,chkFeatureNotSupportedInLibrary,"Feature '%s' requires the F# library for language version %s or greater."
1501+
3360,parsEqualsMissingInTypeDefinition,"Unexpected token in type definition. Expected '=' after the type '%s'."
15011502
useSdkRefs,"Use reference assemblies for .NET framework references when available (Enabled by default)."
15021503
optsLangVersion,"Display the allowed values for language version, specify language version such as 'latest' or 'preview'"
15031504
optsSupportedLangVersions,"Supported language versions:"

src/fsharp/pars.fsy

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,8 +1496,14 @@ tyconDefn:
14961496
| typeNameInfo
14971497
{ TypeDefn($1, SynTypeDefnRepr.Simple(SynTypeDefnSimpleRepr.None($1.Range), $1.Range), [], $1.Range) }
14981498

1499-
| typeNameInfo EQUALS tyconDefnRhsBlock
1500-
{ let nameRange = rhs parseState 1
1499+
| typeNameInfo opt_equals tyconDefnRhsBlock
1500+
{ if not $2 then (
1501+
let (ComponentInfo(_, _, _, lid, _, _, _, _)) = $1
1502+
// While the spec doesn't allow long idents here, the parser doesn't enforce this, so take one ident
1503+
let typeNameId = List.last lid
1504+
raiseParseErrorAt (rhs parseState 2) (FSComp.SR.parsEqualsMissingInTypeDefinition(typeNameId.ToString()))
1505+
)
1506+
let nameRange = rhs parseState 1
15011507
let (tcDefRepr:SynTypeDefnRepr), members = $3 nameRange
15021508
let declRange = unionRanges (rhs parseState 1) tcDefRepr.Range
15031509
let mWhole = (declRange, members) ||> unionRangeWithListBy (fun (mem:SynMemberDefn) -> mem.Range)
@@ -5445,6 +5451,10 @@ deprecated_opt_equals:
54455451
| EQUALS { deprecatedWithError (FSComp.SR.parsNoEqualShouldFollowNamespace()) (lhs parseState); () }
54465452
| /* EMPTY */ { }
54475453

5454+
opt_equals:
5455+
| EQUALS { true }
5456+
| /* EMPTY */ { false }
5457+
54485458
opt_OBLOCKSEP:
54495459
| OBLOCKSEP { }
54505460
| /* EMPTY */ { }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.ComponentTests.ErrorMessages
4+
5+
open Xunit
6+
open FSharp.Test.Utilities
7+
open FSharp.Compiler.SourceCodeServices
8+
9+
10+
module ``Type definition missing equals`` =
11+
12+
[<Fact>]
13+
let ``Missing equals in DU``() =
14+
CompilerAssert.TypeCheckSingleError
15+
"""
16+
type X | A | B
17+
"""
18+
FSharpErrorSeverity.Error
19+
3360
20+
(2, 8, 2, 9)
21+
"Unexpected token in type definition. Expected '=' after the type 'X'."

tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<Compile Include="EmittedIL\Operators.fs" />
2525
<Compile Include="EmittedIL\Literals.fs" />
2626
<Compile Include="EmittedIL\TailCalls.fs" />
27+
<Compile Include="ErrorMessages\TypeEqualsMissingTests.fs" />
2728
<Compile Include="ErrorMessages\AccessOfTypeAbbreviationTests.fs" />
2829
<Compile Include="ErrorMessages\AssignmentErrorTests.fs" />
2930
<Compile Include="ErrorMessages\ClassesTests.fs" />

0 commit comments

Comments
 (0)