Skip to content

Commit 9650a41

Browse files
committed
Improve code based on feedback.
This patch improves the readability of some of the code by using if-let-chains. Also, make use of the `add_feature_diagnostics` function.
1 parent f702e89 commit 9650a41

File tree

6 files changed

+38
-62
lines changed

6 files changed

+38
-62
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -662,29 +662,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
662662
span,
663663
self.allow_gen_future.clone(),
664664
);
665-
if self.tcx.features().closure_track_caller {
666-
let track_caller = self
667-
.attrs
668-
.get(&outer_hir_id.local_id)
669-
.map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
670-
if track_caller {
671-
self.lower_attrs(
672-
hir_id,
673-
&[Attribute {
674-
kind: AttrKind::Normal(ptr::P(NormalAttr {
675-
item: AttrItem {
676-
path: Path::from_ident(Ident::new(sym::track_caller, span)),
677-
args: AttrArgs::Empty,
678-
tokens: None,
679-
},
665+
666+
if self.tcx.features().closure_track_caller
667+
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
668+
&& attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))
669+
{
670+
self.lower_attrs(
671+
hir_id,
672+
&[Attribute {
673+
kind: AttrKind::Normal(ptr::P(NormalAttr {
674+
item: AttrItem {
675+
path: Path::from_ident(Ident::new(sym::track_caller, span)),
676+
args: AttrArgs::Empty,
680677
tokens: None,
681-
})),
682-
id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(),
683-
style: AttrStyle::Outer,
684-
span: unstable_span,
685-
}],
686-
);
687-
}
678+
},
679+
tokens: None,
680+
})),
681+
id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(),
682+
style: AttrStyle::Outer,
683+
span: unstable_span,
684+
}],
685+
);
688686
}
689687

690688
let generator = hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) };

compiler/rustc_error_messages/locales/en-US/lint.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ lint_builtin_mutable_transmutes =
351351
lint_builtin_unstable_features = unstable feature
352352
353353
lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op
354-
.suggestion = enable this unstable feature
354+
.label = this function will not propagate the caller location
355355
356356
lint_builtin_unreachable_pub = unreachable `pub` {$what}
357357
.suggestion = consider restricting its visibility

compiler/rustc_lint/src/builtin.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -1416,33 +1416,27 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
14161416
span: Span,
14171417
hir_id: HirId,
14181418
) {
1419-
if fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller {
1419+
if fn_kind.asyncness() == IsAsync::Async
1420+
&& !cx.tcx.features().closure_track_caller
1421+
&& let attrs = cx.tcx.hir().attrs(hir_id)
14201422
// Now, check if the function has the `#[track_caller]` attribute
1421-
let attrs = cx.tcx.hir().attrs(hir_id);
1422-
let maybe_track_caller = attrs.iter().find(|attr| attr.has_name(sym::track_caller));
1423-
if let Some(attr) = maybe_track_caller {
1423+
&& let Some(attr) = attrs.iter().find(|attr| attr.has_name(sym::track_caller))
1424+
{
14241425
cx.struct_span_lint(
14251426
UNGATED_ASYNC_FN_TRACK_CALLER,
14261427
attr.span,
14271428
fluent::lint_ungated_async_fn_track_caller,
14281429
|lint| {
1429-
lint.span_label(
1430-
span,
1431-
"this function will not propagate the caller location",
1430+
lint.span_label(span, fluent::label);
1431+
rustc_session::parse::add_feature_diagnostics(
1432+
lint,
1433+
&cx.tcx.sess.parse_sess,
1434+
sym::closure_track_caller,
14321435
);
1433-
if cx.tcx.sess.is_nightly_build() {
1434-
lint.span_suggestion(
1435-
attr.span,
1436-
fluent::suggestion,
1437-
"closure_track_caller",
1438-
Applicability::MachineApplicable,
1439-
);
1440-
}
14411436
lint
14421437
},
14431438
);
14441439
}
1445-
}
14461440
}
14471441
}
14481442

src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs

-9
This file was deleted.

src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr

-12
This file was deleted.

src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@ warning: `#[track_caller]` on async functions is a no-op
22
--> $DIR/panic-track-caller.rs:50:1
33
|
44
LL | #[track_caller]
5-
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
5+
| ^^^^^^^^^^^^^^^
66
LL | / async fn bar_track_caller() {
77
LL | | panic!()
88
LL | | }
99
| |_- this function will not propagate the caller location
1010
|
11+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
12+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
1113
= note: `#[warn(ungated_async_fn_track_caller)]` on by default
1214

1315
warning: `#[track_caller]` on async functions is a no-op
1416
--> $DIR/panic-track-caller.rs:62:5
1517
|
1618
LL | #[track_caller]
17-
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
19+
| ^^^^^^^^^^^^^^^
1820
LL | / async fn bar_assoc() {
1921
LL | | panic!();
2022
LL | | }
2123
| |_____- this function will not propagate the caller location
24+
|
25+
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
26+
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
2227

2328
warning: 2 warnings emitted
2429

0 commit comments

Comments
 (0)