Skip to content

Commit 6e6cf3e

Browse files
authored
Improve handling of line directives (#17536)
* fixes #17529, including tests * Cleanup FSharp.Compiler.ComponentTests.fsproj * Adapted namespace and module name of NonStringArgs tests to the new location
1 parent c9bed79 commit 6e6cf3e

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

src/Compiler/SyntaxTree/ParseHelpers.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ let posOfLexPosition (p: Position) = mkPos p.Line p.Column
4343

4444
/// Get an F# compiler range from a lexer range
4545
let mkSynRange (p1: Position) (p2: Position) =
46-
mkFileIndexRange p1.FileIndex (posOfLexPosition p1) (posOfLexPosition p2)
46+
if p1.FileIndex = p2.FileIndex then
47+
mkFileIndexRange p1.FileIndex (posOfLexPosition p1) (posOfLexPosition p2)
48+
else
49+
// This means we had a #line directive in the middle of this syntax element.
50+
mkFileIndexRange p1.FileIndex (posOfLexPosition p1) (posOfLexPosition (p1.ShiftColumnBy 1))
4751

4852
type LexBuffer<'Char> with
4953

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace CompilerDirectives
2+
3+
open Microsoft.FSharp.Control
4+
open Xunit
5+
open FSharp.Test.Compiler
6+
open FSharp.Compiler.CodeAnalysis
7+
open FSharp.Compiler.Text
8+
open FSharp.Compiler.Syntax
9+
10+
module Line =
11+
12+
let checker = FSharpChecker.Create()
13+
14+
let parse (source: string) =
15+
let langVersion = "preview"
16+
let sourceFileName = __SOURCE_FILE__
17+
let parsingOptions =
18+
{ FSharpParsingOptions.Default with
19+
SourceFiles = [| sourceFileName |]
20+
LangVersionText = langVersion
21+
ApplyLineDirectives = true
22+
}
23+
checker.ParseFile(sourceFileName, SourceText.ofString source, parsingOptions) |> Async.RunSynchronously
24+
25+
26+
[<Literal>]
27+
let private case1 = """module A
28+
#line 1 "xyz.fs"
29+
(
30+
printfn ""
31+
)
32+
"""
33+
34+
[<Literal>]
35+
let private case2 = """module A
36+
(
37+
#line 1 "xyz.fs"
38+
printfn ""
39+
)
40+
"""
41+
42+
[<Literal>]
43+
let private case3 = """module A
44+
(
45+
#line 1 "xyz.fs"
46+
)
47+
"""
48+
49+
[<Theory>]
50+
[<InlineData(1, case1, "xyz.fs:(1,0--3,1)")>]
51+
[<InlineData(2, case2, "Line.fs:(2,0--2,1)")>]
52+
[<InlineData(3, case3, "Line.fs:(2,0--2,1)")>]
53+
let ``check expr range interacting with line directive`` (case, source, expectedRange) =
54+
let parseResults = parse source
55+
if parseResults.ParseHadErrors then failwith "unexpected: parse error"
56+
let exprRange =
57+
match parseResults.ParseTree with
58+
| ParsedInput.ImplFile(ParsedImplFileInput(contents = contents)) ->
59+
let (SynModuleOrNamespace(decls = decls)) = List.exactlyOne contents
60+
match List.exactlyOne decls with
61+
| SynModuleDecl.Expr(_, range) -> $"{range.FileName}:{range}"
62+
| _ -> failwith $"unexpected: not an expr"
63+
| ParsedInput.SigFile _ -> failwith "unexpected: sig file"
64+
if exprRange <> expectedRange then
65+
failwith $"case{case}: expected: {expectedRange}, found {exprRange}"
66+

tests/FSharp.Compiler.ComponentTests/ErrorMessages/Directives.fs renamed to tests/FSharp.Compiler.ComponentTests/CompilerDirectives/NonStringArgs.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2-
namespace ErrorMessages
2+
namespace CompilerDirectives
33

44
open Xunit
55
open FSharp.Test.Compiler
66

7-
module HashDirectives =
7+
module NonStringArgs =
88

99
[<InlineData("8.0")>]
1010
[<InlineData("9.0")>]

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
<Compile Include="..\service\FsUnit.fs">
3232
<Link>FsUnit.fs</Link>
3333
</Compile>
34+
<Compile Include="CompilerDirectives\Line.fs" />
35+
<Compile Include="CompilerDirectives\NonStringArgs.fs" />
3436
<Compile Include="Conformance\BasicGrammarElements\AccessibilityAnnotations\Basic\Basic.fs" />
3537
<Compile Include="Conformance\BasicGrammarElements\AccessibilityAnnotations\OnOverridesAndIFaceImpl\OnOverridesAndIFaceImpl.fs" />
3638
<Compile Include="Conformance\BasicGrammarElements\AccessibilityAnnotations\OnTypeMembers\OnTypeMembers.fs" />
@@ -177,9 +179,9 @@
177179
<Compile Include="EmittedIL\TestFunctions\TestFunctions.fs" />
178180
<Compile Include="EmittedIL\Tuples\Tuples.fs" />
179181
<Compile Include="EmittedIL\Nullness\NullnessMetadata.fs" />
182+
<Compile Include="EmittedIL\FixedBindings\FixedBindings.fs" />
180183
<Compile Include="ErrorMessages\TypedInterpolatedStringsTests.fs" />
181184
<!--<Compile Include="EmittedIL\StructDefensiveCopy\StructDefensiveCopy.fs" />-->
182-
<Compile Include="EmittedIL\FixedBindings\FixedBindings.fs" />
183185
<Compile Include="ErrorMessages\UnsupportedAttributes.fs" />
184186
<Compile Include="ErrorMessages\TailCallAttribute.fs" />
185187
<Compile Include="ErrorMessages\IndexingSyntax.fs" />
@@ -196,7 +198,6 @@
196198
<Compile Include="ErrorMessages\MissingExpressionTests.fs" />
197199
<Compile Include="ErrorMessages\ModuleTests.fs" />
198200
<Compile Include="ErrorMessages\NameResolutionTests.fs" />
199-
<Compile Include="ErrorMessages\Directives.fs" />
200201
<Compile Include="ErrorMessages\SuggestionsTests.fs" />
201202
<Compile Include="ErrorMessages\TypeMismatchTests.fs" />
202203
<Compile Include="ErrorMessages\UnitGenericAbstactType.fs" />

0 commit comments

Comments
 (0)