-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tail recursion optimization #29
Comments
This comment was originally written by [email protected] I think this is one of the most important features Dart is missing. Tail call elimination, or a subset of this functionality — tail recursion elimination — supports the implementation of certain algorithms in a recursive way that is easier to think for some programmers (especially those coming from mathemathical or more formal computing science background) and allows for the implementation of parts of programs without side effects, which has been proven to be less error-prone. It's a feature from many functional programming languages we would love to have in Dart. Please note that this feature hurts no one: people without the understanding of it will simply not benefit from it, but it will not make their lives harder. |
Removed Type-Defect label. |
This comment was originally written by [email protected] Added Triaged label. |
Tail recursion elimination needs to be balanced against issues like debugging. There are ways to do this, and maybe at some point we will do something, but honestly, it won't be very soon, if ever. |
Set owner to @gbracha. |
Just to comment on the "this feature hurts no one". Aside from the common debug arguments there is (or can be) also a runtime cost associated with them. Tail call optimization precludes some calling-conventions (which a VM normally can chose freely). I would compare it to disallowing --fomit-frame-pointer for gcc. |
This comment was originally written by [email protected] I would suggest adding tail-call optimization support quickly. For programming with continuations and other advanced control operators, tail-call optimization is essential. Tail-call optimization is also necessary for programming in a functional style using tail-recursion. Finally, DART could take off quickly as a target language for compilers for functional language compilers such as Hop, SMLtoJs, AFAX, and Links, to name just a few. |
Let me just stress that with popular frameworks like Node.js who rely heavily on a continuation passing style, the tail call optimization does seem more than relevant. |
An important goal of Dart is ensuring we can generate efficient JavaScript code. |
This comment was originally written by [email protected] This is a Bug? Run retunr is false. |
main(){ This is not a bug. 'a' is initialized to null and (null is num) returns false. (Please file a new issue for different bug reports) |
Added apr30-triage label. |
Removed apr30-triage label. |
Added triage1 label. |
Removed this from the Later milestone. |
Removed Oldschool-Milestone-Later label. |
It is unlikely that anything will happen on this front unless JS implements it reliably on all browsers. Even then there are interesting issues. See http://gbracha.blogspot.com/2009/12/chased-by-ones-own-tail.html Added Accepted label. |
This comment was originally written by [email protected] As I understand no changes happened with this case? |
Revisions updated by `dart tools/rev_sdk_deps.dart`. test (https://github.com/dart-lang/test/compare/92da93a..3ba78f1): 3ba78f15 2023-03-07 Bartek Pacia fix typo in architecture.md (#1966) tools (https://github.com/dart-lang/tools/compare/a1c3506..bed358e): bed358e 2023-03-07 Devon Carew rev to 0.1.0 (#29) webdev (https://github.com/dart-lang/webdev/compare/c007560..cfe9753): cfe9753 2023-03-07 Elliott Brooks (she/her) Update `dev` versions of DWDS and Webdev (#2022) c37d419 2023-03-07 Daniel Chevalier Fix for listening to custom streams in DWDS. (#2011) yaml (https://github.com/dart-lang/yaml/compare/a6d8781..1ad2f49): 0f80b12 revert updating the type for YamlScalar.value (#139) 1ad2f49 2023-03-01 Kevin Moore Require Dart 2.19, migrate to dart_flutter_team_lints, make associated fixes (#138) 4d369fd 2023-03-01 Kevin Moore benchmark: fix output.json (#137) yaml_edit (https://github.com/dart-lang/yaml_edit/compare/998eea2..6abc42a): 6abc42a 2023-03-08 Devon Carew updates for the next version of package:yaml (#45) 48e5868 2023-03-08 Kevin Moore blast_repo fixes (#46) aaa1d53 2023-03-02 Jonas Finnemann Jensen Add code coverage (#38) 0668eb5 2023-03-02 Jonas Finnemann Jensen Wrap recursively, prepare release (#28) a4ff857 2023-03-01 Mohamed Ishad Update CHANGELOG.md (#37) 2fdfbdb 2023-02-28 Mohamed Ishad Fix for issue #23 (#34) 494ad7c 2023-02-25 MikiPaul fixed typo (#36) Change-Id: I3aee0b2f84e97cf4f1131c002bb4e84ab8ffcc92 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/287560 Commit-Queue: Devon Carew <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
Here's a minimal repro that this CL fixes: `ui.dart` ```dart library dart.ui; import 'dart:ffi'; part 'foo.dart'; ``` `foo.dart` ```dart part of dart.ui; @Native<Void Function()>(symbol: 'foo_func', isLeaf: true) external void foo_func(); ``` When compiling with `compile_platform.dart` with `--target=dart2wasm`, the following error appears: ``` Unhandled exception: Verification error: Target=wasm, VerificationStage.afterModularTransformations: Invalid location with target 'wasm' on FunctionNode() (FunctionNode): RangeError (offset): Invalid value: Not in inclusive range 0..56: 91 Context: 'foo_func_$import'. Node: 'FunctionNode()'. #0 VerificationErrorListener.reportError (package:kernel/verifier.dart:81:5) #1 VerifyingVisitor.problem (package:kernel/verifier.dart:222:14) #2 VerifyingVisitor._getLocation (package:kernel/verifier.dart:1361:7) #3 VerifyingVisitor._hasLocation (package:kernel/verifier.dart:1370:26) #4 VerifyingVisitor.getSameLibraryLastSeenTreeNode (package:kernel/verifier.dart:1342:28) #5 VerifyingVisitor.localContext (package:kernel/verifier.dart:1382:24) #6 VerifyingVisitor.defaultDartType (package:kernel/verifier.dart:1491:41) #7 Visitor.visitVoidType (package:kernel/visitor.dart:1309:37) #8 VoidType.accept (package:kernel/ast.dart:11190:42) #9 FunctionNode.visitChildren (package:kernel/ast.dart:3919:16) #10 VerifyingVisitor.visitChildren (package:kernel/verifier.dart:259:10) #11 VerifyingVisitor.visitWithLocalScope (package:kernel/verifier.dart:266:5) #12 VerifyingVisitor.visitFunctionNode (package:kernel/verifier.dart:721:5) #13 FunctionNode.accept (package:kernel/ast.dart:3908:38) #14 VerifyingVisitor.visitProcedure (package:kernel/verifier.dart:620:19) #15 Procedure.accept (package:kernel/ast.dart:3311:40) #16 visitList (package:kernel/ast.dart:14488:14) #17 Library.visitChildren (package:kernel/ast.dart:591:5) #18 VerifyingVisitor.visitChildren (package:kernel/verifier.dart:259:10) #19 VerifyingVisitor.defaultTreeNode (package:kernel/verifier.dart:196:5) #20 TreeVisitor.visitLibrary (package:kernel/visitor.dart:503:35) #21 VerifyingVisitor.visitLibrary (package:kernel/verifier.dart:367:11) #22 Library.accept (package:kernel/ast.dart:577:38) #23 visitList (package:kernel/ast.dart:14488:14) #24 Component.visitChildren (package:kernel/ast.dart:14320:5) #25 VerifyingVisitor.visitChildren (package:kernel/verifier.dart:259:10) #26 VerifyingVisitor.visitComponent (package:kernel/verifier.dart:342:7) #27 Component.accept (package:kernel/ast.dart:14313:38) #28 VerifyingVisitor.check (package:kernel/verifier.dart:171:15) #29 verifyComponent (package:kernel/verifier.dart:69:20) ... ``` The issue seems to be that after doing this native transformation, the node's `fileUri` references the enclosing library (`ui.dart` above), but the `node.location` references the actual source file (`foo.dart` above) indirectly through `node.fileOffset`. This ends up being an issue when compiling the platform dill in Google3, but I didn't look into why `flutter build web --wasm` isn't broken. Internal bug: b/292172146 Change-Id: I2b8d7d215b2c36354860257ce651d50168e9523d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/315360 Reviewed-by: Ömer Ağacan <[email protected]> Commit-Queue: Jia Hao Goh <[email protected]>
This issue was originally filed by [email protected]
Tailrec optimization is a well-known feature of functional languages.
I propose to add support for it in dart.
The text was updated successfully, but these errors were encountered: