Skip to content

Commit 79837ec

Browse files
authored
Fix more declaration ranges (#7774)
* Fix field declaration ranges * Fix type fields ranges * Add test * Fix more ranges * Update baselines
1 parent ca83e05 commit 79837ec

File tree

6 files changed

+115
-43
lines changed

6 files changed

+115
-43
lines changed

src/fsharp/pars.fsy

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ fileModuleSpec:
676676
| opt_attributes opt_declVisibility moduleIntro moduleSpfnsPossiblyEmptyBlock
677677
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(),rhs parseState 2))
678678
let m2 = rhs parseState 3
679-
let m = (rhs2 parseState 3 4)
679+
let m = (rhs2 parseState 1 4)
680680
let isRec,path2,xml,vis = $3
681681
(fun (isRec2,path,_) ->
682682
if not (isNil path) then errorR(Error(FSComp.SR.parsNamespaceOrModuleNotBoth(),m2))
@@ -745,15 +745,15 @@ moduleSpfn:
745745
if not (isNil $1) then raiseParseErrorAt (rhs parseState 1) (FSComp.SR.parsIgnoreAttributesOnModuleAbbreviation())
746746
match vis with
747747
| Some vis -> raiseParseErrorAt (rhs parseState 1) (FSComp.SR.parsIgnoreVisibilityOnModuleAbbreviationAlwaysPrivate(vis.ToString()))
748-
| _ -> SynModuleSigDecl.ModuleAbbrev(List.head path,$5,rhs2 parseState 3 5) }
748+
| _ -> SynModuleSigDecl.ModuleAbbrev(List.head path,$5,rhs2 parseState 1 5) }
749749

750750
| opt_attributes opt_declVisibility moduleIntro colonOrEquals moduleSpecBlock
751751
{ let isRec, path, xml, vis = $3
752752
if not (isSingleton path) then raiseParseErrorAt (rhs parseState 3) (FSComp.SR.parsModuleDefnMustBeSimpleName())
753753
if isRec then raiseParseErrorAt (rhs parseState 3) (FSComp.SR.parsInvalidUseOfRec())
754754
let info = ComponentInfo($1,[],[],path,xml,false,vis,rhs parseState 3)
755755
if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(),rhs parseState 2))
756-
SynModuleSigDecl.NestedModule(info, isRec, $5, rhs2 parseState 3 5) }
756+
SynModuleSigDecl.NestedModule(info, isRec, $5, rhs2 parseState 1 5) }
757757

758758
| opt_attributes opt_declVisibility tyconSpfns
759759
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(),rhs parseState 2))
@@ -778,7 +778,7 @@ valSpfn:
778778
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(),rhs parseState 2))
779779
let attr1,attr2,isInline,isMutable,vis2,id,doc,explicitValTyparDecls,(ty,arity),konst = ($1),($4),($5),($6),($7),($8),grabXmlDoc(parseState,3),($9),($11),($12)
780780
if not (isNil attr2) then errorR(Deprecated(FSComp.SR.parsAttributesMustComeBeforeVal(),rhs parseState 4))
781-
let m = rhs2 parseState 3 11
781+
let m = rhs2 parseState 1 11
782782
let valSpfn = ValSpfn((attr1@attr2),id,explicitValTyparDecls,ty,arity,isInline,isMutable,doc, vis2,konst,m)
783783
SynModuleSigDecl.Val(valSpfn,m)
784784
}
@@ -965,21 +965,23 @@ classMemberSpfn:
965965
SynMemberSig.Inherit ($4,unionRanges (rhs parseState 3) ($4).Range) }
966966

967967
| opt_attributes opt_declVisibility VAL fieldDecl
968-
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(),rhs parseState 2))
969-
let fld = $4 $1 false
970-
SynMemberSig.ValField(fld,rhs2 parseState 3 4) }
968+
{ let wholeRange = rhs2 parseState 1 4
969+
if Option.isSome $2 then errorR (Error (FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier (), rhs parseState 2))
970+
let fld = $4 $1 false wholeRange
971+
SynMemberSig.ValField (fld, wholeRange) }
971972

