Update track_caller logic/lint after rebase

This commit is contained in:
Bryan Garza 2022-12-07 20:13:22 +00:00
parent e28a07a0a1
commit 2d060034f0
5 changed files with 52 additions and 37 deletions

View File

@ -656,34 +656,35 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ExprKind::Closure(c)
};
let track_caller = self
.attrs
.get(&outer_hir_id.local_id)
.map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
let hir_id = self.lower_node_id(closure_node_id);
if track_caller {
let unstable_span = self.mark_span_with_reason(
DesugaringKind::Async,
span,
self.allow_gen_future.clone(),
);
self.lower_attrs(
hir_id,
&[Attribute {
kind: AttrKind::Normal(ptr::P(NormalAttr {
item: AttrItem {
path: Path::from_ident(Ident::new(sym::track_caller, span)),
args: AttrArgs::Empty,
let unstable_span = self.mark_span_with_reason(
DesugaringKind::Async,
span,
self.allow_gen_future.clone(),
);
if self.tcx.features().closure_track_caller {
let track_caller = self
.attrs
.get(&outer_hir_id.local_id)
.map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
if track_caller {
self.lower_attrs(
hir_id,
&[Attribute {
kind: AttrKind::Normal(ptr::P(NormalAttr {
item: AttrItem {
path: Path::from_ident(Ident::new(sym::track_caller, span)),
args: AttrArgs::Empty,
tokens: None,
},
tokens: None,
},
tokens: None,
})),
id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(),
style: AttrStyle::Outer,
span: unstable_span,
}],
);
})),
id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(),
style: AttrStyle::Outer,
span: unstable_span,
}],
);
}
}
let generator = hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) };

View File

@ -1397,7 +1397,7 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
span: Span,
hir_id: HirId,
) {
if let HirFnKind::ItemFn(_, _, _) = fn_kind && fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller {
if fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller {
// Now, check if the function has the `#[track_caller]` attribute
let attrs = cx.tcx.hir().attrs(hir_id);
let maybe_track_caller = attrs.iter().find(|attr| attr.has_name(sym::track_caller));
@ -1407,12 +1407,20 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
attr.span,
fluent::lint_ungated_async_fn_track_caller,
|lint| {
lint.span_label(span, "this function will not propagate the caller location");
lint.span_label(
span,
"this function will not propagate the caller location",
);
if cx.tcx.sess.is_nightly_build() {
lint.span_suggestion(attr.span, fluent::suggestion, "closure_track_caller", Applicability::MachineApplicable);
lint.span_suggestion(
attr.span,
fluent::suggestion,
"closure_track_caller",
Applicability::MachineApplicable,
);
}
lint
}
},
);
}
}

View File

@ -219,6 +219,7 @@ late_lint_methods!(
// May Depend on constants elsewhere
UnusedBrokenConst: UnusedBrokenConst,
UnstableFeatures: UnstableFeatures,
UngatedAsyncFnTrackCaller: UngatedAsyncFnTrackCaller,
ArrayIntoIter: ArrayIntoIter::default(),
DropTraitConstraints: DropTraitConstraints,
TemporaryCStringAsPtr: TemporaryCStringAsPtr,

View File

@ -10,5 +10,15 @@ LL | | }
|
= note: `#[warn(ungated_async_fn_track_caller)]` on by default
warning: 1 warning emitted
warning: `#[track_caller]` on async functions is a no-op
--> $DIR/panic-track-caller.rs:62:5
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
LL | / async fn bar_assoc() {
LL | | panic!();
LL | | }
| |_____- this function will not propagate the caller location
warning: 2 warnings emitted

View File

@ -59,7 +59,7 @@ async fn foo_track_caller() {
struct Foo;
impl Foo {
#[track_caller]
#[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op
async fn bar_assoc() {
panic!();
}
@ -104,9 +104,4 @@ fn main() {
assert_eq!(panicked_at(|| block_on(foo_assoc())), 69);
#[cfg(nofeat)]
assert_eq!(panicked_at(|| block_on(foo_assoc())), 64);
#[cfg(feat)]
assert_eq!(panicked_at(|| block_on(foo_closure())), 76);
#[cfg(feat)]
assert_eq!(panicked_at(|| block_on(foo_closure())), 74);
}