Skip to content

Commit c0ed677

Browse files
authored
Do not crash in the presence of macro applications. (#3665)
Do not crash in the presense of macro applications. * Dispose of an AnalysisContextCollectionImpl. * Before getting class hierarchy, navigate to possible augmentation declaration. * Do not attempt to access source code of augmentation elements.
1 parent 7a9df65 commit c0ed677

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

lib/src/model/method.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class Method extends ModelElement
113113
return null;
114114
}
115115
var parent = element.enclosingElement as InterfaceElement;
116-
for (var t in parent.allSupertypes) {
116+
var parentDeclaration = parent.augmented?.declaration ?? parent;
117+
for (var t in parentDeclaration.allSupertypes) {
117118
Element? e = t.getMethod(element.name);
118119
if (e != null) {
119120
assert(

lib/src/model/package_builder.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'dart:async';
66

7-
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
87
import 'package:analyzer/dart/analysis/context_root.dart';
98
import 'package:analyzer/dart/analysis/results.dart';
109
import 'package:analyzer/dart/ast/ast.dart';
@@ -136,8 +135,7 @@ class PubPackageBuilder implements PackageBuilder {
136135

137136
late final Map<String, List<Folder>> _packageMap;
138137

139-
late final AnalysisContextCollection _contextCollection =
140-
AnalysisContextCollectionImpl(
138+
late final _contextCollection = AnalysisContextCollectionImpl(
141139
includedPaths: [_config.inputDir],
142140
// TODO(jcollins-g): should we pass excluded directories here instead of
143141
// handling it ourselves?
@@ -464,6 +462,8 @@ class PubPackageBuilder implements PackageBuilder {
464462
specialFiles.difference(files),
465463
addingSpecials: true,
466464
);
465+
// Shutdown macro support.
466+
await _contextCollection.dispose();
467467
}
468468

469469
/// Throws an exception if any configured-to-be-included files were not found

lib/src/model/source_code_mixin.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ mixin SourceCode implements Documentable {
1313

1414
Element? get element;
1515

16-
bool get hasSourceCode => config.includeSource && sourceCode.isNotEmpty;
16+
bool get hasSourceCode =>
17+
config.includeSource && !element.isAugmentation && sourceCode.isNotEmpty;
1718

1819
Library? get library;
1920

@@ -22,3 +23,18 @@ mixin SourceCode implements Documentable {
2223
return modelNode == null ? '' : modelNode.sourceCode;
2324
}
2425
}
26+
27+
extension on Element? {
28+
/// Whether `this` is an augmentation method or property.
29+
///
30+
/// This property should only be referenced for elements whose source code we
31+
/// may wish to refer to.
32+
bool get isAugmentation {
33+
final self = this;
34+
return switch (self) {
35+
ExecutableElement() => self.augmentationTarget != null,
36+
PropertyInducingElement() => self.augmentationTarget != null,
37+
_ => false,
38+
};
39+
}
40+
}

0 commit comments

Comments
 (0)