33use rustc_errors:: ErrorGuaranteed ;
44use rustc_hir as hir;
55use rustc_hir:: def:: { Namespace , Res } ;
6- use rustc_hir:: def_id:: DefId ;
6+ use rustc_hir:: def_id:: { DefId , LocalDefId } ;
77use rustc_hir:: intravisit:: Visitor ;
88use rustc_middle:: hir:: nested_filter;
99use rustc_middle:: traits:: ObligationCauseCode ;
1010use rustc_middle:: ty:: error:: ExpectedFound ;
1111use rustc_middle:: ty:: print:: RegionHighlightMode ;
1212use rustc_middle:: ty:: { self , TyCtxt , TypeVisitable } ;
13- use rustc_span:: Span ;
13+ use rustc_span:: { Span , sym } ;
1414use tracing:: debug;
1515
1616use crate :: error_reporting:: infer:: nice_region_error:: NiceRegionError ;
@@ -33,14 +33,21 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3333 _,
3434 ) = error. clone ( )
3535 && let ( Subtype ( sup_trace) , Subtype ( sub_trace) ) = ( & sup_origin, & sub_origin)
36- && let & ObligationCauseCode :: CompareImplItem { trait_item_def_id, .. } =
37- sub_trace. cause . code ( )
36+ && let & ObligationCauseCode :: CompareImplItem {
37+ trait_item_def_id, impl_item_def_id, ..
38+ } = sub_trace. cause . code ( )
3839 && sub_trace. values == sup_trace. values
3940 && let ValuePairs :: PolySigs ( ExpectedFound { expected, found } ) = sub_trace. values
4041 {
4142 // FIXME(compiler-errors): Don't like that this needs `Ty`s, but
4243 // all of the region highlighting machinery only deals with those.
43- let guar = self . emit_err ( var_origin. span ( ) , expected, found, trait_item_def_id) ;
44+ let guar = self . emit_err (
45+ var_origin. span ( ) ,
46+ expected,
47+ found,
48+ trait_item_def_id,
49+ impl_item_def_id,
50+ ) ;
4451 return Some ( guar) ;
4552 }
4653 None
@@ -52,6 +59,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5259 expected : ty:: PolyFnSig < ' tcx > ,
5360 found : ty:: PolyFnSig < ' tcx > ,
5461 trait_item_def_id : DefId ,
62+ impl_item_def_id : LocalDefId ,
5563 ) -> ErrorGuaranteed {
5664 let trait_sp = self . tcx ( ) . def_span ( trait_item_def_id) ;
5765
@@ -82,18 +90,31 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
8290
8391 let expected_highlight = HighlightBuilder :: build ( expected) ;
8492 let tcx = self . cx . tcx ;
85- let expected = Highlighted {
93+ let mut expected = Highlighted {
8694 highlight : expected_highlight,
8795 ns : Namespace :: TypeNS ,
8896 tcx,
8997 value : expected,
9098 }
9199 . to_string ( ) ;
100+ // Cannot look at parsed codegen attributes, as those don't get emitted while we don't
101+ // allow target_features on trait methods.
102+ if let Some ( trait_item_def_id) = trait_item_def_id. as_local ( ) {
103+ if tcx. has_attr ( trait_item_def_id, sym:: target_feature) {
104+ expected = format ! ( "#[target_features] {expected}" ) ;
105+ }
106+ }
92107 let found_highlight = HighlightBuilder :: build ( found) ;
93- let found =
108+ let mut found =
94109 Highlighted { highlight : found_highlight, ns : Namespace :: TypeNS , tcx, value : found }
95110 . to_string ( ) ;
96111
112+ // Cannot look at parsed codegen attributes, as those don't get emitted while we don't
113+ // allow target_features on trait impl methods.
114+ if tcx. has_attr ( impl_item_def_id, sym:: target_feature) {
115+ found = format ! ( "#[target_features] {found}" ) ;
116+ }
117+
97118 // Get the span of all the used type parameters in the method.
98119 let assoc_item = self . tcx ( ) . associated_item ( trait_item_def_id) ;
99120 let mut visitor = TypeParamSpanVisitor { tcx : self . tcx ( ) , types : vec ! [ ] } ;
0 commit comments