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

Commitf81fea2

Browse files
collect open declarations EntityRefs during compilation
1 parentef55edb commitf81fea2

File tree

10 files changed

+103
-46
lines changed

10 files changed

+103
-46
lines changed

‎src/fsharp/NameResolution.fs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,12 +1222,17 @@ type ItemOccurence =
12221222
/// Result gets suppressed over this text range
12231223
| RelatedText
12241224

1225+
typeOpenDeclaration=
1226+
| OpenoflongId:Identlist*moduleRefs:ModuleOrNamespaceReflist*scopem:range
1227+
| AutoOpenModuleofidents:stringlist*moduleRef:ModuleOrNamespaceRef*scopem:range
1228+
12251229
/// An abstract type for reporting the results of name resolution and type checking.
12261230
typeITypecheckResultsSink=
12271231
abstractNotifyEnvWithScope :range*NameResolutionEnv*AccessorDomain->unit
12281232
abstractNotifyExprHasType :pos*TType*Tastops.DisplayEnv*NameResolutionEnv*AccessorDomain*range->unit
12291233
abstractNotifyNameResolution :pos*Item*Item*TyparInst*ItemOccurence*Tastops.DisplayEnv*NameResolutionEnv*AccessorDomain*range*bool->unit
12301234
abstractNotifyFormatSpecifierLocation :range*int->unit
1235+
abstractNotifyOpenDeclaration :OpenDeclaration->unit
12311236
abstractCurrentSource :stringoption
12321237

12331238
let(|ValRefOfProp|_|)(pi:PropInfo)= pi.ArbitraryValRef
@@ -1460,6 +1465,7 @@ type TcResultsSinkImpl(g, ?source: string) =
14601465
member__.GetHashCode((p:pos,i))= p.Line+101* p.Column+ hash i
14611466
member__.Equals((p1,i1),(p2,i2))= posEq p1 p2&& i1= i2})
14621467
letcapturedMethodGroupResolutions= ResizeArray<_>()
1468+
letcapturedOpenDeclarations= ResizeArray<_>()
14631469
letallowedRange(m:range)=not m.IsSynthetic
14641470

14651471
memberthis.GetResolutions()=
@@ -1468,6 +1474,8 @@ type TcResultsSinkImpl(g, ?source: string) =
14681474
memberthis.GetSymbolUses()=
14691475
TcSymbolUses(g, capturedNameResolutions, capturedFormatSpecifierLocations.ToArray())
14701476

1477+
memberthis.OpenDeclarations= Seq.toList capturedOpenDeclarations
1478+
14711479
interface ITypecheckResultsSinkwith
14721480
membersink.NotifyEnvWithScope(m,nenv,ad)=
14731481
if allowedRange mthen
@@ -1504,6 +1512,9 @@ type TcResultsSinkImpl(g, ?source: string) =
15041512
membersink.NotifyFormatSpecifierLocation(m,numArgs)=
15051513
capturedFormatSpecifierLocations.Add((m, numArgs))
15061514

1515+
membersink.NotifyOpenDeclaration(openDeclaration)=
1516+
capturedOpenDeclarations.Add(openDeclaration)
1517+
15071518
membersink.CurrentSource= source
15081519

15091520

@@ -1550,6 +1561,11 @@ let CallExprHasTypeSink (sink:TcResultsSink) (m:range,nenv,typ,denv,ad) =
15501561
| None->()
15511562
| Some sink-> sink.NotifyExprHasType(m.End,typ,denv,nenv,ad,m)
15521563

1564+
letCallOpenDeclarationSink(sink:TcResultsSink)(openDeclaration:OpenDeclaration)=
1565+
match sink.CurrentSinkwith
1566+
| None->()
1567+
| Some sink-> sink.NotifyOpenDeclaration(openDeclaration)
1568+
15531569
//-------------------------------------------------------------------------
15541570
// Check inferability of type parameters in resolved items.
15551571
//-------------------------------------------------------------------------

‎src/fsharp/NameResolution.fsi‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,16 @@ type internal TcSymbolUses =
307307
/// Get the locations of all the printf format specifiers in the file
308308
memberGetFormatSpecifierLocationsAndArity:unit->(range* int)[]
309309

