| // Copyright (c) 2026, 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. | |
| // ignore_for_file: unused_local_variable, unused_element | |
| class A { | |
| final int f; | |
| A(this.f); | |
| } | |
| void foo() {} | |
| // Unused pure computations are removed. | |
| int unusedPureInstructions(int x, int y) { | |
| var a = x * y; | |
| var b = a + 1; | |
| var c = -b; | |
| var d = x.toDouble() + 1.5; | |
| var e = !(x < y); | |
| return x; | |
| } | |
| // Unused instructions which can throw are kept. | |
| void unusedThrowingInstructions(int x, int y, Object? obj, List<int> list) { | |
| var a = x ~/ y; | |
| var b = obj!; | |
| var c = obj as String; | |
| var d = list[x]; | |
| } | |
| // Unused instructions with side-effects are kept. | |
| void unusedSideEffects(A obj) { | |
| var a = obj.f; | |
| var b = '$obj'; | |
| foo(); | |
| } | |
| // Unused allocations are removed. | |
| void unusedAllocations(int x, Object y) { | |
| var record = (x, y); | |
| var namedRecord = (x, foo: y); | |
| void localFunction() {} | |
| var closure = () => 42; | |
| S id<S>(S v) => v; | |
| int Function(int) instantiated = id; | |
| } | |
| // Allocations initialized with stores are kept, | |
| // as stores have side-effects. | |
| void unusedAllocationsWithStores<T>(int x) { | |
| var closure = () => x; | |
| var genericClosure = () => T; | |
| } | |
| // Allocation is kept if it is used by a live instruction. | |
| (int, int) usedAllocation(int x) { | |
| var unused = (x, x); | |
| return (x, x + 1); | |
| } | |
| // Dead cycle of Phi instructions is removed. | |
| int deadPhiCycle(int n) { | |
| var unused = 0; | |
| var i = 0; | |
| while (i < n) { | |
| unused = unused + 1; | |
| i = i + 1; | |
| } | |
| return i; | |
| } | |
| // Dead cycle of Phi instructions in nested loops is removed. | |
| void deadNestedPhiCycle(int n, int m) { | |
| var unused = 0; | |
| for (var i = 0; i < n; ++i) { | |
| for (var j = 0; j < m; ++j) { | |
| unused = unused ^ (i + j); | |
| } | |
| print(i); | |
| } | |
| } | |
| // Variable is not used in catch block, so ExceptionalInput | |
| // instructions and catch block parameter are removed. | |
| void unusedInCatch(int a) { | |
| var x = 0; | |
| try { | |
| foo(); | |
| x = a + 1; | |
| foo(); | |
| } catch (_) { | |
| print('error'); | |
| } | |
| } | |
| // Variable is used in outer catch block only. | |
| void usedInOuterCatch(int a, int b) { | |
| var x = 0; | |
| try { | |
| x = a; | |
| try { | |
| foo(); | |
| x = b; | |
| foo(); | |
| } catch (_) { | |
| print('inner'); | |
| } | |
| foo(); | |
| } catch (_) { | |
| print(x); | |
| } | |
| } | |
| // Variable is used in inner catch block only. | |
| void usedInInnerCatch(int a, int b) { | |
| var x = 0; | |
| try { | |
| x = a; | |
| try { | |
| foo(); | |
| x = b; | |
| foo(); | |
| } catch (_) { | |
| print(x); | |
| } | |
| x = a + b; | |
| foo(); | |
| } catch (_) { | |
| print('outer'); | |
| } | |
| } | |
| void main() {} |