Skip to content

Commit

Permalink
[stable][cfe] Allow void typed switch expression
Browse files Browse the repository at this point in the history
Fixes #52234

Bug:#52191
Change-Id: I74d749115ec8cab0a89416ca905aa9c4e0d83ccc
Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/299020
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/303020
Commit-Queue: Leaf Petersen <leafp@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
  • Loading branch information
leafpetersen authored and Commit Queue committed May 15, 2023
1 parent 06b4ead commit ea2a2b8
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9389,7 +9389,9 @@ class InferenceVisitorImpl extends InferenceVisitorBase
parent is RelationalPattern && parent.expression == node;

ExpressionInferenceResult expressionResult =
inferExpression(node, context).stopShorting();
// TODO(johnniwinther): Handle [isVoidAllowed] through
// [dispatchExpression].
inferExpression(node, context, isVoidAllowed: true).stopShorting();

if (needsCoercion) {
expressionResult =
Expand Down
25 changes: 25 additions & 0 deletions pkg/front_end/testcases/patterns/issue52191.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2023, 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.

void printBugsSwitch(int n) => switch (n) {
0 => print('no bugs'),
1 => print('one bug'),
_ => print('$n bugs'),
};

void printBugsConditional(int n) => n == 0
? print('no bugs')
: n == 1
? print('one bug')
: print('$n bugs');

main() {
printBugsSwitch(0);
printBugsSwitch(1);
printBugsSwitch(2);

printBugsConditional(0);
printBugsConditional(1);
printBugsConditional(2);
}
45 changes: 45 additions & 0 deletions pkg/front_end/testcases/patterns/issue52191.dart.strong.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
library /*isNonNullableByDefault*/;
import self as self;
import "dart:core" as core;

static method printBugsSwitch(core::int n) → void
return block {
void #t1;
final synthesized core::int #0#0 = n;
#L1:
{
{
if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0) {
#t1 = core::print("no bugs");
break #L1;
}
}
{
if(#C2 =={core::num::==}{(core::Object) → core::bool} #0#0) {
#t1 = core::print("one bug");
break #L1;
}
}
{
if(true) {
#t1 = core::print("${n} bugs");
break #L1;
}
}
}
} =>#t1;
static method printBugsConditional(core::int n) → void
return n =={core::num::==}{(core::Object) → core::bool} 0 ?{void} core::print("no bugs") : n =={core::num::==}{(core::Object) → core::bool} 1 ?{void} core::print("one bug") : core::print("${n} bugs");
static method main() → dynamic {
self::printBugsSwitch(0);
self::printBugsSwitch(1);
self::printBugsSwitch(2);
self::printBugsConditional(0);
self::printBugsConditional(1);
self::printBugsConditional(2);
}

constants {
#C1 = 0
#C2 = 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
library /*isNonNullableByDefault*/;
import self as self;
import "dart:core" as core;

static method printBugsSwitch(core::int n) → void
return block {
void #t1;
final synthesized core::int #0#0 = n;
#L1:
{
{
if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0) {
#t1 = core::print("no bugs");
break #L1;
}
}
{
if(#C2 =={core::num::==}{(core::Object) → core::bool} #0#0) {
#t1 = core::print("one bug");
break #L1;
}
}
{
if(true) {
#t1 = core::print("${n} bugs");
break #L1;
}
}
}
} =>#t1;
static method printBugsConditional(core::int n) → void
return n =={core::num::==}{(core::Object) → core::bool} 0 ?{void} core::print("no bugs") : n =={core::num::==}{(core::Object) → core::bool} 1 ?{void} core::print("one bug") : core::print("${n} bugs");
static method main() → dynamic {
self::printBugsSwitch(0);
self::printBugsSwitch(1);
self::printBugsSwitch(2);
self::printBugsConditional(0);
self::printBugsConditional(1);
self::printBugsConditional(2);
}

