@@ -46,6 +46,7 @@ use rustc_middle::ty::GenericArgsRef;
4646use rustc_middle:: ty:: { self , EarlyBinder , PolyProjectionPredicate , ToPolyTraitRef , ToPredicate } ;
4747use rustc_middle:: ty:: { Ty , TyCtxt , TypeFoldable , TypeVisitableExt } ;
4848use rustc_span:: symbol:: sym;
49+ use rustc_span:: Symbol ;
4950
5051use std:: cell:: { Cell , RefCell } ;
5152use std:: cmp;
@@ -59,42 +60,46 @@ mod candidate_assembly;
5960mod confirmation;
6061
6162#[ derive( Clone , Debug , Eq , PartialEq , Hash ) ]
62- pub enum IntercrateAmbiguityCause {
63- DownstreamCrate { trait_desc : String , self_desc : Option < String > } ,
64- UpstreamCrateUpdate { trait_desc : String , self_desc : Option < String > } ,
65- ReservationImpl { message : String } ,
63+ pub enum IntercrateAmbiguityCause < ' tcx > {
64+ DownstreamCrate { trait_ref : ty :: TraitRef < ' tcx > , self_ty : Option < Ty < ' tcx > > } ,
65+ UpstreamCrateUpdate { trait_ref : ty :: TraitRef < ' tcx > , self_ty : Option < Ty < ' tcx > > } ,
66+ ReservationImpl { message : Symbol } ,
6667}
6768
68- impl IntercrateAmbiguityCause {
69+ impl < ' tcx > IntercrateAmbiguityCause < ' tcx > {
6970 /// Emits notes when the overlap is caused by complex intercrate ambiguities.
7071 /// See #23980 for details.
7172 pub fn add_intercrate_ambiguity_hint ( & self , err : & mut Diagnostic ) {
7273 err. note ( self . intercrate_ambiguity_hint ( ) ) ;
7374 }
7475
7576 pub fn intercrate_ambiguity_hint ( & self ) -> String {
76- match self {
77- IntercrateAmbiguityCause :: DownstreamCrate { trait_desc, self_desc } => {
78- let self_desc = if let Some ( ty) = self_desc {
79- format ! ( " for type `{ty}`" )
80- } else {
81- String :: new ( )
82- } ;
83- format ! ( "downstream crates may implement trait `{trait_desc}`{self_desc}" )
77+ with_no_trimmed_paths ! ( match self {
78+ IntercrateAmbiguityCause :: DownstreamCrate { trait_ref, self_ty } => {
79+ format!(
80+ "downstream crates may implement trait `{trait_desc}`{self_desc}" ,
81+ trait_desc = trait_ref. print_only_trait_path( ) ,
82+ self_desc = if let Some ( self_ty) = self_ty {
83+ format!( " for type `{self_ty}`" )
84+ } else {
85+ String :: new( )
86+ }
87+ )
8488 }
85- IntercrateAmbiguityCause :: UpstreamCrateUpdate { trait_desc, self_desc } => {
86- let self_desc = if let Some ( ty) = self_desc {
87- format ! ( " for type `{ty}`" )
88- } else {
89- String :: new ( )
90- } ;
89+ IntercrateAmbiguityCause :: UpstreamCrateUpdate { trait_ref, self_ty } => {
9190 format!(
9291 "upstream crates may add a new impl of trait `{trait_desc}`{self_desc} \
93- in future versions"
92+ in future versions",
93+ trait_desc = trait_ref. print_only_trait_path( ) ,
94+ self_desc = if let Some ( self_ty) = self_ty {
95+ format!( " for type `{self_ty}`" )
96+ } else {
97+ String :: new( )
98+ }
9499 )
95100 }
96- IntercrateAmbiguityCause :: ReservationImpl { message } => message. clone ( ) ,
97- }
101+ IntercrateAmbiguityCause :: ReservationImpl { message } => message. to_string ( ) ,
102+ } )
98103 }
99104}
100105
@@ -114,7 +119,7 @@ pub struct SelectionContext<'cx, 'tcx> {
114119 /// We don't do his until we detect a coherence error because it can
115120 /// lead to false overflow results (#47139) and because always
116121 /// computing it may negatively impact performance.
117- intercrate_ambiguity_causes : Option < FxIndexSet < IntercrateAmbiguityCause > > ,
122+ intercrate_ambiguity_causes : Option < FxIndexSet < IntercrateAmbiguityCause < ' tcx > > > ,
118123
119124 /// The mode that trait queries run in, which informs our error handling
120125 /// policy. In essence, canonicalized queries need their errors propagated
@@ -270,7 +275,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
270275 /// Gets the intercrate ambiguity causes collected since tracking
271276 /// was enabled and disables tracking at the same time. If
272277 /// tracking is not enabled, just returns an empty vector.
273- pub fn take_intercrate_ambiguity_causes ( & mut self ) -> FxIndexSet < IntercrateAmbiguityCause > {
278+ pub fn take_intercrate_ambiguity_causes ( & mut self ) -> FxIndexSet < IntercrateAmbiguityCause < ' tcx > > {
274279 assert ! ( self . is_intercrate( ) ) ;
275280 self . intercrate_ambiguity_causes . take ( ) . unwrap_or_default ( )
276281 }
@@ -428,19 +433,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
428433 ) ;
429434 if !trait_ref. references_error ( ) {
430435 let self_ty = trait_ref. self_ty ( ) ;
431- let ( trait_desc, self_desc) = with_no_trimmed_paths ! ( {
432- let trait_desc = trait_ref. print_only_trait_path( ) . to_string( ) ;
433- let self_desc =
434- self_ty. has_concrete_skeleton( ) . then( || self_ty. to_string( ) ) ;
435- ( trait_desc, self_desc)
436- } ) ;
436+ let self_ty = self_ty. has_concrete_skeleton ( ) . then ( || self_ty) ;
437437 let cause = if let Conflict :: Upstream = conflict {
438- IntercrateAmbiguityCause :: UpstreamCrateUpdate {
439- trait_desc,
440- self_desc,
441- }
438+ IntercrateAmbiguityCause :: UpstreamCrateUpdate { trait_ref, self_ty }
442439 } else {
443- IntercrateAmbiguityCause :: DownstreamCrate { trait_desc , self_desc }
440+ IntercrateAmbiguityCause :: DownstreamCrate { trait_ref , self_ty }
444441 } ;
445442 debug ! ( ?cause, "evaluate_stack: pushing cause" ) ;
446443 self . intercrate_ambiguity_causes . as_mut ( ) . unwrap ( ) . insert ( cause) ;
@@ -1451,18 +1448,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
14511448 if let ImplCandidate ( def_id) = candidate {
14521449 if let ty:: ImplPolarity :: Reservation = tcx. impl_polarity ( def_id) {
14531450 if let Some ( intercrate_ambiguity_clauses) = & mut self . intercrate_ambiguity_causes {
1454- let value = tcx
1451+ let message = tcx
14551452 . get_attr ( def_id, sym:: rustc_reservation_impl)
14561453 . and_then ( |a| a. value_str ( ) ) ;
1457- if let Some ( value ) = value {
1454+ if let Some ( message ) = message {
14581455 debug ! (
14591456 "filter_reservation_impls: \
14601457 reservation impl ambiguity on {:?}",
14611458 def_id
14621459 ) ;
14631460 intercrate_ambiguity_clauses. insert (
14641461 IntercrateAmbiguityCause :: ReservationImpl {
1465- message : value . to_string ( ) ,
1462+ message,
14661463 } ,
14671464 ) ;
14681465 }
0 commit comments