From f73ea62c999f4ad1f4f42832e0fa97a37ed49235 Mon Sep 17 00:00:00 2001 From: Matthias Hausner Date: Wed, 19 Oct 2016 15:15:02 -0700 Subject: [PATCH] Add check for constness in constructor redirection Const constructor can only redirect to another const constructor. BUG=#27617 R=regis@google.com Review URL: https://codereview.chromium.org/2439593003 . --- runtime/vm/parser.cc | 10 ++++++++-- tests/language/language_analyzer2.status | 1 + tests/language/regress_27617_test.dart | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/language/regress_27617_test.dart diff --git a/runtime/vm/parser.cc b/runtime/vm/parser.cc index a4c7558dd88..868c27bf54d 100644 --- a/runtime/vm/parser.cc +++ b/runtime/vm/parser.cc @@ -3116,7 +3116,13 @@ void Parser::ParseConstructorRedirection(const Class& cls, const Function& redirect_ctor = Function::ZoneHandle(Z, cls.LookupConstructor(ctor_name)); if (redirect_ctor.IsNull()) { - ReportError(call_pos, "constructor '%s' not found", ctor_name.ToCString()); + ReportError(call_pos, "constructor '%s' not found", + String::Handle(Z, redirect_ctor.UserVisibleName()).ToCString()); + } + if (current_function().is_const() && !redirect_ctor.is_const()) { + ReportError(call_pos, + "redirection constructor '%s' must be const", + String::Handle(Z, redirect_ctor.UserVisibleName()).ToCString()); } String& error_message = String::Handle(Z); if (!redirect_ctor.AreValidArguments(arguments->length(), @@ -3124,7 +3130,7 @@ void Parser::ParseConstructorRedirection(const Class& cls, &error_message)) { ReportError(call_pos, "invalid arguments passed to constructor '%s': %s", - ctor_name.ToCString(), + String::Handle(Z, redirect_ctor.UserVisibleName()).ToCString(), error_message.ToCString()); } current_block_->statements->Add( diff --git a/tests/language/language_analyzer2.status b/tests/language/language_analyzer2.status index a06448805ab..be271191005 100644 --- a/tests/language/language_analyzer2.status +++ b/tests/language/language_analyzer2.status @@ -5,6 +5,7 @@ [ $compiler == dart2analyzer ] regress_26668_test: Fail # Issue 26678 +regress_27617_test/1: MissingCompileTimeError # Runtime negative test. No static errors or warnings. closure_call_wrong_argument_count_negative_test: skip diff --git a/tests/language/regress_27617_test.dart b/tests/language/regress_27617_test.dart new file mode 100644 index 00000000000..9a0fdbd9f9f --- /dev/null +++ b/tests/language/regress_27617_test.dart @@ -0,0 +1,16 @@ +// Copyright (c) 2016, 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. + + +class Foo { + final String greeting; + Foo._(this.greeting) { } + + // Const constructor must not redirect to non-const constructor. + const Foo.hi() : this._('hi'); /// 1: compile-time error +} + +main() { + const h = const Foo.hi(); /// 1: continued +}