Skip to content

Commit f496b3c

Browse files
authored
Parser: recover on unfinished record patterns (#15921)
* Parser: recover on unfinished record patterns
1 parent 26b8772 commit f496b3c

21 files changed

+183
-53
lines changed

src/Compiler/SyntaxTree/SyntaxTree.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ type SynPat =
948948

949949
| ArrayOrList of isArray: bool * elementPats: SynPat list * range: range
950950

951-
| Record of fieldPats: ((LongIdent * Ident) * range * SynPat) list * range: range
951+
| Record of fieldPats: ((LongIdent * Ident) * range option * SynPat) list * range: range
952952

953953
| Null of range: range
954954

src/Compiler/SyntaxTree/SyntaxTree.fsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ type SynPat =
11041104
| ArrayOrList of isArray: bool * elementPats: SynPat list * range: range
11051105

11061106
/// A record pattern
1107-
| Record of fieldPats: ((LongIdent * Ident) * range * SynPat) list * range: range
1107+
| Record of fieldPats: ((LongIdent * Ident) * range option * SynPat) list * range: range
11081108

11091109
/// The 'null' pattern
11101110
| Null of range: range

src/Compiler/pars.fsy

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3591,8 +3591,10 @@ atomicPattern:
35913591
{ SynPat.QuoteExpr($1, lhs parseState) }
35923592

35933593
| LBRACE recordPatternElementsAux rbrace
3594-
{ let rs, m = $2
3595-
SynPat.Record(rs, rhs2 parseState 1 3) }
3594+
{ SynPat.Record($2, rhs2 parseState 1 3) }
3595+
3596+
| LBRACE error rbrace
3597+
{ SynPat.Record([], rhs2 parseState 1 3) }
35963598

35973599
| LBRACK listPatternElements RBRACK
35983600
{ SynPat.ArrayOrList(false, $2, lhs parseState) }
@@ -3775,19 +3777,29 @@ conjParenPatternElements:
37753777
| parenPattern AMP parenPattern
37763778
{ $3 :: $1 :: [] }
37773779

3778-
recordPatternElementsAux: /* Fix 1190 */
3780+
recordPatternElementsAux:
37793781
| recordPatternElement opt_seps
3780-
{ [$1], lhs parseState }
3782+
{ [$1] }
37813783

37823784
| recordPatternElement seps recordPatternElementsAux
3783-
{ let r = $1
3784-
let (rs, dropMark) = $3
3785-
(r :: rs), lhs parseState }
3785+
{ $1 :: $3 }
37863786

37873787
recordPatternElement:
37883788
| path EQUALS parenPattern
3789-
{ let mEquals = rhs parseState 2
3790-
(List.frontAndBack $1.LongIdent, mEquals, $3) }
3789+
{ let mPath = $1.Range
3790+
let mEquals = rhs parseState 2
3791+
let mPat = $3.Range
3792+
List.frontAndBack $1.LongIdent, Some mEquals, $3 }
3793+
3794+
| path EQUALS recover
3795+
{ let mPath = $1.Range
3796+
let mEquals = rhs parseState 2
3797+
let pat = SynPat.Wild(mEquals.EndRange)
3798+
List.frontAndBack $1.LongIdent, Some mEquals, pat }
3799+
3800+
| path recover
3801+
{ let pat = SynPat.Wild($1.Range.EndRange)
3802+
List.frontAndBack $1.LongIdent, None, pat }
37913803

37923804
listPatternElements:
37933805
| /* EMPTY */

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.debug.bsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8106,8 +8106,8 @@ FSharp.Compiler.Syntax.SynPat+QuoteExpr: FSharp.Compiler.Text.Range get_range()
81068106
FSharp.Compiler.Syntax.SynPat+QuoteExpr: FSharp.Compiler.Text.Range range
81078107
FSharp.Compiler.Syntax.SynPat+Record: FSharp.Compiler.Text.Range get_range()
81088108
FSharp.Compiler.Syntax.SynPat+Record: FSharp.Compiler.Text.Range range
8109-
FSharp.Compiler.Syntax.SynPat+Record: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Text.Range,FSharp.Compiler.Syntax.SynPat]] fieldPats
8110-
FSharp.Compiler.Syntax.SynPat+Record: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Text.Range,FSharp.Compiler.Syntax.SynPat]] get_fieldPats()
8109+
FSharp.Compiler.Syntax.SynPat+Record: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range],FSharp.Compiler.Syntax.SynPat]] fieldPats
8110+
FSharp.Compiler.Syntax.SynPat+Record: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range],FSharp.Compiler.Syntax.SynPat]] get_fieldPats()
81118111
FSharp.Compiler.Syntax.SynPat+Tags: Int32 Ands
81128112
FSharp.Compiler.Syntax.SynPat+Tags: Int32 ArrayOrList
81138113
FSharp.Compiler.Syntax.SynPat+Tags: Int32 As
@@ -8200,7 +8200,7 @@ FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewOptionalVal(FSha
82008200
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewOr(FSharp.Compiler.Syntax.SynPat, FSharp.Compiler.Syntax.SynPat, FSharp.Compiler.Text.Range, FSharp.Compiler.SyntaxTrivia.SynPatOrTrivia)
82018201
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewParen(FSharp.Compiler.Syntax.SynPat, FSharp.Compiler.Text.Range)
82028202
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewQuoteExpr(FSharp.Compiler.Syntax.SynExpr, FSharp.Compiler.Text.Range)
8203-
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewRecord(Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Text.Range,FSharp.Compiler.Syntax.SynPat]], FSharp.Compiler.Text.Range)
8203+
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewRecord(Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range],FSharp.Compiler.Syntax.SynPat]], FSharp.Compiler.Text.Range)
82048204
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewTuple(Boolean, Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.SynPat], Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.Range], FSharp.Compiler.Text.Range)
82058205
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewTyped(FSharp.Compiler.Syntax.SynPat, FSharp.Compiler.Syntax.SynType, FSharp.Compiler.Text.Range)
82068206
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewWild(FSharp.Compiler.Text.Range)

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.release.bsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8106,8 +8106,8 @@ FSharp.Compiler.Syntax.SynPat+QuoteExpr: FSharp.Compiler.Text.Range get_range()
81068106
FSharp.Compiler.Syntax.SynPat+QuoteExpr: FSharp.Compiler.Text.Range range
81078107
FSharp.Compiler.Syntax.SynPat+Record: FSharp.Compiler.Text.Range get_range()
81088108
FSharp.Compiler.Syntax.SynPat+Record: FSharp.Compiler.Text.Range range
8109-
FSharp.Compiler.Syntax.SynPat+Record: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Text.Range,FSharp.Compiler.Syntax.SynPat]] fieldPats
8110-
FSharp.Compiler.Syntax.SynPat+Record: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Text.Range,FSharp.Compiler.Syntax.SynPat]] get_fieldPats()
8109+
FSharp.Compiler.Syntax.SynPat+Record: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range],FSharp.Compiler.Syntax.SynPat]] fieldPats
8110+
FSharp.Compiler.Syntax.SynPat+Record: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range],FSharp.Compiler.Syntax.SynPat]] get_fieldPats()
81118111
FSharp.Compiler.Syntax.SynPat+Tags: Int32 Ands
81128112
FSharp.Compiler.Syntax.SynPat+Tags: Int32 ArrayOrList
81138113
FSharp.Compiler.Syntax.SynPat+Tags: Int32 As
@@ -8200,7 +8200,7 @@ FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewOptionalVal(FSha
82008200
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewOr(FSharp.Compiler.Syntax.SynPat, FSharp.Compiler.Syntax.SynPat, FSharp.Compiler.Text.Range, FSharp.Compiler.SyntaxTrivia.SynPatOrTrivia)
82018201
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewParen(FSharp.Compiler.Syntax.SynPat, FSharp.Compiler.Text.Range)
82028202
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewQuoteExpr(FSharp.Compiler.Syntax.SynExpr, FSharp.Compiler.Text.Range)
8203-
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewRecord(Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Text.Range,FSharp.Compiler.Syntax.SynPat]], FSharp.Compiler.Text.Range)
8203+
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewRecord(Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.Ident],FSharp.Compiler.Syntax.Ident],Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range],FSharp.Compiler.Syntax.SynPat]], FSharp.Compiler.Text.Range)
82048204
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewTuple(Boolean, Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.SynPat], Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.Range], FSharp.Compiler.Text.Range)
82058205
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewTyped(FSharp.Compiler.Syntax.SynPat, FSharp.Compiler.Syntax.SynType, FSharp.Compiler.Text.Range)
82068206
FSharp.Compiler.Syntax.SynPat: FSharp.Compiler.Syntax.SynPat NewWild(FSharp.Compiler.Text.Range)

