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