1
1
use crate :: infer:: canonical:: query_response;
2
- use crate :: infer:: { InferCtxt , InferOk } ;
2
+ use crate :: infer:: InferCtxt ;
3
3
use crate :: traits:: query:: type_op:: TypeOpOutput ;
4
4
use crate :: traits:: query:: Fallible ;
5
5
use crate :: traits:: ObligationCtxt ;
6
6
use rustc_infer:: infer:: region_constraints:: RegionConstraintData ;
7
+ use rustc_middle:: traits:: query:: NoSolution ;
7
8
use rustc_span:: source_map:: DUMMY_SP ;
8
9
9
10
use std:: fmt;
10
11
11
- pub struct CustomTypeOp < F , G > {
12
+ pub struct CustomTypeOp < F > {
12
13
closure : F ,
13
- description : G ,
14
+ description : & ' static str ,
14
15
}
15
16
16
- impl < F , G > CustomTypeOp < F , G > {
17
- pub fn new < ' tcx , R > ( closure : F , description : G ) -> Self
17
+ impl < F > CustomTypeOp < F > {
18
+ pub fn new < ' tcx , R > ( closure : F , description : & ' static str ) -> Self
18
19
where
19
- F : FnOnce ( & InferCtxt < ' tcx > ) -> Fallible < InferOk < ' tcx , R > > ,
20
- G : Fn ( ) -> String ,
20
+ F : FnOnce ( & ObligationCtxt < ' _ , ' tcx > ) -> Fallible < R > ,
21
21
{
22
22
CustomTypeOp { closure, description }
23
23
}
24
24
}
25
25
26
- impl < ' tcx , F , R : fmt:: Debug , G > super :: TypeOp < ' tcx > for CustomTypeOp < F , G >
26
+ impl < ' tcx , F , R : fmt:: Debug > super :: TypeOp < ' tcx > for CustomTypeOp < F >
27
27
where
28
- F : for <' a , ' cx > FnOnce ( & ' a InferCtxt < ' tcx > ) -> Fallible < InferOk < ' tcx , R > > ,
29
- G : Fn ( ) -> String ,
28
+ F : FnOnce ( & ObligationCtxt < ' _ , ' tcx > ) -> Fallible < R > ,
30
29
{
31
30
type Output = R ;
32
31
/// We can't do any custom error reporting for `CustomTypeOp`, so
@@ -41,24 +40,21 @@ where
41
40
info ! ( "fully_perform({:?})" , self ) ;
42
41
}
43
42
44
- Ok ( scrape_region_constraints ( infcx, || ( self . closure ) ( infcx ) ) ?. 0 )
43
+ Ok ( scrape_region_constraints ( infcx, self . closure ) ?. 0 )
45
44
}
46
45
}
47
46
48
- impl < F , G > fmt:: Debug for CustomTypeOp < F , G >
49
- where
50
- G : Fn ( ) -> String ,
51
- {
47
+ impl < F > fmt:: Debug for CustomTypeOp < F > {
52
48
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
53
- write ! ( f , "{}" , ( self . description) ( ) )
49
+ self . description . fmt ( f )
54
50
}
55
51
}
56
52
57
53
/// Executes `op` and then scrapes out all the "old style" region
58
54
/// constraints that result, creating query-region-constraints.
59
55
pub fn scrape_region_constraints < ' tcx , Op : super :: TypeOp < ' tcx , Output = R > , R > (
60
56
infcx : & InferCtxt < ' tcx > ,
61
- op : impl FnOnce ( ) -> Fallible < InferOk < ' tcx , R > > ,
57
+ op : impl FnOnce ( & ObligationCtxt < ' _ , ' tcx > ) -> Fallible < R > ,
62
58
) -> Fallible < ( TypeOpOutput < ' tcx , Op > , RegionConstraintData < ' tcx > ) > {
63
59
// During NLL, we expect that nobody will register region
64
60
// obligations **except** as part of a custom type op (and, at the
@@ -72,16 +68,20 @@ pub fn scrape_region_constraints<'tcx, Op: super::TypeOp<'tcx, Output = R>, R>(
72
68
pre_obligations,
73
69
) ;
74
70
75
- let InferOk { value, obligations } = infcx. commit_if_ok ( |_| op ( ) ) ?;
76
- let ocx = ObligationCtxt :: new ( infcx) ;
77
- ocx. register_obligations ( obligations) ;
78
- let errors = ocx. select_all_or_error ( ) ;
79
- if !errors. is_empty ( ) {
80
- infcx. tcx . sess . diagnostic ( ) . delay_span_bug (
81
- DUMMY_SP ,
82
- format ! ( "errors selecting obligation during MIR typeck: {:?}" , errors) ,
83
- ) ;
84
- }
71
+ let value = infcx. commit_if_ok ( |_| {
72
+ let ocx = ObligationCtxt :: new_in_snapshot ( infcx) ;
73
+ let value = op ( & ocx) ?;
74
+ let errors = ocx. select_all_or_error ( ) ;
75
+ if errors. is_empty ( ) {
76
+ Ok ( value)
77
+ } else {
78
+ infcx. tcx . sess . delay_span_bug (
79
+ DUMMY_SP ,
80
+ format ! ( "errors selecting obligation during MIR typeck: {:?}" , errors) ,
81
+ ) ;
82
+ Err ( NoSolution )
83
+ }
84
+ } ) ?;
85
85
86
86
let region_obligations = infcx. take_registered_region_obligations ( ) ;
87
87
let region_constraint_data = infcx. take_and_reset_region_constraints ( ) ;
0 commit comments