Skip to content

Commit 9388e3c

Browse files
authored
Handle null library in _NameTypeChecker. (#791)
* Handle null library in _NameTypeChecker. * Add a test.
1 parent fba3cbc commit 9388e3c

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

source_gen/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.0.2-wip
2+
3+
- Bug fix: fix possible null pointer exception in `TypeChecker.typeNamed` on
4+
invalid code.
5+
16
## 4.0.1
27

38
- Require `analyzer: '>=8.1.1 <9.0.0'`.

source_gen/lib/src/type_checker.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ class _NameTypeChecker extends TypeChecker {
257257

258258
@override
259259
bool isExactly(Element element) {
260-
final uri = element.library!.uri;
260+
final library = element.library;
261+
if (library == null) return false;
262+
final uri = library.uri;
261263
return element.name == _typeName &&
262264
(_inPackage == null ||
263265
(((uri.scheme == 'dart') == _inSdk) &&

source_gen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 4.0.1
2+
version: 4.0.2-wip
33
description: >-
44
Source code generation builders and utilities for the Dart build system
55
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen

source_gen/test/type_checker_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ void main() {
208208
);
209209
});
210210

211+
group('isExactly', () {
212+
test('should not crash with null library', () {
213+
final element = core.typeProvider.dynamicType.element!;
214+
expect(element.library, isNull);
215+
expect(checkMapMixin().isExactly(element), isFalse);
216+
});
217+
});
218+
211219
group('isExactlyType', () {
212220
test('should not crash with null element', () {
213221
final voidType = core.typeProvider.voidType;

0 commit comments

Comments
 (0)