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

Commitf087d4f

Browse files
authored
Merge pull request #3803 from vasily-kirichenko/typed-open-declarations-2
Typed Unused Opens analyzer
2 parents1422322 +6a978f1 commitf087d4f

File tree

31 files changed

+1323
-527
lines changed

31 files changed

+1323
-527
lines changed

‎fcs/FSharp.Compiler.Service.Tests.netcore/FSharp.Compiler.Service.Tests.netcore.fsproj‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<ProjectSdk="Microsoft.NET.Sdk">
1+
<ProjectSdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netcoreapp1.0</TargetFramework>
44
<DefineConstants>$(DefineConstants);DOTNETCORE;FX_ATLEAST_45;FX_ATLEAST_PORTABLE;FX_NO_RUNTIMEENVIRONMENT;FX_RESHAPED_REFLECTION;TODO_REWORK_ASSEMBLY_LOAD;</DefineConstants>
@@ -42,6 +42,7 @@
4242
</ItemGroup>
4343
<ItemGroup>
4444
<PackageReferenceInclude="Microsoft.NET.Test.Sdk"Version="15.0.0" />
45+
<PackageReferenceInclude="NUnit3TestAdapter"Version="3.8.0" />
4546
<PackageReferenceInclude="NUnitLite"Version="3.6.1" />
4647
<PackageReferenceInclude="NUnit"Version="3.6.1" />
4748
<PackageReferenceInclude="FSharp.Core"Version="4.1.*"PrivateAssets="All" />

‎fcs/FSharp.Compiler.Service.netstandard/FSharp.Compiler.Service.netstandard.fsproj‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@
536536
<CompileInclude="$(FSharpSourcesRoot)\fsharp\symbols\Exprs.fs">
537537
<Link>Symbols/Exprs.fs</Link>
538538
</Compile>
539+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\symbols\SymbolPatterns.fsi">
540+
<Link>Symbols/SymbolPatterns.fsi</Link>
541+
</Compile>
542+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\symbols\SymbolPatterns.fs">
543+
<Link>Symbols/SymbolPatterns.fs</Link>
544+
</Compile>
539545
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\IncrementalBuild.fsi">
540546
<Link>Service/IncrementalBuild.fsi</Link>
541547
</Compile>
@@ -632,6 +638,12 @@
632638
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\ServiceStructure.fs">
633639
<Link>Service/ServiceStructure.fs</Link>
634640
</Compile>
641+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\ServiceAnalysis.fsi">
642+
<Link>Service/ServiceAnalysis.fsi</Link>
643+
</Compile>
644+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\ServiceAnalysis.fs">
645+
<Link>Service/ServiceAnalysis.fs</Link>
646+
</Compile>
635647
<CompileInclude="$(FSharpSourcesRoot)\fsharp\fsi\fsi.fsi">
636648
<Link>Service/fsi.fsi</Link>
637649
</Compile>

‎fcs/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,12 @@
512512
<CompileInclude="$(FSharpSourcesRoot)\fsharp\symbols\Exprs.fs">
513513
<Link>Symbols/Exprs.fs</Link>
514514
</Compile>
515+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\symbols\SymbolPatterns.fsi">
516+
<Link>Symbols/SymbolPatterns.fsi</Link>
517+
</Compile>
518+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\symbols\SymbolPatterns.fs">
519+
<Link>Symbols/SymbolPatterns.fs</Link>
520+
</Compile>
515521
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\IncrementalBuild.fsi">
516522
<Link>Service/IncrementalBuild.fsi</Link>
517523
</Compile>
@@ -608,6 +614,12 @@
608614
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\ServiceStructure.fs">
609615
<Link>Service/ServiceStructure.fs</Link>
610616
</Compile>
617+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\ServiceAnalysis.fsi">
618+
<Link>Service/ServiceAnalysis.fsi</Link>
619+
</Compile>
620+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\ServiceAnalysis.fs">
621+
<Link>Service/ServiceAnalysis.fs</Link>
622+
</Compile>
611623
<CompileInclude="$(FSharpSourcesRoot)\fsharp\fsi\fsi.fsi">
612624
<Link>Service/fsi.fsi</Link>
613625
</Compile>

