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

Implement Termination for Option<()> #61360

Conversation

GuillaumeGomez
Copy link
Member

After a question about this here, I decided to open this PR. However, is this how we should implement it? Should we consider None as a failure (because that's how I implemented it)?

Anyway, I think having this PR open is a good place to discuss about it (and closing it if we don't want it of course).

cc @ollie27 @rust-lang/compiler

@rust-highfive
Copy link
Collaborator

r? @withoutboats

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

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 30, 2019
@estebank estebank added I-nominated T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 30, 2019
@jonas-schievink
Copy link
Contributor

This seems like more of a libs/lang issue than compiler, no?

@jonas-schievink jonas-schievink added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 30, 2019
@ollie27
Copy link
Member

ollie27 commented May 30, 2019

Should we consider None as a failure

The fact that that is even a question suggests that we shouldn't add this impl. See also rust-lang/rfcs#1937 (comment).

@cramertj
Copy link
Member

IMO it's non-obvious what fn main() -> Option<()> means.

@jonas-schievink jonas-schievink added the relnotes Marks issues that should be documented in the release notes of the next release. label May 30, 2019
@jonas-schievink jonas-schievink added this to the 1.37 milestone May 30, 2019
@Centril Centril added T-lang Relevant to the language team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 30, 2019
@Centril
Copy link
Contributor

Centril commented May 30, 2019

This seems like a matter primarily for the language team.

@GuillaumeGomez
Copy link
Member Author

Oh yes it is, sorry!

@cramertj: Absolutely. I first thought than in any case, we should just return 0 and here we go. Then I decided to go for an "error" in case of None. It'd make a rustdoc test automatic implementation a bit better to have it. But if not, we'll revert the add of this feature. I just prefer to check our options first.

@llogiq
Copy link
Contributor

llogiq commented May 30, 2019

@cramertj we have Option<_> results in checked_* arithmetic methods, in many iterator methods, in from_str and in the patterns API. In all of those cases, None would denote the path out. I don't see why using None as the failure case is even questioned, given the automatic error conversion of the Try trait.

@cramertj
Copy link
Member

I'm not confused by None being the "failure"-esque case, I'm confused by what we'd expect returning "None" from main to do. What status code would you expect that to return, and why? Would you expect an error message to be printed? It feels poorly-motivated to me.

@@ -1625,6 +1625,17 @@ impl<E: fmt::Debug> Termination for Result<(), E> {
}
}

#[unstable(feature = "termination_trait_lib", issue = "43301")]
impl Termination for Option<()> {
Copy link
Member

Choose a reason for hiding this comment

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

why only () rather than anything implementing Termination?

Copy link
Contributor

Choose a reason for hiding this comment

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

Would that flatten the Option? I'm afraid this could lead to too clever code like fn main -> Option<Option<Result<(), _>>> { .. }.

@llogiq
Copy link
Contributor

llogiq commented May 31, 2019

I'd expect None returning the canonical failure code, which is 1 on most unixoid systems. And since we have no suitable error message to print, I'd expect no error message.

@GuillaumeGomez GuillaumeGomez force-pushed the impl-termination-option branch from 63a8cd1 to 24c1b73 Compare May 31, 2019 10:58
@GuillaumeGomez
Copy link
Member Author

@llogiq So the current code (with the update) is how you would do it? That's convenient. :)

What the others are thinking about it?

@scottmcm
Copy link
Member

scottmcm commented Jun 1, 2019

We had a conversation about specific impls and such in the first stabilization issue, #48453, and the RFC (https://github.com/rust-lang/rfcs/blob/master/text/1937-ques-in-main.md) had Result but not Option. Is there new information that makes Option a good candidate for a main function?

I, of course, agree with @ollie27's comment 😄

Additionally, I don't want Option<impl Termination>, the same way I didn't want Result<impl Termination, _> (which I in fact explicitly removed in #48497).

And since we have no suitable error message to print,

I consider that a good reason to not implement Termination here.

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Jun 6, 2019

We discussed this in the @rust-lang/lang team meeting today, though notably @scottmcm was not present. The consensus was that we were "not opposed" (some mildly in favor, some neutral, some mildly opposed but not enough to block).

Speaking personally, I found the idea of returning Option<()> in main uncompelling, but using it in tests made a lot of sense to me. And in that setting, it seems natural that having an "early return" from foo? ought to be considered a test failure (else you would write assert!(x.is_none()) or something).

Ultimately, we decided we'd rather defer the final details to the @rust-lang/libs team. If someone from that team wants to move to FCP, it would be approved by the lang team (modulo the fact that not everyone was present). But if they have doubts and would prefer to wait, that seems ok.

@dtolnay
Copy link
Member

dtolnay commented Jun 6, 2019

I would prefer not to do this. I agree with Niko that in main this is not compelling. Regarding tests I would add:

using it in tests made a lot of sense to me. And in that setting, it seems natural that having an "early return" from foo? ought to be considered a test failure (else you would write assert!(x.is_none()) or something).

Nope, just use unwrap in tests. Then you even get to find out which step failed. Writing Some(()) at the bottom of the test to mean success is not great.

The main justifiable use case I see for this impl is in doc tests with a hidden function signature. These allow the doc test to be rendered by rustdoc with ? the way the user might see it in their own code:

/// ```
/// # fn main() -> Option<()> {
/// let x = f()?;
/// let y = g(x)?;
/// # Some(())
/// # }
/// ```

But for these it seems fine to recommend something like:

/// ```
/// # try {
/// let x = f()?;
/// let y = g(x)?;
/// # }.unwrap()
/// ```

@llogiq
Copy link
Contributor

llogiq commented Jun 7, 2019

@dtolnay in current nightly, you can just add Some(()) and use ? and rustdoc will insert the missing bits even without Termination (because that's not available in core and we want to be able to test no_std code). So I guess I'm with Niko in not finding a compelling use for Option-Termination.

@QuietMisdreavus
Copy link
Member

I'm late to the party, but if the libs team decides to close this, i would prefer #61279 be reverted. I left a longer comment over there: #61279 (comment)

@GuillaumeGomez
Copy link
Member Author

I'm late to the party, but if the libs team decides to close this, i would prefer #61279 be reverted.

👍

@Centril
Copy link
Contributor

Centril commented Jun 30, 2019

I'm late to the party, but if the libs team decides to close this, i would prefer #61279 be reverted.

👍

@QuietMisdreavus @GuillaumeGomez Should we revert #61279 until such time that an affirmative decision to support Termination for Option<()> is made?

@GuillaumeGomez
Copy link
Member Author

Fine by me.

bors added a commit that referenced this pull request Jul 1, 2019
…r=Centril

Revert "implicit `Option`-returning doctests"

Reverts #61279 as discussed in #61360.

cc @Centril
@Centril Centril modified the milestones: 1.37, 1.38 Jul 2, 2019
@Dylan-DPC-zz
Copy link

Dylan-DPC-zz commented Jul 15, 2019

Closing this based on #61360 (comment).

Let's terminate the termination
- Guillaume Gomez

@QuietMisdreavus
Copy link
Member

My comment was more of an "if this gets closed" - i was not calling for its closure myself, just deferring to libs team, which was pinged up-thread as the responsible party.

@Centril Centril removed this from the 1.38 milestone Jul 26, 2019
@Centril Centril removed the relnotes Marks issues that should be documented in the release notes of the next release. label Jul 26, 2019
@dtolnay dtolnay assigned dtolnay and unassigned withoutboats Mar 24, 2024
@GuillaumeGomez GuillaumeGomez deleted the impl-termination-option branch August 19, 2024 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.