|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Verify that calling a method which returns Never breaks control flow. |
| 6 | +// Regression test for https://github.com/dart-lang/sdk/issues/59941. |
| 7 | + |
| 8 | +import 'package:expect/expect.dart'; |
| 9 | +import 'package:vm/testing/il_matchers.dart'; |
| 10 | + |
| 11 | +@pragma('vm:never-inline') |
| 12 | +void myprint(Object o) { |
| 13 | + print(o); |
| 14 | +} |
| 15 | + |
| 16 | +@pragma('vm:never-inline') |
| 17 | +Never bar() { |
| 18 | + throw 'baz'; |
| 19 | +} |
| 20 | + |
| 21 | +@pragma('vm:never-inline') |
| 22 | +@pragma('vm:testing:print-flow-graph') |
| 23 | +int foo(bool condition, int arg) { |
| 24 | + int i = arg; |
| 25 | + if (condition) { |
| 26 | + i = 2; |
| 27 | + bar(); // <-- Return type `Never`, so it always throws and can never fall through. |
| 28 | + } |
| 29 | + return i; |
| 30 | +} |
| 31 | + |
| 32 | +main() { |
| 33 | + Expect.equals(42, foo(false, 42)); |
| 34 | + Expect.equals(43, foo(false, 43)); |
| 35 | + Expect.throws(() { |
| 36 | + foo(true, 44); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +void matchIL$foo(FlowGraph graph) { |
| 41 | + graph.dump(); |
| 42 | + graph.match([ |
| 43 | + match.block('Graph', []), |
| 44 | + match.block('Function', [ |
| 45 | + 'condition' << match.Parameter(index: 0), |
| 46 | + 'arg' << match.Parameter(index: 1), |
| 47 | + match.CheckStackOverflow(), |
| 48 | + match.Branch( |
| 49 | + match.StrictCompare('condition', match.any, kind: '==='), |
| 50 | + ifTrue: 'B3', |
| 51 | + ifFalse: 'B4', |
| 52 | + ), |
| 53 | + ]), |
| 54 | + 'B3' << match.block('Target', [match.StaticCall(), match.Stop()]), |
| 55 | + 'B4' << match.block('Target', [match.DartReturn('arg')]), |
| 56 | + ]); |
| 57 | +} |
0 commit comments