Skip to content

Commit 190212c

Browse files
committed
Prohibit #[track_caller] within trait impls as well as decls.
1 parent f70ed29 commit 190212c

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/librustc_typeck/check/wfcheck.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -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

src/test/ui/rfc-2091-track-caller/error-with-trait-fn-impl.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | #![feature(track_caller)]
77
= note: `#[warn(incomplete_features)]` on by default
88

99
error[E0738]: `#[track_caller]` is not supported in traits yet.
10-
--> $DIR/error-with-trait-fn-impl.rs:4:5
10+
--> $DIR/error-with-trait-fn-impl.rs:8:5
1111
|
1212
LL | #[track_caller]
1313
| ^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)