Skip to content

Commit e389d77

Browse files
authored
Correct SynAttribute range (#13998)
* Add failing test. * Update range for SynAttribute. * Use unionRanges instead of mkFileIndexRange. * Update ranges in unit tests. * Fix ServiceTests. * Update failing FSharpSuite.Tests. * Update negative tests. * Trigger CI
1 parent ffdfc71 commit e389d77

File tree

18 files changed

+154
-96
lines changed

18 files changed

+154
-96
lines changed

src/Compiler/Service/ServiceParsedInputOps.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ module ParsedInput =
581581
|> Option.orElseWith (fun () -> ifPosInRange r (fun _ -> List.tryPick (walkSynModuleDecl isTopLevel) decls))
582582

583583
and walkAttribute (attr: SynAttribute) =
584-
if isPosInRange attr.Range then
584+
if isPosInRange attr.TypeName.Range then
585585
Some EntityKind.Attribute
586586
else
587587
None

src/Compiler/pars.fsy

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,18 +1561,23 @@ attributeListElements:
15611561
attribute:
15621562
/* A custom attribute */
15631563
| path opt_HIGH_PRECEDENCE_APP opt_atomicExprAfterType
1564-
{ let arg = match $3 with None -> mkSynUnit $1.Range | Some e -> e
1565-
({ TypeName=$1; ArgExpr=arg; Target=None; AppliesToGetterAndSetter=false; Range=$1.Range } : SynAttribute) }
1564+
{ let arg = match $3 with None -> mkSynUnit $1.Range | Some e -> e
1565+
let m = unionRanges $1.Range arg.Range
1566+
({ TypeName=$1; ArgExpr=arg; Target=None; AppliesToGetterAndSetter=false; Range=m } : SynAttribute) }
15661567

15671568
/* A custom attribute with an attribute target */
15681569
| attributeTarget path opt_HIGH_PRECEDENCE_APP opt_atomicExprAfterType
1569-
{ let arg = match $4 with None -> mkSynUnit $2.Range | Some e -> e
1570-
({ TypeName=$2; ArgExpr=arg; Target=$1; AppliesToGetterAndSetter=false; Range=$2.Range } : SynAttribute) }
1570+
{ let arg = match $4 with None -> mkSynUnit $2.Range | Some e -> e
1571+
let startRange = match $1 with Some (ident:Ident) -> ident.idRange | None -> $2.Range
1572+
let m = unionRanges startRange arg.Range
1573+
({ TypeName=$2; ArgExpr=arg; Target=$1; AppliesToGetterAndSetter=false; Range=m } : SynAttribute) }
15711574

15721575
/* A custom attribute with an attribute target */
15731576
| attributeTarget OBLOCKBEGIN path oblockend opt_HIGH_PRECEDENCE_APP opt_atomicExprAfterType
15741577
{ let arg = match $6 with None -> mkSynUnit $3.Range | Some e -> e
1575-
({ TypeName=$3; ArgExpr=arg; Target=$1; AppliesToGetterAndSetter=false; Range=$3.Range } : SynAttribute) }
1578+
let startRange = match $1 with Some ident -> ident.idRange | None -> $3.Range
1579+
let m = unionRanges startRange arg.Range
1580+
({ TypeName=$3; ArgExpr=arg; Target=$1; AppliesToGetterAndSetter=false; Range=m } : SynAttribute) }
15761581

15771582

15781583
/* The target of a custom attribute */

tests/FSharp.Compiler.ComponentTests/Conformance/BasicTypeAndModuleDefinitions/GeneratedEqualityHashingComparison/Attributes/Diags/Diags.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Diags =
1717
|> compile
1818
|> shouldFail
1919
|> withDiagnostics [
20-
(Error 501, Line 7, Col 3, Line 7, Col 23, "The object constructor 'StructuralComparisonAttribute' takes 0 argument(s) but is here given 1. The required signature is 'new: unit -> StructuralComparisonAttribute'.")
20+
(Error 501, Line 7, Col 3, Line 7, Col 30, "The object constructor 'StructuralComparisonAttribute' takes 0 argument(s) but is here given 1. The required signature is 'new: unit -> StructuralComparisonAttribute'.")
2121
]
2222

2323
// SOURCE=E_AdjustUses01b.fs SCFLAGS=--test:ErrorRanges # E_AdjustUses01b.fs
@@ -28,6 +28,6 @@ module Diags =
2828
|> compile
2929
|> shouldFail
3030
|> withDiagnostics [
31-
(Error 501, Line 7, Col 3, Line 7, Col 23, "The object constructor 'StructuralComparisonAttribute' takes 0 argument(s) but is here given 1. The required signature is 'new: unit -> StructuralComparisonAttribute'.")
31+
(Error 501, Line 7, Col 3, Line 7, Col 30, "The object constructor 'StructuralComparisonAttribute' takes 0 argument(s) but is here given 1. The required signature is 'new: unit -> StructuralComparisonAttribute'.")
3232
]
3333