310+
/// Represents open declaration statement.
311+
#if COMPILER_PUBLIC_API
312+
type OpenDeclaration=
313+
#else
314+
typeinternalOpenDeclaration=
315+
#endif
316+
/// Ordinary open declaration, i.e. one which opens a namespace or module.
317+
| OpenoflongId:Identlist*moduleRefs:ModuleOrNamespaceReflist*scopem:range
318+
/// Syntethic open declaration generated for auto open modules.
319+
| AutoOpenModuleofidents:stringlist*moduleRef:ModuleOrNamespaceRef*scopem:range
310320

311321
/// An abstract type for reporting the results of name resolution and type checking
312322
typeITypecheckResultsSink=
@@ -323,6 +333,9 @@ type ITypecheckResultsSink =
323333
/// Record that a printf format specifier occurred at a specific location in the source
324334
abstractNotifyFormatSpecifierLocation :range*int->unit
325335

336+
/// Record that an open declaration occured in a given scope range
337+
abstractNotifyOpenDeclaration :OpenDeclaration->unit
338+
326339
/// Get the current source
327340
abstractCurrentSource :stringoption
328341

@@ -337,6 +350,10 @@ type internal TcResultsSinkImpl =
337350

338351
/// Get all the uses of all symbols reported to the sink
339352
memberGetSymbolUses:unit->TcSymbolUses
353+
354+
/// Get all open declarations reported to the sink
355+
memberOpenDeclarations:OpenDeclaration list
356+
340357
interface ITypecheckResultsSink
341358

342359
/// An abstract type for reporting the results of name resolution and type checking, and which allows
@@ -364,6 +381,9 @@ val internal CallNameResolutionSinkReplacing : TcResultsSink -> range * Name
364381
/// Report a specific name resolution at a source range
365382
val internal CallExprHasTypeSink: TcResultsSink-> range* NameResolutionEnv* TType* DisplayEnv* AccessorDomain-> unit
366383

384+
/// Report an open declaration
385+
val internal CallOpenDeclarationSink: TcResultsSink-> OpenDeclaration-> unit
386+
367387
/// Get all the available properties of a type(both intrinsic and extension)
368388
val internal AllPropInfosOfTypeInScope: InfoReader-> NameResolutionEnv-> string option* AccessorDomain-> FindMemberFlag-> range-> TType-> PropInfo list
369389

‎src/fsharp/TypeChecker.fs‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,12 @@ let AddLocalTyconsAndReport tcSink scopem g amap m tycons env =
441441
// Open a structure or an IL namespace
442442
//-------------------------------------------------------------------------
443443

444-
let OpenModulesOrNamespaces tcSink g amap scopem root env mvvs =
444+
let OpenModulesOrNamespaces tcSink g amap scopem root env mvvsopenDeclaration=
445445
let env =
446446
if isNil mvvs then env else
447447
ModifyNameResEnv (fun nenv -> AddModulesAndNamespacesContentsToNameEnv g amap env.eAccessRights scopem root nenv mvvs) env
448448
CallEnvSink tcSink (scopem, env.NameEnv, env.eAccessRights)
449+
CallOpenDeclarationSink tcSink openDeclaration
449450
env
450451

451452
let AddRootModuleOrNamespaceRefs g amap m env modrefs =
@@ -691,7 +692,9 @@ let ImplicitlyOpenOwnNamespace tcSink g amap scopem enclosingNamespacePath env =
691692

692693
let ad = env.eAccessRights
693694
match ResolveLongIndentAsModuleOrNamespace ResultCollectionSettings.AllResults amap scopem OpenQualified env.eNameResEnv ad enclosingNamespacePathToOpen with
694-
| Result modrefs -> OpenModulesOrNamespaces tcSink g amap scopem false env (List.map p23 modrefs)
695+
| Result modrefs ->
696+
let modrefs = List.map p23 modrefs
697+
OpenModulesOrNamespaces tcSink g amap scopem false env modrefs (OpenDeclaration.Open (enclosingNamespacePathToOpen, modrefs, scopem))
695698
| Exception _ -> env
696699

