Files
sdk/pkg/front_end/testcases/patterns/simple_pattern_variable_declaration.dart.strong.expect
T
Johnni Winther c4724e5a89 [cfe] Add Throw.forErrorHandling
This adds a `forErrorHandling` to the `Throw` node. This is used that the `throw` is *not* present in the source code but added to ensure
correctness and/or soundness of the generated code.

This is used for instance in the lowering for handling duplicate writes
to a late final field or for pattern assignments that don't match.

In response to https://github.com/dart-lang/sdk/issues/53519

TEST=updated ast-to-text

Change-Id: Ie103829d98fda9cd7b64e9e3d893e77d1e86d7d8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/347900
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
Reviewed-by: Liam Appelbe <liama@google.com>
2024-02-19 12:12:02 +00:00

63 lines
4.0 KiB
Plaintext

library;
import self as self;
import "dart:core" as core;
static method test1(dynamic x) → dynamic {
hoisted has-declared-initializer core::int y;
{
final synthesized dynamic #0#0 = x as{TypeError,ForDynamic} core::List<core::int>;
synthesized dynamic #0#6;
synthesized core::bool #0#6#isSet = false;
if(!(#0#0 is core::List<dynamic> && #0#0{core::List<dynamic>}.{core::List::length}{core::int} =={core::num::==}{(core::Object) → core::bool} #C1 && ((#0#6#isSet ?{dynamic} #0#6{dynamic} : let final dynamic #t1 = #0#6#isSet = true in #0#6 = #0#0{core::List<dynamic>}.{core::List::[]}(0){(core::int) → dynamic}) is core::int && (let final dynamic #t2 = y = (#0#6#isSet ?{dynamic} #0#6{dynamic} : let final dynamic #t3 = #0#6#isSet = true in #0#6 = #0#0{core::List<dynamic>}.{core::List::[]}(0){(core::int) → dynamic}) as{Unchecked} core::int in true))))
throw{for-error-handling} new core::StateError::•("Pattern matching error");
}
return y;
}
static method test2(dynamic x) → dynamic {
hoisted has-declared-initializer core::String a;
hoisted has-declared-initializer core::String b;
{
final synthesized dynamic #0#0 = x as{TypeError,ForDynamic} core::List<core::String>;
synthesized core::int #0#2;
synthesized core::bool #0#2#isSet = false;
synthesized dynamic #0#6;
synthesized core::bool #0#6#isSet = false;
synthesized dynamic #0#7;
synthesized core::bool #0#7#isSet = false;
if(!(#0#0 is core::List<dynamic> && (#0#2#isSet ?{core::int} #0#2{core::int} : let final dynamic #t4 = #0#2#isSet = true in #0#2 = #0#0{core::List<dynamic>}.{core::List::length}{core::int}).{core::num::>=}(#C2){(core::num) → core::bool} && ((#0#6#isSet ?{dynamic} #0#6{dynamic} : let final dynamic #t5 = #0#6#isSet = true in #0#6 = #0#0{core::List<dynamic>}.{core::List::[]}(0){(core::int) → dynamic}) is core::String && (let final dynamic #t6 = a = (#0#6#isSet ?{dynamic} #0#6{dynamic} : let final dynamic #t7 = #0#6#isSet = true in #0#6 = #0#0{core::List<dynamic>}.{core::List::[]}(0){(core::int) → dynamic}) as{Unchecked} core::String in true)) && ((#0#7#isSet ?{dynamic} #0#7{dynamic} : let final dynamic #t8 = #0#7#isSet = true in #0#7 = #0#0{core::List<dynamic>}.{core::List::[]}((#0#2#isSet ?{core::int} #0#2{core::int} : let final dynamic #t9 = #0#2#isSet = true in #0#2 = #0#0{core::List<dynamic>}.{core::List::length}{core::int}).{core::num::-}(1){(core::num) → core::int}){(core::int) → dynamic}) is core::String && (let final dynamic #t10 = b = (#0#7#isSet ?{dynamic} #0#7{dynamic} : let final dynamic #t11 = #0#7#isSet = true in #0#7 = #0#0{core::List<dynamic>}.{core::List::[]}((#0#2#isSet ?{core::int} #0#2{core::int} : let final dynamic #t12 = #0#2#isSet = true in #0#2 = #0#0{core::List<dynamic>}.{core::List::length}{core::int}).{core::num::-}(1){(core::num) → core::int}){(core::int) → dynamic}) as{Unchecked} core::String in true))))
throw{for-error-handling} new core::StateError::•("Pattern matching error");
}
return a.{core::String::+}(b){(core::String) → core::String};
}
static method main() → dynamic {
self::expectEquals(self::test1(<core::int>[0]), 0);
self::expectThrows(() → void => self::test1(<dynamic>[]));
self::expectThrows(() → void => self::test1(<core::int>[0, 1, 2]));
self::expectEquals(self::test2(<core::String>["one", "two", "three", "four"]), "onefour");
self::expectThrows(() → void => self::test2(<core::String>["one"]));
self::expectThrows(() → void => self::test2("one"));
self::expectThrows(() → void => self::test2(null));
}
static method expectEquals(dynamic x, dynamic y) → dynamic {
if(!(x =={core::Object::==}{(core::Object) → core::bool} y)) {
throw "Expected ${x} to be equal to ${y}.";
}
}
static method expectThrows(() → void f) → dynamic {
core::bool hasThrown = true;
try {
f(){() → void};
hasThrown = false;
}
on core::Object catch(final core::Object e) {
}
if(!hasThrown) {
throw "Expected function to throw.";
}
}
constants {
#C1 = 1
#C2 = 2
}