Skip to content

Commit d645a4c

Browse files
committed
Auto merge of rust-lang#148871 - WaffleLapkin:never-simplifications, r=lcnr
Remove context dependant `!` fallback ... and minor cleanup. r? lcnr
2 parents 88bd39b + 37ecc8e commit d645a4c

39 files changed

+298
-704
lines changed

compiler/rustc_feature/src/removed.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ declare_features! (
189189
Some("subsumed by `#![feature(allocator_internals)]`")),
190190
/// Allows use of unary negate on unsigned integers, e.g., -e for e: u8
191191
(removed, negate_unsigned, "1.0.0", Some(29645), None),
192+
/// Allows diverging expressions to fall back to `!` rather than `()`.
193+
(removed, never_type_fallback, "CURRENT_RUSTC_VERSION", Some(65992), Some("removed in favor of unconditional fallback"), 148871),
192194
/// Allows `#[no_coverage]` on functions.
193195
/// The feature was renamed to `coverage_attribute` and the attribute to `#[coverage(on|off)]`
194196
(removed, no_coverage, "1.74.0", Some(84605), Some("renamed to `coverage_attribute`"), 114656),

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,6 @@ declare_features! (
594594
(incomplete, never_patterns, "1.76.0", Some(118155)),
595595
/// Allows the `!` type. Does not imply 'exhaustive_patterns' (below) any more.
596596
(unstable, never_type, "1.13.0", Some(35121)),
597-
/// Allows diverging expressions to fall back to `!` rather than `()`.
598-
(unstable, never_type_fallback, "1.41.0", Some(65992)),
599597
/// Switch `..` syntax to use the new (`Copy + IntoIterator`) range types.
600598
(unstable, new_range, "1.86.0", Some(123741)),
601599
/// Allows `#![no_core]`.

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
265265
// so we can just make the hidden type be `!`.
266266
// For backwards compatibility reasons, we fall back to
267267
// `()` until we the diverging default is changed.
268-
EarlyBinder::bind(Ty::new_diverging_default(tcx))
268+
EarlyBinder::bind(tcx.types.unit)
269269
}
270270
}
271271
DefiningScopeKind::MirBorrowck => match tcx.mir_borrowck(owner_def_id) {

compiler/rustc_hir_typeck/src/fallback.rs

Lines changed: 51 additions & 245 deletions
Large diffs are not rendered by default.

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ pub(crate) struct FnCtxt<'a, 'tcx> {
115115

116116
pub(super) root_ctxt: &'a TypeckRootCtxt<'tcx>,
117117

118-
pub(super) fallback_has_occurred: Cell<bool>,
118+
/// True if a divirging inference variable has been set to `()`/`!` because
119+
/// of never type fallback. This is only used for diagnostics.
120+
pub(super) diverging_fallback_has_occurred: Cell<bool>,
119121

120122
pub(super) diverging_fallback_behavior: DivergingFallbackBehavior,
121123
pub(super) diverging_block_behavior: DivergingBlockBehavior,
@@ -153,7 +155,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
153155
by_id: Default::default(),
154156
}),
155157
root_ctxt,
156-
fallback_has_occurred: Cell::new(false),
158+
diverging_fallback_has_occurred: Cell::new(false),
157159
diverging_fallback_behavior,
158160
diverging_block_behavior,
159161
trait_ascriptions: Default::default(),
@@ -190,7 +192,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
190192
TypeErrCtxt {
191193
infcx: &self.infcx,
192194
typeck_results: Some(self.typeck_results.borrow()),
193-
fallback_has_occurred: self.fallback_has_occurred.get(),
195+
diverging_fallback_has_occurred: self.diverging_fallback_has_occurred.get(),
194196
normalize_fn_sig: Box::new(|fn_sig| {
195197
if fn_sig.has_escaping_bound_vars() {
196198
return fn_sig;
@@ -505,11 +507,6 @@ fn default_fallback(tcx: TyCtxt<'_>) -> DivergingFallbackBehavior {
505507
return DivergingFallbackBehavior::ToNever;
506508
}
507509

508-
// `feature(never_type_fallback)`: fallback to `!` or `()` trying to not break stuff
509-
if tcx.features().never_type_fallback() {
510-
return DivergingFallbackBehavior::ContextDependent;
511-
}
512-
513510
// Otherwise: fallback to `()`
514511
DivergingFallbackBehavior::ToUnit
515512
}
@@ -536,7 +533,6 @@ fn parse_never_type_options_attr(
536533
let mode = item.value_str().unwrap();
537534
match mode {
538535
sym::unit => fallback = Some(DivergingFallbackBehavior::ToUnit),
539-
sym::niko => fallback = Some(DivergingFallbackBehavior::ContextDependent),
540536
sym::never => fallback = Some(DivergingFallbackBehavior::ToNever),
541537
sym::no => fallback = Some(DivergingFallbackBehavior::NoFallback),
542538
_ => {

compiler/rustc_hir_typeck/src/typeck_root_ctxt.rs

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
use std::cell::{Cell, RefCell};
22
use std::ops::Deref;
33

4-
use rustc_data_structures::unord::{UnordMap, UnordSet};
4+
use rustc_data_structures::unord::UnordSet;
55
use rustc_hir::def_id::LocalDefId;
6-
use rustc_hir::{self as hir, HirId, HirIdMap, LangItem};
6+
use rustc_hir::{self as hir, HirId, HirIdMap};
77
use rustc_infer::infer::{InferCtxt, InferOk, OpaqueTypeStorageEntries, TyCtxtInferExt};
88
use rustc_middle::span_bug;
99
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypingMode};
1010
use rustc_span::Span;
1111
use rustc_span::def_id::LocalDefIdMap;
12-
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
13-
use rustc_trait_selection::traits::{
14-
self, FulfillmentError, PredicateObligation, TraitEngine, TraitEngineExt as _,
15-
};
16-
use tracing::{debug, instrument};
12+
use rustc_trait_selection::traits::{self, FulfillmentError, TraitEngine, TraitEngineExt as _};
13+
use tracing::instrument;
1714

1815
use super::callee::DeferredCallResolution;
1916

20-
#[derive(Debug, Default, Copy, Clone)]
21-
pub(crate) struct InferVarInfo {
22-
/// This is true if we identified that this Ty (`?T`) is found in a `?T: Foo`
23-
/// obligation, where:
24-
///
25-
/// * `Foo` is not `Sized`
26-
/// * `(): Foo` may be satisfied
27-
pub self_in_trait: bool,
28-
/// This is true if we identified that this Ty (`?T`) is found in a `<_ as
29-
/// _>::AssocType = ?T`
30-
pub output: bool,
31-
}
32-
3317
/// Data shared between a "typeck root" and its nested bodies,
3418
/// e.g. closures defined within the function. For example:
3519
/// ```ignore (illustrative)
@@ -83,8 +67,6 @@ pub(crate) struct TypeckRootCtxt<'tcx> {
8367
/// we record that type variable here. This is later used to inform
8468
/// fallback. See the `fallback` module for details.
8569
pub(super) diverging_type_vars: RefCell<UnordSet<Ty<'tcx>>>,
86-
87-
pub(super) infer_var_info: RefCell<UnordMap<ty::TyVid, InferVarInfo>>,
8870
}
8971

9072
impl<'tcx> Deref for TypeckRootCtxt<'tcx> {
@@ -119,7 +101,6 @@ impl<'tcx> TypeckRootCtxt<'tcx> {
119101
deferred_asm_checks: RefCell::new(Vec::new()),
120102
deferred_repeat_expr_checks: RefCell::new(Vec::new()),
121103
diverging_type_vars: RefCell::new(Default::default()),
122-
infer_var_info: RefCell::new(Default::default()),
123104
}
124105
}
125106

@@ -129,8 +110,6 @@ impl<'tcx> TypeckRootCtxt<'tcx> {
129110
span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
130111
}
131112

132-
self.update_infer_var_info(&obligation);
133-
134113
self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
135114
}
136115

@@ -147,46 +126,4 @@ impl<'tcx> TypeckRootCtxt<'tcx> {
147126
self.register_predicates(infer_ok.obligations);
148127
infer_ok.value
149128
}
150-
151-
fn update_infer_var_info(&self, obligation: &PredicateObligation<'tcx>) {
152-
let infer_var_info = &mut self.infer_var_info.borrow_mut();
153-
154-
// (*) binder skipped
155-
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(tpred)) =
156-
obligation.predicate.kind().skip_binder()
157-
&& let Some(ty) =
158-
self.shallow_resolve(tpred.self_ty()).ty_vid().map(|t| self.root_var(t))
159-
&& !self.tcx.is_lang_item(tpred.trait_ref.def_id, LangItem::Sized)
160-
{
161-
let new_self_ty = self.tcx.types.unit;
162-
163-
// Then construct a new obligation with Self = () added
164-
// to the ParamEnv, and see if it holds.
165-
let o = obligation.with(
166-
self.tcx,
167-
obligation.predicate.kind().rebind(
168-
// (*) binder moved here
169-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(
170-
tpred.with_replaced_self_ty(self.tcx, new_self_ty),
171-
)),
172-
),
173-
);
174-
// Don't report overflow errors. Otherwise equivalent to may_hold.
175-
if let Ok(result) = self.probe(|_| self.evaluate_obligation(&o))
176-
&& result.may_apply()
177-
{
178-
infer_var_info.entry(ty).or_default().self_in_trait = true;
179-
}
180-
}
181-
182-
if let ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) =
183-
obligation.predicate.kind().skip_binder()
184-
// If the projection predicate (Foo::Bar == X) has X as a non-TyVid,
185-
// we need to make it into one.
186-
&& let Some(vid) = predicate.term.as_type().and_then(|ty| ty.ty_vid())
187-
{
188-
debug!("infer_var_info: {:?}.output = true", vid);
189-
infer_var_info.entry(vid).or_default().output = true;
190-
}
191-
}
192129
}

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -878,11 +878,6 @@ impl<'tcx> Ty<'tcx> {
878878
Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, tcx.types.str_)
879879
}
880880

