Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

WaffleLapkin
Copy link
Member

The feature is not yet implemented, so I'm not sure if we should merge this right away, promoting an incomplete feature is probably not the best idea. But the docs can be reviewed while the implementation work is being done.

@rustbot
Copy link
Collaborator

rustbot commented Jun 27, 2023

r? @joshtriplett

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 27, 2023
@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@Rageking8 Rageking8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small nits.

library/std/src/keyword_docs.rs Outdated Show resolved Hide resolved
library/std/src/keyword_docs.rs Outdated Show resolved Hide resolved
library/std/src/keyword_docs.rs Outdated Show resolved Hide resolved
Co-authored-by: Rageking8 <106309953+Rageking8@users.noreply.github.com>
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-14 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- src/keyword_docs.rs - become_keyword (line 1269) stdout ----
error: the feature `explicit_tail_calls` is incomplete and may not be safe to use and/or cause compiler crashes
##[error] --> src/keyword_docs.rs:1269:12
  |
3 | #![feature(explicit_tail_calls)]
  |
  = note: see issue #112788 <https://github.com/rust-lang/rust/issues/112788> for more information
note: the lint level is defined here
 --> src/keyword_docs.rs:1267:9
---

error: function cannot return without recursing
##[error] --> src/keyword_docs.rs:1273:1
  |
7 | fn halt() -> ! {
  | ^^^^^^^^^^^^^^ cannot return without recursing
8 |     become halt()
  |            ------ recursive call site
  |
  = help: a `loop` may express intention better if this is on purpose
  = note: `#[deny(unconditional_recursion)]` implied by `#[deny(warnings)]`
error: aborting due to 2 previous errors

Couldn't compile the test.
---- src/keyword_docs.rs - become_keyword (line 1238) stdout ----
---- src/keyword_docs.rs - become_keyword (line 1238) stdout ----
error: the feature `explicit_tail_calls` is incomplete and may not be safe to use and/or cause compiler crashes
##[error] --> src/keyword_docs.rs:1239:12
  |
3 | #![feature(explicit_tail_calls)]
  |
  = note: see issue #112788 <https://github.com/rust-lang/rust/issues/112788> for more information
note: the lint level is defined here
 --> src/keyword_docs.rs:1237:9
---
    src/keyword_docs.rs - become_keyword (line 1238)

test result: FAILED. 1120 passed; 2 failed; 17 ignored; 0 measured; 0 filtered out; finished in 15.90s

error: doctest failed, to rerun pass `-p std --doc`

/// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// identical, they both do nothing, forever. However, `stack_overflow` is
/// identical: they both do nothing, forever. However, `stack_overflow` is

///
/// For example note that the functions `halt` and `halt_loop` below are
/// identical, they both do nothing, forever. However, `stack_overflow` is
/// different from them, even though it is written almost identically to
Copy link
Contributor

Choose a reason for hiding this comment

The 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
/// different from them. Even though it is written almost identically to

Comment on lines +1262 to +1285
/// For example note that the functions `halt` and `halt_loop` below are
/// identical, they both do nothing, forever. However, `stack_overflow` is
/// different from them, even though it is written almost identically to
/// `halt`, `stack_overflow` exhausts the stack and so causes a stack
/// overflow, instead of running forever.
///
///
/// ```
/// #![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
/// }
/// ```
Copy link
Contributor

Choose a reason for hiding this comment

The 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 become in "real" code. I think we can do slightly better than this, as the documentation should focus on improving the good cases, like e.g. writing "natural" recursive merge-sorts. The example improvement can still be contrived, however.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Comment on lines +1262 to +1266
/// For example note that the functions `halt` and `halt_loop` below are
/// identical, they both do nothing, forever. However, `stack_overflow` is
/// different from them, even though it is written almost identically to
/// `halt`, `stack_overflow` exhausts the stack and so causes a stack
/// overflow, instead of running forever.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't LLVM allowed to optimize stack_overflow() into loop { }? I know we don't allow it to remove infinite loops, but...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is allowed, but it also is allowed not to.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

//
/// Perform a tail-call of a function.
///
/// A `become` transfers the execution flow to a function in such a way, that
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A `become` transfers the execution flow to a function in such a way, that
/// A `become` transfers the execution flow to a function in such a way that

@workingjubilee
Copy link
Contributor

Unsure about the direction of this documentation focusing on the absurd case.

@rustbot author
r? @workingjubilee

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 31, 2023
@JohnCSimon
Copy link
Member

@WaffleLapkin
ping from triage - can you post your status on this PR? This PR has not received an update in a few months. Thank you!

@WaffleLapkin WaffleLapkin added the S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. label Dec 18, 2023
@WaffleLapkin
Copy link
Member Author

@JohnCSimon this is blocked on actually implementing the feature (it would be silly to document a feature one can't use). Also I need to address the review comments above.

Note for future self: example of fold, an explicit note about drop order (which llvm can't necessarily change).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants