Skip to content

Commit eb59838

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Fix PartElementImpl.enclosingUnit.
`PartElementImpl.enclosingElement3` has static type `Element?`, but in practice it is always of type `CompilationUnitElementImpl`, due to this code from `CompilationUnitElementImpl.parts=`: set parts(List<PartElementImpl> parts) { for (var part in parts) { part.enclosingElement3 = this; var uri = part.uri; if (uri is DirectiveUriWithUnitImpl) { uri.unit.enclosingElement3 = this; } } _parts = parts; } Therefore, `PartElementImpl.enclosingUnit` can just cast `enclosingElement3` to the proper type and return it. This behavior was untested, so I've added a test in `part_test.dart`. Without the fix, the test throws a `TypeError`. Change-Id: I3e6f186826aa92cda9c3887c85c5eddc1fea7f31 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/408641 Reviewed-by: Konstantin Shcheglov <[email protected]> Auto-Submit: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 4227821 commit eb59838

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9653,10 +9653,8 @@ class PartElementImpl extends _ExistingElementImpl
96539653
}
96549654

96559655
@override
9656-
CompilationUnitElementImpl get enclosingUnit {
9657-
var enclosingLibrary = enclosingElement3 as LibraryElementImpl;
9658-
return enclosingLibrary._definingCompilationUnit;
9659-
}
9656+
CompilationUnitElementImpl get enclosingUnit =>
9657+
enclosingElement3 as CompilationUnitElementImpl;
96609658

96619659
@override
96629660
String get identifier => 'part';

pkg/analyzer/test/src/dart/resolution/part_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/src/dart/element/element.dart';
56
import 'package:analyzer/src/dart/error/syntactic_errors.dart';
67
import 'package:analyzer/src/error/codes.dart';
8+
import 'package:test/test.dart';
79
import 'package:test_reflective_loader/test_reflective_loader.dart';
810

911
import 'context_collection_resolution.dart';
@@ -16,6 +18,19 @@ main() {
1618

1719
@reflectiveTest
1820
class PartDirectiveResolutionTest extends PubPackageResolutionTest {
21+
test_enclosingUnit() async {
22+
newFile('$testPackageLibPath/a.dart', r'''
23+
part of 'test.dart';
24+
''');
25+
26+
await assertNoErrorsInCode(r'''
27+
part 'a.dart';
28+
''');
29+
expect(
30+
(findNode.part('part').partInclude! as PartElementImpl).enclosingUnit,
31+
same(findNode.unit.declaredFragment));
32+
}
33+
1934
test_inLibrary_configurations_default() async {
2035
newFile('$testPackageLibPath/a.dart', r'''
2136
part of 'test.dart';

0 commit comments

Comments
 (0)