-
Notifications
You must be signed in to change notification settings - Fork 12.1k
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
Document become
keyword
#113095
base: master
Are you sure you want to change the base?
Document become
keyword
#113095
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1228,6 +1228,69 @@ mod ref_keyword {} | |||||
/// ``` | ||||||
mod return_keyword {} | ||||||
|
||||||
#[doc(keyword = "become")] | ||||||
// | ||||||
/// Perform a tail-call of a function. | ||||||
/// | ||||||
/// A `become` transfers the execution flow to a function in such a way, that | ||||||
/// returning from the callee returns to the caller of the current function: | ||||||
/// | ||||||
/// ``` | ||||||
/// #![feature(explicit_tail_calls)] | ||||||
/// | ||||||
/// fn a() -> u32 { | ||||||
/// become b(); | ||||||
/// } | ||||||
/// | ||||||
/// fn b() -> u32 { | ||||||
/// return 2; // this return directly returns to the main ---+ | ||||||
/// } // | | ||||||
/// // | | ||||||
/// fn main() { // | | ||||||
/// let res = a(); // <--------------------------------------+ | ||||||
/// assert_eq!(res, 2); | ||||||
/// } | ||||||
/// ``` | ||||||
/// | ||||||
/// This is an optimization that allows function calls to not exhaust the stack. | ||||||
/// This is most useful for (mutually) recursive algorithms, but may be used in | ||||||
/// other cases too. | ||||||
/// | ||||||
/// It is guaranteed that the call will not cause unbounded stack growth if it | ||||||
/// is part of a recursive cycle in the call graph. | ||||||
/// | ||||||
/// For example note that the functions `halt` and `halt_loop` below are | ||||||
/// identical, they both do nothing, forever. However, `stack_overflow` is | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// different from them, even though it is written almost identically to | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// `halt`, `stack_overflow` exhausts the stack and so causes a stack | ||||||
/// overflow, instead of running forever. | ||||||
Comment on lines
+1262
to
+1266
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't LLVM allowed to optimize There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is allowed, but it also is allowed not to. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I guess it's allowed to do it in all these cases, right? I guess what I'm concerned about is the example being so trivial that it doesn't hold up to even trivial examination. |
||||||
/// | ||||||
/// | ||||||
/// ``` | ||||||
/// #![feature(explicit_tail_calls)] | ||||||
/// | ||||||
/// # #[allow(unreachable_code)] | ||||||
/// fn halt() -> ! { | ||||||
/// become halt() | ||||||
/// } | ||||||
/// | ||||||
/// fn halt_loop() -> ! { | ||||||
/// loop {} | ||||||
/// } | ||||||
/// | ||||||
/// # #[allow(unconditional_recursion)] | ||||||
/// fn stack_overflow() -> ! { | ||||||
/// stack_overflow() // implicit return | ||||||
/// } | ||||||
/// ``` | ||||||
Comment on lines
+1262
to
+1285
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This discusses a function that is "obviously wrong", which means it does not make it clear why one wants to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense, hmm. I guess the problem (similarly to the discussions on the RFC) is that there is no concise example where using tail calls makes sense in rust — most, if not all, small examples can be written just as good with a loop. Maybe it would make sense to have two examples? One a bit silly, maybe a slice fold, and the other longer one with something like an interpreter? Reading it now I see that this is a bad example, but I'm not sure what example would be good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A silly fold would be good! I'm not looking for "a loop wouldn't be just as good", just something that actually feels like something a human would want to write. |
||||||
/// | ||||||
/// Note that from an algorithmic standpoint, loops and tail-calls are | ||||||
/// interchangeable, you can always rewrite a loop to use tail-calls | ||||||
/// instead and vice versa. They are, however, very different in the code | ||||||
/// structure, so sometimes one approach can make more sense than the other. | ||||||
#[cfg(not(bootstrap))] | ||||||
mod become_keyword {} | ||||||
|
||||||
#[doc(keyword = "self")] | ||||||
// | ||||||
/// The receiver of a method, or the current module. | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.