dart-sdk/tests/language/const_error_multiply_initialized_test.dart
Paul Berry 2f610a3ff5 Report a compile-time error if a const field is multiply initialized.
This CL makes analyzer consistent with the VM and the spec, by
producing a compile-time error for code such as:

    class C {
      final x = 1;
      const C() : x = 2;
    }
    main() {
      const C();
    }

Note that Dart2js also produces an error in this circumstance, but its
error is too general (the error should only be produced if the const
constructor is invoked using "const"; if the const constructor is
invoked using "new", or is not invoked at all, it should only be a
warning).  See #23618.

R=brianwilkerson@google.com

Review URL: https://codereview.chromium.org//1175073002.
2015-06-10 14:02:43 -07:00

29 lines
1.1 KiB
Dart

// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// 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.
// If a constant constructor contains an initializer, or an initializing
// formal, for a final field which itself has an initializer at its
// declaration, then a runtime error should occur if that constructor is
// invoked using "new", but there should be no compile-time error. However, if
// the constructor is invoked using "const", there should be a compile-time
// error, since it is a compile-time error for evaluation of a constant object
// to result in an uncaught exception.
import "package:expect/expect.dart";
class C {
final x = 1;
const C() : x = 2; /// 01: compile-time error
const C() : x = 2; /// 02: static type warning
const C(this.x); /// 03: compile-time error
const C(this.x); /// 04: static type warning
}
main() {
const C(); /// 01: continued
Expect.throws(() => new C()); /// 02: continued
const C(2); /// 03: continued
Expect.throws(() => new C(2)); /// 04: continued
}