Skip to content

Commit

Permalink
Auto merge of rust-lang#134299 - RalfJung:remove-start, r=compiler-er…
Browse files Browse the repository at this point in the history
…rors

remove support for the (unstable) #[start] attribute

As explained by `@Noratrieb:`
`#[start]` should be deleted. It's nothing but an accidentally leaked implementation detail that's a not very useful mix between "portable" entrypoint logic and bad abstraction.

I think the way the stable user-facing entrypoint should work (and works today on stable) is pretty simple:
- `std`-using cross-platform programs should use `fn main()`. the compiler, together with `std`, will then ensure that code ends up at `main` (by having a platform-specific entrypoint that gets directed through `lang_start` in `std` to `main` - but that's just an implementation detail)
- `no_std` platform-specific programs should use `#![no_main]` and define their own platform-specific entrypoint symbol with `#[no_mangle]`, like `main`, `_start`, `WinMain` or `my_embedded_platform_wants_to_start_here`. most of them only support a single platform anyways, and need cfg for the different platform's ways of passing arguments or other things *anyways*

`#[start]` is in a super weird position of being neither of those two. It tries to pretend that it's cross-platform, but its signature is  a total lie. Those arguments are just stubbed out to zero on ~~Windows~~ wasm, for example. It also only handles the platform-specific entrypoints for a few platforms that are supported by `std`, like Windows or Unix-likes. `my_embedded_platform_wants_to_start_here` can't use it, and neither could a libc-less Linux program.
So we have an attribute that only works in some cases anyways, that has a signature that's a total lie (and a signature that, as I might want to add, has changed recently, and that I definitely would not be comfortable giving *any* stability guarantees on), and where there's a pretty easy way to get things working without it in the first place.

Note that this feature has **not** been RFCed in the first place.

*This comment was posted [in May](rust-lang#29633 (comment)) and so far nobody spoke up in that issue with a usecase that would require keeping the attribute.*

Closes rust-lang#29633

try-job: x86_64-gnu-nopt
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
try-job: test-various
  • Loading branch information
bors committed Jan 21, 2025
2 parents e7f1e42 + 759212c commit d22dcb9
Show file tree
Hide file tree
Showing 22 changed files with 37 additions and 185 deletions.
13 changes: 2 additions & 11 deletions tests/ui/borrow_as_ptr_no_std.fixed
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
#![warn(clippy::borrow_as_ptr)]
#![feature(lang_items, start, libc)]
#![no_std]
#![crate_type = "lib"]

#[clippy::msrv = "1.75"]
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
let val = 1;
let _p = core::ptr::addr_of!(val);

let mut val_mut = 1;
let _p_mut = core::ptr::addr_of_mut!(val_mut);
0
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
13 changes: 2 additions & 11 deletions tests/ui/borrow_as_ptr_no_std.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
#![warn(clippy::borrow_as_ptr)]
#![feature(lang_items, start, libc)]
#![no_std]
#![crate_type = "lib"]

#[clippy::msrv = "1.75"]
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
let val = 1;
let _p = &val as *const i32;

let mut val_mut = 1;
let _p_mut = &mut val_mut as *mut i32;
0
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
4 changes: 2 additions & 2 deletions tests/ui/borrow_as_ptr_no_std.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: borrow as raw pointer
--> tests/ui/borrow_as_ptr_no_std.rs:9:14
--> tests/ui/borrow_as_ptr_no_std.rs:8:14
|
LL | let _p = &val as *const i32;
| ^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of!(val)`
Expand All @@ -8,7 +8,7 @@ LL | let _p = &val as *const i32;
= help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`

error: borrow as raw pointer
--> tests/ui/borrow_as_ptr_no_std.rs:12:18
--> tests/ui/borrow_as_ptr_no_std.rs:11:18
|
LL | let _p_mut = &mut val_mut as *mut i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of_mut!(val_mut)`
Expand Down
13 changes: 2 additions & 11 deletions tests/ui/box_default_no_std.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(lang_items, start, libc)]
#![warn(clippy::box_default)]
#![no_std]
#![crate_type = "lib"]

pub struct NotBox<T> {
_value: T,
Expand All @@ -18,16 +18,7 @@ impl<T: Default> Default for NotBox<T> {
}
}

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
let _p = NotBox::new(isize::default());
0
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
13 changes: 2 additions & 11 deletions tests/ui/crashes/ice-7410.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@compile-flags: -Clink-arg=-nostartfiles
//@ignore-target: apple windows

#![feature(lang_items, start, libc)]
#![crate_type = "lib"]
#![no_std]
#![allow(clippy::if_same_then_else)]
#![allow(clippy::redundant_pattern_matching)]
Expand All @@ -15,18 +15,9 @@ impl Drop for S {
fn drop(&mut self) {}
}

#[start]
fn main(argc: isize, argv: *const *const u8) -> isize {
pub fn main(argc: isize, argv: *const *const u8) -> isize {
if let Some(_) = Some(S) {
} else {
}
0
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
32 changes: 0 additions & 32 deletions tests/ui/crate_level_checks/no_std_main_recursion.rs

This file was deleted.

3 changes: 1 addition & 2 deletions tests/ui/crate_level_checks/no_std_swap.fixed
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![no_std]
#![feature(lang_items, start, libc)]
#![crate_type = "lib"]

use core::panic::PanicInfo;

#[warn(clippy::all)]
fn main() {
pub fn main() {
let mut a = 42;
let mut b = 1337;

Expand Down
3 changes: 1 addition & 2 deletions tests/ui/crate_level_checks/no_std_swap.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![no_std]
#![feature(lang_items, start, libc)]
#![crate_type = "lib"]

use core::panic::PanicInfo;

#[warn(clippy::all)]
fn main() {
pub fn main() {
let mut a = 42;
let mut b = 1337;

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/crate_level_checks/no_std_swap.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this looks like you are trying to swap `a` and `b`
--> tests/ui/crate_level_checks/no_std_swap.rs:12:5
--> tests/ui/crate_level_checks/no_std_swap.rs:11:5
|
LL | / a = b;
... |
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/def_id_nocore.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ignore-target: apple

#![feature(no_core, lang_items, start)]
#![feature(no_core, lang_items)]
#![no_core]
#![allow(clippy::missing_safety_doc)]

Expand Down
20 changes: 2 additions & 18 deletions tests/ui/empty_loop_no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,11 @@
//@ignore-target: apple

#![warn(clippy::empty_loop)]
#![feature(lang_items, start, libc)]
#![crate_type = "lib"]
#![no_std]

use core::panic::PanicInfo;

#[start]
fn main(argc: isize, argv: *const *const u8) -> isize {
pub fn main(argc: isize, argv: *const *const u8) -> isize {
// This should trigger the lint
loop {}
//~^ ERROR: empty `loop {}` wastes CPU cycles
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
// This should NOT trigger the lint
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {
// This should also trigger the lint
loop {}
//~^ ERROR: empty `loop {}` wastes CPU cycles
}
12 changes: 2 additions & 10 deletions tests/ui/empty_loop_no_std.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: empty `loop {}` wastes CPU cycles
--> tests/ui/empty_loop_no_std.rs:13:5
--> tests/ui/empty_loop_no_std.rs:10:5
|
LL | loop {}
| ^^^^^^^
Expand All @@ -8,13 +8,5 @@ LL | loop {}
= note: `-D clippy::empty-loop` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::empty_loop)]`

error: empty `loop {}` wastes CPU cycles
--> tests/ui/empty_loop_no_std.rs:26:5
|
LL | loop {}
| ^^^^^^^
|
= help: you should either use `panic!()` or add a call pausing or sleeping the thread to the loop body

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

13 changes: 2 additions & 11 deletions tests/ui/floating_point_arithmetic_nostd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(lang_items, start)]
#![crate_type = "lib"]
#![warn(clippy::imprecise_flops)]
#![warn(clippy::suboptimal_flops)]
#![no_std]
Expand All @@ -17,15 +17,6 @@ fn fake_abs1(num: f64) -> f64 {
if num >= 0.0 { num } else { -num }
}

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
0
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
10 changes: 0 additions & 10 deletions tests/ui/missing_const_for_fn/cant_be_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//@aux-build:../auxiliary/proc_macros.rs

#![warn(clippy::missing_const_for_fn)]
#![feature(start)]
#![feature(type_alias_impl_trait)]

extern crate helper;
Expand Down Expand Up @@ -71,15 +70,6 @@ mod with_test_fn {
}
}