tests/FSharp.Compiler.ComponentTests/Conformance/BasicTypeAndModuleDefinitions/GeneratedEqualityHashingComparison/Attributes/Legacy/Legacy.fs

Lines changed: 55 additions & 55 deletions
Large diffs are not rendered by default.

tests/FSharp.Compiler.ComponentTests/Conformance/DeclarationElements/CustomAttributes/AttributeUsage/AttributeUsage.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ module AttributeUsage =
7979
|> shouldFail
8080
|> withDiagnostics [
8181
(Error 842, Line 21, Col 21, Line 21, Col 22, "This attribute is not valid for use on this language element")
82-
(Error 842, Line 24, Col 28, Line 24, Col 29, "This attribute is not valid for use on this language element")
83-
(Error 842, Line 27, Col 15, Line 27, Col 16, "This attribute is not valid for use on this language element")
82+
(Error 842, Line 24, Col 21, Line 24, Col 29, "This attribute is not valid for use on this language element")
83+
(Error 842, Line 27, Col 7, Line 27, Col 16, "This attribute is not valid for use on this language element")
8484
]
8585

8686
// SOURCE=E_AttributeTargets02.fs # E_AttributeTargets02.fs
@@ -90,9 +90,9 @@ module AttributeUsage =
9090
|> verifyCompile
9191
|> shouldFail
9292
|> withDiagnostics [
93-
(Error 842, Line 14, Col 17, Line 14, Col 34, "This attribute is not valid for use on this language element")
94-
(Error 842, Line 24, Col 14, Line 24, Col 29, "This attribute is not valid for use on this language element")
95-
(Error 842, Line 29, Col 25, Line 29, Col 40, "This attribute is not valid for use on this language element")
93+
(Error 842, Line 14, Col 7, Line 14, Col 34, "This attribute is not valid for use on this language element")
94+
(Error 842, Line 24, Col 7, Line 24, Col 36, "This attribute is not valid for use on this language element")
95+
(Error 842, Line 29, Col 15, Line 29, Col 47, "This attribute is not valid for use on this language element")
9696
]
9797

9898
// SOURCE=E_ConditionalAttribute.fs SCFLAGS="--test:ErrorRanges" # E_ConditionalAttribute.fs

tests/FSharp.Compiler.ComponentTests/Conformance/DeclarationElements/CustomAttributes/Basic/Basic.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module Basic =
4141
|> verifyCompile
4242
|> shouldFail
4343
|> withDiagnostics [
44-
(Error 841, Line 7, Col 12, Line 7, Col 49, "This attribute is not valid for use on this language element. Assembly attributes should be attached to a 'do ()' declaration, if necessary within an F# module.")
44+
(Error 841, Line 7, Col 3, Line 7, Col 111, "This attribute is not valid for use on this language element. Assembly attributes should be attached to a 'do ()' declaration, if necessary within an F# module.")
4545
]
4646

4747
// SOURCE=E_AttributeApplication02.fs SCFLAGS="--test:ErrorRanges" # E_AttributeApplication02.fs
@@ -106,8 +106,8 @@ module Basic =
106106
(Error 1, Line 10, Col 3, Line 10, Col 59, "This expression was expected to have type\n 'int array' \nbut here has type\n 'unit' ")
107107
(Error 267, Line 10, Col 3, Line 10, Col 59, "This is not a valid constant expression or custom attribute value")
108108
(Error 850, Line 10, Col 3, Line 10, Col 59, "This attribute cannot be used in this version of F#")
109-
(Error 850, Line 13, Col 3, Line 13, Col 52, "This attribute cannot be used in this version of F#")
110-
(Error 850, Line 16, Col 13, Line 16, Col 37, "This attribute cannot be used in this version of F#")
109+
(Error 850, Line 13, Col 3, Line 13, Col 101, "This attribute cannot be used in this version of F#")
110+
(Error 850, Line 16, Col 3, Line 16, Col 50, "This attribute cannot be used in this version of F#")
111111
]
112112

113113
// SOURCE=E_AttributeTargetSpecifications.fs # E_AttributeTargetSpecifications.fs
@@ -305,7 +305,7 @@ module Basic =
305305
|> verifyCompile
306306
|> shouldFail
307307
|> withDiagnostics [
308-
(Error 429, Line 16, Col 28, Line 16, Col 31, "The attribute type 'CA1' has 'AllowMultiple=false'. Multiple instances of this attribute cannot be attached to a single language element.")
308+
(Error 429, Line 16, Col 28, Line 16, Col 37, "The attribute type 'CA1' has 'AllowMultiple=false'. Multiple instances of this attribute cannot be attached to a single language element.")
309309
]
310310

