Skip to content

Commit

Permalink
Flow analysis: add unit tests of try/finally handling.
Browse files Browse the repository at this point in the history
Change-Id: I406cf610d3f62af94c998a82e8e580cb9bd2d9b5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115611
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
stereotype441 authored and commit-bot@chromium.org committed Sep 5, 2019
1 parent b179ef3 commit ea35690
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
86 changes: 86 additions & 0 deletions pkg/front_end/test/fasta/flow_analysis/flow_analysis_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,92 @@ main() {
});
});

test('tryFinallyStatement_finallyBegin() restores pre-try state', () {
var h = _Harness();
var x = h.addVar('x', 'int?');
var y = h.addVar('y', 'int?');
h.run((flow) {
h.declare(x, initialized: true);
h.declare(y, initialized: true);
h.promote(y, 'int');
flow.tryFinallyStatement_bodyBegin();
h.promote(x, 'int');
expect(flow.promotedType(x).type, 'int');
expect(flow.promotedType(y).type, 'int');
flow.tryFinallyStatement_finallyBegin({});
expect(flow.promotedType(x), isNull);
expect(flow.promotedType(y).type, 'int');
flow.tryFinallyStatement_end({});
});
});

test(
'tryFinallyStatement_finallyBegin() un-promotes variables assigned in '
'body', () {
var h = _Harness();
var x = h.addVar('x', 'int?');
h.run((flow) {
h.declare(x, initialized: true);
h.promote(x, 'int');
expect(flow.promotedType(x).type, 'int');
flow.tryFinallyStatement_bodyBegin();
flow.write(x);
h.promote(x, 'int');
expect(flow.promotedType(x).type, 'int');
flow.tryFinallyStatement_finallyBegin({x});
expect(flow.promotedType(x), isNull);
flow.tryFinallyStatement_end({});
});
});

test('tryFinallyStatement_end() restores promotions from try body', () {
var h = _Harness();
var x = h.addVar('x', 'int?');
var y = h.addVar('y', 'int?');
h.run((flow) {
h.declare(x, initialized: true);
h.declare(y, initialized: true);
flow.tryFinallyStatement_bodyBegin();
h.promote(x, 'int');
expect(flow.promotedType(x).type, 'int');
flow.tryFinallyStatement_finallyBegin({});
expect(flow.promotedType(x), isNull);
h.promote(y, 'int');
expect(flow.promotedType(y).type, 'int');
flow.tryFinallyStatement_end({});
// Both x and y should now be promoted.
expect(flow.promotedType(x).type, 'int');
expect(flow.promotedType(y).type, 'int');
});
});

test(
'tryFinallyStatement_end() does not restore try body promotions for '
'variables assigned in finally', () {
var h = _Harness();
var x = h.addVar('x', 'int?');
var y = h.addVar('y', 'int?');
h.run((flow) {
h.declare(x, initialized: true);
h.declare(y, initialized: true);
flow.tryFinallyStatement_bodyBegin();
h.promote(x, 'int');
expect(flow.promotedType(x).type, 'int');
flow.tryFinallyStatement_finallyBegin({});
expect(flow.promotedType(x), isNull);
flow.write(x);
flow.write(y);
h.promote(y, 'int');
expect(flow.promotedType(y).type, 'int');
flow.tryFinallyStatement_end({x, y});
// x should not be re-promoted, because it might have been assigned a
// non-promoted value in the "finally" block. But y's promotion still
// stands, because y was promoted in the finally block.
expect(flow.promotedType(x), isNull);
expect(flow.promotedType(y).type, 'int');
});
});

test('whileStatement_conditionBegin() un-promotes', () {
var h = _Harness();
var x = h.addVar('x', 'int?');
Expand Down
1 change: 1 addition & 0 deletions pkg/front_end/test/spell_checking_list_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,7 @@ stage
stamps
standalone
standard
stands
star
start
started
Expand Down

0 comments on commit ea35690

Please sign in to comment.