@@ -337,6 +337,26 @@ pub struct InferCtxt<'tcx> {
337
337
338
338
normalize_fn_sig_for_diagnostic :
339
339
Option < Lrc < dyn Fn ( & InferCtxt < ' tcx > , ty:: PolyFnSig < ' tcx > ) -> ty:: PolyFnSig < ' tcx > > > ,
340
+
341
+ /// During coherence we have to assume that other crates may add
342
+ /// additional impls which we currently don't know about.
343
+ ///
344
+ /// To deal with this evaluation should be conservative
345
+ /// and consider the possibility of impls from outside this crate.
346
+ /// This comes up primarily when resolving ambiguity. Imagine
347
+ /// there is some trait reference `$0: Bar` where `$0` is an
348
+ /// inference variable. If `intercrate` is true, then we can never
349
+ /// say for sure that this reference is not implemented, even if
350
+ /// there are *no impls at all for `Bar`*, because `$0` could be
351
+ /// bound to some type that in a downstream crate that implements
352
+ /// `Bar`.
353
+ ///
354
+ /// Outside of coherence we set this to false because we are only
355
+ /// interested in types that the user could actually have written.
356
+ /// In other words, we consider `$0: Bar` to be unimplemented if
357
+ /// there is no type that the user could *actually name* that
358
+ /// would satisfy it. This avoids crippling inference, basically.
359
+ pub intercrate : bool ,
340
360
}
341
361
342
362
/// See the `error_reporting` module for more details.
@@ -554,6 +574,7 @@ pub struct InferCtxtBuilder<'tcx> {
554
574
considering_regions : bool ,
555
575
normalize_fn_sig_for_diagnostic :
556
576
Option < Lrc < dyn Fn ( & InferCtxt < ' tcx > , ty:: PolyFnSig < ' tcx > ) -> ty:: PolyFnSig < ' tcx > > > ,
577
+ intercrate : bool ,
557
578
}
558
579
559
580
pub trait TyCtxtInferExt < ' tcx > {
@@ -567,6 +588,7 @@ impl<'tcx> TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
567
588
defining_use_anchor : DefiningAnchor :: Error ,
568
589
considering_regions : true ,
569
590
normalize_fn_sig_for_diagnostic : None ,
591
+ intercrate : false ,
570
592
}
571
593
}
572
594
}
@@ -583,6 +605,11 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
583
605
self
584
606
}
585
607
608
+ pub fn intercrate ( mut self ) -> Self {
609
+ self . intercrate = true ;
610
+ self
611
+ }
612
+
586
613
pub fn ignoring_regions ( mut self ) -> Self {
587
614
self . considering_regions = false ;
588
615
self
@@ -622,6 +649,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
622
649
defining_use_anchor,
623
650
considering_regions,
624
651
ref normalize_fn_sig_for_diagnostic,
652
+ intercrate,
625
653
} = * self ;
626
654
InferCtxt {
627
655
tcx,
@@ -641,6 +669,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
641
669
normalize_fn_sig_for_diagnostic : normalize_fn_sig_for_diagnostic
642
670
. as_ref ( )
643
671
. map ( |f| f. clone ( ) ) ,
672
+ intercrate,
644
673
}
645
674
}
646
675
}
0 commit comments