‎src/buildfromsource/FSharp.Compiler.Private/FSharp.Compiler.Private.fsproj‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,12 @@
492492
<CompileInclude="$(FSharpSourcesRoot)\fsharp\symbols\Exprs.fs">
493493
<Link>Symbols/Exprs.fs</Link>
494494
</Compile>
495+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\symbols\SymbolPatterns.fsi">
496+
<Link>Symbols/SymbolPatterns.fsi</Link>
497+
</Compile>
498+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\symbols\SymbolPatterns.fs">
499+
<Link>Symbols/SymbolPatterns.fs</Link>
500+
</Compile>
495501

496502
<!-- the incremental builder and service .-->
497503
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\IncrementalBuild.fsi">
@@ -590,6 +596,12 @@
590596
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\ServiceStructure.fs">
591597
<Link>Service/ServiceStructure.fs</Link>
592598
</Compile>
599+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\ServiceAnalysis.fsi">
600+
<Link>Service/ServiceAnalysis.fsi</Link>
601+
</Compile>
602+
<CompileInclude="$(FSharpSourcesRoot)\fsharp\vs\ServiceAnalysis.fs">
603+
<Link>Service/ServiceAnalysis.fs</Link>
604+
</Compile>
593605

594606
<!-- the core of the F# Interactive fsi.exe implementation-->
595607
<FsSrGenInclude="$(FSharpSourcesRoot)\fsharp\fsi\FSIstrings.txt">

‎src/fsharp/FSharp.Compiler.Private/FSharp.Compiler.Private.fsproj‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,12 @@
546546
<CompileInclude="..\symbols\Exprs.fs">
547547
<Link>Symbols/Exprs.fs</Link>
548548
</Compile>
549+
<CompileInclude="..\symbols\SymbolPatterns.fsi">
550+
<Link>Symbols/SymbolPatterns.fsi</Link>
551+
</Compile>
552+
<CompileInclude="..\symbols\SymbolPatterns.fs">
553+
<Link>Symbols/SymbolPatterns.fs</Link>
554+
</Compile>
549555

550556
<!-- the incremental builder and service .-->
551557
<CompileInclude="..\vs\IncrementalBuild.fsi">
@@ -644,7 +650,13 @@
644650
<CompileInclude="..\vs\ServiceStructure.fs">
645651
<Link>Service/ServiceStructure.fs</Link>
646652
</Compile>
647-
653+
<CompileInclude="..\vs\ServiceAnalysis.fsi">
654+
<Link>Service/ServiceAnalysis.fsi</Link>
655+
</Compile>
656+
<CompileInclude="..\vs\ServiceAnalysis.fs">
657+
<Link>Service/ServiceAnalysis.fs</Link>
658+
</Compile>
659+
648660
<!-- the core of the F# Interactive fsi.exe implementation-->
649661
<EmbeddedTextInclude="..\fsi\FSIstrings.txt">
650662
<Link>FSIstrings.txt</Link>

‎src/fsharp/NameResolution.fs‎

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

1225+
typeOpenDeclaration=
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}
1243+
12251244
/// An abstract type for reporting the results of name resolution and type checking.
12261245
typeITypecheckResultsSink=
12271246
abstractNotifyEnvWithScope :range*NameResolutionEnv*AccessorDomain->unit
12281247
abstractNotifyExprHasType :pos*TType*Tastops.DisplayEnv*NameResolutionEnv*AccessorDomain*range->unit
12291248
abstractNotifyNameResolution :pos*Item*Item*TyparInst*ItemOccurence*Tastops.DisplayEnv*NameResolutionEnv*AccessorDomain*range*bool->unit
12301249
abstractNotifyFormatSpecifierLocation :range*int->unit
1250+
abstractNotifyOpenDeclaration :OpenDeclaration->unit
12311251
abstractCurrentSource :stringoption
12321252

