Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbf6bd3f

Browse files
Vasily KirichenkoVasily Kirichenko
Vasily Kirichenko
authored and
Vasily Kirichenko
committed
properly handle own namespace opening
1 parente146709 commitbf6bd3f

File tree

9 files changed

+59
-26
lines changed

9 files changed

+59
-26
lines changed

‎src/fsharp/NameResolution.fs‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,15 +1223,23 @@ type ItemOccurence =
12231223
| RelatedText
12241224

12251225
typeOpenDeclaration=
1226-
{ Idents:Ident list
1227-
ModuleRefs:ModuleOrNamespaceRef list
1228-
AppliedScope:range}
1229-
memberthis.Range=
1230-
match this.Identswith
1231-
|[]-> None
1232-
| first:: rest->
1233-
letlast= rest|> List.tryLast|> Option.defaultValue first
1234-
Some(mkRange this.AppliedScope.FileName first.idRange.Start last.idRange.End)
1226+
{ LongId:Ident list
1227+
Range:range option
1228+
Modules:ModuleOrNamespaceRef list
1229+
AppliedScope:range
1230+
IsOwnNamespace:bool}
1231+
1232+
static memberCreate(longId:Ident list,modules:ModuleOrNamespaceRef list,appliedScope:range,isOwnNamespace:bool)=
1233+
{ LongId= longId
1234+
Range=
1235+
match longIdwith
1236+
|[]-> None
1237+
| first:: rest->
1238+
letlast= rest|> List.tryLast|> Option.defaultValue first
1239+
Some(mkRange appliedScope.FileName first.idRange.Start last.idRange.End)
1240+
Modules= modules
1241+
AppliedScope= appliedScope
1242+
IsOwnNamespace= isOwnNamespace}
12351243

12361244
/// An abstract type for reporting the results of name resolution and type checking.
12371245
typeITypecheckResultsSink=

‎src/fsharp/NameResolution.fsi‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type NameResolver =
2222
memberamap:ImportMap
2323
memberg:TcGlobals
2424

25-
///Try to find a type with a record field ofthegiven name
25+
///Get the active pattern elements defined in a module, if any. Cache intheslot in the module type.
2626
valActivePatternElemsOfModuleOrNamespace:ModuleOrNamespaceRef->NameMap<ActivePatternElemRef>
2727

2828
[<NoEquality; NoComparison; RequireQualifiedAccess>]
@@ -312,18 +312,24 @@ type internal TcSymbolUses =
312312

313313
/// Represents open declaration statement.
314314
type internal OpenDeclaration=
315-
{///Idents.
316-
Idents:Ident list
315+
{///Long identifier as it's presented in soruce code.
316+
LongId:Ident list
317317

318+
/// Full range of the open declaration.
319+
Range:range option
320+
318321
/// Modules or namespaces which is opened with this declaration.
319-
ModuleRefs:ModuleOrNamespaceRef list
322+
Modules:ModuleOrNamespaceRef list
320323

321324
/// Scope in which open declaration is visible.
322-
AppliedScope:range}
325+
AppliedScope:range
326+
327+
/// If it's `namespace Xxx.Yyy` declaration.
328+
IsOwnNamespace:bool}
329+
330+
/// Create a new instance of OpenDeclaration.
331+
static memberCreate:longId:Ident list* modules: ModuleOrNamespaceRef list* appliedScope: range* isOwnNamespace: bool-> OpenDeclaration
323332
324-
/// Range of the open declaration.
325-
memberRange:range option
326-
327333
/// An abstract type for reporting the results of name resolution and type checking
328334
type ITypecheckResultsSink=
329335

‎src/fsharp/TypeChecker.fs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,8 @@ let ImplicitlyOpenOwnNamespace tcSink g amap scopem enclosingNamespacePath env =
699699
match ResolveLongIndentAsModuleOrNamespace ResultCollectionSettings.AllResults amap scopem OpenQualified env.eNameResEnv ad enclosingNamespacePathToOpen with
700700
| Result modrefs ->
701701
let modrefs = List.map p23 modrefs
702-
OpenModulesOrNamespaces tcSink g amap scopem false env modrefs { Idents = enclosingNamespacePathToOpen; ModuleRefs = modrefs; AppliedScope = scopem }
702+
let openDecl = OpenDeclaration.Create (enclosingNamespacePathToOpen, modrefs, scopem, true)
703+
OpenModulesOrNamespaces tcSink g amap scopem false env modrefs openDecl
703704
| Exception _ -> env
704705

705706

@@ -12079,7 +12080,8 @@ let TcOpenDecl tcSink (g:TcGlobals) amap m scopem env (longId : Ident list) =
1207912080
let modrefs = List.map p23 modrefs
1208012081
modrefs |> List.iter (fun modref -> CheckEntityAttributes g modref m |> CommitOperationResult)
1208112082

12082-
let env = OpenModulesOrNamespaces tcSink g amap scopem false env modrefs { Idents = longId; ModuleRefs = modrefs; AppliedScope = scopem }
12083+
let openDecl = OpenDeclaration.Create (longId, modrefs, scopem, false)
12084+
let env = OpenModulesOrNamespaces tcSink g amap scopem false env modrefs openDecl
1208312085
env
1208412086