697700

@@ -1837,6 +1840,7 @@ let MakeAndPublishSimpleVals cenv env m names mergeNamesInOneNameresEnv =
18371840
nameResolutions.Add(pos, item, itemGroup, itemTyparInst, occurence, denv, nenv, ad, m, replacing)
18381841
member this.NotifyExprHasType(_, _, _, _, _, _) = assert false // no expr typings in MakeSimpleVals
18391842
member this.NotifyFormatSpecifierLocation(_, _) = ()
1843+
member this.NotifyOpenDeclaration(_) = ()
18401844
member this.CurrentSource = None }
18411845

18421846
use _h = WithNewTypecheckResultsSink(sink, cenv.tcSink)
@@ -12067,9 +12071,10 @@ let TcOpenDecl tcSink (g:TcGlobals) amap m scopem env (longId : Ident list) =
1206712071
if IsPartiallyQualifiedNamespace modref then
1206812072
errorR(Error(FSComp.SR.tcOpenUsedWithPartiallyQualifiedPath(fullDisplayTextOfModRef modref), m)))
1206912073

12070-
modrefs |> List.iter (fun (_, modref, _) -> CheckEntityAttributes g modref m |> CommitOperationResult)
12074+
let modrefs = List.map p23 modrefs
12075+
modrefs |> List.iter (fun modref -> CheckEntityAttributes g modref m |> CommitOperationResult)
1207112076

12072-
let env = OpenModulesOrNamespaces tcSink g amap scopem false env(List.map p23 modrefs)
12077+
let env = OpenModulesOrNamespaces tcSink g amap scopem false envmodrefs (OpenDeclaration.Open (longId, modrefs, scopem))
1207312078
env
1207412079

1207512080

@@ -16834,7 +16839,7 @@ let ApplyAssemblyLevelAutoOpenAttributeToTcEnv g amap (ccu: CcuThunk) scopem env
1683416839
let modref = mkNonLocalTyconRef (mkNonLocalEntityRef ccu (Array.ofList h)) t
1683516840
match modref.TryDeref with
1683616841
| VNone -> warn()
16837-
| VSome _ -> OpenModulesOrNamespaces TcResultsSink.NoSink g amap scopem root env [modref]
16842+
| VSome _ -> OpenModulesOrNamespaces TcResultsSink.NoSink g amap scopem root env [modref] (OpenDeclaration.AutoOpenModule (p, modref, scopem))
1683816843

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

‎src/fsharp/vs/IncrementalBuild.fs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,7 @@ type TypeCheckAccumulator =
10281028
tcEnvAtEndOfFile:TcEnv
10291029
tcResolutions:TcResolutions list
10301030
tcSymbolUses:TcSymbolUses list
1031+
tcOpenDeclarations:OpenDeclaration list
10311032
topAttribs:TopAttribs option
10321033
typedImplFiles:TypedImplFile list
10331034
tcDependencyFiles:string list
@@ -1102,6 +1103,7 @@ type PartialCheckResults =
11021103
Errors:(PhasedDiagnostic* FSharpErrorSeverity) list
11031104
TcResolutions:TcResolutions list
11041105
TcSymbolUses:TcSymbolUses list
1106+
TcOpenDeclarations:OpenDeclaration list
11051107
TcDependencyFiles:string list
11061108
TopAttribs:TopAttribs option
11071109
TimeStamp:System.DateTime
@@ -1116,6 +1118,7 @@ type PartialCheckResults =
11161118
Errors= tcAcc.tcErrors
11171119
TcResolutions= tcAcc.tcResolutions
11181120
TcSymbolUses= tcAcc.tcSymbolUses
1121+
TcOpenDeclarations= tcAcc.tcOpenDeclarations
11191122
TcDependencyFiles= tcAcc.tcDependencyFiles
11201123
TopAttribs= tcAcc.topAttribs
11211124
TimeStamp= timestamp
@@ -1327,6 +1330,7 @@ type IncrementalBuilder(tcGlobals, frameworkTcImports, nonFrameworkAssemblyInput
13271330
tcEnvAtEndOfFile=tcInitial
13281331
tcResolutions=[]
13291332
tcSymbolUses=[]
1333+
tcOpenDeclarations=[]
13301334
topAttribs=None
13311335
typedImplFiles=[]
13321336
tcDependencyFiles=basicDependencies
@@ -1375,6 +1379,7 @@ type IncrementalBuilder(tcGlobals, frameworkTcImports, nonFrameworkAssemblyInput
13751379
typedImplFiles=typedImplFiles
13761380
tcResolutions=tcAcc.tcResolutions@[tcResolutions]
13771381
tcSymbolUses=tcAcc.tcSymbolUses@[tcSymbolUses]
1382+
tcOpenDeclarations=tcAcc.tcOpenDeclarations@ sink.OpenDeclarations
13781383
tcErrors= tcAcc.tcErrors@ parseErrors@ capturingErrorLogger.GetErrors()
13791384
tcDependencyFiles= filename:: tcAcc.tcDependencyFiles}
13801385
}

