Skip to content

Commit b4a609e

Browse files
Anna GringauzeCommit Queue
Anna Gringauze
authored and
Commit Queue
committed
[DDC] Use temporary names for synthesized CFE variables
Closes: #51589 Change-Id: I9d52b894419165f7e371c4e4fc56b81cb587d935 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/288829 Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Mark Zhou <[email protected]> Commit-Queue: Anna Gringauze <[email protected]>
1 parent 2cc96bf commit b4a609e

File tree

4 files changed

+349
-87
lines changed

4 files changed

+349
-87
lines changed

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5027,13 +5027,36 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
50275027
return id;
50285028
}
50295029

5030+
/// Detects temporary variables so we can avoid displaying
5031+
/// them in the debugger if needed.
5032+
bool _isTemporaryVariable(VariableDeclaration v) =>
5033+
v.isLowered ||
5034+
v.isSynthesized ||
5035+
v.name == null ||
5036+
v.name!.startsWith('#');
5037+
5038+
/// Creates a temporary name recognized by the debugger.
5039+
/// Assumes `_isTemporaryVariable(v)` is true.
5040+
String? _debuggerFriendlyTemporaryVariableName(VariableDeclaration v) {
5041+
assert(_isTemporaryVariable(v));
5042+
5043+
// Show extension 'this' in the debugger.
5044+
// Do not show the rest of temporary variables.
5045+
if (isExtensionThis(v)) {
5046+
return extractLocalNameFromVariable(v);
5047+
} else if (v.name != null) {
5048+
return 't\$${v.name}';
5049+
}
5050+
return null;
5051+
}
5052+
50305053
js_ast.Identifier _emitVariableRef(VariableDeclaration v) {
5031-
var name = v.name;
5032-
if (name == null || name.startsWith('#')) {
5033-
name = name == null ? 't${_tempVariables.length}' : name.substring(1);
5054+
if (_isTemporaryVariable(v)) {
5055+
var name = _debuggerFriendlyTemporaryVariableName(v);
5056+
name ??= 't\$${_tempVariables.length}';
50345057
return _tempVariables.putIfAbsent(v, () => _emitTemporaryId(name!));
50355058
}
5036-
return _emitIdentifier(name);
5059+
return _emitIdentifier(v.name!);
50375060
}
50385061

50395062
/// Emits the declaration of a variable.

pkg/dev_compiler/lib/src/kernel/expression_compiler_worker.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ class ExpressionCompilerWorker {
587587
alwaysCreateNewNamedNodes: true);
588588
return component;
589589
}
590+
_processedOptions.ticker.logMs('File for $uri does not exist.');
590591
return null;
591592
}
592593

pkg/dev_compiler/test/expression_compiler/expression_compiler_e2e_shared.dart