tests/service/data/SyntaxTree/Lambda/ComplexArgumentsLambdaHasArrowRange.fs.bsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ImplFile
2525
(NoneAtInvisible, Ident _arg2,
2626
[SynMatchClause
2727
(Record
28-
([(([], Y), (3,9--3,10),
28+
([(([], Y), Some (3,9--3,10),
2929
ListCons
3030
(Named
3131
(SynIdent (h, None), false, None,
@@ -88,7 +88,7 @@ ImplFile
8888
(2,4--2,10));
8989
Paren
9090
(Record
91-
([(([], Y), (3,9--3,10),
91+
([(([], Y), Some (3,9--3,10),
9292
ListCons
9393
(Named
9494
(SynIdent (h, None), false, None, (3,11--3,12)),
@@ -108,7 +108,7 @@ ImplFile
108108
(NoneAtInvisible, Ident _arg2,
109109
[SynMatchClause
110110
(Record
111-
([(([], Y), (3,9--3,10),
111+
([(([], Y), Some (3,9--3,10),
112112
ListCons
113113
(Named
114114
(SynIdent (h, None), false, None,

tests/service/data/SyntaxTree/Lambda/DestructedLambdaHasArrowRange.fs.bsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ImplFile
1414
(NoneAtInvisible, Ident _arg1,
1515
[SynMatchClause
1616
(Record
17-
([(([], X), (2,8--2,9),
17+
([(([], X), Some (2,8--2,9),
1818
Named
1919
(SynIdent (x, None), false, None, (2,10--2,11)))],
2020
(2,4--2,13)), None,
@@ -35,14 +35,14 @@ ImplFile
3535
WithKeyword = (2,4--2,22) }),
3636
Some
3737
([Record
38-
([(([], X), (2,8--2,9),
38+
([(([], X), Some (2,8--2,9),
3939
Named (SynIdent (x, None), false, None, (2,10--2,11)))],
4040
(2,4--2,13))],
4141
Match
4242
(NoneAtInvisible, Ident _arg1,
4343
[SynMatchClause
4444
(Record
45-
([(([], X), (2,8--2,9),
45+
([(([], X), Some (2,8--2,9),
4646
Named
4747
(SynIdent (x, None), false, None, (2,10--2,11)))],
4848
(2,4--2,13)), None,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Module
2+
3+
match () with
4+
| { A = _ } -> ()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
ImplFile
2+
(ParsedImplFileInput
3+
("/root/Pattern/Record 01.fs", false, QualifiedNameOfFile Module, [], [],
4+
[SynModuleOrNamespace
5+
([Module], false, NamedModule,
6+
[Expr
7+
(Match
8+
(Yes (3,0--3,13), Const (Unit, (3,6--3,8)),
9+
[SynMatchClause
10+
(Record
11+
([(([], A), Some (4,6--4,7), Wild (4,8--4,9))],
12+
(4,2--4,11)), None, Const (Unit, (4,15--4,17)),
13+
(4,2--4,17), Yes, { ArrowRange = Some (4,12--4,14)
14+
BarRange = Some (4,0--4,1) })],
15+
(3,0--4,17), { MatchKeyword = (3,0--3,5)
16+
WithKeyword = (3,9--3,13) }), (3,0--4,17))],
17+
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None,
18+
(1,0--4,17), { LeadingKeyword = Module (1,0--1,6) })], (true, true),
19+
{ ConditionalDirectives = []
20+
CodeComments = [] }, set []))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Module
2+
3+
match () with
4+
| { A = _; B = _ } -> ()

0 commit comments

Comments
 (0)