972973
| opt_attributes opt_declVisibility STATIC VAL fieldDecl
973-
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(),rhs parseState 2))
974-
SynMemberSig.ValField($5 $1 true,rhs2 parseState 3 5) }
974+
{ let wholeRange = rhs2 parseState 1 5
975+
if Option.isSome $2 then errorR (Error (FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier (), rhs parseState 2))
976+
SynMemberSig.ValField($5 $1 true wholeRange, wholeRange) }
975977

976978
| opt_attributes opt_declVisibility STATIC typeKeyword tyconSpfn
977979
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(),rhs parseState 2))
978-
SynMemberSig.NestedType($5,rhs2 parseState 3 5) }
980+
SynMemberSig.NestedType($5,rhs2 parseState 1 5) }
979981

980982
| opt_attributes opt_declVisibility NEW COLON topTypeWithTypeConstraints
981983
{ let vis,doc,(ty,valSynInfo) = $2,grabXmlDoc(parseState,3),$5
982-
let m = unionRanges (rhs parseState 3) ty.Range
984+
let m = unionRanges (rhs parseState 1) ty.Range
983985
let isInline = false
984986
let valSpfn = ValSpfn ($1, mkSynId (rhs parseState 3) "new", noInferredTypars, ty, valSynInfo, isInline, false, doc, vis, None, m)
985987
SynMemberSig.Member(valSpfn, CtorMemberFlags,m) }
@@ -1254,7 +1256,7 @@ moduleDefn:
12541256
| Choice2Of2 def ->
12551257
if not (isSingleton path) then raiseParseErrorAt (rhs parseState 3) (FSComp.SR.parsModuleAbbreviationMustBeSimpleName())
12561258
let info = ComponentInfo(attribs,[],[],path,xml,false,vis,rhs parseState 3)
1257-
[ SynModuleDecl.NestedModule(info, isRec, def, false,(rhs2 parseState 3 4, def) ||> unionRangeWithListBy (fun d -> d.Range) ) ] }
1259+
[ SynModuleDecl.NestedModule(info, isRec, def, false,(rhs2 parseState 1 4, def) ||> unionRangeWithListBy (fun d -> d.Range) ) ] }
12581260

12591261
/* unattached custom attributes */
12601262
| attributes recover
@@ -1850,8 +1852,8 @@ classDefnMember:
18501852
if Option.isSome $2 then errorR(Error(FSComp.SR.parsInterfacesHaveSameVisibilityAsEnclosingType(),rhs parseState 3))
18511853
let mWhole =
18521854
match $5 with
1853-
| None -> rhs2 parseState 3 4
1854-
| Some(mems) -> (rhs2 parseState 3 4, mems) ||> unionRangeWithListBy (fun (mem:SynMemberDefn) -> mem.Range)
1855+
| None -> rhs2 parseState 1 4
1856+
| Some(mems) -> (rhs2 parseState 1 4, mems) ||> unionRangeWithListBy (fun (mem:SynMemberDefn) -> mem.Range)
18551857
[ SynMemberDefn.Interface ($4, $5, mWhole) ] }
18561858

18571859
| opt_attributes opt_declVisibility abstractMemberFlags opt_inline nameop opt_explicitValTyparDecls COLON topTypeWithTypeConstraints classMemberSpfnGetSet opt_ODECLEND
@@ -1860,7 +1862,7 @@ classDefnMember:
18601862
let getSetRangeOpt, getSet = $9
18611863
let getSetAdjuster arity = match arity,getSet with SynValInfo([],_),MemberKind.Member -> MemberKind.PropertyGet | _ -> getSet
18621864
let wholeRange =
1863-
let m = rhs parseState 3
1865+
let m = rhs parseState 1
18641866
match getSetRangeOpt with
18651867
| None -> unionRanges m ty.Range
18661868
| Some m2 -> unionRanges m m2
@@ -1875,11 +1877,13 @@ classDefnMember:
18751877

18761878
| opt_attributes opt_declVisibility valDefnDecl opt_ODECLEND
18771879
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(),rhs parseState 2))
1878-
$3 None $1 false }
1880+
let rangeStart = rhs parseState 1
1881+
$3 rangeStart $1 false }
18791882

