From 06dbd06e4deab2255d310d38ed0ea28becf43664 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 4 Aug 2020 07:30:04 +0200 Subject: [PATCH 1/2] forbid `#[track_caller]` on main --- src/librustc_typeck/lib.rs | 36 ++++++++++++++++++- .../rfc-2091-track-caller/error-with-main.rs | 4 +++ .../error-with-main.stderr | 12 +++++++ .../rfc-2091-track-caller/error-with-start.rs | 7 ++++ .../error-with-start.stderr | 12 +++++++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/rfc-2091-track-caller/error-with-main.rs create mode 100644 src/test/ui/rfc-2091-track-caller/error-with-main.stderr create mode 100644 src/test/ui/rfc-2091-track-caller/error-with-start.rs create mode 100644 src/test/ui/rfc-2091-track-caller/error-with-start.stderr diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 9ba2545ba63cb..016f68bfc6b3b 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -100,7 +100,7 @@ use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::util; use rustc_session::config::EntryFnType; -use rustc_span::{Span, DUMMY_SP}; +use rustc_span::{symbol::sym, Span, DUMMY_SP}; use rustc_target::spec::abi::Abi; use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _; use rustc_trait_selection::traits::{ @@ -194,6 +194,23 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: LocalDefId) { .emit(); error = true; } + + for attr in it.attrs { + if attr.check_name(sym::track_caller) { + tcx.sess + .struct_span_err( + attr.span, + "`main` function is not allowed to be `#[track_caller]`", + ) + .span_label( + main_span, + "`main` function is not allowed to be `#[track_caller]`", + ) + .emit(); + error = true; + } + } + if error { return; } @@ -274,6 +291,23 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: LocalDefId) { .emit(); error = true; } + + for attr in it.attrs { + if attr.check_name(sym::track_caller) { + tcx.sess + .struct_span_err( + attr.span, + "start is not allowed to be `#[track_caller]`", + ) + .span_label( + start_span, + "start is not allowed to be `#[track_caller]`", + ) + .emit(); + error = true; + } + } + if error { return; } diff --git a/src/test/ui/rfc-2091-track-caller/error-with-main.rs b/src/test/ui/rfc-2091-track-caller/error-with-main.rs new file mode 100644 index 0000000000000..b2ea31bb5178b --- /dev/null +++ b/src/test/ui/rfc-2091-track-caller/error-with-main.rs @@ -0,0 +1,4 @@ +#[track_caller] //~ ERROR `main` function is not allowed to be +fn main() { + panic!("{}: oh no", std::panic::Location::caller()); +} diff --git a/src/test/ui/rfc-2091-track-caller/error-with-main.stderr b/src/test/ui/rfc-2091-track-caller/error-with-main.stderr new file mode 100644 index 0000000000000..f05f88e7d71ea --- /dev/null +++ b/src/test/ui/rfc-2091-track-caller/error-with-main.stderr @@ -0,0 +1,12 @@ +error: `main` function is not allowed to be `#[track_caller]` + --> $DIR/error-with-main.rs:1:1 + | +LL | #[track_caller] + | ^^^^^^^^^^^^^^^ +LL | / fn main() { +LL | | panic!("{}: oh no", std::panic::Location::caller()); +LL | | } + | |_- `main` function is not allowed to be `#[track_caller]` + +error: aborting due to previous error + diff --git a/src/test/ui/rfc-2091-track-caller/error-with-start.rs b/src/test/ui/rfc-2091-track-caller/error-with-start.rs new file mode 100644 index 0000000000000..678cb7fa404f5 --- /dev/null +++ b/src/test/ui/rfc-2091-track-caller/error-with-start.rs @@ -0,0 +1,7 @@ +#![feature(start)] + +#[start] +#[track_caller] //~ ERROR start is not allowed to be `#[track_caller]` +fn start(_argc: isize, _argv: *const *const u8) -> isize { + panic!("{}: oh no", std::panic::Location::caller()); +} diff --git a/src/test/ui/rfc-2091-track-caller/error-with-start.stderr b/src/test/ui/rfc-2091-track-caller/error-with-start.stderr new file mode 100644 index 0000000000000..03b2ce5514f55 --- /dev/null +++ b/src/test/ui/rfc-2091-track-caller/error-with-start.stderr @@ -0,0 +1,12 @@ +error: start is not allowed to be `#[track_caller]` + --> $DIR/error-with-start.rs:4:1 + | +LL | #[track_caller] + | ^^^^^^^^^^^^^^^ +LL | / fn start(_argc: isize, _argv: *const *const u8) -> isize { +LL | | panic!("{}: oh no", std::panic::Location::caller()); +LL | | } + | |_- start is not allowed to be `#[track_caller]` + +error: aborting due to previous error + From 9127e27cec22cb130d0a96094196995d72b19030 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 4 Aug 2020 21:36:21 +0200 Subject: [PATCH 2/2] tweak error message --- src/librustc_typeck/lib.rs | 8 ++++---- src/test/ui/async-await/issue-68523-start.rs | 2 +- src/test/ui/async-await/issue-68523-start.stderr | 4 ++-- src/test/ui/rfc-2091-track-caller/error-with-start.rs | 2 +- src/test/ui/rfc-2091-track-caller/error-with-start.stderr | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 016f68bfc6b3b..e203d51f612aa 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -285,9 +285,9 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: LocalDefId) { tcx.sess, span, E0752, - "start is not allowed to be `async`" + "`start` is not allowed to be `async`" ) - .span_label(span, "start is not allowed to be `async`") + .span_label(span, "`start` is not allowed to be `async`") .emit(); error = true; } @@ -297,11 +297,11 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: LocalDefId) { tcx.sess .struct_span_err( attr.span, - "start is not allowed to be `#[track_caller]`", + "`start` is not allowed to be `#[track_caller]`", ) .span_label( start_span, - "start is not allowed to be `#[track_caller]`", + "`start` is not allowed to be `#[track_caller]`", ) .emit(); error = true; diff --git a/src/test/ui/async-await/issue-68523-start.rs b/src/test/ui/async-await/issue-68523-start.rs index 5988dffd68fa7..2ced88a16cc45 100644 --- a/src/test/ui/async-await/issue-68523-start.rs +++ b/src/test/ui/async-await/issue-68523-start.rs @@ -4,6 +4,6 @@ #[start] pub async fn start(_: isize, _: *const *const u8) -> isize { -//~^ ERROR start is not allowed to be `async` +//~^ ERROR `start` is not allowed to be `async` 0 } diff --git a/src/test/ui/async-await/issue-68523-start.stderr b/src/test/ui/async-await/issue-68523-start.stderr index e471945900e7d..3a0a3b5dece10 100644 --- a/src/test/ui/async-await/issue-68523-start.stderr +++ b/src/test/ui/async-await/issue-68523-start.stderr @@ -1,8 +1,8 @@ -error[E0752]: start is not allowed to be `async` +error[E0752]: `start` is not allowed to be `async` --> $DIR/issue-68523-start.rs:6:1 | LL | pub async fn start(_: isize, _: *const *const u8) -> isize { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ start is not allowed to be `async` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `start` is not allowed to be `async` error: aborting due to previous error diff --git a/src/test/ui/rfc-2091-track-caller/error-with-start.rs b/src/test/ui/rfc-2091-track-caller/error-with-start.rs index 678cb7fa404f5..0cab47170631b 100644 --- a/src/test/ui/rfc-2091-track-caller/error-with-start.rs +++ b/src/test/ui/rfc-2091-track-caller/error-with-start.rs @@ -1,7 +1,7 @@ #![feature(start)] #[start] -#[track_caller] //~ ERROR start is not allowed to be `#[track_caller]` +#[track_caller] //~ ERROR `start` is not allowed to be `#[track_caller]` fn start(_argc: isize, _argv: *const *const u8) -> isize { panic!("{}: oh no", std::panic::Location::caller()); } diff --git a/src/test/ui/rfc-2091-track-caller/error-with-start.stderr b/src/test/ui/rfc-2091-track-caller/error-with-start.stderr index 03b2ce5514f55..1a1f3e0449136 100644 --- a/src/test/ui/rfc-2091-track-caller/error-with-start.stderr +++ b/src/test/ui/rfc-2091-track-caller/error-with-start.stderr @@ -1,4 +1,4 @@ -error: start is not allowed to be `#[track_caller]` +error: `start` is not allowed to be `#[track_caller]` --> $DIR/error-with-start.rs:4:1 | LL | #[track_caller] @@ -6,7 +6,7 @@ LL | #[track_caller] LL | / fn start(_argc: isize, _argv: *const *const u8) -> isize { LL | | panic!("{}: oh no", std::panic::Location::caller()); LL | | } - | |_- start is not allowed to be `#[track_caller]` + | |_- `start` is not allowed to be `#[track_caller]` error: aborting due to previous error