12331253
let(|ValRefOfProp|_|)(pi:PropInfo)= pi.ArbitraryValRef
@@ -1460,6 +1480,7 @@ type TcResultsSinkImpl(g, ?source: string) =
14601480
member__.GetHashCode((p:pos,i))= p.Line+101* p.Column+ hash i
14611481
member__.Equals((p1,i1),(p2,i2))= posEq p1 p2&& i1= i2})
14621482
letcapturedMethodGroupResolutions= ResizeArray<_>()
1483+
letcapturedOpenDeclarations= ResizeArray<_>()
14631484
letallowedRange(m:range)=not m.IsSynthetic
14641485

14651486
memberthis.GetResolutions()=
@@ -1468,6 +1489,8 @@ type TcResultsSinkImpl(g, ?source: string) =
14681489
memberthis.GetSymbolUses()=
14691490
TcSymbolUses(g, capturedNameResolutions, capturedFormatSpecifierLocations.ToArray())
14701491

1492+
memberthis.OpenDeclarations= Seq.toList capturedOpenDeclarations
1493+
14711494
interface ITypecheckResultsSinkwith
14721495
membersink.NotifyEnvWithScope(m,nenv,ad)=
14731496
if allowedRange mthen
@@ -1504,6 +1527,9 @@ type TcResultsSinkImpl(g, ?source: string) =
15041527
membersink.NotifyFormatSpecifierLocation(m,numArgs)=
15051528
capturedFormatSpecifierLocations.Add((m, numArgs))
15061529

1530+
membersink.NotifyOpenDeclaration(openDeclaration)=
1531+
capturedOpenDeclarations.Add(openDeclaration)
1532+
15071533
membersink.CurrentSource= source
15081534

15091535

@@ -1550,6 +1576,11 @@ let CallExprHasTypeSink (sink:TcResultsSink) (m:range,nenv,typ,denv,ad) =
15501576
| None->()
15511577
| Some sink-> sink.NotifyExprHasType(m.End,typ,denv,nenv,ad,m)
15521578

1579+
letCallOpenDeclarationSink(sink:TcResultsSink)(openDeclaration:OpenDeclaration)=
1580+
match sink.CurrentSinkwith
1581+
| None->()
1582+
| Some sink-> sink.NotifyOpenDeclaration(openDeclaration)
1583+
15531584
//-------------------------------------------------------------------------
15541585
// Check inferability of type parameters in resolved items.
15551586
//-------------------------------------------------------------------------

‎src/fsharp/NameResolution.fsi‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ type NameResolver =
2222
memberamap:ImportMap
2323
memberg:TcGlobals
2424

25+
/// Get the active pattern elements defined in a module, if any. Cache in the slot in the module type.
26+
valActivePatternElemsOfModuleOrNamespace:ModuleOrNamespaceRef->NameMap<ActivePatternElemRef>
27+
2528
[<NoEquality; NoComparison; RequireQualifiedAccess>]
2629
/// Represents the item with which a named argument is associated.
2730
type ArgumentContainer=
@@ -307,7 +310,26 @@ type internal TcSymbolUses =
307310
/// Get the locations of all the printf format specifiers in the file
308311
memberGetFormatSpecifierLocationsAndArity:unit->(range* int)[]
309312

