Update Expression.inConstantContext to support RecordLiteral(s).

Change-Id: I02c3692c359e65822d30a4e931b14dec5963fef0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/260065
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2022-09-20 20:32:55 +00:00 committed by Commit Bot
parent 3add88c955
commit eb00d038c7
4 changed files with 101 additions and 9 deletions

View file

@ -4150,8 +4150,7 @@ abstract class ExpressionImpl extends AstNodeImpl
child is MapLiteralEntry ||
child is SpreadElement ||
child is IfElement ||
child is ForElement ||
child is RecordLiteral) {
child is ForElement) {
var parent = child.parent;
if (parent is ConstantContextForExpressionImpl) {
return true;
@ -4167,6 +4166,8 @@ abstract class ExpressionImpl extends AstNodeImpl
} else if (parent is Annotation) {
// Inside an annotation.
return true;
} else if (parent is RecordLiteral && parent.constKeyword != null) {
return true;
} else if (parent is VariableDeclaration) {
var grandParent = parent.parent;
// Inside the initializer for a `const` variable declaration.

View file

@ -1015,13 +1015,6 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
@override
DartObjectImpl? visitRecordLiteral(RecordLiteral node) {
if (!node.isConst) {
// TODO(brianwilkerson) Merge the error codes into a single error code or
// declare a new error code specific to records.
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.MISSING_CONST_IN_LIST_LITERAL, node);
return null;
}
var nodeType = node.staticType;
if (nodeType == null) {
return null;

View file

@ -431,6 +431,34 @@ f() {
assertInContext("[]", true);
}
test_inConstantContext_listLiteral_namedFields_recordLiteral_false() {
parse('''
final x = (0, foo: [1]);
''');
assertInContext('[1]', false);
}
test_inConstantContext_listLiteral_namedFields_recordLiteral_true() {
parse('''
final x = const (0, foo: [1]);
''');
assertInContext('[1]', true);
}
test_inConstantContext_listLiteral_positionalFields_recordLiteral_false() {
parse('''
final x = (0, [1]);
''');
assertInContext('[1]', false);
}
test_inConstantContext_listLiteral_positionalFields_recordLiteral_true() {
parse('''
final x = const (0, [1]);
''');
assertInContext('[1]', true);
}
test_inConstantContext_listLiteral_switch_true() {
parse('''
f(v) {

View file

@ -126,6 +126,76 @@ f({x: y}) {}
]);
}
test_function_named_constList() async {
await assertNoErrorsInCode(r'''
void f({x: const [0, 1]}) {}
''');
}
test_function_named_constList_elements_listLiteral() async {
await assertNoErrorsInCode(r'''
void f({x: const [0, [1]]}) {}
''');
}
test_function_named_constRecord() async {
await assertNoErrorsInCode(r'''
void f({x: const (0, 1)}) {}
''');
}
test_function_named_constRecord_namedFields_listLiteral() async {
await assertNoErrorsInCode(r'''
void f({x: const (0, foo: [1])}) {}
''');
}
test_function_named_constRecord_positionalFields_listLiteral() async {
await assertNoErrorsInCode(r'''
void f({x: const (0, [1])}) {}
''');
}
test_function_named_record_namedFields_integerLiteral() async {
await assertNoErrorsInCode(r'''
void f({x: (a: 0, b: 1)}) {}
''');
}
test_function_named_record_namedFields_listLiteral() async {
await assertErrorsInCode(r'''
void f({x: (a: 0, b: [1])}) {}
''', [
error(CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE, 21, 3),
]);
}
test_function_named_record_namedFields_listLiteral_const() async {
await assertNoErrorsInCode(r'''
void f({x: (a: 0, b: const [1])}) {}
''');
}
test_function_named_record_positionalFields_integerLiteral() async {
await assertNoErrorsInCode(r'''
void f({x: (0, 1)}) {}
''');
}
test_function_named_record_positionalFields_listLiteral() async {
await assertErrorsInCode(r'''
void f({x: (0, [1])}) {}
''', [
error(CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE, 15, 3),
]);
}
test_function_named_record_positionalFields_listLiteral_const() async {
await assertNoErrorsInCode(r'''
void f({x: (0, const [1])}) {}
''');
}
test_function_positional() async {
await assertErrorsInCode(r'''
int y = 0;