Skip to content

Commit

Permalink
Add support for BlockExpression.
Browse files Browse the repository at this point in the history
Since we already have support for Block in our visitors, we can extend
them to support BlockExpression fairly easily.

co19_2 and language_2 tests still need to be unskipped, but we can't do
that until CFE respects the --enable-experiment=control-flow-collections
flag.

Change-Id: I48da19cc33dbf35c5ead3ea13266b01d3e0de959
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/97820
Commit-Queue: Mayank Patke <fishythefish@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
  • Loading branch information
fishythefish authored and commit-bot@chromium.org committed Mar 26, 2019
1 parent 61f0f5b commit 419243f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 25 deletions.
6 changes: 6 additions & 0 deletions pkg/compiler/lib/src/inferrer/builder_kernel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,12 @@ class KernelTypeGraphBuilder extends ir.Visitor<TypeInformation> {
return visit(node.body);
}

@override
TypeInformation visitBlockExpression(ir.BlockExpression node) {
visit(node.body);
return visit(node.value);
}

@override
TypeInformation visitForInStatement(ir.ForInStatement node) {
if (node.iterable is ir.ThisExpression) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/compiler/lib/src/ir/scope_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,13 @@ class ScopeModelBuilder extends ir.Visitor<InitializerComplexity>
return const InitializerComplexity.lazy();
}

@override
InitializerComplexity visitBlockExpression(ir.BlockExpression node) {
visitNode(node.body);
visitNode(node.value);
return const InitializerComplexity.lazy();
}

@override
InitializerComplexity visitCatch(ir.Catch node) {
visitInContext(node.guard, VariableUse.explicit);
Expand Down
6 changes: 6 additions & 0 deletions pkg/compiler/lib/src/ir/static_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,12 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
return super.visitLet(node);
}

@override
ir.DartType visitBlockExpression(ir.BlockExpression node) {
visitNode(node.body);
return super.visitBlockExpression(node);
}

ir.DartType _computeInstantiationType(
ir.Instantiation node, ir.FunctionType expressionType) {
return ir.Substitution.fromPairs(
Expand Down
5 changes: 5 additions & 0 deletions pkg/compiler/lib/src/ir/static_type_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ abstract class StaticTypeBase extends ir.Visitor<ir.DartType> {
return visitNode(node.body);
}

@override
ir.DartType visitBlockExpression(ir.BlockExpression node) {
return visitNode(node.value);
}

@override
ir.DartType visitInvalidExpression(ir.InvalidExpression node) =>
const DoesNotCompleteType();
Expand Down
5 changes: 5 additions & 0 deletions pkg/compiler/lib/src/ir/visitors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ class Constantifier extends ir.ExpressionVisitor<ConstantExpression> {
return defaultExpression(node);
}

@override
ConstantExpression visitBlockExpression(ir.BlockExpression node) {
return defaultExpression(node);
}

/// Compute the [ConstantConstructor] corresponding to the const constructor
/// [node].
ConstantConstructor computeConstantConstructor(ir.Constructor node) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/compiler/lib/src/ssa/builder_kernel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3304,6 +3304,12 @@ class KernelSsaGraphBuilder extends ir.Visitor
node.body.accept(this);
}

@override
void visitBlockExpression(ir.BlockExpression node) {
node.body.accept(this);
node.value.accept(this);
}

/// Extracts the list of instructions for the positional subset of arguments.
List<HInstruction> _visitPositionalArguments(ir.Arguments arguments) {
List<HInstruction> result = <HInstruction>[];
Expand Down
36 changes: 36 additions & 0 deletions tests/language_2/spread_collections/runtime_error_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// SharedOptions=--enable-experiment=spread-collections

import 'package:expect/expect.dart';

// Typed as dynamic to also test spreading a value of type dynamic.
final dynamic list = [1, 2, 3, 4];
final dynamic map = {1: 1, 2: 2, 3: 3, 4: 4};
final dynamic set = {1, 2, 3, 4};

void main() {
dynamic nonIterable = 3;
Expect.throwsTypeError(() => <int>[...nonIterable]);
Expect.throwsTypeError(() => <int>{...nonIterable});

dynamic nonMap = 3;
Expect.throwsTypeError(() => <int, int>{...nonMap});

dynamic wrongIterableType = <String>["s"];
Expect.throwsTypeError(() => <int>[...wrongIterableType]);
Expect.throwsTypeError(() => <int>{...wrongIterableType});

dynamic wrongKeyType = <String, int>{"s": 1};
dynamic wrongValueType = <int, String>{1: "s"};
Expect.throwsTypeError(() => <int, int>{...wrongKeyType});
Expect.throwsTypeError(() => <int, int>{...wrongValueType});

// Mismatched collection types.
Expect.throwsTypeError(() => <int>[...map]);
Expect.throwsTypeError(() => <int, int>{...list});
Expect.throwsTypeError(() => <int, int>{...set});
Expect.throwsTypeError(() => <int>{...map});
}
25 changes: 0 additions & 25 deletions tests/language_2/spread_collections/spread_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ void main() {
testSet();
testDuplicateKeys();
testKeyOrder();
testCastFailures();
}

void testList() {
Expand Down Expand Up @@ -197,27 +196,3 @@ void testKeyOrder() {
set = <Equality>{log(e1a), ...<Equality>[log(e1b), log(e2a), log(e2b)]};
Expect.equals("1:a,1:b,2:a,2:b", transcript.join(","));
}

void testCastFailures() {
dynamic nonIterable = 3;
Expect.throwsTypeError(() => <int>[...nonIterable]);
Expect.throwsTypeError(() => <int>{...nonIterable});

dynamic nonMap = 3;
Expect.throwsTypeError(() => <int, int>{...nonMap});

dynamic wrongIterableType = <String>["s"];
Expect.throwsTypeError(() => <int>[...wrongIterableType]);
Expect.throwsTypeError(() => <int>{...wrongIterableType});

dynamic wrongKeyType = <String, int>{"s": 1};
dynamic wrongValueType = <int, String>{1: "s"};
Expect.throwsTypeError(() => <int, int>{...wrongKeyType});
Expect.throwsTypeError(() => <int, int>{...wrongValueType});

// Mismatched collection types.
Expect.throwsTypeError(() => <int>[...map]);
Expect.throwsTypeError(() => <int, int>{...list});
Expect.throwsTypeError(() => <int, int>{...set});
Expect.throwsTypeError(() => <int>{...map});
}

0 comments on commit 419243f

Please sign in to comment.