Skip to content

Commit f89568b

Browse files
committed
add further notes to the doc section
1 parent 9af60a1 commit f89568b

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

text/0000-try-trait.md

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,44 @@ and figure out the set of legal return types for the function to
294294
have. So if the code is invoking `foo.write()?` (i.e., applying `?` to
295295
an `io::Result`), then we could offer a suggestion like "consider
296296
changing the return type to `Result<(), io::Error>`" or perhaps just
297-
"consider changing the return type to a `Result"`. Note however that
298-
if `?` is used within an impl of a trait method, or within `main()`,
299-
or in some other context where the user is not free to change the type
300-
signature, then we should make this suggestion. In the case of an impl
301-
of a trait defined in the current crate, we could consider suggesting
302-
that the user change the definition of the trait.
297+
"consider changing the return type to a `Result"`.
298+
299+
Note however that if `?` is used within an impl of a trait method, or
300+
within `main()`, or in some other context where the user is not free
301+
to change the type signature, then we should not make this
302+
suggestion. In the case of an impl of a trait defined in the current
303+
crate, we could consider suggesting that the user change the
304+
definition of the trait.
305+
306+
Especially in contexts where the return type cannot be changed, but
307+
possibly in other contexts as well, it would make sense to advise the
308+
user about how they can catch an error instead, if they chose. Once
309+
`catch` is implemented, this could be as simple as saying "consider
310+
introducing a `catch`, or changing the return type to ...". In the
311+
absence of `catch`, we would have to suggest the introduction of a
312+
`match` block.
313+
314+
In the extended error message, for those cases where the return type
315+
cannot easily be changed, we might consider suggesting that the
316+
fallible portion of the code is refactored into a helper function, thus
317+
roughly following this pattern:
318+
319+
```rust
320+
fn inner_main() -> Result<(), HLError> {
321+
let args = parse_cmdline()?;
322+
// all the real work here
323+
}
324+
325+
fn main() {
326+
process::exit(match inner_main() {
327+
Ok(_) => 0,
328+
Err(ref e) => {
329+
writeln!(io::stderr(), "{}", e).unwrap();
330+
1
331+
}
332+
});
333+
}
334+
```
303335

304336
On an implementation note, it would probably be helpful for improving
305337
the error message if `?` were not desugared when lowering from AST to

0 commit comments

Comments
 (0)