Index: pkg/front_end/lib/src/fasta/type_inference/type_inference_engine.dart |
diff --git a/pkg/front_end/lib/src/fasta/type_inference/type_inference_engine.dart b/pkg/front_end/lib/src/fasta/type_inference/type_inference_engine.dart |
index 61300251613832180c7f06abfbecb5ebac667d7f..8fe000017c5f4eb63722fca146d9d57d415991a1 100644 |
--- a/pkg/front_end/lib/src/fasta/type_inference/type_inference_engine.dart |
+++ b/pkg/front_end/lib/src/fasta/type_inference/type_inference_engine.dart |
@@ -514,13 +514,17 @@ abstract class TypeInferenceEngineImpl extends TypeInferenceEngine { |
DartType _inferInitializingFormalType(KernelVariableDeclaration formal) { |
assert(KernelVariableDeclaration.isImplicitlyTyped(formal)); |
- Class enclosingClass = formal.parent.parent.parent; |
- for (var field in enclosingClass.fields) { |
- if (field.name.name == formal.name) { |
- return field.type; |
+ var enclosingClass = formal.parent?.parent?.parent; |
+ if (enclosingClass is Class) { |
+ for (var field in enclosingClass.fields) { |
+ if (field.name.name == formal.name) { |
+ return field.type; |
+ } |
} |
} |
- // No matching field. The error should be reported elsewhere. |
+ // No matching field, or something else has gone wrong (e.g. initializing |
+ // formal outside of a class declaration). The error should be reported |
+ // elsewhere, so just infer `dynamic`. |
return const DynamicType(); |
} |