// Allowing on this function, because it would lint, which we don't want in this case.
// if we have `#[start]` and `#[test]` check `is_entrypoint_fn(cx, def_id.to_def_id())` is stopped
// working
#[allow(clippy::missing_const_for_fn)]
#[start]
fn init(num: isize, something: *const *const u8) -> isize {
1
}

trait Foo {
// This should not be suggested to be made const
// (rustc doesn't allow const trait methods)
Expand Down
13 changes: 2 additions & 11 deletions tests/ui/missing_spin_loop_no_std.fixed
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
#![warn(clippy::missing_spin_loop)]
#![feature(lang_items, start, libc)]
#![crate_type = "lib"]
#![no_std]

use core::sync::atomic::{AtomicBool, Ordering};

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
// This should trigger the lint
let b = AtomicBool::new(true);
// This should lint with `core::hint::spin_loop()`
while b.load(Ordering::Acquire) { core::hint::spin_loop() }
0
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
13 changes: 2 additions & 11 deletions tests/ui/missing_spin_loop_no_std.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
#![warn(clippy::missing_spin_loop)]
#![feature(lang_items, start, libc)]
#![crate_type = "lib"]
#![no_std]

use core::sync::atomic::{AtomicBool, Ordering};

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
// This should trigger the lint
let b = AtomicBool::new(true);
// This should lint with `core::hint::spin_loop()`
while b.load(Ordering::Acquire) {}
0
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
2 changes: 1 addition & 1 deletion tests/ui/missing_spin_loop_no_std.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: busy-waiting loop should at least have a spin loop hint
--> tests/ui/missing_spin_loop_no_std.rs:12:37
--> tests/ui/missing_spin_loop_no_std.rs:11:37
|
LL | while b.load(Ordering::Acquire) {}
| ^^ help: try: `{ core::hint::spin_loop() }`
Expand Down
7 changes: 4 additions & 3 deletions tests/ui/result_unit_error_no_std.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(lang_items, start, libc)]
#![feature(lang_items, libc)]
#![no_std]
#![no_main]
#![warn(clippy::result_unit_err)]

#[clippy::msrv = "1.80"]
Expand All @@ -12,8 +13,8 @@ pub fn returns_unit_error_lint() -> Result<u32, ()> {
Err(())
}

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
#[no_mangle]
extern "C" fn main(_argc: core::ffi::c_int, _argv: *const *const u8) -> core::ffi::c_int {
0
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/result_unit_error_no_std.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this returns a `Result<_, ()>`
--> tests/ui/result_unit_error_no_std.rs:11:1
--> tests/ui/result_unit_error_no_std.rs:12:1
|
LL | pub fn returns_unit_error_lint() -> Result<u32, ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
13 changes: 2 additions & 11 deletions tests/ui/zero_ptr_no_std.fixed
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
#![feature(lang_items, start, libc)]
#![crate_type = "lib"]
#![no_std]
#![deny(clippy::zero_ptr)]

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
let _ = core::ptr::null::<usize>();
let _ = core::ptr::null_mut::<f64>();
let _: *const u8 = core::ptr::null();
0
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
Loading

0 comments on commit d22dcb9

Please sign in to comment.