18801883
| opt_attributes opt_declVisibility STATIC valDefnDecl opt_ODECLEND
18811884
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(),rhs parseState 2))
1882-
$4 (Some (rhs parseState 3)) $1 true }
1885+
let rangeStart = rhs parseState 1
1886+
$4 rangeStart $1 true }
18831887

18841888
| opt_attributes opt_declVisibility memberFlags autoPropsDefnDecl opt_ODECLEND
18851889
{ let rangeStart = rhs parseState 1
@@ -1889,7 +1893,7 @@ classDefnMember:
18891893
$4 $1 isStatic flags rangeStart }
18901894

18911895
| opt_attributes opt_declVisibility NEW atomicPattern optAsSpec EQUALS typedSeqExprBlock opt_ODECLEND
1892-
{ let m = unionRanges (rhs2 parseState 3 6) $7.Range
1896+
{ let m = unionRanges (rhs2 parseState 1 6) $7.Range
18931897
let expr = $7
18941898
let valSynData = SynValData (Some CtorMemberFlags, SynValInfo([SynInfo.InferSynArgInfoFromPat $4],SynInfo.unnamedRetVal), $5)
18951899
let vis = $2
@@ -1900,7 +1904,7 @@ classDefnMember:
19001904

19011905
| opt_attributes opt_declVisibility STATIC typeKeyword tyconDefn
19021906
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(),rhs parseState 2))
1903-
[ SynMemberDefn.NestedType($5,None,rhs2 parseState 3 5) ] }
1907+
[ SynMemberDefn.NestedType($5,None,rhs2 parseState 1 5) ] }
19041908

19051909

19061910
/* A 'val' definition in an object type definition */
@@ -1909,8 +1913,8 @@ valDefnDecl:
19091913
{ let mRhs = rhs2 parseState 4 6
19101914
let doc = grabXmlDoc(parseState,4)
19111915
let mValDecl = rhs2 parseState 1 6
1912-
(fun mLeft attribs isStatic ->
1913-
let mValDecl = match mLeft with None -> mValDecl | Some m -> unionRanges m mValDecl
1916+
(fun rangeStart attribs isStatic ->
1917+
let mValDecl = unionRanges rangeStart mValDecl
19141918
let fld = Field(attribs,isStatic,Some $4,$6,$2,doc,$3,mRhs)
19151919
[ SynMemberDefn.ValField(fld, mValDecl) ]) }
19161920

@@ -2294,20 +2298,20 @@ attrUnionCaseDecl:
22942298

22952299
| opt_attributes opt_access unionCaseName OF unionCaseRepr opt_OBLOCKSEP
22962300
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsUnionCasesCannotHaveVisibilityDeclarations(),rhs parseState 2))
2297-
let mDecl = rhs2 parseState 3 5
2301+
let mDecl = rhs2 parseState 1 5
22982302
(fun xmlDoc -> Choice2Of2 (UnionCase ( $1, $3,UnionCaseFields $5,xmlDoc,None,mDecl)))
22992303
}
23002304

23012305
| opt_attributes opt_access unionCaseName COLON topType opt_OBLOCKSEP
23022306
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsUnionCasesCannotHaveVisibilityDeclarations(),rhs parseState 2))
23032307
libraryOnlyWarning(lhs parseState)
2304-
let mDecl = rhs2 parseState 3 5
2308+
let mDecl = rhs2 parseState 1 5
23052309
(fun xmlDoc -> Choice2Of2 (UnionCase ( $1, $3,UnionCaseFullType $5,xmlDoc,None,mDecl)))
23062310
}
23072311

23082312
| opt_attributes opt_access unionCaseName EQUALS constant opt_OBLOCKSEP
23092313
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsEnumFieldsCannotHaveVisibilityDeclarations(),rhs parseState 2))
2310-
let mDecl = rhs2 parseState 3 5
2314+
let mDecl = rhs2 parseState 1 5
23112315
(fun xmlDoc -> Choice1Of2 (EnumCase ( $1, $3,$5,xmlDoc,mDecl)))
23122316
}
23132317

@@ -2365,18 +2369,18 @@ recdFieldDeclList:
23652369