‎src/fsharp/vs/IncrementalBuild.fsi‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ type internal PartialCheckResults =
5454
/// Represents the collected uses of symbols from type checking
5555
TcSymbolUses:TcSymbolUses list
5656

57+
/// Represents open declarations
58+
TcOpenDeclarations:OpenDeclaration list
59+
5760
TcDependencyFiles:string list
5861

5962
/// Represents the collected attributes to apply to the module of assuembly generates

‎src/fsharp/vs/ServiceAnalysis.fs‎

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,36 @@
33
namespaceMicrosoft.FSharp.Compiler.SourceCodeServices
44

55
openMicrosoft.FSharp.Compiler
6+
openMicrosoft.FSharp.Compiler.NameResolution
67
openMicrosoft.FSharp.Compiler.Ast
78
openMicrosoft.FSharp.Compiler.Range
89

910
moduleUnusedOpens=
11+
openMicrosoft.FSharp.Compiler.PrettyNaming
12+
1013
/// Represents single open statement.
1114
typeOpenStatement=
1215
{/// Full namespace or module identifier as it's presented in source code.
13-
LiteralIdent:string
14-
/// All possible namespace or module identifiers, including the literal one.
15-
AllPossibleIdents:Set<string>
16+
Idents:Set<string>
1617
/// Range of open statement itself.
1718
Range:range
1819
/// Enclosing module or namespace range (that is, the scope on in which this open statement is visible).
1920
ModuleRange:range}
2021

21-
let recvisitSynModuleOrNamespaceDecls(parent:Ast.LongIdent)(decls:SynModuleDecls)(moduleRange:range):OpenStatement list=
22-
[for declin declsdo
23-
match declwith
24-
| SynModuleDecl.Open(LongIdentWithDots.LongIdentWithDots(id= longId), range)->
25-
letliteralIdent= longId|> List.map(fun l-> l.idText)|> String.concat"."
26-
yield
27-
{ LiteralIdent= literalIdent
28-
AllPossibleIdents=
29-
set[yield literalIdent
30-
// `open N.M` can open N.M module from parent module as well, if it's non empty
31-
ifnot(List.isEmpty parent)then
32-
yield(parent@ longId|> List.map(fun l-> l.idText)|> String.concat".")]
33-
Range= range
34-
ModuleRange= moduleRange}
35-
36-
| SynModuleDecl.NestedModule(SynComponentInfo.ComponentInfo(longId= longId),_, decls,_,moduleRange)->
37-
yield! visitSynModuleOrNamespaceDecls longId decls moduleRange
38-
|_->()]
39-
40-
letgetOpenStatements(parsedInput:ParsedInput):OpenStatement list=
41-
match parsedInputwith
42-
| ParsedInput.ImplFile(ParsedImplFileInput(modules= modules))->
43-
[for mdin modulesdo
44-
letSynModuleOrNamespace(longId= longId; decls= decls; range= moduleRange)= md
45-
yield! visitSynModuleOrNamespaceDecls longId decls moduleRange]
46-
|_->[]
22+
letgetOpenStatements(openDeclarations:OpenDeclaration list):OpenStatement list=
23+
openDeclarations
24+
|> List.choose(fun openDeclaration->
25+
match openDeclarationwith
26+
| OpenDeclaration.Open(longId, moduleRefs, scopem)whennot(List.isEmpty longId)->
27+
28+
Some{ Idents= moduleRefs|> List.map(fun x-> x.DisplayName)|> Set.ofList
29+
Range=
30+
letfirst= List.head longId
31+
letlast= List.last longId
32+
mkRange scopem.FileName first.idRange.Start last.idRange.End
33+
ModuleRange= scopem}
34+
|_-> None// for now
35+
)
4736

