Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Unified Diff: pkg/analyzer/test/generated/compile_time_error_code_test.dart

Issue 1175073002: Report a compile-time error if a const field is multiply initialized. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/test/generated/compile_time_error_code_test.dart
diff --git a/pkg/analyzer/test/generated/compile_time_error_code_test.dart b/pkg/analyzer/test/generated/compile_time_error_code_test.dart
index f8fd52812b5fd950cbdf800c12f1cdd49c148c3c..58871cc982fda84b18df411bb0c095af885a910d 100644
--- a/pkg/analyzer/test/generated/compile_time_error_code_test.dart
+++ b/pkg/analyzer/test/generated/compile_time_error_code_test.dart
@@ -1040,6 +1040,47 @@ const C = a.m;''');
verify([source]);
}
+ void test_constEvalThrowsException_finalAlreadySet_initializer() {
+ // If a final variable has an initializer at the site of its declaration,
+ // and at the site of the constructor, then invoking that constructor would
+ // produce a runtime error; hence invoking that constructor via the "const"
+ // keyword results in a compile-time error.
+ Source source = addSource('''
+class C {
+ final x = 1;
+ const C() : x = 2;
+}
+var x = const C();
+''');
+ computeLibrarySourceErrors(source);
+ assertErrors(source, [
+ CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
+ StaticWarningCode.FIELD_INITIALIZED_IN_INITIALIZER_AND_DECLARATION
+ ]);
+ verify([source]);
+ }
+
+ void test_constEvalThrowsException_finalAlreadySet_initializing_formal() {
+ // If a final variable has an initializer at the site of its declaration,
+ // and it is initialized using an initializing formal at the site of the
+ // constructor, then invoking that constructor would produce a runtime
+ // error; hence invoking that constructor via the "const" keyword results
+ // in a compile-time error.
+ Source source = addSource('''
+class C {
+ final x = 1;
+ const C(this.x);
+}
+var x = const C(2);
+''');
+ computeLibrarySourceErrors(source);
+ assertErrors(source, [
+ CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
+ StaticWarningCode.FINAL_INITIALIZED_IN_DECLARATION_AND_CONSTRUCTOR
+ ]);
+ verify([source]);
+ }
+
void test_constEvalThrowsException_unaryBitNot_null() {
Source source = addSource("const C = ~null;");
computeLibrarySourceErrors(source);
« no previous file with comments | « pkg/analyzer/lib/src/generated/constant.dart ('k') | tests/language/const_error_multiply_initialized_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698