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

Clarify/fix formatting docs concerning fmt::Result/fmt::Error #35862

Merged
merged 3 commits into from
Aug 30, 2016

Conversation

Stebalien
Copy link
Contributor

  1. fmt::Result != io::Result<()>
  2. Formatters should only propagate errors, not return their own.

Confusion on reddit: https://www.reddit.com/r/rust/comments/4yorxr/is_implt_tostring_for_t_where_t_display_sized_a/

@rust-highfive
Copy link
Collaborator

r? @alexcrichton

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

@Stebalien
Copy link
Contributor Author

r? @steveklabnik

@briansmith
Copy link
Contributor

This kind of documentation is an indication that the API is designed wrongly. In particular, the user of the thing that implements Debug or whatever can pass a fmt: &mut Formatter that keeps track of whether it failed, and then query fmt after the operation to see if it had any failure. Thus, the fmt method should just not have a return type.

In some cases, the implementation of fmt has to choose between panicking and returning Err(fmt::Error). In general, I don't want my code to cause panics so I'm included to return Err(fmt::Error). The suggested documentation says this is not how it is intended to do, but it still isn't clear why it would be worse than calling panic!.

@Stebalien
Copy link
Contributor Author

@briansmith I agree it's not ideal but it's stable...

In particular, the user of the thing that implements Debug or whatever can pass a fmt: &mut Formatter that keeps track of whether it failed, and then query fmt after the operation to see if it had any failure. Thus, the fmt method should just not have a return type.

That's how it works currently. The point of the result is to tell caller that an error has occurred and that it should stop formatting. The actual error is stashed elsewhere. For example:

impl Display for Something {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Display::fmt(&self.message, f)?; // bail early on error
        Display::fmt(&":", f)?; // bail early on error
        Display::fmt(&self.detail, f)?;
    }
}

In some cases, the implementation of fmt has to choose between panicking and returning Err(fmt::Error). In general, I don't want my code to cause panics so I'm included to return Err(fmt::Error). The suggested documentation says this is not how it is intended to do, but it still isn't clear why it would be worse than calling panic!.

If it's a bug (programmer error or variant violation), it should panic; that's the point of panic. However, if your Display implementation can fail normally, you shouldn't be implementing Display; formatting is not a fallible operation (the documentation should probably explicitly mention this).

Also, in this case, the error will just be dropped on the floor in trait implementations like ToString.

Doing otherwise would break traits like `ToString`.
@Stebalien
Copy link
Contributor Author

@briansmith is the updated documentation clear now?

@briansmith
Copy link
Contributor

briansmith commented Aug 20, 2016

Yes, it is clearer. You might clarify it further by saying more of what you said in your comment above: "If any other kind of error could occur, that is a sign that Display and/or Debug shouldn't be implemented for the given type; regardless the function must not return fmt::Error in such cases; it may panic! instead."

@Stebalien
Copy link
Contributor Author

@briansmith now that I actually pushed the change I was asking about... any better?

@briansmith
Copy link
Contributor

Yes, it's clearer.

@GuillaumeGomez
Copy link
Member

👍

cc @steveklabnik

They're the same thing but it's better to keep the terminology consistent.
@steveklabnik
Copy link
Member

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Aug 25, 2016

📌 Commit c7d5f7e has been approved by steveklabnik

steveklabnik added a commit to steveklabnik/rust that referenced this pull request Aug 25, 2016
Clarify/fix formatting docs concerning fmt::Result/fmt::Error

1. `fmt::Result` != `io::Result<()>`
2. Formatters should only propagate errors, not return their own.

Confusion on reddit: https://www.reddit.com/r/rust/comments/4yorxr/is_implt_tostring_for_t_where_t_display_sized_a/
steveklabnik added a commit to steveklabnik/rust that referenced this pull request Aug 26, 2016
Clarify/fix formatting docs concerning fmt::Result/fmt::Error

1. `fmt::Result` != `io::Result<()>`
2. Formatters should only propagate errors, not return their own.

Confusion on reddit: https://www.reddit.com/r/rust/comments/4yorxr/is_implt_tostring_for_t_where_t_display_sized_a/
steveklabnik added a commit to steveklabnik/rust that referenced this pull request Aug 26, 2016
Clarify/fix formatting docs concerning fmt::Result/fmt::Error

1. `fmt::Result` != `io::Result<()>`
2. Formatters should only propagate errors, not return their own.

Confusion on reddit: https://www.reddit.com/r/rust/comments/4yorxr/is_implt_tostring_for_t_where_t_display_sized_a/
Manishearth added a commit to Manishearth/rust that referenced this pull request Aug 27, 2016
Clarify/fix formatting docs concerning fmt::Result/fmt::Error

1. `fmt::Result` != `io::Result<()>`
2. Formatters should only propagate errors, not return their own.

Confusion on reddit: https://www.reddit.com/r/rust/comments/4yorxr/is_implt_tostring_for_t_where_t_display_sized_a/
@bors
Copy link
Contributor

bors commented Aug 29, 2016

⌛ Testing commit c7d5f7e with merge d62f786...

@bors
Copy link
Contributor

bors commented Aug 29, 2016

💔 Test failed - auto-mac-64-opt-rustbuild

@arielb1
Copy link
Contributor

arielb1 commented Aug 29, 2016

@bors retry

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 29, 2016
Clarify/fix formatting docs concerning fmt::Result/fmt::Error

1. `fmt::Result` != `io::Result<()>`
2. Formatters should only propagate errors, not return their own.

Confusion on reddit: https://www.reddit.com/r/rust/comments/4yorxr/is_implt_tostring_for_t_where_t_display_sized_a/
bors added a commit that referenced this pull request Aug 29, 2016
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 30, 2016
Clarify/fix formatting docs concerning fmt::Result/fmt::Error

1. `fmt::Result` != `io::Result<()>`
2. Formatters should only propagate errors, not return their own.

Confusion on reddit: https://www.reddit.com/r/rust/comments/4yorxr/is_implt_tostring_for_t_where_t_display_sized_a/
bors added a commit that referenced this pull request Aug 30, 2016
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 30, 2016
Clarify/fix formatting docs concerning fmt::Result/fmt::Error

1. `fmt::Result` != `io::Result<()>`
2. Formatters should only propagate errors, not return their own.

Confusion on reddit: https://www.reddit.com/r/rust/comments/4yorxr/is_implt_tostring_for_t_where_t_display_sized_a/
bors added a commit that referenced this pull request Aug 30, 2016
@bors bors merged commit c7d5f7e into rust-lang:master Aug 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants