Fix an exception in the ErrorVerifier

Fixes https://github.com/dart-lang/sdk/issues/46952

Change-Id: I2cfd8778d6a26cf3107868264713a171470c6bf2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/210762
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Brian Wilkerson 2021-08-21 16:07:40 +00:00 committed by commit-bot@chromium.org
parent 3238d433fa
commit 86c660faaa
2 changed files with 21 additions and 2 deletions

View file

@ -2809,8 +2809,12 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
var lateKeyword = variableList.lateKeyword;
if (lateKeyword == null) return;
var hasConstConstructor =
_enclosingClass!.constructors.any((c) => c.isConst);
var enclosingClass = _enclosingClass;
if (enclosingClass == null) {
// The field is in an extension and should handled elsewhere.
return;
}
var hasConstConstructor = enclosingClass.constructors.any((c) => c.isConst);
if (!hasConstConstructor) return;
errorReporter.reportErrorForToken(

View file

@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/dart/error/syntactic_errors.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
@ -69,4 +70,18 @@ class A {
}
''');
}
test_inExtension() async {
// https://github.com/dart-lang/sdk/issues/46952
// This test is here because the code that tests for
// LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR is where the referenced issue was
// caused.
await assertErrorsInCode('''
extension E on int {
late final int i;
}
''', [
error(ParserErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD, 38, 1),
]);
}
}