Skip to content

Commit

Permalink
Rename DivergingFallbackBehavior variants and don't use ::*
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed May 17, 2024
1 parent 0f63cd1 commit e79aafc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
15 changes: 7 additions & 8 deletions compiler/rustc_hir_typeck/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use rustc_span::{def_id::LocalDefId, Span};
#[derive(Copy, Clone)]
pub enum DivergingFallbackBehavior {
/// Always fallback to `()` (aka "always spontaneous decay")
FallbackToUnit,
ToUnit,
/// Sometimes fallback to `!`, but mainly fallback to `()` so that most of the crates are not broken.
FallbackToNiko,
ContextDependent,
/// Always fallback to `!` (which should be equivalent to never falling back + not making
/// never-to-any coercions unless necessary)
FallbackToNever,
ToNever,
/// Don't fallback at all
NoFallback,
}
Expand Down Expand Up @@ -403,13 +403,12 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
diverging_fallback.insert(diverging_ty, ty);
};

use DivergingFallbackBehavior::*;
match behavior {
FallbackToUnit => {
DivergingFallbackBehavior::ToUnit => {
debug!("fallback to () - legacy: {:?}", diverging_vid);
fallback_to(self.tcx.types.unit);
}
FallbackToNiko => {
DivergingFallbackBehavior::ContextDependent => {
if found_infer_var_info.self_in_trait && found_infer_var_info.output {
// This case falls back to () to ensure that the code pattern in
// tests/ui/never_type/fallback-closure-ret.rs continues to
Expand Down Expand Up @@ -445,14 +444,14 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
fallback_to(self.tcx.types.never);
}
}
FallbackToNever => {
DivergingFallbackBehavior::ToNever => {
debug!(
"fallback to ! - `rustc_never_type_mode = \"fallback_to_never\")`: {:?}",
diverging_vid
);
fallback_to(self.tcx.types.never);
}
NoFallback => {
DivergingFallbackBehavior::NoFallback => {
debug!(
"no fallback - `rustc_never_type_mode = \"no_fallback\"`: {:?}",
diverging_vid
Expand Down
18 changes: 7 additions & 11 deletions compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,27 +390,23 @@ fn never_type_behavior(tcx: TyCtxt<'_>) -> (DivergingFallbackBehavior, Diverging

/// Returns the default fallback which is used when there is no explicit override via `#![never_type_options(...)]`.
fn default_fallback(tcx: TyCtxt<'_>) -> DivergingFallbackBehavior {
use DivergingFallbackBehavior::*;

// Edition 2024: fallback to `!`
if tcx.sess.edition().at_least_rust_2024() {
return FallbackToNever;
return DivergingFallbackBehavior::ToNever;
}

// `feature(never_type_fallback)`: fallback to `!` or `()` trying to not break stuff
if tcx.features().never_type_fallback {
return FallbackToNiko;
return DivergingFallbackBehavior::ContextDependent;
}

// Otherwise: fallback to `()`
FallbackToUnit
DivergingFallbackBehavior::ToUnit
}

fn parse_never_type_options_attr(
tcx: TyCtxt<'_>,
) -> (Option<DivergingFallbackBehavior>, Option<DivergingBlockBehavior>) {
use DivergingFallbackBehavior::*;

// Error handling is dubious here (unwraps), but that's probably fine for an internal attribute.
// Just don't write incorrect attributes <3

Expand All @@ -426,10 +422,10 @@ fn parse_never_type_options_attr(
if item.has_name(sym::fallback) && fallback.is_none() {
let mode = item.value_str().unwrap();
match mode {
sym::unit => fallback = Some(FallbackToUnit),
sym::niko => fallback = Some(FallbackToNiko),
sym::never => fallback = Some(FallbackToNever),
sym::no => fallback = Some(NoFallback),
sym::unit => fallback = Some(DivergingFallbackBehavior::ToUnit),
sym::niko => fallback = Some(DivergingFallbackBehavior::ContextDependent),
sym::never => fallback = Some(DivergingFallbackBehavior::ToNever),
sym::no => fallback = Some(DivergingFallbackBehavior::NoFallback),
_ => {
tcx.dcx().span_err(item.span(), format!("unknown never type fallback mode: `{mode}` (supported: `unit`, `niko`, `never` and `no`)"));
}
Expand Down

0 comments on commit e79aafc

Please sign in to comment.