Skip to content

Commit d35c175

Browse files
authored
Fix extern declaration accessibility (#10399)
* Keep extern declaration access * Add test * Simplify test * Check no modifier, explicit public modifier * Cleanup * Fix test
1 parent a6fd66e commit d35c175

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

src/fsharp/pars.fsy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2643,7 +2643,7 @@ cPrototype:
26432643
SynExpr.Ident (ident("failwith", rhs parseState 6)),
26442644
SynExpr.Const (SynConst.String("extern was not given a DllImport attribute", rhs parseState 8), rhs parseState 8),
26452645
mRhs)
2646-
(fun attrs vis ->
2646+
(fun attrs _ ->
26472647
let bindingId = SynPat.LongIdent (LongIdentWithDots([nm], []), None, Some noInferredTypars, SynArgPats.Pats [SynPat.Tuple(false, args, argsm)], vis, nmm)
26482648
let binding = mkSynBinding
26492649
(xmlDoc, bindingId)

tests/fsharp/typeProviders/helloWorld/provided.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,5 @@ module TheOuterType =
196196

197197
module DllImportSmokeTest =
198198
[<System.Runtime.InteropServices.DllImport("kernel32.dll")>]
199-
extern System.UInt32 private GetLastError()
199+
extern System.UInt32 GetLastError()
200200

tests/service/Common.fs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,15 @@ let matchBraces (name: string, code: string) =
228228
let braces = checker.MatchBraces(filePath, FSharp.Compiler.Text.SourceText.ofString code, options) |> Async.RunSynchronously
229229
braces
230230

231-
let parseSourceCodeAndGetModule (source: string) =
232-
match parseSourceCode ("test", source) with
233-
| Some (ParsedInput.ImplFile (ParsedImplFileInput (_, _, _, _, _, [ moduleOrNamespace ], _))) -> moduleOrNamespace
231+
232+
let getSingleModuleLikeDecl (input: ParsedInput option) =
233+
match input with
234+
| Some (ParsedInput.ImplFile (ParsedImplFileInput (modules = [ decl ]))) -> decl
234235
| _ -> failwith "Could not get module decls"
236+
237+
let parseSourceCodeAndGetModule (source: string) =
238+
parseSourceCode ("test", source) |> getSingleModuleLikeDecl
239+
235240

236241
/// Extract range info
237242
let tups (m:Range.range) = (m.StartLine, m.StartColumn), (m.EndLine, m.EndColumn)

tests/service/Symbols.fs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ module Tests.Service.Symbols
88
#endif
99

1010
open FSharp.Compiler.Service.Tests.Common
11+
open FSharp.Compiler.SourceCodeServices
12+
open FSharp.Compiler.SyntaxTree
1113
open FsUnit
1214
open NUnit.Framework
13-
open FSharp.Compiler.SourceCodeServices
1415

1516
module ActivePatterns =
1617

@@ -57,6 +58,37 @@ match "foo" with
5758
getCaseUsages partialPatternInput 7 |> Array.head |> getGroupName |> shouldEqual "|String|_|"
5859

5960

61+
module ExternDeclarations =
62+
[<Test>]
63+
let ``Access modifier`` () =
64+
let parseResults, checkResults = getParseAndCheckResults """
65+
extern int a()
66+
extern int public b()
67+
extern int private c()
68+
"""
69+
let (SynModuleOrNamespace (decls = decls)) = getSingleModuleLikeDecl parseResults.ParseTree
70+
71+
[ None
72+
Some SynAccess.Public
73+
Some SynAccess.Private ]
74+
|> List.zip decls
75+
|> List.iter (fun (actual, expected) ->
76+
match actual with
77+
| SynModuleDecl.Let (_, [Binding (accessibility = access)], _) -> access |> should equal expected
78+
| decl -> failwithf "unexpected decl: %O" decl)
79+
80+
[ "a", (true, false, false, false)
81+
"b", (true, false, false, false)
82+
"c", (false, false, false, true) ]
83+
|> List.iter (fun (name, expected) ->
84+
match findSymbolByName name checkResults with
85+
| :? FSharpMemberOrFunctionOrValue as mfv ->
86+
let access = mfv.Accessibility
87+
(access.IsPublic, access.IsProtected, access.IsInternal, access.IsPrivate)
88+
|> should equal expected
89+
| _ -> failwithf "Couldn't get mfv: %s" name)
90+
91+
6092
module XmlDocSig =
6193

6294
[<Test>]

0 commit comments

Comments
 (0)