4837
letgetAutoOpenAccessPath(ent:FSharpEntity)=
4938
// Some.Namespace+AutoOpenedModule+Entity
@@ -87,7 +76,7 @@ module UnusedOpens =
8776
{ Ident:string
8877
Location:range}
8978

90-
letgetUnusedOpens(symbolUses:FSharpSymbolUse[],parsedInput:ParsedInput,getSourceLineStr:int->string):range list=
79+
letgetUnusedOpens(symbolUses:FSharpSymbolUse[],openDeclarations:OpenDeclaration list,getSourceLineStr:int->string):range list=
9180
letgetPartNamespace(symbolUse:FSharpSymbolUse)(fullName:string)=
9281
// given a symbol range such as `Text.ISegment` and a full name of `MonoDevelop.Core.Text.ISegment`, return `MonoDevelop.Core`
9382
letlength= symbolUse.RangeAlternate.EndColumn- symbolUse.RangeAlternate.StartColumn
@@ -156,19 +145,20 @@ module UnusedOpens =
156145
let recfilterInner acc(list:OpenStatement list)(seenOpenStatements:OpenStatement list)=
157146

158147
letnotUsed(os:OpenStatement)=
159-
if os.LiteralIdent.StartsWith"`global`"thenfalse
148+
if os.Idents|> Set.exists(fun x-> x.StartsWithMangledGlobalName)thenfalse
160149
else
161150
letnotUsedAnywhere=
162151
not(namespacesInUse|> List.exists(fun nsu->
163-
rangeContainsRange os.ModuleRange nsu.Location&& os.AllPossibleIdents|> Set.contains nsu.Ident))
152+
rangeContainsRange os.ModuleRange nsu.Location&& os.Idents|> Set.contains nsu.Ident))
164153
if notUsedAnywherethentrue
165154
else
166155
letalreadySeen=
167156
seenOpenStatements
168157
|> List.exists(fun seenNs->
169158
// if such open statement has already been marked as used in this or outer module, we skip it
170159
// (that is, do not mark as used so far)
171-
rangeContainsRange seenNs.ModuleRange os.ModuleRange&& os.LiteralIdent= seenNs.LiteralIdent)
160+
rangeContainsRange seenNs.ModuleRange os.ModuleRange&&
161+
not(os.Idents|> Set.intersect seenNs.Idents|> Set.isEmpty))
172162
alreadySeen
173163

174164
match listwith
@@ -180,4 +170,4 @@ module UnusedOpens =
180170

181171
filterInner[] list[]
182172

183-
parsedInput|> getOpenStatements|> filter|> List.map(fun os-> os.Range)
173+
openDeclarations|> getOpenStatements|> filter|> List.map(fun os-> os.Range)

‎src/fsharp/vs/ServiceAnalysis.fsi‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespaceMicrosoft.FSharp.Compiler.SourceCodeServices
44

55
openMicrosoft.FSharp.Compiler.Ast
6+
openMicrosoft.FSharp.Compiler.NameResolution
67
openMicrosoft.FSharp.Compiler.Range
78

89
#if COMPILER_PUBLIC_API
910
moduleUnusedOpens=
1011
#else
1112
moduleinternalUnusedOpens=
1213
#endif
13-
valgetUnusedOpens:symbolUses:FSharpSymbolUse[]*parsedInput: ParsedInput* getSourceLineStr:(int-> string)-> range list
14+
valgetUnusedOpens:symbolUses:FSharpSymbolUse[]*openDeclarations: OpenDeclaration list* getSourceLineStr:(int-> string)-> range list

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp