Skip to content

trigger error for const A(B()) if B non-const #33515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/analyzer/lib/src/dart/constant/evaluation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 27 additions & 1 deletion pkg/analyzer/test/generated/compile_time_error_code_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
10 changes: 9 additions & 1 deletion pkg/analyzer/test/generated/constant_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,21 @@ class C {

void test_constructorInvocation_noArgs() {
EvaluationResult result =
_getExpressionValue("const C()", context: 'class C {}');
_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);
DartObject value = result.value;
expect(value, isNull);
}

void test_constructorInvocation_simpleArgs() {
EvaluationResult result = _getExpressionValue("const C(1)", context: '''
class C {
Expand Down