Skip to content

Commit 9ac53f7

Browse files
munificentCommit Queue
authored andcommitted
[private named parameters] Test constant evaluation in analyzer and CFE.
Change-Id: I3fea84eed754b199f31f96c1e9f0b76182db8152 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/465821 Reviewed-by: Johnni Winther <[email protected]> Auto-Submit: Bob Nystrom <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent 7126af1 commit 9ac53f7

8 files changed

+148
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,30 @@ void f(Object? x) {
13161316
''');
13171317
}
13181318

1319+
test_privateNamedParameter() async {
1320+
await assertNoErrorsInCode('''
1321+
class C {
1322+
final int _x;
1323+
final int _y;
1324+
const C({required this._x, required this._y});
1325+
int get xy => _x + _y; // Avoid unused field warning.
1326+
}
1327+
const c = C(x: 123, y: 456);
1328+
''');
1329+
var result = _topLevelVar('c');
1330+
assertDartObjectText(result, '''
1331+
C
1332+
_x: int 123
1333+
_y: int 456
1334+
constructorInvocation
1335+
constructor: <testLibrary>::@class::C::@constructor::new
1336+
namedArguments
1337+
x: int 123
1338+
y: int 456
1339+
variable: <testLibrary>::@topLevelVariable::c
1340+
''');
1341+
}
1342+
13191343
test_propertyAccess_nullAware_dynamic_length_notNull() async {
13201344
await assertNoErrorsInCode(r'''
13211345
const dynamic d = 'foo';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
class C {
6+
final int _x;
7+
final int _y;
8+
const C({required this._x, required this._y});
9+
}
10+
const c = C(x: 123, y: 456);
11+
12+
main() {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class C extends core::Object /*hasConstConstructor*/ {
6+
final field core::int _x;
7+
final field core::int _y;
8+
const constructor •({required core::int x, required core::int y}) → self::C
9+
: self::C::_x = x, self::C::_y = y, super core::Object::•()
10+
;
11+
}
12+
static const field self::C c = #C3;
13+
static method main() → dynamic {}
14+
15+
constants {
16+
#C1 = 123
17+
#C2 = 456
18+
#C3 = self::C {_x:#C1, _y:#C2}
19+
}
20+
21+
22+
Constructor coverage from constants:
23+
org-dartlang-testcase:///constant_evaluation.dart:
24+
- C. (from org-dartlang-testcase:///constant_evaluation.dart:8:9)
25+
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class C extends core::Object /*hasConstConstructor*/ {
6+
final field core::int _x;
7+
final field core::int _y;
8+
const constructor •({required core::int x, required core::int y}) → self::C
9+
: self::C::_x = x, self::C::_y = y, super core::Object::•()
10+
;
11+
}
12+
static const field self::C c = #C3;
13+
static method main() → dynamic {}
14+
15+
constants {
16+
#C1 = 123
17+
#C2 = 456
18+
#C3 = self::C {_x:#C1, _y:#C2}
19+
}
20+
21+
22+
Constructor coverage from constants:
23+
org-dartlang-testcase:///constant_evaluation.dart:
24+
- C. (from org-dartlang-testcase:///constant_evaluation.dart:8:9)
25+
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class C extends core::Object /*hasConstConstructor*/ {
6+
final field core::int _x;
7+
final field core::int _y;
8+
const constructor •({required core::int x, required core::int y}) → self::C
9+
: self::C::_x = x, self::C::_y = y, super core::Object::•()
10+
;
11+
}
12+
static const field self::C c = const self::C::•(x: 123, y: 456);
13+
static method main() → dynamic
14+
;
15+
16+
17+
Extra constant evaluation status:
18+
Evaluated: ConstructorInvocation @ org-dartlang-testcase:///constant_evaluation.dart:10:11 -> InstanceConstant(const C{C._x: 123, C._y: 456})
19+
Extra constant evaluation: evaluated: 3, effectively constant: 1
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class C extends core::Object /*hasConstConstructor*/ {
6+
final field core::int _x;
7+
final field core::int _y;
8+
const constructor •({required core::int x, required core::int y}) → self::C
9+
: self::C::_x = x, self::C::_y = y, super core::Object::•()
10+
;
11+
}
12+
static const field self::C c = #C3;
13+
static method main() → dynamic {}
14+
15+
constants {
16+
#C1 = 123
17+
#C2 = 456
18+
#C3 = self::C {_x:#C1, _y:#C2}
19+
}
20+
21+
22+
Constructor coverage from constants:
23+
org-dartlang-testcase:///constant_evaluation.dart:
24+
- C. (from org-dartlang-testcase:///constant_evaluation.dart:8:9)
25+
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class C {
2+
final int _x;
3+
final int _y;
4+
const C({required this._x, required this._y});
5+
}
6+
7+
const c = C(x: 123, y: 456);
8+
9+
main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class C {
2+
const C({required this._x, required this._y});
3+
final int _x;
4+
final int _y;
5+
}
6+
7+
const c = C(x: 123, y: 456);
8+
9+
main() {}

0 commit comments

Comments
 (0)