rust/compiler/rustc_mir_transform/src
Nicholas Nethercote fa4f8925f1 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.
2024-08-30 08:18:41 +10:00
..
coroutine Fix projections when parent capture is by-ref 2024-08-14 13:24:07 -04:00
coverage Use more slice patterns inside the compiler 2024-08-07 13:37:52 +02:00
inline Reformat use declarations. 2024-07-29 08:26:52 +10:00
shim rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
abort_unwinding_calls.rs Shrink TyKind::FnPtr. 2024-08-09 14:33:25 +10:00
add_call_guards.rs remove redundant imports 2023-12-10 10:56:22 +08:00
add_moves_for_packed_drops.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
add_retag.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
add_subtyping_projections.rs remove redundant imports 2023-12-10 10:56:22 +08:00
check_alignment.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
check_const_item_mutation.rs Rename TyCtxt::emit_spanned_lint as TyCtxt::emit_node_span_lint. 2024-01-23 08:09:05 +11:00
check_packed_ref.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
cleanup_post_borrowck.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
copy_prop.rs Merge borrowed locals too. 2024-04-20 19:20:38 +00:00
coroutine.rs Simplify some redundant field names 2024-08-21 01:31:42 -04:00
cost_checker.rs Fix conflicts after rebase 2024-07-07 18:16:38 +02:00
cross_crate_inline.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
ctfe_limit.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
dataflow_const_prop.rs Remove Option<!> return types. 2024-08-30 08:18:41 +10:00
dead_store_elimination.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
deduce_param_attrs.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
deduplicate_blocks.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
deref_separator.rs remove redundant imports 2023-12-10 10:56:22 +08:00
dest_prop.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
dump_mir.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
early_otherwise_branch.rs Use more slice patterns inside the compiler 2024-08-07 13:37:52 +02:00
elaborate_box_derefs.rs Stop doing weird index stuff in ElaborateBoxDerefs 2024-08-02 17:45:55 -04:00
elaborate_drops.rs Stop using MoveDataParamEnv for places that don't need a param-env 2024-07-29 11:59:47 -04:00
errors.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
ffi_unwind_calls.rs Shrink TyKind::FnPtr. 2024-08-09 14:33:25 +10:00
function_item_references.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
gvn.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
inline.rs Add and use IndexVec::append 2024-08-13 13:40:05 -07:00
instsimplify.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
jump_threading.rs Remove Option<!> return types. 2024-08-30 08:18:41 +10:00
known_panics_lint.rs Remove Option<!> return types. 2024-08-30 08:18:41 +10:00
large_enums.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
lib.rs MIR required_consts, mentioned_items: ensure we do not forget to fill these lists 2024-08-01 15:49:25 +02:00
lint.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
lower_intrinsics.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
lower_slice_len.rs Replace NormalizeArrayLen with GVN 2024-06-20 22:16:59 -07:00
match_branches.rs Re-enable SimplifyToExp in match_branches. 2024-08-03 10:55:46 +08:00
mentioned_items.rs MIR required_consts, mentioned_items: ensure we do not forget to fill these lists 2024-08-01 15:49:25 +02:00
multiple_return_terminators.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
nrvo.rs Revert "Auto merge of #115105 - cjgillot:dest-prop-default, r=oli-obk" 2024-05-31 00:22:40 +00:00
pass_manager.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
prettify.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
promote_consts.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
ref_prop.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
remove_noop_landing_pads.rs Support tail calls in mir via TerminatorKind::TailCall 2024-07-07 17:11:04 +02:00
remove_place_mention.rs remove redundant imports 2023-12-10 10:56:22 +08:00
remove_storage_markers.rs Move condition enabling the pass to is_enabled 2024-01-23 20:58:44 +01:00
remove_uninit_drops.rs Stop using MoveDataParamEnv for places that don't need a param-env 2024-07-29 11:59:47 -04:00
remove_unneeded_drops.rs Merge dead bb pruning and unreachable bb deduplication. 2024-01-07 15:12:10 +00:00
remove_zsts.rs Add CoroutineClosure to TyKind, AggregateKind, UpvarArgs 2024-02-06 02:22:58 +00:00
required_consts.rs MIR required_consts, mentioned_items: ensure we do not forget to fill these lists 2024-08-01 15:49:25 +02:00
reveal_all.rs MIR visitor: constant -> const_operand 2024-06-13 15:37:13 +02:00
shim.rs Auto merge of #128812 - nnethercote:shrink-TyKind-FnPtr, r=compiler-errors 2024-08-14 00:56:53 +00:00
simplify_branches.rs simplify_branches: add comment 2024-03-24 12:53:03 +01:00
simplify_comparison_integral.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
simplify.rs When deduplicating unreachable blocks, erase the source information. 2024-08-03 21:23:35 -07:00
single_use_consts.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
sroa.rs Use is_lang_item more aggressively 2024-06-14 16:54:29 -04:00
ssa.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00
unreachable_enum_branching.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
unreachable_prop.rs Remove extern crate rustc_middle from rustc_mir_transform. 2024-05-13 08:20:18 +10:00
validate.rs rename AddressOf -> RawBorrow inside the compiler 2024-08-18 19:46:53 +02:00