@@ -8,12 +8,17 @@ use rustc_data_structures::{
88use rustc_hir as hir;
99use rustc_hir:: intravisit:: Visitor ;
1010use rustc_hir:: HirId ;
11- use rustc_infer:: infer:: { DefineOpaqueTypes , InferOk } ;
1211use rustc_middle:: bug;
12+ use rustc_infer:: {
13+ infer:: { DefineOpaqueTypes , InferOk } ,
14+ traits:: ObligationCause ,
15+ } ;
1316use rustc_middle:: ty:: { self , Ty , TyCtxt , TypeSuperVisitable , TypeVisitable } ;
1417use rustc_session:: lint;
1518use rustc_span:: DUMMY_SP ;
1619use rustc_span:: { def_id:: LocalDefId , Span } ;
20+ use rustc_trait_selection:: traits:: ObligationCtxt ;
21+ use rustc_type_ir:: TyVid ;
1722
1823#[ derive( Copy , Clone ) ]
1924pub enum DivergingFallbackBehavior {
@@ -344,6 +349,9 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
344349 // `!`.
345350 let mut diverging_fallback = UnordMap :: with_capacity ( diverging_vids. len ( ) ) ;
346351 let unsafe_infer_vars = OnceCell :: new ( ) ;
352+
353+ self . lint_obligations_broken_by_never_type_fallback_change ( behavior, & diverging_vids) ;
354+
347355 for & diverging_vid in & diverging_vids {
348356 let diverging_ty = Ty :: new_var ( self . tcx , diverging_vid) ;
349357 let root_vid = self . root_var ( diverging_vid) ;
@@ -468,6 +476,46 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
468476 }
469477 }
470478
479+ fn lint_obligations_broken_by_never_type_fallback_change (
480+ & self ,
481+ behavior : DivergingFallbackBehavior ,
482+ diverging_vids : & [ TyVid ] ,
483+ ) {
484+ let DivergingFallbackBehavior :: FallbackToUnit = behavior else { return } ;
485+
486+ // Returns errors which happen if fallback is set to `fallback`
487+ let try_out = |fallback| {
488+ self . probe ( |_| {
489+ let obligations = self . fulfillment_cx . borrow ( ) . pending_obligations ( ) ;
490+ let ocx = ObligationCtxt :: new ( & self . infcx ) ;
491+ ocx. register_obligations ( obligations. iter ( ) . cloned ( ) ) ;
492+
493+ for & diverging_vid in diverging_vids {
494+ let diverging_ty = Ty :: new_var ( self . tcx , diverging_vid) ;
495+
496+ _ = ocx. eq ( & ObligationCause :: dummy ( ) , self . param_env , diverging_ty, fallback) ;
497+ }
498+
499+ ocx. select_where_possible ( )
500+ } )
501+ } ;
502+
503+ // If we have no errors with `fallback = ()`, but *do* have errors with `fallback = !`,
504+ // then this code will be broken by the never type fallback change.qba
505+ let unit_errors = try_out ( self . tcx . types . unit ) ;
506+ if unit_errors. is_empty ( )
507+ && let never_errors = try_out ( self . tcx . types . never )
508+ && !never_errors. is_empty ( )
509+ {
510+ self . tcx . emit_node_span_lint (
511+ lint:: builtin:: DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK ,
512+ self . tcx . local_def_id_to_hir_id ( self . body_id ) ,
513+ self . tcx . def_span ( self . body_id ) ,
514+ errors:: DependencyOnUnitNeverTypeFallback { } ,
515+ )
516+ }
517+ }
518+
471519 /// Returns a graph whose nodes are (unresolved) inference variables and where
472520 /// an edge `?A -> ?B` indicates that the variable `?A` is coerced to `?B`.
473521 fn create_coercion_graph ( & self ) -> VecGraph < ty:: TyVid , true > {
0 commit comments