23662370
/* A field declaration in a record type */
23672371
recdFieldDecl:
2368-
| opt_attributes fieldDecl
2369-
{ let fld = $2 $1 false
2370-
let (Field(a,b,c,d,e,f,vis,g)) = fld
2371-
if Option.isSome vis then errorR(Error(FSComp.SR.parsRecordFieldsCannotHaveVisibilityDeclarations(),rhs parseState 2))
2372-
Field(a,b,c,d,e,f,None,g) }
2372+
| opt_attributes fieldDecl
2373+
{ let wholeRange = rhs2 parseState 1 2
2374+
let fld = $2 $1 false wholeRange
2375+
let (Field (a, b, c, d, e, f, vis, wholeRange)) = fld
2376+
if Option.isSome vis then errorR (Error (FSComp.SR.parsRecordFieldsCannotHaveVisibilityDeclarations (), rhs parseState 2))
2377+
Field (a, b, c, d, e, f, None, wholeRange) }
23732378

23742379
/* Part of a field or val declaration in a record type or object type */
23752380
fieldDecl:
23762381
| opt_mutable opt_access ident COLON typ
2377-
{ let mRhs = rhs2 parseState 3 5
2378-
let xmlDoc = grabXmlDoc(parseState,3)
2379-
(fun attrs stat -> Field(attrs, stat,Some $3,$5,$1,xmlDoc,$2,mRhs)) }
2382+
{ let xmlDoc = grabXmlDoc (parseState, 3)
2383+
fun attrs stat wholeRange -> Field(attrs, stat, Some $3, $5, $1, xmlDoc, $2, wholeRange) }
23802384

23812385

23822386
/* An exception definition */

tests/fsharpqa/Source/Conformance/DeclarationElements/AccessibilityAnnotations/PermittedLocations/E_accessibilityOnInterface01.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// #Regression #Conformance #DeclarationElements #Accessibility
22
//
3-
//<Expects status="error" span="(13,12-13,67)" id="FS0561">Accessibility modifiers are not allowed on this member\. Abstract slots always have the same visibility as the enclosing type</Expects>
3+
//<Expects status="error" span="(13,5-13,67)" id="FS0561">Accessibility modifiers are not allowed on this member\. Abstract slots always have the same visibility as the enclosing type</Expects>
44
//
55

66
type public IDoStuffAsWell =

tests/fsharpqa/Source/Conformance/DeclarationElements/AccessibilityAnnotations/PermittedLocations/E_accessibilityOnInterface02.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// #Regression #Conformance #DeclarationElements #Accessibility
22
//
3-
//<Expects status="error" id="FS0561" span="(15,13-15,68)">Accessibility modifiers are not allowed on this member\. Abstract slots always have the same visibility as the enclosing type</Expects>
3+
//<Expects status="error" id="FS0561" span="(15,5-15,68)">Accessibility modifiers are not allowed on this member\. Abstract slots always have the same visibility as the enclosing type</Expects>
44
//
55

66
#light

tests/fsharpqa/Source/Conformance/DeclarationElements/AccessibilityAnnotations/PermittedLocations/E_accessibilityOnInterface03.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// #Regression #Conformance #DeclarationElements #Accessibility
22
//
3-
//<Expects status="error" id="FS0561" span="(15,14-15,69)">Accessibility modifiers are not allowed on this member\. Abstract slots always have the same visibility as the enclosing type</Expects>
3+
//<Expects status="error" id="FS0561" span="(15,5-15,69)">Accessibility modifiers are not allowed on this member\. Abstract slots always have the same visibility as the enclosing type</Expects>
44
//
55

66
#light

tests/fsharpqa/Source/Conformance/DeclarationElements/LetBindings/Basic/E_Literals02.fsi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// #Regression #Conformance #DeclarationElements #LetBindings
2-
//<Expects status="error" span="(13,1)" id="FS0876">A declaration may only be the \[<Literal>\] attribute if a constant value is also given, e\.g\. 'val x : int = 1'$</Expects>
3-
//<Expects status="error" span="(16,1)" id="FS0876">A declaration may only be the \[<Literal>\] attribute if a constant value is also given, e\.g\. 'val x : int = 1'$</Expects>
2+
//<Expects status="error" span="(12,1)" id="FS0876">A declaration may only be the \[<Literal>\] attribute if a constant value is also given, e\.g\. 'val x : int = 1'$</Expects>
3+
//<Expects status="error" span="(15,1)" id="FS0876">A declaration may only be the \[<Literal>\] attribute if a constant value is also given, e\.g\. 'val x : int = 1'$</Expects>
44