881-
#[inline]
882-
pub fn new_diverging_default(tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
883-
if tcx.features().never_type_fallback() { tcx.types.never } else { tcx.types.unit }
884-
}
885-
886881
// lang and diagnostic tys
887882

888883
fn new_generic_adt(tcx: TyCtxt<'tcx>, wrapper_def_id: DefId, ty_param: Ty<'tcx>) -> Ty<'tcx> {

compiler/rustc_trait_selection/src/error_reporting/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct TypeErrCtxt<'a, 'tcx> {
2121
pub infcx: &'a InferCtxt<'tcx>,
2222

2323
pub typeck_results: Option<std::cell::Ref<'a, ty::TypeckResults<'tcx>>>,
24-
pub fallback_has_occurred: bool,
24+
pub diverging_fallback_has_occurred: bool,
2525

2626
pub normalize_fn_sig: Box<dyn Fn(ty::PolyFnSig<'tcx>) -> ty::PolyFnSig<'tcx> + 'a>,
2727

@@ -36,7 +36,7 @@ impl<'tcx> InferCtxt<'tcx> {
3636
TypeErrCtxt {
3737
infcx: self,
3838
typeck_results: None,
39-
fallback_has_occurred: false,
39+
diverging_fallback_has_occurred: false,
4040
normalize_fn_sig: Box::new(|fn_sig| fn_sig),
4141
autoderef_steps: Box::new(|ty| {
4242
debug_assert!(false, "shouldn't be using autoderef_steps outside of typeck");

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
541541
// variable that used to fallback to `()` now falling back to `!`. Issue a
542542
// note informing about the change in behaviour.
543543
if leaf_trait_predicate.skip_binder().self_ty().is_never()
544-
&& self.fallback_has_occurred
544+
&& self.diverging_fallback_has_occurred
545545
{
546546
let predicate = leaf_trait_predicate.map_bound(|trait_pred| {
547547
trait_pred.with_replaced_self_ty(self.tcx, tcx.types.unit)
@@ -550,8 +550,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
550550
if self.predicate_may_hold(&unit_obligation) {
551551
err.note(
552552
"this error might have been caused by changes to \
553-
Rust's type-inference algorithm (see issue #48950 \
554-
<https://github.com/rust-lang/rust/issues/48950> \
553+
Rust's type-inference algorithm (see issue #148922 \
554+
<https://github.com/rust-lang/rust/issues/148922> \
555555
for more information)",
556556
);
557557
err.help("you might have intended to use the type `()` here instead");

tests/ui/binding/empty-types-in-patterns.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ run-pass
2+
//@ edition: 2024
23

3-
#![feature(never_type, never_type_fallback)]
4+
#![feature(never_type)]
45
#![feature(exhaustive_patterns)]
56

67
#![allow(unreachable_patterns)]

0 commit comments

Comments
 (0)