Skip to content

Commit

Permalink
Version 3.5.0-67.0.dev
Browse files Browse the repository at this point in the history
Merge 95d8710 into dev
  • Loading branch information
Dart CI committed Apr 16, 2024
2 parents f2464b2 + 95d8710 commit 57d7cba
Show file tree
Hide file tree
Showing 20 changed files with 293 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,12 @@ CompileTimeErrorCode.NON_CONSTANT_MAP_VALUE_FROM_DEFERRED_LIBRARY:
status: needsFix
notes: |-
Remove the `deferred` keyword from the import.
CompileTimeErrorCode.NON_CONSTANT_RECORD_FIELD:
status: noFix
CompileTimeErrorCode.NON_CONSTANT_RECORD_FIELD_FROM_DEFERRED_LIBRARY:
status: needsFix
notes: |-
Remove the `deferred` keyword from the import.
CompileTimeErrorCode.NON_CONSTANT_RELATIONAL_PATTERN_EXPRESSION:
status: hasFix
notes: |-
Expand Down
41 changes: 27 additions & 14 deletions pkg/analyzer/lib/src/dart/constant/constant_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
@override
void visitInstanceCreationExpression(InstanceCreationExpression node) {
if (node.isConst) {
NamedType namedType = node.constructorName.type;
var namedType = node.constructorName.type;
_checkForConstWithTypeParameters(
namedType, CompileTimeErrorCode.CONST_WITH_TYPE_PARAMETERS);

Expand Down Expand Up @@ -299,13 +299,13 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
super.visitListLiteral(node);
if (node.isConst) {
var nodeType = node.staticType as InterfaceType;
DartType elementType = nodeType.typeArguments[0];
var elementType = nodeType.typeArguments[0];
var verifier = _ConstLiteralVerifier(
this,
errorCode: CompileTimeErrorCode.NON_CONSTANT_LIST_ELEMENT,
listElementType: elementType,
);
for (CollectionElement element in node.elements) {
for (var element in node.elements) {
verifier.verify(element);
}
}
Expand All @@ -315,7 +315,7 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
void visitMapPattern(MapPattern node) {
node.typeArguments?.accept(this);

final featureSet = _currentLibrary.featureSet;
var featureSet = _currentLibrary.featureSet;
var uniqueKeys = HashMap<DartObjectImpl, Expression>(
hashCode: (_) => 0,
equals: (a, b) {
Expand Down Expand Up @@ -367,6 +367,20 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
_validateDefaultValues(node.parameters);
}

@override
void visitRecordLiteral(RecordLiteral node) {
super.visitRecordLiteral(node);

if (node.isConst) {
for (var field in node.fields) {
_evaluateAndReportError(
field,
CompileTimeErrorCode.NON_CONSTANT_RECORD_FIELD,
);
}
}
}

@override
void visitRelationalPattern(RelationalPattern node) {
super.visitRelationalPattern(node);
Expand Down Expand Up @@ -403,7 +417,6 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
var nodeType = node.staticType as InterfaceType;
var keyType = nodeType.typeArguments[0];
var valueType = nodeType.typeArguments[1];
bool reportEqualKeys = true;
var config = _MapVerifierConfig(
keyType: keyType,
valueType: valueType,
Expand All @@ -413,16 +426,12 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
errorCode: CompileTimeErrorCode.NON_CONSTANT_MAP_ELEMENT,
mapConfig: config,
);
for (CollectionElement entry in node.elements) {
for (var entry in node.elements) {
verifier.verify(entry);
}
if (reportEqualKeys) {
for (var duplicateEntry in config.duplicateKeys.entries) {
_errorReporter.reportError(_diagnosticFactory.equalKeysInConstMap(
_errorReporter.source,
duplicateEntry.key,
duplicateEntry.value));
}
for (var duplicateEntry in config.duplicateKeys.entries) {
_errorReporter.reportError(_diagnosticFactory.equalKeysInConstMap(
_errorReporter.source, duplicateEntry.key, duplicateEntry.value));
}
}
}
Expand Down Expand Up @@ -689,6 +698,10 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
errorCode,
CompileTimeErrorCode
.NON_CONSTANT_LIST_ELEMENT_FROM_DEFERRED_LIBRARY) ||
identical(
errorCode,
CompileTimeErrorCode
.NON_CONSTANT_RECORD_FIELD_FROM_DEFERRED_LIBRARY) ||
identical(
errorCode,
CompileTimeErrorCode
Expand Down Expand Up @@ -1131,7 +1144,7 @@ class _ConstLiteralVerifier {
);
}

/// Return `true` if the [node] is a potential constant.
/// Returns whether the [node] is a potential constant.
bool _reportNotPotentialConstants(AstNode node) {
var notPotentiallyConstants = getNotPotentiallyConstants(
node,
Expand Down
7 changes: 5 additions & 2 deletions pkg/analyzer/lib/src/dart/constant/evaluation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1721,8 +1721,8 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
switch (evaluationResult) {
case null:
// The constant value isn't computed yet, or there is an error while
// computing. We will mark it and determine whether or not to continue
// the evaluation upstream.
// computing. We will mark it and determine whether or not to
// continue the evaluation upstream.
return InvalidConstant.genericError(errorNode, isUnresolved: true);
case DartObjectImpl():
if (identifier == null) {
Expand Down Expand Up @@ -1856,6 +1856,9 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
return CompileTimeErrorCode
.NON_CONSTANT_MAP_VALUE_FROM_DEFERRED_LIBRARY;
}
} else if (current is RecordLiteral) {
return CompileTimeErrorCode
.NON_CONSTANT_RECORD_FIELD_FROM_DEFERRED_LIBRARY;
} else if (current is SetOrMapLiteral) {
return CompileTimeErrorCode.SET_ELEMENT_FROM_DEFERRED_LIBRARY;
} else if (current is SpreadElement) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/analyzer/lib/src/dart/constant/utilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ class ConstantExpressionsDependenciesFinder extends RecursiveAstVisitor {
super.visitMapPatternEntry(node);
}

@override
void visitRecordLiteral(RecordLiteral node) {
if (node.isConst) {
_find(node);
} else {
super.visitRecordLiteral(node);
}
}

@override
void visitRelationalPattern(RelationalPattern node) {
_find(node.operand);
Expand Down
20 changes: 20 additions & 0 deletions pkg/analyzer/lib/src/error/codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3779,6 +3779,26 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
uniqueName: 'NON_CONSTANT_MAP_VALUE_FROM_DEFERRED_LIBRARY',
);

/// No parameters.
static const CompileTimeErrorCode NON_CONSTANT_RECORD_FIELD =
CompileTimeErrorCode(
'NON_CONSTANT_RECORD_FIELD',
"The fields in a const record literal must be constants.",
correctionMessage:
"Try removing the keyword 'const' from the record literal.",
);

/// No parameters.
static const CompileTimeErrorCode
NON_CONSTANT_RECORD_FIELD_FROM_DEFERRED_LIBRARY = CompileTimeErrorCode(
'NON_CONSTANT_RECORD_FIELD_FROM_DEFERRED_LIBRARY',
"Constant values from a deferred library can't be used as fields in a "
"'const' record literal.",
correctionMessage:
"Try removing the keyword 'const' from the record literal or removing "
"the keyword 'deferred' from the import.",
);

/// No parameters.
static const CompileTimeErrorCode NON_CONSTANT_RELATIONAL_PATTERN_EXPRESSION =
CompileTimeErrorCode(
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/lib/src/error/error_code_values.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.NON_CONSTANT_MAP_PATTERN_KEY,
CompileTimeErrorCode.NON_CONSTANT_MAP_VALUE,
CompileTimeErrorCode.NON_CONSTANT_MAP_VALUE_FROM_DEFERRED_LIBRARY,
CompileTimeErrorCode.NON_CONSTANT_RECORD_FIELD,
CompileTimeErrorCode.NON_CONSTANT_RECORD_FIELD_FROM_DEFERRED_LIBRARY,
CompileTimeErrorCode.NON_CONSTANT_RELATIONAL_PATTERN_EXPRESSION,
CompileTimeErrorCode.NON_CONSTANT_SET_ELEMENT,
CompileTimeErrorCode.NON_CONST_GENERATIVE_ENUM_CONSTRUCTOR,
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/lint/linter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ class _ConstantAnalysisErrorListener extends AnalysisErrorListener {
case CompileTimeErrorCode.NON_CONSTANT_MAP_ELEMENT:
case CompileTimeErrorCode.NON_CONSTANT_MAP_KEY:
case CompileTimeErrorCode.NON_CONSTANT_MAP_VALUE:
case CompileTimeErrorCode.NON_CONSTANT_RECORD_FIELD:
case CompileTimeErrorCode.NON_CONSTANT_SET_ELEMENT:
hasConstError = true;
}
Expand Down
34 changes: 22 additions & 12 deletions pkg/analyzer/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1835,12 +1835,6 @@ CompileTimeErrorCode:
const E();
}
```
NON_CONSTANT_MAP_KEY_FROM_DEFERRED_LIBRARY:
sharedName: COLLECTION_ELEMENT_FROM_DEFERRED_LIBRARY
problemMessage: "Constant values from a deferred library can't be used as keys in a 'const' map literal."
correctionMessage: "Try removing the keyword 'const' from the map literal or removing the keyword 'deferred' from the import."
hasPublishedDocs: true
comment: No parameters.
NON_CONSTANT_LIST_ELEMENT_FROM_DEFERRED_LIBRARY:
sharedName: COLLECTION_ELEMENT_FROM_DEFERRED_LIBRARY
problemMessage: "Constant values from a deferred library can't be used as values in a 'const' list literal."
Expand Down Expand Up @@ -1904,6 +1898,23 @@ CompileTimeErrorCode:
```dart
var l = const [0];
```
NON_CONSTANT_MAP_KEY_FROM_DEFERRED_LIBRARY:
sharedName: COLLECTION_ELEMENT_FROM_DEFERRED_LIBRARY
problemMessage: "Constant values from a deferred library can't be used as keys in a 'const' map literal."
correctionMessage: "Try removing the keyword 'const' from the map literal or removing the keyword 'deferred' from the import."
hasPublishedDocs: true
comment: No parameters.
NON_CONSTANT_MAP_VALUE_FROM_DEFERRED_LIBRARY:
sharedName: COLLECTION_ELEMENT_FROM_DEFERRED_LIBRARY
problemMessage: "Constant values from a deferred library can't be used as values in a 'const' map literal."
correctionMessage: "Try removing the keyword 'const' from the map literal or removing the keyword 'deferred' from the import."
hasPublishedDocs: true
comment: No parameters.
NON_CONSTANT_RECORD_FIELD_FROM_DEFERRED_LIBRARY:
problemMessage: "Constant values from a deferred library can't be used as fields in a 'const' record literal."
correctionMessage: "Try removing the keyword 'const' from the record literal or removing the keyword 'deferred' from the import."
hasPublishedDocs: false
comment: No parameters.
PATTERN_CONSTANT_FROM_DEFERRED_LIBRARY:
problemMessage: Constant values from a deferred library can't be used in patterns.
correctionMessage: Try removing the keyword 'deferred' from the import.
Expand Down Expand Up @@ -1997,12 +2008,6 @@ CompileTimeErrorCode:
correctionMessage: "Try removing the keyword 'const' from the set literal or removing the keyword 'deferred' from the import."
hasPublishedDocs: true
comment: No parameters.
NON_CONSTANT_MAP_VALUE_FROM_DEFERRED_LIBRARY:
sharedName: COLLECTION_ELEMENT_FROM_DEFERRED_LIBRARY
problemMessage: "Constant values from a deferred library can't be used as values in a 'const' map literal."
correctionMessage: "Try removing the keyword 'const' from the map literal or removing the keyword 'deferred' from the import."
hasPublishedDocs: true
comment: No parameters.
CONCRETE_CLASS_HAS_ENUM_SUPERINTERFACE:
problemMessage: "Concrete classes can't have 'Enum' as a superinterface."
correctionMessage: Try specifying a different interface, or remove it from the list.
Expand Down Expand Up @@ -11529,6 +11534,11 @@ CompileTimeErrorCode:
var a = 'a';
var m = {0: a};
```
NON_CONSTANT_RECORD_FIELD:
problemMessage: The fields in a const record literal must be constants.
correctionMessage: "Try removing the keyword 'const' from the record literal."
hasPublishedDocs: false
comment: No parameters.
NON_CONSTANT_RELATIONAL_PATTERN_EXPRESSION:
problemMessage: The relational pattern expression must be a constant.
correctionMessage: Try using a constant instead.
Expand Down
70 changes: 58 additions & 12 deletions pkg/analyzer/test/src/dart/constant/evaluation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2702,6 +2702,15 @@ Record({int f1, int f2})
''');
}

test_visitRecordLiteral_namedField_final() async {
await assertErrorsInCode(r'''
final bar = '';
({String bar, }) foo() => const (bar: bar, );
''', [
error(CompileTimeErrorCode.NON_CONSTANT_RECORD_FIELD, 54, 3),
]);
}

test_visitRecordLiteral_objectField_generic() async {
await assertNoErrorsInCode(r'''
class A<T> {
Expand Down Expand Up @@ -2737,6 +2746,15 @@ Record(int, int, int)
''');
}

test_visitRecordLiteral_positionalField_final() async {
await assertErrorsInCode(r'''
final bar = '';
(String, ) foo() => const (bar, );
''', [
error(CompileTimeErrorCode.NON_CONSTANT_RECORD_FIELD, 43, 3),
]);
}

test_visitRecordLiteral_withoutEnvironment() async {
await assertNoErrorsInCode(r'''
const a = (1, 'b', c: false);
Expand Down Expand Up @@ -4881,18 +4899,6 @@ const a = bool.fromEnvironment('dart.library.js_util');
''');
}

test_bool_fromEnvironment_dartLibraryJsUtil_ifElement_list() async {
await assertNoErrorsInCode('''
const a = bool.fromEnvironment('dart.library.js_util');
const x = [3, if (a) ...[1] else ...[1, 2], 4];
''');
final result = _topLevelVar('x');
assertDartObjectText(result, '''
<unknown> List<int>
variable: self::@variable::x
''');
}

test_bool_fromEnvironment_dartLibraryJsUtil_ifElement_list_eqeq_known() async {
await assertNoErrorsInCode('''
const a = bool.fromEnvironment('dart.library.js_util');
Expand Down Expand Up @@ -5059,6 +5065,32 @@ class A {
]);
}

test_bool_fromEnvironment_dartLibraryJsUtil_ifStatement_list() async {
await assertNoErrorsInCode('''
const a = bool.fromEnvironment('dart.library.js_util');
const x = [3, if (a) ...[1] else ...[1, 2], 4];
''');
final result = _topLevelVar('x');
assertDartObjectText(result, '''
<unknown> List<int>
variable: self::@variable::x
''');
}

test_bool_fromEnvironment_dartLibraryJsUtil_recordField_nonConstant() async {
await assertErrorsInCode('''
const a = bool.fromEnvironment('dart.library.js_util');
var b = 7;
var x = const A((b, ));
class A {
const A((int, ) p);
}
''', [
error(CompileTimeErrorCode.INVALID_CONSTANT, 84, 1),
]);
}

test_bool_hasEnvironment() async {
await assertNoErrorsInCode('''
const a = bool.hasEnvironment('a');
Expand Down Expand Up @@ -5307,6 +5339,20 @@ void main() {
]);
}

@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/55467')
test_listLiteral_expression_nonConstant() async {
await assertErrorsInCode('''
var b = 7;
var x = const A([b]);
class A {
const A(List<int> p);
}
''', [
error(CompileTimeErrorCode.NON_CONSTANT_LIST_ELEMENT, 28, 1),
]);
}

test_redirectingConstructor_typeParameter() async {
await assertNoErrorsInCode('''
class A<T> {
Expand Down
Loading

0 comments on commit 57d7cba

Please sign in to comment.