Skip to content

Commit 295d4a2

Browse files
committed
Refactor never type behavior code
1 parent 5958f5e commit 295d4a2

File tree

1 file changed

+23
-8
lines changed
  • compiler/rustc_hir_typeck/src/fn_ctxt

1 file changed

+23
-8
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
123123
body_id: LocalDefId,
124124
) -> FnCtxt<'a, 'tcx> {
125125
let (diverging_fallback_behavior, diverging_block_behavior) =
126-
parse_never_type_options_attr(root_ctxt.tcx);
126+
never_type_behavior(root_ctxt.tcx);
127127
FnCtxt {
128128
body_id,
129129
param_env,
@@ -385,9 +385,30 @@ impl<'tcx> LoweredTy<'tcx> {
385385
}
386386
}
387387

388+
fn never_type_behavior(tcx: TyCtxt<'_>) -> (DivergingFallbackBehavior, DivergingBlockBehavior) {
389+
let (fallback, block) = parse_never_type_options_attr(tcx);
390+
let fallback = fallback.unwrap_or_else(|| default_fallback(tcx));
391+
let block = block.unwrap_or_default();
392+
393+
(fallback, block)
394+
}
395+
396+
/// Returns the default fallback which is used when there is no explicit override via `#![never_type_options(...)]`.
397+
fn default_fallback(tcx: TyCtxt<'_>) -> DivergingFallbackBehavior {
398+
use DivergingFallbackBehavior::*;
399+
400+
// `feature(never_type_fallback)`: fallback to `!` or `()` trying to not break stuff
401+
if tcx.features().never_type_fallback {
402+
return FallbackToNiko;
403+
}
404+
405+
// Otherwise: fallback to `()`
406+
FallbackToUnit
407+
}
408+
388409
fn parse_never_type_options_attr(
389410
tcx: TyCtxt<'_>,
390-
) -> (DivergingFallbackBehavior, DivergingBlockBehavior) {
411+
) -> (Option<DivergingFallbackBehavior>, Option<DivergingBlockBehavior>) {
391412
use DivergingFallbackBehavior::*;
392413

393414
// Error handling is dubious here (unwraps), but that's probably fine for an internal attribute.
@@ -437,11 +458,5 @@ fn parse_never_type_options_attr(
437458
);
438459
}
439460

440-
let fallback = fallback.unwrap_or_else(|| {
441-
if tcx.features().never_type_fallback { FallbackToNiko } else { FallbackToUnit }
442-
});
443-
444-
let block = block.unwrap_or_default();
445-
446461
(fallback, block)
447462
}

0 commit comments

Comments
 (0)