1
- use super :: { DefineOpaqueTypes , InferResult } ;
2
1
use crate :: errors:: OpaqueHiddenTypeDiag ;
3
2
use crate :: infer:: { InferCtxt , InferOk } ;
4
- use crate :: traits:: { self , PredicateObligation } ;
3
+ use crate :: traits:: { self , Obligation } ;
5
4
use hir:: def_id:: { DefId , LocalDefId } ;
6
5
use rustc_data_structures:: fx:: FxIndexMap ;
7
6
use rustc_data_structures:: sync:: Lrc ;
8
7
use rustc_hir as hir;
8
+ use rustc_middle:: traits:: solve:: Goal ;
9
9
use rustc_middle:: traits:: ObligationCause ;
10
10
use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
11
11
use rustc_middle:: ty:: fold:: BottomUpFolder ;
@@ -21,6 +21,8 @@ mod table;
21
21
pub type OpaqueTypeMap < ' tcx > = FxIndexMap < OpaqueTypeKey < ' tcx > , OpaqueTypeDecl < ' tcx > > ;
22
22
pub use table:: { OpaqueTypeStorage , OpaqueTypeTable } ;
23
23
24
+ use super :: DefineOpaqueTypes ;
25
+
24
26
/// Information about the opaque types whose values we
25
27
/// are inferring in this function (these are the `impl Trait` that
26
28
/// appear in the return type).
@@ -62,11 +64,23 @@ impl<'tcx> InferCtxt<'tcx> {
62
64
{
63
65
let def_span = self . tcx . def_span ( def_id) ;
64
66
let span = if span. contains ( def_span) { def_span } else { span } ;
65
- let code = traits:: ObligationCauseCode :: OpaqueReturnType ( None ) ;
66
- let cause = ObligationCause :: new ( span, body_id, code) ;
67
67
let ty_var = self . next_ty_var ( span) ;
68
68
obligations. extend (
69
- self . handle_opaque_type ( ty, ty_var, & cause, param_env) . unwrap ( ) . obligations ,
69
+ self . handle_opaque_type ( ty, ty_var, span, param_env)
70
+ . unwrap ( )
71
+ . into_iter ( )
72
+ . map ( |goal| {
73
+ Obligation :: new (
74
+ self . tcx ,
75
+ ObligationCause :: new (
76
+ span,
77
+ body_id,
78
+ traits:: ObligationCauseCode :: OpaqueReturnType ( None ) ,
79
+ ) ,
80
+ goal. param_env ,
81
+ goal. predicate ,
82
+ )
83
+ } ) ,
70
84
) ;
71
85
ty_var
72
86
}
@@ -80,17 +94,17 @@ impl<'tcx> InferCtxt<'tcx> {
80
94
& self ,
81
95
a : Ty < ' tcx > ,
82
96
b : Ty < ' tcx > ,
83
- cause : & ObligationCause < ' tcx > ,
97
+ span : Span ,
84
98
param_env : ty:: ParamEnv < ' tcx > ,
85
- ) -> InferResult < ' tcx , ( ) > {
99
+ ) -> Result < Vec < Goal < ' tcx , ty :: Predicate < ' tcx > > > , TypeError < ' tcx > > {
86
100
let process = |a : Ty < ' tcx > , b : Ty < ' tcx > | match * a. kind ( ) {
87
101
ty:: Alias ( ty:: Opaque , ty:: AliasTy { def_id, args, .. } ) if def_id. is_local ( ) => {
88
102
let def_id = def_id. expect_local ( ) ;
89
103
if self . intercrate {
90
104
// See comment on `insert_hidden_type` for why this is sufficient in coherence
91
105
return Some ( self . register_hidden_type (
92
106
OpaqueTypeKey { def_id, args } ,
93
- cause . clone ( ) ,
107
+ span ,
94
108
param_env,
95
109
b,
96
110
) ) ;
@@ -143,18 +157,13 @@ impl<'tcx> InferCtxt<'tcx> {
143
157
&& self . tcx . is_type_alias_impl_trait ( b_def_id)
144
158
{
145
159
self . tcx . dcx ( ) . emit_err ( OpaqueHiddenTypeDiag {
146
- span : cause . span ,
160
+ span,
147
161
hidden_type : self . tcx . def_span ( b_def_id) ,
148
162
opaque_type : self . tcx . def_span ( def_id) ,
149
163
} ) ;
150
164
}
151
165
}
152
- Some ( self . register_hidden_type (
153
- OpaqueTypeKey { def_id, args } ,
154
- cause. clone ( ) ,
155
- param_env,
156
- b,
157
- ) )
166
+ Some ( self . register_hidden_type ( OpaqueTypeKey { def_id, args } , span, param_env, b) )
158
167
}
159
168
_ => None ,
160
169
} ;
@@ -464,24 +473,23 @@ impl<'tcx> InferCtxt<'tcx> {
464
473
fn register_hidden_type (
465
474
& self ,
466
475
opaque_type_key : OpaqueTypeKey < ' tcx > ,
467
- cause : ObligationCause < ' tcx > ,
476
+ span : Span ,
468
477
param_env : ty:: ParamEnv < ' tcx > ,
469
478
hidden_ty : Ty < ' tcx > ,
470
- ) -> InferResult < ' tcx , ( ) > {
471
- let mut obligations = Vec :: new ( ) ;
479
+ ) -> Result < Vec < Goal < ' tcx , ty :: Predicate < ' tcx > > > , TypeError < ' tcx > > {
480
+ let mut goals = Vec :: new ( ) ;
472
481
473
- self . insert_hidden_type ( opaque_type_key, & cause , param_env, hidden_ty, & mut obligations ) ?;
482
+ self . insert_hidden_type ( opaque_type_key, span , param_env, hidden_ty, & mut goals ) ?;
474
483
475
484
self . add_item_bounds_for_hidden_type (
476
485
opaque_type_key. def_id . to_def_id ( ) ,
477
486
opaque_type_key. args ,
478
- cause,
479
487
param_env,
480
488
hidden_ty,
481
- & mut obligations ,
489
+ & mut goals ,
482
490
) ;
483
491
484
- Ok ( InferOk { value : ( ) , obligations } )
492
+ Ok ( goals )
485
493
}
486
494
487
495
/// Insert a hidden type into the opaque type storage, making sure
@@ -507,38 +515,35 @@ impl<'tcx> InferCtxt<'tcx> {
507
515
pub fn insert_hidden_type (
508
516
& self ,
509
517
opaque_type_key : OpaqueTypeKey < ' tcx > ,
510
- cause : & ObligationCause < ' tcx > ,
518
+ span : Span ,
511
519
param_env : ty:: ParamEnv < ' tcx > ,
512
520
hidden_ty : Ty < ' tcx > ,
513
- obligations : & mut Vec < PredicateObligation < ' tcx > > ,
521
+ goals : & mut Vec < Goal < ' tcx , ty :: Predicate < ' tcx > > > ,
514
522
) -> Result < ( ) , TypeError < ' tcx > > {
515
523
// Ideally, we'd get the span where *this specific `ty` came
516
524
// from*, but right now we just use the span from the overall
517
525
// value being folded. In simple cases like `-> impl Foo`,
518
526
// these are the same span, but not in cases like `-> (impl
519
527
// Foo, impl Bar)`.
520
- let span = cause. span ;
521
528
if self . intercrate {
522
529
// During intercrate we do not define opaque types but instead always
523
530
// force ambiguity unless the hidden type is known to not implement
524
531
// our trait.
525
- obligations. push ( traits:: Obligation :: new (
526
- self . tcx ,
527
- cause. clone ( ) ,
528
- param_env,
529
- ty:: PredicateKind :: Ambiguous ,
530
- ) )
532
+ goals. push ( Goal :: new ( self . tcx , param_env, ty:: PredicateKind :: Ambiguous ) )
531
533
} else {
532
534
let prev = self
533
535
. inner
534
536
. borrow_mut ( )
535
537
. opaque_types ( )
536
538
. register ( opaque_type_key, OpaqueHiddenType { ty : hidden_ty, span } ) ;
537
539
if let Some ( prev) = prev {
538
- obligations . extend (
539
- self . at ( cause , param_env)
540
+ goals . extend (
541
+ self . at ( & ObligationCause :: dummy_with_span ( span ) , param_env)
540
542
. eq ( DefineOpaqueTypes :: Yes , prev, hidden_ty) ?
541
- . obligations ,
543
+ . obligations
544
+ . into_iter ( )
545
+ // FIXME: Shuttling between obligations and goals is awkward.
546
+ . map ( Goal :: from) ,
542
547
) ;
543
548
}
544
549
} ;
@@ -550,10 +555,9 @@ impl<'tcx> InferCtxt<'tcx> {
550
555
& self ,
551
556
def_id : DefId ,
552
557
args : ty:: GenericArgsRef < ' tcx > ,
553
- cause : ObligationCause < ' tcx > ,
554
558
param_env : ty:: ParamEnv < ' tcx > ,
555
559
hidden_ty : Ty < ' tcx > ,
556
- obligations : & mut Vec < PredicateObligation < ' tcx > > ,
560
+ goals : & mut Vec < Goal < ' tcx , ty :: Predicate < ' tcx > > > ,
557
561
) {
558
562
let tcx = self . tcx ;
559
563
// Require that the hidden type is well-formed. We have to
@@ -567,12 +571,7 @@ impl<'tcx> InferCtxt<'tcx> {
567
571
// type during MIR borrowck, causing us to infer the wrong
568
572
// lifetime for its member constraints which then results in
569
573
// unexpected region errors.
570
- obligations. push ( traits:: Obligation :: new (
571
- tcx,
572
- cause. clone ( ) ,
573
- param_env,
574
- ty:: ClauseKind :: WellFormed ( hidden_ty. into ( ) ) ,
575
- ) ) ;
574
+ goals. push ( Goal :: new ( tcx, param_env, ty:: ClauseKind :: WellFormed ( hidden_ty. into ( ) ) ) ) ;
576
575
577
576
let item_bounds = tcx. explicit_item_bounds ( def_id) ;
578
577
for ( predicate, _) in item_bounds. iter_instantiated_copied ( tcx, args) {
@@ -588,13 +587,18 @@ impl<'tcx> InferCtxt<'tcx> {
588
587
&& !tcx. is_impl_trait_in_trait ( projection_ty. def_id )
589
588
&& !self . next_trait_solver ( ) =>
590
589
{
591
- self . projection_ty_to_infer (
590
+ let ty_var = self . next_ty_var ( self . tcx . def_span ( projection_ty. def_id ) ) ;
591
+ goals. push ( Goal :: new (
592
+ self . tcx ,
592
593
param_env,
593
- projection_ty,
594
- cause. clone ( ) ,
595
- 0 ,
596
- obligations,
597
- )
594
+ ty:: PredicateKind :: Clause ( ty:: ClauseKind :: Projection (
595
+ ty:: ProjectionPredicate {
596
+ projection_term : projection_ty. into ( ) ,
597
+ term : ty_var. into ( ) ,
598
+ } ,
599
+ ) ) ,
600
+ ) ) ;
601
+ ty_var
598
602
}
599
603
// Replace all other mentions of the same opaque type with the hidden type,
600
604
// as the bounds must hold on the hidden type after all.
@@ -611,12 +615,7 @@ impl<'tcx> InferCtxt<'tcx> {
611
615
612
616
// Require that the predicate holds for the concrete type.
613
617
debug ! ( ?predicate) ;
614
- obligations. push ( traits:: Obligation :: new (
615
- self . tcx ,
616
- cause. clone ( ) ,
617
- param_env,
618
- predicate,
619
- ) ) ;
618
+ goals. push ( Goal :: new ( self . tcx , param_env, predicate) ) ;
620
619
}
621
620
}
622
621
}
0 commit comments