Skip to content

Commit 13f68f4

Browse files
committed
adjust documentation
1 parent 1227d5d commit 13f68f4

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

text/0000-try-trait.md

+36-4
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,16 @@ This merits more investigation but need not hold up the RFC.
248248
# How We Teach This
249249
[how-we-teach-this]: #how-we-teach-this
250250

251+
### Where and how to document it
252+
251253
This RFC proposes extending an existing operator to permit the same
252-
general short-circuiting pattern to be used with more types. This more
253-
general behavior can be taught at the same time as the `?` operator is
254-
introduced more generally, as part of teaching how best to do error
255-
handling in Rust.
254+
general short-circuiting pattern to be used with more types. When
255+
initially teaching the `?` operator, it would probably be best to
256+
stick to examples around `Result`, so as to avoid confusing the
257+
issue. However, at that time we can also mention that `?` can be
258+
overloaded and offer a link to more comprehensive documentation, which
259+
would show how `?` can be applied to `Option` and then explain the
260+
desugaring and how one goes about implementing one's own impls.
256261

257262
The reference will have to be updated to include the new trait,
258263
naturally. The Rust book and Rust by example should be expanded to
@@ -264,6 +269,33 @@ impl and when it is not (i.e., expand on the concept of semantic
264269
equivalence used to justify why we do not permit `Option` to be
265270
interconverted with `Result` and so forth).
266271

272+
### Error messages
273+
274+
Another important factor is the error message when `?` is used in a
275+
function whose return type is not suitable. The current error message
276+
in this scenario is quite opaque and directly references the `Carrer`
277+
trait. A better message would be contingent on whether `?` is applied
278+
to a value that implements the `Try` trait (for any return type). If
279+
not, we can give a message like
280+
281+
> `?` cannot be applied to a value of type `Foo`
282+
283+
If, however, `?` *can* be applied to this type, but not with a function
284+
of the given return type, we can instead report something like this:
285+
286+
> cannot use the `?` operator in a function that returns `()`
287+
288+
or perhaps if we want to be more strictly correct:
289+
290+
> `?` cannot be applied to a `io::Result` in a function that returns `()`
291+
292+
We could also go further and analyze the type to which `?` is applied
293+
and figure out the set of legal return types for the function to
294+
have. So if the code is invoking `foo.write()?` (i.e., applying `?` to an
295+
`io::Result`), then we could offer a suggestion like "consider changing
296+
the return type to `Result<(), io::Error>`" or perhaps just "consider
297+
changing the return type to a `Result"`.
298+
267299
# Drawbacks
268300
[drawbacks]: #drawbacks
269301

0 commit comments

Comments
 (0)