Skip to content

Commit fd391a0

Browse files
authored
Correct range of struct SynExpr.AnonRecd. (#14715)
1 parent fd6eb11 commit fd391a0

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/Compiler/pars.fsy

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4904,10 +4904,11 @@ objExprInterface:
49044904

49054905
braceBarExpr:
49064906
| STRUCT braceBarExprCore
4907-
{ $2 true }
4907+
{ let mStruct = rhs parseState 1
4908+
$2 (Some mStruct) }
49084909

49094910
| braceBarExprCore
4910-
{ $1 false }
4911+
{ $1 None }
49114912

49124913
braceBarExprCore:
49134914
| LBRACE_BAR recdExprCore bar_rbrace
@@ -4918,7 +4919,9 @@ braceBarExprCore:
49184919
| SynExprRecordField((SynLongIdent([id], _, _), _), mEquals, None, _) -> Some (id, mEquals, arbExpr("anonField", id.idRange))
49194920
| _ -> reportParseErrorAt (rhs parseState 1) (FSComp.SR.parsInvalidAnonRecdType()); None)
49204921
let m = rhs2 parseState 1 3
4921-
(fun isStruct -> SynExpr.AnonRecd (isStruct, orig, flds, m)) }
4922+
(fun (mStruct: range option) ->
4923+
let m = match mStruct with | None -> m | Some mStruct -> unionRanges mStruct m
4924+
SynExpr.AnonRecd (mStruct.IsSome, orig, flds, m)) }
49224925

49234926
| LBRACE_BAR recdExprCore recover
49244927
{ reportParseErrorAt (rhs parseState 1) (FSComp.SR.parsUnmatchedBraceBar())
@@ -4929,21 +4932,29 @@ braceBarExprCore:
49294932
| SynExprRecordField((SynLongIdent([id], _, _), _), mEquals, None, _) -> Some (id, mEquals, arbExpr("anonField", id.idRange))
49304933
| _ -> reportParseErrorAt (rhs parseState 1) (FSComp.SR.parsInvalidAnonRecdType()); None)
49314934
let m = rhs2 parseState 1 2
4932-
(fun isStruct -> SynExpr.AnonRecd (isStruct, orig, flds, m)) }
4935+
(fun (mStruct: range option) ->
4936+
let m = match mStruct with | None -> m | Some mStruct -> unionRanges mStruct m
4937+
SynExpr.AnonRecd (mStruct.IsSome, orig, flds, m)) }
49334938

49344939
| LBRACE_BAR error bar_rbrace
49354940
{ // silent recovery
49364941
let m = rhs2 parseState 1 3
4937-
(fun _ -> arbExpr("braceBarExpr", m)) }
4942+
(fun (mStruct: range option) ->
4943+
let m = match mStruct with | None -> m | Some mStruct -> unionRanges mStruct m
4944+
arbExpr("braceBarExpr", m)) }
49384945

49394946
| LBRACE_BAR recover
49404947
{ reportParseErrorAt (rhs parseState 1) (FSComp.SR.parsUnmatchedBraceBar())
49414948
let m = rhs2 parseState 1 1
4942-
(fun isStruct -> SynExpr.AnonRecd (isStruct, None, [], m)) }
4949+
(fun (mStruct: range option) ->
4950+
let m = match mStruct with | None -> m | Some mStruct -> unionRanges mStruct m
4951+
SynExpr.AnonRecd (mStruct.IsSome, None, [], m)) }
49434952

49444953
| LBRACE_BAR bar_rbrace
49454954
{ let m = rhs2 parseState 1 2
4946-
(fun isStruct -> SynExpr.AnonRecd (isStruct, None, [], m)) }
4955+
(fun (mStruct: range option) ->
4956+
let m = match mStruct with | None -> m | Some mStruct -> unionRanges mStruct m
4957+
SynExpr.AnonRecd (mStruct.IsSome, None, [], m)) }
49474958

49484959
anonLambdaExpr:
49494960
| FUN atomicPatterns RARROW typedSequentialExprBlock

tests/service/SyntaxTreeTests/ExpressionTests.fs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,25 @@ type CFoo() =
494494
assertRange (7,4) (7, 67) m
495495
| _ -> Assert.Fail $"Could not get valid AST, got {ast}"
496496

497+
[<Test>]
498+
let ``SynExpr.AnonRecd with struct keyword`` () =
499+
let ast =
500+
getParseResults """
501+
struct
502+
{| Foo =
503+
// meh
504+
someValue |}
505+
506+
struct {| |}
507+
"""
508+
509+
match ast with
510+
| ParsedInput.ImplFile(ParsedImplFileInput(contents = [
511+
SynModuleOrNamespace.SynModuleOrNamespace(decls = [
512+
SynModuleDecl.Expr(expr = SynExpr.AnonRecd(range = m1))
513+
SynModuleDecl.Expr(expr = SynExpr.AnonRecd(range = m2))
514+
])
515+
])) ->
516+
assertRange (2,0) (5, 16) m1
517+
assertRange (7, 0) (7, 12) m2
518+
| _ -> Assert.Fail $"Could not get valid AST, got {ast}"

0 commit comments

Comments
 (0)