Skip to content

Commit 605709a

Browse files
committed
Fix _isDynamicExpression() in DartUnitHighlightsComputer2.
The staticType of an "expression" can be `null` if it is not really an expression, e.g. an import prefix, or a name of an extension. [email protected], [email protected] Change-Id: I9eb328453e77fa51ff0f2d289972ad1274a9a5e6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/114558 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent a2c7e7c commit 605709a

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

pkg/analysis_server/lib/src/computer/computer_highlights2.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/ast/ast.dart';
6-
import 'package:analyzer/dart/ast/standard_resolution_map.dart';
76
import 'package:analyzer/dart/ast/token.dart';
87
import 'package:analyzer/dart/ast/visitor.dart';
98
import 'package:analyzer/dart/element/element.dart';
@@ -425,10 +424,8 @@ class DartUnitHighlightsComputer2 {
425424
}
426425

427426
static bool _isDynamicExpression(Expression e) {
428-
if (e is SimpleIdentifier && e.staticElement is PrefixElement) {
429-
return false;
430-
}
431-
return resolutionMap.staticTypeForExpression(e).isDynamic;
427+
var type = e.staticType;
428+
return type != null && type.isDynamic;
432429
}
433430
}
434431

pkg/analysis_server/test/src/computer/highlights2_computer_test.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ extension E on String {}
3838
_check(HighlightRegionType.BUILT_IN, 'on');
3939
}
4040

41+
test_methodInvocation_ofExtensionOverride_unresolved() async {
42+
createAnalysisOptionsFile(experiments: ['extension-methods']);
43+
await _computeHighlights('''
44+
extension E on int {}
45+
46+
main() {
47+
E(0).foo();
48+
}
49+
''', hasErrors: true);
50+
_check(HighlightRegionType.IDENTIFIER_DEFAULT, 'foo');
51+
}
52+
4153
void _check(HighlightRegionType expectedType, String expectedText) {
4254
for (var region in highlights) {
4355
if (region.type == expectedType) {
@@ -52,11 +64,20 @@ extension E on String {}
5264
fail('Expected region of type $expectedType with text "$expectedText"');
5365
}
5466

55-
Future<void> _computeHighlights(String content) async {
67+
Future<void> _computeHighlights(
68+
String content, {
69+
bool hasErrors = false,
70+
}) async {
5671
this.content = content;
5772
newFile(sourcePath, content: content);
5873
ResolvedUnitResult result = await session.getResolvedUnit(sourcePath);
59-
expect(result.errors, hasLength(0));
74+
75+
if (hasErrors) {
76+
expect(result.errors, isNotEmpty);
77+
} else {
78+
expect(result.errors, isEmpty);
79+
}
80+
6081
DartUnitHighlightsComputer2 computer =
6182
new DartUnitHighlightsComputer2(result.unit);
6283
highlights = computer.compute();

pkg/analysis_server/test/src/computer/highlights_computer_test.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ extension E on String {}
3838
_check(HighlightRegionType.BUILT_IN, 'on');
3939
}
4040

41+
test_methodInvocation_ofExtensionOverride_unresolved() async {
42+
createAnalysisOptionsFile(experiments: ['extension-methods']);
43+
await _computeHighlights('''
44+
extension E on int {}
45+
46+
main() {
47+
E(0).foo();
48+
}
49+
''', hasErrors: true);
50+
_check(HighlightRegionType.IDENTIFIER_DEFAULT, 'foo');
51+
}
52+
4153
void _check(HighlightRegionType expectedType, String expectedText) {
4254
for (var region in highlights) {
4355
if (region.type == expectedType) {
@@ -52,11 +64,20 @@ extension E on String {}
5264
fail('Expected region of type $expectedType with text "$expectedText"');
5365
}
5466

55-
Future<void> _computeHighlights(String content) async {
67+
Future<void> _computeHighlights(
68+
String content, {
69+
bool hasErrors = false,
70+
}) async {
5671
this.content = content;
5772
newFile(sourcePath, content: content);
5873
ResolvedUnitResult result = await session.getResolvedUnit(sourcePath);
59-
expect(result.errors, hasLength(0));
74+
75+
if (hasErrors) {
76+
expect(result.errors, isNotEmpty);
77+
} else {
78+
expect(result.errors, isEmpty);
79+
}
80+
6081
DartUnitHighlightsComputer computer =
6182
new DartUnitHighlightsComputer(result.unit);
6283
highlights = computer.compute();

0 commit comments

Comments
 (0)