mirror of
https://github.com/dart-lang/sdk
synced 2024-11-02 10:49:00 +00:00
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:
parent
3add88c955
commit
eb00d038c7
4 changed files with 101 additions and 9 deletions
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue