@@ -4,8 +4,19 @@ use rustc_data_structures::{
4
4
graph:: { iterate:: DepthFirstSearch , vec_graph:: VecGraph } ,
5
5
unord:: { UnordBag , UnordMap , UnordSet } ,
6
6
} ;
7
+ use rustc_hir:: def_id:: CRATE_DEF_ID ;
7
8
use rustc_infer:: infer:: { DefineOpaqueTypes , InferOk } ;
8
9
use rustc_middle:: ty:: { self , Ty } ;
10
+ use rustc_span:: sym;
11
+
12
+ enum DivergingFallbackBehavior {
13
+ /// Always fallback to `()` (aka "always spontaneous decay")
14
+ FallbackToUnit ,
15
+ /// Sometimes fallback to `!`, but mainly fallback to `()` so that most of the crates are not broken.
16
+ FallbackToNiko ,
17
+ /// Don't fallback at all
18
+ NoFallback ,
19
+ }
9
20
10
21
impl < ' tcx > FnCtxt < ' _ , ' tcx > {
11
22
/// Performs type inference fallback, setting `FnCtxt::fallback_has_occurred`
@@ -64,7 +75,9 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
64
75
return false ;
65
76
}
66
77
67
- let diverging_fallback = self . calculate_diverging_fallback ( & unresolved_variables) ;
78
+ let diverging_behavior = self . diverging_fallback_behavior ( ) ;
79
+ let diverging_fallback =
80
+ self . calculate_diverging_fallback ( & unresolved_variables, diverging_behavior) ;
68
81
69
82
// We do fallback in two passes, to try to generate
70
83
// better error messages.
@@ -78,6 +91,27 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
78
91
fallback_occurred
79
92
}
80
93
94
+ fn diverging_fallback_behavior ( & self ) -> DivergingFallbackBehavior {
95
+ let Some ( ( mode, span) ) = self
96
+ . tcx
97
+ . get_attr ( CRATE_DEF_ID , sym:: rustc_never_type_mode)
98
+ . map ( |attr| ( attr. value_str ( ) . unwrap ( ) , attr. span ) )
99
+ else {
100
+ return DivergingFallbackBehavior :: FallbackToUnit ;
101
+ } ;
102
+
103
+ match mode {
104
+ sym:: fallback_to_unit => DivergingFallbackBehavior :: FallbackToUnit ,
105
+ sym:: fallback_to_niko => DivergingFallbackBehavior :: FallbackToNiko ,
106
+ sym:: no_fallback => DivergingFallbackBehavior :: NoFallback ,
107
+ _ => {
108
+ self . tcx . dcx ( ) . span_err ( span, format ! ( "unknown never type mode: `{mode}` (supported: `fallback_to_unit`, `fallback_to_niko`, and `no_fallback`)" ) ) ;
109
+
110
+ DivergingFallbackBehavior :: FallbackToUnit
111
+ }
112
+ }
113
+ }
114
+
81
115
fn fallback_effects ( & self ) -> bool {
82
116
let unsolved_effects = self . unsolved_effects ( ) ;
83
117
@@ -232,6 +266,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
232
266
fn calculate_diverging_fallback (
233
267
& self ,
234
268
unresolved_variables : & [ Ty < ' tcx > ] ,
269
+ behavior : DivergingFallbackBehavior ,
235
270
) -> UnordMap < Ty < ' tcx > , Ty < ' tcx > > {
236
271
debug ! ( "calculate_diverging_fallback({:?})" , unresolved_variables) ;
237
272
@@ -345,39 +380,51 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
345
380
output : infer_var_infos. items ( ) . any ( |info| info. output ) ,
346
381
} ;
347
382
348
- if found_infer_var_info. self_in_trait && found_infer_var_info. output {
349
- // This case falls back to () to ensure that the code pattern in
350
- // tests/ui/never_type/fallback-closure-ret.rs continues to
351
- // compile when never_type_fallback is enabled.
352
- //
353
- // This rule is not readily explainable from first principles,
354
- // but is rather intended as a patchwork fix to ensure code
355
- // which compiles before the stabilization of never type
356
- // fallback continues to work.
357
- //
358
- // Typically this pattern is encountered in a function taking a
359
- // closure as a parameter, where the return type of that closure
360
- // (checked by `relationship.output`) is expected to implement
361
- // some trait (checked by `relationship.self_in_trait`). This
362
- // can come up in non-closure cases too, so we do not limit this
363
- // rule to specifically `FnOnce`.
364
- //
365
- // When the closure's body is something like `panic!()`, the
366
- // return type would normally be inferred to `!`. However, it
367
- // needs to fall back to `()` in order to still compile, as the
368
- // trait is specifically implemented for `()` but not `!`.
369
- //
370
- // For details on the requirements for these relationships to be
371
- // set, see the relationship finding module in
372
- // compiler/rustc_trait_selection/src/traits/relationships.rs.
373
- debug ! ( "fallback to () - found trait and projection: {:?}" , diverging_vid) ;
374
- diverging_fallback. insert ( diverging_ty, self . tcx . types . unit ) ;
375
- } else if can_reach_non_diverging {
376
- debug ! ( "fallback to () - reached non-diverging: {:?}" , diverging_vid) ;
377
- diverging_fallback. insert ( diverging_ty, self . tcx . types . unit ) ;
378
- } else {
379
- debug ! ( "fallback to ! - all diverging: {:?}" , diverging_vid) ;
380
- diverging_fallback. insert ( diverging_ty, Ty :: new_diverging_default ( self . tcx ) ) ;
383
+ use DivergingFallbackBehavior :: * ;
384
+ match behavior {
385
+ FallbackToUnit => {
386
+ debug ! ( "fallback to () - legacy: {:?}" , diverging_vid) ;
387
+ diverging_fallback. insert ( diverging_ty, self . tcx . types . unit ) ;
388
+ }
389
+ FallbackToNiko => {
390
+ if found_infer_var_info. self_in_trait && found_infer_var_info. output {
391
+ // This case falls back to () to ensure that the code pattern in
392
+ // tests/ui/never_type/fallback-closure-ret.rs continues to
393
+ // compile when never_type_fallback is enabled.
394
+ //
395
+ // This rule is not readily explainable from first principles,
396
+ // but is rather intended as a patchwork fix to ensure code
397
+ // which compiles before the stabilization of never type
398
+ // fallback continues to work.
399
+ //
400
+ // Typically this pattern is encountered in a function taking a
401
+ // closure as a parameter, where the return type of that closure
402
+ // (checked by `relationship.output`) is expected to implement
403
+ // some trait (checked by `relationship.self_in_trait`). This
404
+ // can come up in non-closure cases too, so we do not limit this
405
+ // rule to specifically `FnOnce`.
406
+ //
407
+ // When the closure's body is something like `panic!()`, the
408
+ // return type would normally be inferred to `!`. However, it
409
+ // needs to fall back to `()` in order to still compile, as the
410
+ // trait is specifically implemented for `()` but not `!`.
411
+ //
412
+ // For details on the requirements for these relationships to be
413
+ // set, see the relationship finding module in
414
+ // compiler/rustc_trait_selection/src/traits/relationships.rs.
415
+ debug ! ( "fallback to () - found trait and projection: {:?}" , diverging_vid) ;
416
+ diverging_fallback. insert ( diverging_ty, self . tcx . types . unit ) ;
417
+ } else if can_reach_non_diverging {
418
+ debug ! ( "fallback to () - reached non-diverging: {:?}" , diverging_vid) ;
419
+ diverging_fallback. insert ( diverging_ty, self . tcx . types . unit ) ;
420
+ } else {
421
+ debug ! ( "fallback to ! - all diverging: {:?}" , diverging_vid) ;
422
+ diverging_fallback. insert ( diverging_ty, self . tcx . types . never ) ;
423
+ }
424
+ }
425
+ NoFallback => {
426
+ debug ! ( "no fallback - `rustc_never_type_mode = " no_fallback"`: {:?}" , diverging_vid) ;
427
+ }
381
428
}
382
429
}
383
430
0 commit comments