55

66
// FSB 1981, Signature must contain the literal value

tests/service/StructureTests.fs

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ let ``nested module``() =
6666
"""
6767
module MyModule =
6868
()
69+
70+
[<Foo>]
71+
module Module =
72+
()
6973
"""
70-
=> [ (2, 0, 3, 6), (2, 15, 3, 6) ]
74+
=> [ (2, 0, 3, 6), (2, 15, 3, 6)
75+
(5, 0, 7, 6), (6, 13, 7, 6) ]
7176

7277
[<Test>]
7378
let ``module with multiline function``() =
@@ -113,19 +118,20 @@ type Color =
113118
let ``record with interface``() =
114119
"""
115120
type Color =
116-
{ Red: int
117-
Green: int
118-
Blue: int
119-
}
121+
{ Red:
122+
int
123+
mutable Blue:
124+
int }
120125
121126
interface IDisposable with
122127
member __.Dispose() =
123128
(docEventListener :> IDisposable).Dispose()
124129
"""
125130
=>
126131
[ (2, 5, 10, 55), (2, 11, 10, 55)
127-
(3, 4, 4, 14), (3, 4, 4, 14)
132+
(3, 4, 6, 15), (3, 4, 6, 15)
128133
(3, 6, 4, 13), (3, 6, 4, 13)
134+
(5, 6, 6, 13), (5, 6, 6, 13)
129135
(8, 4, 10, 55), (8, 25, 10, 55)
130136
(9, 8, 10, 55), (9, 27, 10, 55)
131137
(9, 15, 10, 55), (9, 27, 10, 55) ]
@@ -607,3 +613,65 @@ module NestedModule =
607613
"""
608614
=> [ (4, 0, 5, 15), (4, 13, 5, 15)
609615
(9, 0, 10, 15), (9, 19, 10, 15) ]
616+
617+
[<Test>]
618+
let ``Member val`` () =
619+
"""
620+
type T() =
621+
member val field1 =
622+
()
623+
624+
[<CompiledName("Field2")>]
625+
member val field2 =
626+
()
627+
628+
static member val field3 =
629+
()
630+
631+
[<CompiledName("Field4")>]
632+
static member val field4 =
633+
()
634+
"""
635+
=> [ (2, 5, 15, 10), (2, 7, 15, 10)
636+
(3, 4, 4, 10), (3, 4, 4, 10)
637+
(6, 4, 8, 10), (6, 4, 8, 10)
638+
(10, 4, 11, 10), (10, 4, 11, 10)
639+
(13, 4, 15, 10), (13, 4, 15, 10) ]
640+
641+
[<Test>]
642+
let ``Secondary constructors`` () =
643+
"""
644+
type T() =
645+
new () =
646+
T ()
647+
648+
internal new () =
649+
T ()
650+
651+
[<Foo>]
652+
new () =
653+
T ()
654+
"""
655+
=> [ (2, 5, 11, 12), (2, 7, 11, 12)
656+
(3, 4, 4, 12), (3, 7, 4, 12)
657+
(3, 4, 4, 12), (4, 8, 4, 12)
658+
(6, 4, 7, 12), (6, 16, 7, 12)
659+
(6, 4, 7, 12), (7, 8, 7, 12)
660+
(9, 4, 11, 12), (10, 7, 11, 12)
661+
(9, 4, 11, 12), (11, 8, 11, 12) ]
662+
663+
664+
[<Test>]
665+
let ``Abstract members`` () =
666+
"""
667+
type T() =
668+
abstract Foo:
669+
int
670+
671+
[<Foo>]
672+
abstract Foo:
673+
int
674+
"""
675+
=> [ (2, 5, 8, 11), (2, 7, 8, 11)
676+
(3, 4, 4, 11), (4, 8, 4, 11)
677+
(6, 4, 8, 11), (8, 8, 8, 11) ]

0 commit comments

Comments
 (0)