@@ -176,7 +176,7 @@ func (snapshot *snapshot) Analyze(ctx context.Context, pkgs map[PackageID]unit,
176
176
// Filter and sort enabled root analyzers.
177
177
// A disabled analyzer may still be run if required by another.
178
178
toSrc := make (map [* analysis.Analyzer ]* source.Analyzer )
179
- var enabled []* analysis.Analyzer
179
+ var enabled []* analysis.Analyzer // enabled subset + transitive requirements
180
180
for _ , a := range analyzers {
181
181
if a .IsEnabled (snapshot .view .Options ()) {
182
182
toSrc [a .Analyzer ] = a
@@ -190,16 +190,16 @@ func (snapshot *snapshot) Analyze(ctx context.Context, pkgs map[PackageID]unit,
190
190
191
191
// Register fact types of required analyzers.
192
192
enabled = requiredAnalyzers (enabled )
193
- var useFacts []* analysis.Analyzer
193
+ var facty []* analysis.Analyzer // facty subset of enabled + transitive requirements
194
194
for _ , a := range enabled {
195
195
if len (a .FactTypes ) > 0 {
196
- useFacts = append (useFacts , a )
196
+ facty = append (facty , a )
197
197
for _ , f := range a .FactTypes {
198
198
gob .Register (f ) // <2us
199
199
}
200
200
}
201
201
}
202
- useFacts = requiredAnalyzers (useFacts )
202
+ facty = requiredAnalyzers (facty )
203
203
204
204
// File set for this batch (entire graph) of analysis.
205
205
fset := token .NewFileSet ()
@@ -208,7 +208,9 @@ func (snapshot *snapshot) Analyze(ctx context.Context, pkgs map[PackageID]unit,
208
208
// build the DAG of packages we're going to analyze.
209
209
//
210
210
// Root nodes will run the enabled set of analyzers,
211
- // whereas dependencies will run only the useFacts set.
211
+ // whereas dependencies will run only the facty set.
212
+ // Because (by construction) enabled is a superset of facty,
213
+ // we can analyze each node with exactly one set of analyzers.
212
214
nodes := make (map [PackageID ]* analysisNode )
213
215
var leaves []* analysisNode // nodes with no unfinished successors
214
216
var makeNode func (from * analysisNode , id PackageID ) (* analysisNode , error )
@@ -225,7 +227,7 @@ func (snapshot *snapshot) Analyze(ctx context.Context, pkgs map[PackageID]unit,
225
227
an = & analysisNode {
226
228
fset : fset ,
227
229
m : m ,
228
- analyzers : useFacts , // all nodes run at least the facty analyzers
230
+ analyzers : facty , // all nodes run at least the facty analyzers
229
231
allDeps : make (map [PackagePath ]* analysisNode ),
230
232
exportDeps : make (map [PackagePath ]* analysisNode ),
231
233
}
@@ -338,6 +340,23 @@ func (snapshot *snapshot) Analyze(ctx context.Context, pkgs map[PackageID]unit,
338
340
var results []* source.Diagnostic
339
341
for _ , root := range roots {
340
342
for _ , a := range enabled {
343
+ // Skip analyzers that were added only to
344
+ // fulfil requirements of the original set.
345
+ srcAnalyzer , ok := toSrc [a ]
346
+ if ! ok {
347
+ // Although this 'skip' operation is logically sound,
348
+ // it is nonetheless surprising that its absence should
349
+ // cause #60909 since none of the analyzers added for
350
+ // requirements (e.g. ctrlflow, inspect, buildssa)
351
+ // is capable of reporting diagnostics.
352
+ if summary := root .summary .Actions [a .Name ]; summary != nil {
353
+ if n := len (summary .Diagnostics ); n > 0 {
354
+ bug .Reportf ("Internal error: got %d unexpected diagnostics from analyzer %s. This analyzer was added only to fulfil the requirements of the requested set of analyzers, and it is not expected that such analyzers report diagnostics. Please report this in issue #60909." , n , a )
355
+ }
356
+ }
357
+ continue
358
+ }
359
+
341
360
// Inv: root.summary is the successful result of run (via runCached).
342
361
summary , ok := root .summary .Actions [a .Name ]
343
362
if summary == nil {
@@ -348,7 +367,7 @@ func (snapshot *snapshot) Analyze(ctx context.Context, pkgs map[PackageID]unit,
348
367
continue // action failed
349
368
}
350
369
for _ , gobDiag := range summary .Diagnostics {
351
- results = append (results , toSourceDiagnostic (toSrc [ a ] , & gobDiag ))
370
+ results = append (results , toSourceDiagnostic (srcAnalyzer , & gobDiag ))
352
371
}
353
372
}
354
373
}
0 commit comments