constants {
#C1 = 0
#C2 = 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void printBugsSwitch(int n) => switch (n) {};
void printBugsConditional(int n) => n == 0
? print('no bugs')
: n == 1
? print('one bug')
: print('$n bugs');
main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
switch (n) {}
void printBugsSwitch(int n) =>
---- unknown chunk starts ----
;
---- unknown chunk ends ----
main() {}
void printBugsConditional(int n) => n == 0 ? print('no bugs') : n == 1 ? print('one bug') : print('$n bugs');
47 changes: 47 additions & 0 deletions pkg/front_end/testcases/patterns/issue52191.dart.weak.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
library /*isNonNullableByDefault*/;
import self as self;
import "dart:core" as core;
import "dart:_internal" as _in;

static method printBugsSwitch(core::int n) → void
return block {
void #t1;
final synthesized core::int #0#0 = n;
#L1:
{
{
if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0) {
#t1 = core::print("no bugs");
break #L1;
}
}
{
if(#C2 =={core::num::==}{(core::Object) → core::bool} #0#0) {
#t1 = core::print("one bug");
break #L1;
}
}
{
if(true) {
#t1 = core::print("${n} bugs");
break #L1;
}
}
throw new _in::ReachabilityError::•("`null` encountered as case in a switch expression with a non-nullable type.");
}
} =>#t1;
static method printBugsConditional(core::int n) → void
return n =={core::num::==}{(core::Object) → core::bool} 0 ?{void} core::print("no bugs") : n =={core::num::==}{(core::Object) → core::bool} 1 ?{void} core::print("one bug") : core::print("${n} bugs");
static method main() → dynamic {
self::printBugsSwitch(0);
self::printBugsSwitch(1);
self::printBugsSwitch(2);
self::printBugsConditional(0);
self::printBugsConditional(1);
self::printBugsConditional(2);
}

constants {
#C1 = 0
#C2 = 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
library /*isNonNullableByDefault*/;
import self as self;
import "dart:core" as core;
import "dart:_internal" as _in;

static method printBugsSwitch(core::int n) → void
return block {
void #t1;
final synthesized core::int #0#0 = n;
#L1:
{
{
if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0) {
#t1 = core::print("no bugs");
break #L1;
}
}
{
if(#C2 =={core::num::==}{(core::Object) → core::bool} #0#0) {
#t1 = core::print("one bug");
break #L1;
}
}
{
if(true) {
#t1 = core::print("${n} bugs");
break #L1;
}
}
throw new _in::ReachabilityError::•("`null` encountered as case in a switch expression with a non-nullable type.");
}
} =>#t1;
static method printBugsConditional(core::int n) → void
return n =={core::num::==}{(core::Object) → core::bool} 0 ?{void} core::print("no bugs") : n =={core::num::==}{(core::Object) → core::bool} 1 ?{void} core::print("one bug") : core::print("${n} bugs");
static method main() → dynamic {
self::printBugsSwitch(0);
self::printBugsSwitch(1);
self::printBugsSwitch(2);
self::printBugsConditional(0);
self::printBugsConditional(1);
self::printBugsConditional(2);
}

constants {
#C1 = 0
#C2 = 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
library /*isNonNullableByDefault*/;
import self as self;
import "dart:core" as core;

static method printBugsSwitch(core::int n) → void
;
static method printBugsConditional(core::int n) → void
;
static method main() → dynamic
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
library /*isNonNullableByDefault*/;
import self as self;
import "dart:core" as core;
import "dart:_internal" as _in;

static method printBugsSwitch(core::int n) → void
return block {
void #t1;
final synthesized core::int #0#0 = n;
#L1:
{
{
if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0) {
#t1 = core::print("no bugs");
break #L1;
}
}
{
if(#C2 =={core::num::==}{(core::Object) → core::bool} #0#0) {
#t1 = core::print("one bug");
break #L1;
}
}
{
if(true) {
#t1 = core::print("${n} bugs");
break #L1;
}
}
throw new _in::ReachabilityError::•("`null` encountered as case in a switch expression with a non-nullable type.");
}
} =>#t1;
static method printBugsConditional(core::int n) → void
return n =={core::num::==}{(core::Object) → core::bool} 0 ?{void} core::print("no bugs") : n =={core::num::==}{(core::Object) → core::bool} 1 ?{void} core::print("one bug") : core::print("${n} bugs");
static method main() → dynamic {
self::printBugsSwitch(0);
self::printBugsSwitch(1);
self::printBugsSwitch(2);
self::printBugsConditional(0);
self::printBugsConditional(1);
self::printBugsConditional(2);
}

constants {
#C1 = 0
#C2 = 1
}

0 comments on commit ea2a2b8

Please sign in to comment.