310-
313+
/// Represents open declaration statement.
314+
type internal OpenDeclaration=
315+
{/// Long identifier as it's presented in soruce code.
316+
LongId:Ident list
317+
318+
/// Full range of the open declaration.
319+
Range:range option
320+
321+
/// Modules or namespaces which is opened with this declaration.
322+
Modules:ModuleOrNamespaceRef list
323+
324+
/// Scope in which open declaration is visible.
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
332+
311333
/// An abstract type for reporting the results of name resolution and type checking
312334
type ITypecheckResultsSink=
313335

@@ -323,6 +345,9 @@ type ITypecheckResultsSink =
323345
/// Record that a printf format specifier occurred at a specific location in the source
324346
abstractNotifyFormatSpecifierLocation :range*int->unit
325347

348+
/// Record that an open declaration occured in a given scope range
349+
abstractNotifyOpenDeclaration :OpenDeclaration->unit
350+
326351
/// Get the current source
327352
abstractCurrentSource :stringoption
328353

@@ -337,6 +362,10 @@ type internal TcResultsSinkImpl =
337362

338363
/// Get all the uses of all symbols reported to the sink
339364
memberGetSymbolUses:unit->TcSymbolUses
365+
366+
/// Get all open declarations reported to the sink
367+
memberOpenDeclarations:OpenDeclaration list
368+
340369
interface ITypecheckResultsSink
341370

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

396+
/// Report an open declaration
397+
val internal CallOpenDeclarationSink: TcResultsSink-> OpenDeclaration-> unit
398+
367399
/// Get all the available properties of a type(both intrinsic and extension)
368400
val internal AllPropInfosOfTypeInScope: InfoReader-> NameResolutionEnv-> string option* AccessorDomain-> FindMemberFlag-> range-> TType-> PropInfo list
369401

‎src/fsharp/TypeChecker.fs‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,18 @@ 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
450+
match openDeclaration.Range with
451+
| None -> ()
452+
| Some range ->
453+
for modul in mvvs do
454+
let item = Item.ModuleOrNamespaces [modul]
455+
CallNameResolutionSink tcSink (range, env.NameEnv, item, item, emptyTyparInst, ItemOccurence.Use, env.DisplayEnv, env.eAccessRights)
449456
env
450457

451458
let AddRootModuleOrNamespaceRefs g amap m env modrefs =
@@ -691,7 +698,10 @@ let ImplicitlyOpenOwnNamespace tcSink g amap scopem enclosingNamespacePath env =
691698

692699
let ad = env.eAccessRights
693700
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)
701+
| Result modrefs ->
702+
let modrefs = List.map p23 modrefs
703+
let openDecl = OpenDeclaration.Create (enclosingNamespacePathToOpen, modrefs, scopem, true)
704+
OpenModulesOrNamespaces tcSink g amap scopem false env modrefs openDecl
695705
| Exception _ -> env
696706

697707

@@ -1867,6 +1877,7 @@ let MakeAndPublishSimpleVals cenv env m names mergeNamesInOneNameresEnv =
18671877
nameResolutions.Add(pos, item, itemGroup, itemTyparInst, occurence, denv, nenv, ad, m, replacing)
18681878
member this.NotifyExprHasType(_, _, _, _, _, _) = assert false // no expr typings in MakeSimpleVals
18691879
member this.NotifyFormatSpecifierLocation(_, _) = ()
1880+
member this.NotifyOpenDeclaration(_) = ()
18701881
member this.CurrentSource = None }
18711882

18721883
use _h = WithNewTypecheckResultsSink(sink, cenv.tcSink)
@@ -12099,9 +12110,11 @@ let TcOpenDecl tcSink (g:TcGlobals) amap m scopem env (longId : Ident list) =
1209912110
if IsPartiallyQualifiedNamespace modref then
1210012111
errorR(Error(FSComp.SR.tcOpenUsedWithPartiallyQualifiedPath(fullDisplayTextOfModRef modref), m)))
1210112112

12102-
modrefs |> List.iter (fun (_, modref, _) -> CheckEntityAttributes g modref m |> CommitOperationResult)
12113+
let modrefs = List.map p23 modrefs
12114+
modrefs |> List.iter (fun modref -> CheckEntityAttributes g modref m |> CommitOperationResult)
1210312115

12104-
let env = OpenModulesOrNamespaces tcSink g amap scopem false env (List.map p23 modrefs)
12116+
let openDecl = OpenDeclaration.Create (longId, modrefs, scopem, false)
12117+
let env = OpenModulesOrNamespaces tcSink g amap scopem false env modrefs openDecl
1210512118
env
1210612119

1210712120

@@ -16866,7 +16879,9 @@ let ApplyAssemblyLevelAutoOpenAttributeToTcEnv g amap (ccu: CcuThunk) scopem env
1686616879
let modref = mkNonLocalTyconRef (mkNonLocalEntityRef ccu (Array.ofList h)) t
1686716880
match modref.TryDeref with
1686816881
| VNone -> warn()
16869-
| VSome _ -> OpenModulesOrNamespaces TcResultsSink.NoSink g amap scopem root env [modref]
16882+
| VSome _ ->
16883+
let openDecl = OpenDeclaration.Create ([], [modref], scopem, false)
16884+
OpenModulesOrNamespaces TcResultsSink.NoSink g amap scopem root env [modref] openDecl
1687016885

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp