Skip to content

Commit 519247e

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer] Add tests for behaviour between wildcards and constant evaluation.
Fixes #56244 Change-Id: Ie5f615bf8956aff33d4015c355bb90e125084c63 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/380282 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 99da8ce commit 519247e

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

pkg/analyzer/test/src/dart/constant/evaluation_test.dart

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,28 @@ bool true
20302030
''');
20312031
}
20322032

2033+
test_visitFunctionReference_wildcard_local() async {
2034+
await assertErrorsInCode(r'''
2035+
test() {
2036+
void _() {}
2037+
const c = _;
2038+
print(c);
2039+
}
2040+
''', [
2041+
error(WarningCode.DEAD_CODE, 11, 11),
2042+
error(CompileTimeErrorCode.UNDEFINED_IDENTIFIER, 35, 1),
2043+
error(CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE, 35,
2044+
1),
2045+
]);
2046+
}
2047+
2048+
test_visitFunctionReference_wildcard_top() async {
2049+
await assertNoErrorsInCode(r'''
2050+
void _() {}
2051+
const c = _;
2052+
''');
2053+
}
2054+
20332055
test_visitInstanceCreationExpression_noArgs() async {
20342056
await assertNoErrorsInCode('''
20352057
class A {
@@ -4528,6 +4550,27 @@ int 42
45284550
''');
45294551
}
45304552

4553+
test_visitSimpleIdentifier_wildcard_local() async {
4554+
await assertErrorsInCode(r'''
4555+
test() {
4556+
const _ = true;
4557+
const c = _;
4558+
}
4559+
''', [
4560+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 35, 1),
4561+
error(CompileTimeErrorCode.UNDEFINED_IDENTIFIER, 39, 1),
4562+
error(CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE, 39,
4563+
1),
4564+
]);
4565+
}
4566+
4567+
test_visitSimpleIdentifier_wildcard_top() async {
4568+
await assertNoErrorsInCode(r'''
4569+
const _ = true;
4570+
const c = _;
4571+
''');
4572+
}
4573+
45314574
test_visitSimpleIdentifier_withoutEnvironment() async {
45324575
await assertNoErrorsInCode(r'''
45334576
const a = b;
@@ -5837,6 +5880,80 @@ B<int>
58375880
(super): A
58385881
f: Type int
58395882
variable: <testLibraryFragment>::@topLevelVariable::a
5883+
''');
5884+
}
5885+
5886+
test_wildcard_regularInitializer() async {
5887+
await assertNoErrorsInCode('''
5888+
class A {
5889+
final int _;
5890+
const A(this._);
5891+
int x() => _; // Avoid unused field warning.
5892+
}
5893+
const a = const A(1);
5894+
''');
5895+
var result = _topLevelVar('a');
5896+
assertDartObjectText(result, '''
5897+
A
5898+
_: int 1
5899+
variable: <testLibraryFragment>::@topLevelVariable::a
5900+
''');
5901+
}
5902+
5903+
test_wildcard_regularInitializer_initializerList() async {
5904+
await assertErrorsInCode('''
5905+
class A {
5906+
final int _;
5907+
final int y;
5908+
const A(this._): y = _;
5909+
}
5910+
''', [
5911+
error(CompileTimeErrorCode.INVALID_CONSTANT, 63, 1),
5912+
error(CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER, 63, 1),
5913+
]);
5914+
}
5915+
5916+
test_wildcard_superInitializer() async {
5917+
await assertNoErrorsInCode('''
5918+
class A {
5919+
final int _;
5920+
const A(this._);
5921+
int x() => _; // Avoid unused field warning.
5922+
}
5923+
class B extends A {
5924+
const B(super._);
5925+
}
5926+
const a = const B(1);
5927+
''');
5928+
var result = _topLevelVar('a');
5929+
assertDartObjectText(result, '''
5930+
B
5931+
(super): A
5932+
_: int 1
5933+
variable: <testLibraryFragment>::@topLevelVariable::a
5934+
''');
5935+
}
5936+
5937+
test_wildcard_superInitializer_multiple() async {
5938+
await assertNoErrorsInCode('''
5939+
class A {
5940+
final int _;
5941+
final int y;
5942+
const A(this._, this.y);
5943+
int x() => _; // Avoid unused field warning.
5944+
}
5945+
class B extends A {
5946+
const B(super._, super._);
5947+
}
5948+
const a = const B(1, 2);
5949+
''');
5950+
var result = _topLevelVar('a');
5951+
assertDartObjectText(result, '''
5952+
B
5953+
(super): A
5954+
_: int 2
5955+
y: int 2
5956+
variable: <testLibraryFragment>::@topLevelVariable::a
58405957
''');
58415958
}
58425959
}

0 commit comments

Comments
 (0)