311311
// SOURCE=W_StructLayoutExplicit01.fs SCFLAGS="--test:ErrorRanges" PEVER="/Exp_Fail" # W_StructLayoutExplicit01.fs

tests/FSharp.Compiler.ComponentTests/Conformance/DeclarationElements/LetBindings/Basic/Basic.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ module Basic =
5353
|> shouldFail
5454
|> withDiagnostics [
5555
(Error 683, Line 14, Col 6, Line 14, Col 27, "Attributes are not allowed within patterns")
56-
(Error 842, Line 14, Col 8, Line 14, Col 23, "This attribute is not valid for use on this language element")
56+
(Error 842, Line 14, Col 8, Line 14, Col 25, "This attribute is not valid for use on this language element")
5757
(Error 683, Line 14, Col 42, Line 14, Col 63, "Attributes are not allowed within patterns")
58-
(Error 842, Line 14, Col 44, Line 14, Col 59, "This attribute is not valid for use on this language element")
58+
(Error 842, Line 14, Col 44, Line 14, Col 61, "This attribute is not valid for use on this language element")
5959
]
6060

6161
// SOURCE=E_ErrorsForInlineValue.fs SCFLAGS="--test:ErrorRanges" # E_ErrorsForInlineValue.fs

tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnsupportedAttributes.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type C() =
2727
Range = { StartLine = 3
2828
StartColumn = 13
2929
EndLine = 3
30-
EndColumn = 37 }
30+
EndColumn = 41 }
3131
Message =
3232
"This attribute is currently unsupported by the F# compiler. Applying it will not achieve its intended effect." }
3333
{ Error = Warning 202
@@ -41,7 +41,7 @@ type C() =
4141
Range = { StartLine = 6
4242
StartColumn = 22
4343
EndLine = 6
44-
EndColumn = 78 }
44+
EndColumn = 82 }
4545
Message =
4646
"This attribute is currently unsupported by the F# compiler. Applying it will not achieve its intended effect." }
4747
{ Error = Warning 202

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.Tests.fsproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@
119119
<Compile Include="..\service\SyntaxTreeTests\SynIdentTests.fs">
120120
<Link>SyntaxTree\SynIdentTests.fs</Link>
121121
</Compile>
122+
<Compile Include="..\service\SyntaxTreeTests\AttributeTests.fs">
123+
<Link>SyntaxTree\AttributeTests.fs</Link>
124+
</Compile>
122125
<Compile Include="..\service\FileSystemTests.fs">
123126
<Link>FileSystemTests.fs</Link>
124127
</Compile>

tests/fsharp/typecheck/sigs/neg20.bsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ neg20.fs(216,5,216,12): typecheck error FS0842: This attribute is not valid for
271271

272272
neg20.fs(219,5,219,15): typecheck error FS0842: This attribute is not valid for use on this language element
273273

274-
neg20.fs(222,5,222,24): typecheck error FS0842: This attribute is not valid for use on this language element
274+
neg20.fs(222,5,222,31): typecheck error FS0842: This attribute is not valid for use on this language element
275275

276276
neg20.fs(225,5,225,22): typecheck error FS0842: This attribute is not valid for use on this language element
277277

@@ -289,17 +289,17 @@ neg20.fs(243,5,243,23): typecheck error FS0842: This attribute is not valid for
289289

290290
neg20.fs(249,9,249,27): typecheck error FS0842: This attribute is not valid for use on this language element
291291

292-
neg20.fs(255,5,255,21): typecheck error FS0842: This attribute is not valid for use on this language element
292+
neg20.fs(255,5,255,28): typecheck error FS0842: This attribute is not valid for use on this language element
293293

294-
neg20.fs(258,5,258,31): typecheck error FS0842: This attribute is not valid for use on this language element
294+
neg20.fs(258,5,258,38): typecheck error FS0842: This attribute is not valid for use on this language element
295295

296296
neg20.fs(261,5,261,17): typecheck error FS0842: This attribute is not valid for use on this language element
297297

298298
neg20.fs(265,5,265,24): typecheck error FS0842: This attribute is not valid for use on this language element
299299

300300
neg20.fs(268,5,268,27): typecheck error FS0842: This attribute is not valid for use on this language element
301301

302-
neg20.fs(271,5,271,13): typecheck error FS0842: This attribute is not valid for use on this language element
302+
neg20.fs(271,5,271,15): typecheck error FS0842: This attribute is not valid for use on this language element
303303

304304
neg20.fs(278,14,278,95): typecheck error FS0507: No accessible member or object constructor named 'ProcessStartInfo' takes 0 arguments. Note the call to this member also provides 2 named arguments.
305305

0 commit comments

Comments
 (0)