diff --git a/pkg/compiler/lib/src/compiler.dart b/pkg/compiler/lib/src/compiler.dart index 7c1d58e9bf1..d567bbaed7c 100644 --- a/pkg/compiler/lib/src/compiler.dart +++ b/pkg/compiler/lib/src/compiler.dart @@ -922,7 +922,12 @@ abstract class Compiler implements LibraryLoaderListener { library.implementation.forEachLocalMember(enqueueAll); library.imports.forEach((ImportElement import) { if (import.isDeferred) { - world.addToWorkList(import.prefix.loadLibrary); + // `import.prefix` and `loadLibrary` may be `null` when the deferred + // import has compile-time errors. + GetterElement loadLibrary = import.prefix?.loadLibrary; + if (loadLibrary != null) { + world.addToWorkList(loadLibrary); + } } }); } diff --git a/tests/compiler/dart2js/serialization/test_data.dart b/tests/compiler/dart2js/serialization/test_data.dart index 99e759db373..9d0e67342c6 100644 --- a/tests/compiler/dart2js/serialization/test_data.dart +++ b/tests/compiler/dart2js/serialization/test_data.dart @@ -490,6 +490,43 @@ test() { 'b.dart': ''' ''', }), + + const Test('Deferred without prefix', const { + 'main.dart': ''' +import 'a.dart'; + +main() { + test(); +} +''', + }, preserializedSourceFiles: const { + 'a.dart': ''' +import 'b.dart' deferred; +test() {} +''', + 'b.dart': ''' +''', + }, expectedErrorCount: 1), + + const Test('Deferred with duplicate prefix', const { + 'main.dart': ''' +import 'a.dart'; + +main() { + test(); +} +''', + }, preserializedSourceFiles: const { + 'a.dart': ''' +import 'b.dart' deferred as pre; +import 'c.dart' deferred as pre; +test() {} +''', + 'b.dart': ''' +''', + 'c.dart': ''' +''', + }, expectedErrorCount: 1), ]; class Test {