Add 'external-effect' pragma support to all the backends.
Call sites targeting a procedure annotated with `external-effect` will not produce any code, including the argument which will not be evaluated. However, the single parameter will be treated as 'live' for the purposes of any global analysis the backends do. This is useful for things like protobuf shaking where a user may want to retain certain protobuf messages without actually emitting the code that retains those messages. Today this functionality is available internally in the vm and wasm SDK libraries. dart2js has similar functionality represented via the opaqueTrue and opaqueFalse booleans (which will cause conditional branches to get shaken after analysis). This will replace dart2js's opaque(True/False). This also adds validation to the frontend to ensure a method annotated with 'external-effect' is well-formed. Change-Id: If1c4096673e655c58fe7638840a16125003e7809 Tested: Backend tests for codegen were added. A frontend test was added for the validation. A language test was added to confirm the behavior. Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/476020 Reviewed-by: Alexander Markov <alexmarkov@google.com> Commit-Queue: Nate Biggs <natebiggs@google.com>
This commit is contained in:
committed by
dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent
d29b88d152
commit
3d2d6492c1
@@ -0,0 +1,35 @@
|
||||
// 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.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
@pragma('external-effect')
|
||||
external void externalEffect(Object? o);
|
||||
|
||||
void noExternalEffect(Object? o) {}
|
||||
|
||||
List<Object> used = [];
|
||||
|
||||
Null use(int o) {
|
||||
used.add(o);
|
||||
return null;
|
||||
}
|
||||
|
||||
const Object constObj = Object();
|
||||
|
||||
Null useConstObject() {
|
||||
used.add(constObj);
|
||||
return null;
|
||||
}
|
||||
|
||||
void main() {
|
||||
externalEffect(use(3));
|
||||
externalEffect(useConstObject());
|
||||
Expect.isTrue(used.isEmpty);
|
||||
noExternalEffect(use(4));
|
||||
noExternalEffect(useConstObject());
|
||||
Expect.equals(used.length, 2);
|
||||
Expect.equals(used[0], 4);
|
||||
Expect.equals(used[1], constObj);
|
||||
}
|
||||
Reference in New Issue
Block a user