Lines changed: 168 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,46 @@ main() {
6464
// TODO(nshahan) Merge with [runAgnosticSharedTests] after we no longer need to
6565
// test support for evaluation in legacy (pre-null safety) code.
6666
void runNullSafeSharedTests(SetupCompilerOptions setup, TestDriver driver) {
67+
group('Exceptions', () {
68+
const exceptionSource = r'''
69+
void main() {
70+
try {
71+
throw Exception('meow!');
72+
} catch (e, s) {
73+
// Breakpoint: bp
74+
print('Cat says: \$e:\$s');
75+
}
76+
}
77+
''';
78+
79+
setUpAll(() async {
80+
await driver.initSource(setup, exceptionSource);
81+
});
82+
83+
tearDownAll(() async {
84+
await driver.cleanupTest();
85+
});
86+
87+
test('error', () async {
88+
await driver.check(
89+
breakpointId: 'bp',
90+
expression: 'e.toString()',
91+
expectedResult: 'meow!');
92+
});
93+
94+
test('stack trace', () async {
95+
await driver.check(
96+
breakpointId: 'bp', expression: 's.toString()', expectedResult: '');
97+
});
98+
99+
test('scope', () async {
100+
await driver.checkScope(breakpointId: 'bp', expectedScope: {
101+
'e': 'e',
102+
's': 's',
103+
});
104+
});
105+
});
106+
67107
group('Records', () {
68108
const recordsSource = '''
69109
void main() {
@@ -77,8 +117,7 @@ void runNullSafeSharedTests(SetupCompilerOptions setup, TestDriver driver) {
77117
''';
78118

79119
setUpAll(() async {
80-
await driver
81-
.initSource(setup, recordsSource, experiments: {'records': true});
120+
await driver.initSource(setup, recordsSource);
82121
});
83122

84123
tearDownAll(() async {
@@ -170,6 +209,121 @@ void runNullSafeSharedTests(SetupCompilerOptions setup, TestDriver driver) {
170209
});
171210
});
172211

212+
group('Patterns', () {
213+
const patternsSource = r'''
214+
void main() {
215+
int foo(Object? obj) {
216+
switch (obj) {
217+
case [int a, double b] || [double b, int a]:
218+
// Breakpoint: bp1
219+
return a;
220+
case [int a, String b] || [String a, int b]:
221+
// Breakpoint: bp2
222+
return a;
223+
default:
224+
// Breakpoint: bp3
225+
return 0;
226+
}
227+
}
228+
229+
final one = foo([1,2]);
230+
final ten = foo([10,'20']);
231+
final zero = foo(0);
232+
233+
// Breakpoint: bp4
234+
print('$one, $ten, $zero');
235+
}
236+
''';
237+
238+
setUpAll(() async {
239+
await driver.initSource(setup, patternsSource);
240+
});
241+
242+
tearDownAll(() async {
243+
await driver.cleanupTest();
244+
});
245+
246+
test('first case match', () async {
247+
await driver.check(
248+
breakpointId: 'bp1', expression: 'a.toString()', expectedResult: '1');
249+
});
250+
251+
test('second case match', () async {
252+
await driver.check(
253+
breakpointId: 'bp2',
254+
expression: 'a.toString()',
255+
expectedResult: '10');
256+
});
257+
258+
test('default case match', () async {
259+
await driver.check(
260+
breakpointId: 'bp3',
261+
expression: 'obj.toString()',
262+
expectedResult: '0');
263+
});
264+
265+
test('first case match result', () async {
266+
await driver.check(
267+
breakpointId: 'bp4',
268+
expression: 'one.toString()',
269+
expectedResult: '1');
270+
});
271+
272+
test('second case match result', () async {
273+
await driver.check(
274+
breakpointId: 'bp4',
275+
expression: 'ten.toString()',
276+
expectedResult: '10');
277+
});
278+
279+
test('default match result', () async {
280+
await driver.check(
281+
breakpointId: 'bp4',
282+
expression: 'zero.toString()',
283+
expectedResult: '0');
284+
});
285+
286+
// TODO(annagrin): Remove renamed variables here and below after
287+
// const evaluator stops renaming variables used in cases.
288+
//
289+
// Issue: https://github.com/dart-lang/sdk/issues/51554
290+
test('first case scope', () async {
291+
await driver.checkScope(breakpointId: 'bp1', expectedScope: {
292+
'a': '1',
293+
'b': '2',
294+
r'a$351': r'a$351',
295+
r'b$351': r'b$351',
296+
'obj': 'obj'
297+
});
298+
});
299+
300+
test('second case scope', () async {
301+
await driver.checkScope(breakpointId: 'bp2', expectedScope: {
302+
'a': '10',
303+
'b': '10',
304+
r'a$351': '10',
305+
r'b$351': '\'20\'',
306+
'obj': 'obj'
307+
});
308+
});
309+
310+
test('default case scope', () async {
311+
await driver.checkScope(breakpointId: 'bp3', expectedScope: {
312+
'a': 'a',
313+
'b': 'b',
314+
r'a$351': r'a$351',
315+
r'b$351': r'b$351',
316+
'obj': '0'
317+
});
318+
});
319+
320+
test('result scope', () async {
321+
await driver.checkScope(
322+
breakpointId: 'bp4',
323+
expectedScope: {'foo': 'foo', 'one': '1', 'ten': '10', 'zero': '0'});
324+
});
325+
});
326+
173327
group('Correct null safety mode used', () {
174328
var source = '''
175329
const soundNullSafety = !(<Null>[] is List<int>);
@@ -461,6 +615,11 @@ void runNullSafeSharedTests(SetupCompilerOptions setup, TestDriver driver) {
461615
expression: 'e != E2.id2 && E.id2 != E2.id2',
462616
expectedResult: 'true');
463617
});
618+
test('scope', () async {
619+
await driver.checkScope(breakpointId: 'bp', expectedScope: {
620+
'e': 'e',
621+
});
622+
});
464623
});
465624

466625
group('Automatically inserted argument null checks', () {
@@ -699,6 +858,13 @@ void runAgnosticSharedTests(SetupCompilerOptions setup, TestDriver driver) {
699858
expression: 'this',
700859
expectedError: "Error: Expected identifier, but got 'this'");
701860
});
861+
862+
test('scope', () async {
863+
await driver.checkScope(breakpointId: 'bp', expectedScope: {
864+
r'$this': '\'1234\'',
865+
'ret': '1234',
866+
});
867+
});
702868
});
703869

704870
group('Expression compiler tests in static function:', () {

0 commit comments

Comments
 (0)