diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-08-31 10:08:56 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-31 10:08:56 +0200 |
| commit | 44185520cf0b86e4c0b1fc0340076355baa316b8 (patch) | |
| tree | f8f4fbe2904b41decbb565217f4a97ec21b38606 /compiler/rustc_codegen_llvm/src/errors.rs | |
| parent | 9510beba4d3709722952c1e92d756968a4b9e349 (diff) | |
| parent | fa4f8925f1170ccd86bc242f8566b6a74f63be16 (diff) | |
| download | rust-44185520cf0b86e4c0b1fc0340076355baa316b8.tar.gz rust-44185520cf0b86e4c0b1fc0340076355baa316b8.zip | |
Rollup merge of #129724 - nnethercote:rm-Option-bang, r=fee1-dead
Remove `Option<!>` return types.
Several compiler functions have `Option<!>` for their return type. That's odd. The only valid return value is `None`, so why is this type used?
Because it lets you write certain patterns slightly more concisely. E.g. if you have these common patterns:
```
let Some(a) = f() else { return };
let Ok(b) = g() else { return };
```
you can shorten them to these:
```
let a = f()?;
let b = g().ok()?;
```
Huh.
An `Option` return type typically designates success/failure. How should I interpret the type signature of a function that always returns (i.e. doesn't panic), does useful work (modifying `&mut` arguments), and yet only ever fails? This idiom subverts the type system for a cute syntactic trick.
Furthermore, returning `Option<!>` from a function F makes things syntactically more convenient within F, but makes things worse at F's callsites. The callsites can themselves use `?` with F but should not, because they will get an unconditional early return, which is almost certainly not desirable. Instead the return value should be ignored. (Note that some of callsites of `process_operand`, `process_immedate`, `process_assign` actually do use `?`, though the early return doesn't matter in these cases because nothing of significance comes after those calls. Ugh.)
When I first saw this pattern I had no idea how to interpret it, and it took me several minutes of close reading to understand everything I've written above. I even started a Zulip thread about it to make sure I understood it properly. "Save a few characters by introducing types so weird that compiler devs have to discuss it on Zulip" feels like a bad trade-off to me. This commit replaces all the `Option<!>` return values and uses `else`/`return` (or something similar) to replace the relevant `?` uses. The result is slightly more verbose but much easier to understand.
r? ``````@cjgillot``````
Diffstat (limited to 'compiler/rustc_codegen_llvm/src/errors.rs')
0 files changed, 0 insertions, 0 deletions
