From e7c9ac41ffc83b44c3f6248020d7ca5f6c9db9df Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Wed, 20 Jun 2018 11:01:41 +0200 Subject: [PATCH 1/4] trigger error for const A(B()) if B non-const --- .../lib/src/dart/constant/evaluation.dart | 5 ++++ .../compile_time_error_code_test.dart | 28 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart index b65aa462930f..eb2e46bd8a61 100644 --- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart +++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart @@ -417,6 +417,11 @@ class ConstantEvaluationEngine { ConstantVisitor constantVisitor, ErrorReporter errorReporter, {ConstructorInvocation invocation}) { + if (!constructor.isConst) { + errorReporter.reportErrorForNode( + CompileTimeErrorCode.CONST_WITH_NON_CONST, node); + return null; + } if (!getConstructorImpl(constructor).isCycleFree) { // It's not safe to evaluate this constructor, so bail out. // TODO(paulberry): ensure that a reasonable error message is produced 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 06b145482f04..d993148ababf 100644 --- a/pkg/analyzer/test/generated/compile_time_error_code_test.dart +++ b/pkg/analyzer/test/generated/compile_time_error_code_test.dart @@ -1613,7 +1613,33 @@ main() { } '''); await computeAnalysisResult(source); - assertErrors(source, [CompileTimeErrorCode.CONST_WITH_NON_CONST]); + // TODO(a14n): the error CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE is + // redundant and ought to be suppressed. + assertErrors(source, [ + CompileTimeErrorCode.CONST_WITH_NON_CONST, + CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE + ]); + verify([source]); + } + + test_constWithNonConst_in_const_context() async { + Source source = addSource(r''' +class A { + const A(x); +} +class B { +} +main() { + const A(B()); +} +'''); + await computeAnalysisResult(source); + // TODO(a14n): the error CONST_WITH_NON_CONSTANT_ARGUMENT is redundant and + // ought to be suppressed. + assertErrors(source, [ + CompileTimeErrorCode.CONST_WITH_NON_CONST, + CompileTimeErrorCode.CONST_WITH_NON_CONSTANT_ARGUMENT + ]); verify([source]); } From 95550718b69271a889b2d710843bf3178e319393 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Wed, 20 Jun 2018 17:06:25 +0200 Subject: [PATCH 2/4] update failing test --- pkg/analyzer/test/generated/constant_test.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/analyzer/test/generated/constant_test.dart b/pkg/analyzer/test/generated/constant_test.dart index f94b02651512..86791322992e 100644 --- a/pkg/analyzer/test/generated/constant_test.dart +++ b/pkg/analyzer/test/generated/constant_test.dart @@ -189,10 +189,9 @@ class C { void test_constructorInvocation_noArgs() { EvaluationResult result = _getExpressionValue("const C()", context: 'class C {}'); - expect(result.isValid, isTrue); + expect(result.isValid, isFalse); DartObject value = result.value; - expect(value, isNotNull); - expect(value.type.name, 'C'); + expect(value, isNull); } void test_constructorInvocation_simpleArgs() { From 2b367229afc530d0fbd064185664fbbc2afb5843 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Wed, 20 Jun 2018 17:30:11 +0200 Subject: [PATCH 3/4] address review comments --- pkg/analyzer/test/generated/constant_test.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/analyzer/test/generated/constant_test.dart b/pkg/analyzer/test/generated/constant_test.dart index 86791322992e..8c5dadf38521 100644 --- a/pkg/analyzer/test/generated/constant_test.dart +++ b/pkg/analyzer/test/generated/constant_test.dart @@ -187,6 +187,15 @@ class C { } void test_constructorInvocation_noArgs() { + EvaluationResult result = + _getExpressionValue("const C()", context: 'class C {const C();}'); + expect(result.isValid, isTrue); + DartObject value = result.value; + expect(value, isNotNull); + expect(value.type.name, 'C'); + } + + void test_constructorInvocation_noConstConstructor() { EvaluationResult result = _getExpressionValue("const C()", context: 'class C {}'); expect(result.isValid, isFalse); From f46c0cc993fc22eea8e2741929e15f175b78a619 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Wed, 20 Jun 2018 21:46:42 +0200 Subject: [PATCH 4/4] add failing test --- .../generated/compile_time_error_code_kernel_test.dart | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/analyzer/test/generated/compile_time_error_code_kernel_test.dart b/pkg/analyzer/test/generated/compile_time_error_code_kernel_test.dart index 9832e8c670f6..d10e5f8411ef 100644 --- a/pkg/analyzer/test/generated/compile_time_error_code_kernel_test.dart +++ b/pkg/analyzer/test/generated/compile_time_error_code_kernel_test.dart @@ -537,6 +537,13 @@ class CompileTimeErrorCodeTest_Kernel extends CompileTimeErrorCodeTest_Driver { await super.test_constWithNonConst_with(); } + @override + @failingTest + test_constWithNonConst_in_const_context() async { + // Bad state: No data for () at 58 + await super.test_constWithNonConst_in_const_context(); + } + @override @failingTest test_constWithNonConstantArgument_annotation() async {