@@ -173,7 +173,7 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: DefId) {
173173 } ;
174174 check_associated_item ( tcx, trait_item. hir_id , trait_item. span , method_sig) ;
175175
176- // Prohibits applying `#[track_caller]` to trait methods
176+ // Prohibits applying `#[track_caller]` to trait decls
177177 for attr in & trait_item. attrs {
178178 if attr. check_name ( sym:: track_caller) {
179179 struct_span_err ! (
@@ -194,6 +194,31 @@ pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: DefId) {
194194 hir:: ImplItemKind :: Method ( ref sig, _) => Some ( sig) ,
195195 _ => None
196196 } ;
197+
198+ // Prohibits applying `#[track_caller]` to trait impls
199+ if method_sig. is_some ( ) {
200+ let track_caller_attr = impl_item. attrs . iter ( )
201+ . find ( |a| a. check_name ( sym:: track_caller) ) ;
202+ if let Some ( tc_attr) = track_caller_attr {
203+ let parent_hir_id = tcx. hir ( ) . get_parent_item ( hir_id) ;
204+ let containing_item = tcx. hir ( ) . expect_item ( parent_hir_id) ;
205+ let containing_impl_trait_ref = match & containing_item. kind {
206+ hir:: ItemKind :: Impl ( _, _, _, _, tr, _, _) => tr,
207+ _ => bug ! ( "parent of an ImplItem must be an Impl" ) ,
208+ } ;
209+
210+ // if the impl block this item is within is for a trait...
211+ if containing_impl_trait_ref. is_some ( ) {
212+ struct_span_err ! (
213+ tcx. sess,
214+ tc_attr. span,
215+ E0738 ,
216+ "`#[track_caller]` is not supported in traits yet."
217+ ) . emit ( ) ;
218+ }
219+ }
220+ }
221+
197222 check_associated_item ( tcx, impl_item. hir_id , impl_item. span , method_sig) ;
198223}
199224
0 commit comments