Skip to content

Commit 3c596e8

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Report an error when a potentially non-nullable local variable is not initialized.
[email protected], [email protected] Change-Id: Ie1d148ff584c202edc334659eeb4f792bb8773d4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106620 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent b9edd17 commit 3c596e8

14 files changed

Lines changed: 443 additions & 150 deletions

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ const List<ErrorCode> errorCodeValues = const [
236236
CompileTimeErrorCode.NON_GENERATIVE_CONSTRUCTOR,
237237
CompileTimeErrorCode.NON_SYNC_FACTORY,
238238
CompileTimeErrorCode.NOT_ENOUGH_REQUIRED_ARGUMENTS,
239+
CompileTimeErrorCode.NOT_INITIALIZED_POTENTIALLY_NON_NULLABLE_LOCAL_VARIABLE,
239240
CompileTimeErrorCode.NOT_ITERABLE_SPREAD,
240241
CompileTimeErrorCode.NOT_MAP_SPREAD,
241242
CompileTimeErrorCode.NOT_NULL_AWARE_NULL_SPREAD,

pkg/analyzer/lib/src/error/codes.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,22 @@ class CompileTimeErrorCode extends ErrorCode {
24522452
"{0} required argument(s) expected, but {1} found.",
24532453
correction: "Try adding the missing arguments.");
24542454

2455+
/**
2456+
* It is an error if a potentially non-nullable local variable which has no
2457+
* initializer expression and is not marked `late` is used before it is
2458+
* definitely assigned.
2459+
*
2460+
* TODO(scheglov) Update the code and the message when implement definite
2461+
* assignment analysis.
2462+
*/
2463+
static const CompileTimeErrorCode
2464+
NOT_INITIALIZED_POTENTIALLY_NON_NULLABLE_LOCAL_VARIABLE =
2465+
const CompileTimeErrorCode(
2466+
'NOT_INITIALIZED_POTENTIALLY_NON_NULLABLE_LOCAL_VARIABLE',
2467+
"Non-nullable local variable '{0}' must be initialized.",
2468+
correction:
2469+
"Try giving it an initializer expression, or mark it 'late'.");
2470+
24552471
/**
24562472
* No parameters.
24572473
*/

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
14391439
@override
14401440
void visitVariableDeclarationStatement(VariableDeclarationStatement node) {
14411441
_checkForFinalNotInitialized(node.variables);
1442+
_checkForNotInitializedPotentiallyNonNullableLocalVariable(node.variables);
14421443
super.visitVariableDeclarationStatement(node);
14431444
}
14441445

@@ -3367,6 +3368,43 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
33673368
}
33683369
}
33693370

3371+
void _checkForNotInitializedPotentiallyNonNullableLocalVariable(
3372+
VariableDeclarationList node,
3373+
) {
3374+
// Const and final checked separately.
3375+
if (node.isConst || node.isFinal) {
3376+
return;
3377+
}
3378+
3379+
if (!_isNonNullable) {
3380+
return;
3381+
}
3382+
3383+
if (node.isLate) {
3384+
return;
3385+
}
3386+
3387+
if (node.type == null) {
3388+
return;
3389+
}
3390+
var type = node.type.type;
3391+
3392+
if (!_typeSystem.isPotentiallyNonNullable(type)) {
3393+
return;
3394+
}
3395+
3396+
for (var variable in node.variables) {
3397+
if (variable.initializer == null) {
3398+
_errorReporter.reportErrorForNode(
3399+
CompileTimeErrorCode
3400+
.NOT_INITIALIZED_POTENTIALLY_NON_NULLABLE_LOCAL_VARIABLE,
3401+
variable.name,
3402+
[variable.name.name],
3403+
);
3404+
}
3405+
}
3406+
}
3407+
33703408
/**
33713409
* If there are no constructors in the given [members], verify that all
33723410
* final fields are initialized. Cases in which there is at least one

0 commit comments

Comments
 (0)