Skip to content

Commit 06dbd06

Browse files
committed
forbid #[track_caller] on main
1 parent d51b71a commit 06dbd06

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

src/librustc_typeck/lib.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ use rustc_middle::ty::query::Providers;
100100
use rustc_middle::ty::{self, Ty, TyCtxt};
101101
use rustc_middle::util;
102102
use rustc_session::config::EntryFnType;
103-
use rustc_span::{Span, DUMMY_SP};
103+
use rustc_span::{symbol::sym, Span, DUMMY_SP};
104104
use rustc_target::spec::abi::Abi;
105105
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
106106
use rustc_trait_selection::traits::{
@@ -194,6 +194,23 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: LocalDefId) {
194194
.emit();
195195
error = true;
196196
}
197+
198+
for attr in it.attrs {
199+
if attr.check_name(sym::track_caller) {
200+
tcx.sess
201+
.struct_span_err(
202+
attr.span,
203+
"`main` function is not allowed to be `#[track_caller]`",
204+
)
205+
.span_label(
206+
main_span,
207+
"`main` function is not allowed to be `#[track_caller]`",
208+
)
209+
.emit();
210+
error = true;
211+
}
212+
}
213+
197214
if error {
198215
return;
199216
}
@@ -274,6 +291,23 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: LocalDefId) {
274291
.emit();
275292
error = true;
276293
}
294+
295+
for attr in it.attrs {
296+
if attr.check_name(sym::track_caller) {
297+
tcx.sess
298+
.struct_span_err(
299+
attr.span,
300+
"start is not allowed to be `#[track_caller]`",
301+
)
302+
.span_label(
303+
start_span,
304+
"start is not allowed to be `#[track_caller]`",
305+
)
306+
.emit();
307+
error = true;
308+
}
309+
}
310+
277311
if error {
278312
return;
279313
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[track_caller] //~ ERROR `main` function is not allowed to be
2+
fn main() {
3+
panic!("{}: oh no", std::panic::Location::caller());
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: `main` function is not allowed to be `#[track_caller]`
2+
--> $DIR/error-with-main.rs:1:1
3+
|
4+
LL | #[track_caller]
5+
| ^^^^^^^^^^^^^^^
6+
LL | / fn main() {
7+
LL | | panic!("{}: oh no", std::panic::Location::caller());
8+
LL | | }
9+
| |_- `main` function is not allowed to be `#[track_caller]`
10+
11+
error: aborting due to previous error
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(start)]
2+
3+
#[start]
4+
#[track_caller] //~ ERROR start is not allowed to be `#[track_caller]`
5+
fn start(_argc: isize, _argv: *const *const u8) -> isize {
6+
panic!("{}: oh no", std::panic::Location::caller());
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: start is not allowed to be `#[track_caller]`
2+
--> $DIR/error-with-start.rs:4:1
3+
|
4+
LL | #[track_caller]
5+
| ^^^^^^^^^^^^^^^
6+
LL | / fn start(_argc: isize, _argv: *const *const u8) -> isize {
7+
LL | | panic!("{}: oh no", std::panic::Location::caller());
8+
LL | | }
9+
| |_- start is not allowed to be `#[track_caller]`
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)