1208512087

@@ -16844,7 +16846,9 @@ let ApplyAssemblyLevelAutoOpenAttributeToTcEnv g amap (ccu: CcuThunk) scopem env
1684416846
let modref = mkNonLocalTyconRef (mkNonLocalEntityRef ccu (Array.ofList h)) t
1684516847
match modref.TryDeref with
1684616848
| VNone -> warn()
16847-
| VSome _ -> OpenModulesOrNamespaces TcResultsSink.NoSink g amap scopem root env [modref] { Idents = []; ModuleRefs = [modref]; AppliedScope = scopem }
16849+
| VSome _ ->
16850+
let openDecl = OpenDeclaration.Create ([], [modref], scopem, false)
16851+
OpenModulesOrNamespaces TcResultsSink.NoSink g amap scopem root env [modref] openDecl
1684816852

1684916853
// Add the CCU and apply the "AutoOpen" attributes
1685016854
let AddCcuToTcEnv(g, amap, scopem, env, assemblyName, ccu, autoOpens, internalsVisible) =

‎src/fsharp/symbols/Symbols.fs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2272,7 +2272,8 @@ type FSharpOpenDeclaration =
22722272
{ LongId:Ident list
22732273
Range:range option
22742274
Modules:FSharpEntity list
2275-
AppliedScope:range}
2275+
AppliedScope:range
2276+
IsOwnNamespace:bool}
22762277

22772278
[<Sealed>]
22782279
typeFSharpSymbolUse(g:TcGlobals,denv: DisplayEnv,symbol:FSharpSymbol,itemOcc,range: range)=

‎src/fsharp/symbols/Symbols.fsi‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,10 @@ type internal FSharpOpenDeclaration =
10851085
Modules:FSharpEntity list
10861086

10871087
/// Scope in which open declaration is visible.
1088-
AppliedScope:range}
1088+
AppliedScope:range
1089+
1090+
/// If it's `namespace Xxx.Yyy` declaration.
1091+
IsOwnNamespace:bool}
10891092

10901093
/// Represents the use of an F# symbol from F# source code
10911094
[<Sealed>]

‎src/fsharp/vs/ServiceAnalysis.fs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module UnusedOpens =
4848

4949
letgetOpenStatements(openDeclarations:FSharpOpenDeclaration list):OpenStatement list=
5050
openDeclarations
51+
|> List.filter(fun x->not x.IsOwnNamespace)
5152
|> List.choose(fun openDecl->
5253
match openDecl.LongId, openDecl.Rangewith
5354
| firstId::_, Some range->

‎src/fsharp/vs/service.fs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,10 +2037,12 @@ type FSharpCheckFileResults(filename: string, errors: FSharpErrorInfo[], scopeOp
20372037
|> Option.map(fun scope->
20382038
letcenv= Impl.cenv(scope.TcGlobals, scope.ThisCcu, scope.TcImports)
20392039
scope.OpenDeclarations|> List.map(fun x->
2040-
{ LongId= x.Idents
2040+
{ LongId= x.LongId
20412041
Range= x.Range
2042-
Modules= x.ModuleRefs|> List.map(fun x-> FSharpEntity(cenv, x))
2043-
AppliedScope= x.AppliedScope}))
2042+
Modules= x.Modules|> List.map(fun x-> FSharpEntity(cenv, x))
2043+
AppliedScope= x.AppliedScope
2044+
IsOwnNamespace= x.IsOwnNamespace}
2045+
: FSharpOpenDeclaration))
20442046
|> Option.defaultValue[]
20452047

20462048
overrideinfo.ToString()="FSharpCheckFileResults("+ filename+")"

‎vsintegration/src/FSharp.Editor/Diagnostics/UnusedDeclarationsAnalyzer.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type internal UnusedDeclarationsAnalyzer() =
3535
match symbolwith
3636
// Determining that a record, DU or module is used anywhere requires inspecting all their enclosed entities (fields, cases and func / vals)
3737
// for usages, which is too expensive to do. Hence we never gray them out.
38-
|:? FSharpEntityas ewhen e.IsFSharpRecord|| e.IsFSharpUnion|| e.IsInterface|| e.IsFSharpModule|| e.IsClass->false
38+
|:? FSharpEntityas ewhen e.IsFSharpRecord|| e.IsFSharpUnion|| e.IsInterface|| e.IsFSharpModule|| e.IsClass|| e.IsNamespace->false
3939
// FCS returns inconsistent results for override members; we're skipping these symbols.
4040
|:? FSharpMemberOrFunctionOrValueas fwhen
4141
f.IsOverrideOrExplicitInterfaceImplementation||

‎vsintegration/tests/unittests/UnusedOpensTests.fs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,3 +649,11 @@ open System
649649
let f (x: 'a when 'a :> IDisposable) = ()
650650
"""
651651
=>[]
652+
653+
[<Test>]
654+
let``namespace declaration should never be marked as unused``()=
655+
"""
656+
namespace Library2
657+
type T() = class end
658+
"""
659+
=>[]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp