Skip to content

Remove the From derive macro from prelude #145563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ pub mod assert_matches {
pub use crate::macros::{assert_matches, debug_assert_matches};
}

#[unstable(feature = "derive_from", issue = "144889")]
/// Unstable module containing the unstable `From` derive macro.
pub mod from {
#[unstable(feature = "derive_from", issue = "144889")]
pub use crate::macros::builtin::From;
}

// We don't export this through #[macro_export] for now, to avoid breakage.
#[unstable(feature = "autodiff", issue = "124509")]
/// Unstable module containing the unstable `autodiff` macro.
Expand Down
7 changes: 0 additions & 7 deletions library/core/src/prelude/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,3 @@ pub use crate::macros::builtin::deref;
reason = "`type_alias_impl_trait` has open design concerns"
)]
pub use crate::macros::builtin::define_opaque;

#[unstable(
feature = "derive_from",
issue = "144889",
reason = "`derive(From)` is unstable"
)]
pub use crate::macros::builtin::From;
8 changes: 8 additions & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,14 @@ pub use core::{
unreachable, write, writeln,
};

// Re-export unstable derive macro defined through core.
#[unstable(feature = "derive_from", issue = "144889")]
/// Unstable module containing the unstable `From` derive macro.
pub mod from {
#[unstable(feature = "derive_from", issue = "144889")]
pub use core::from::From;
}

// Include a number of private modules that exist solely to provide
// the rustdoc documentation for primitive types. Using `include!`
// because rustdoc only looks for these modules at the crate level.
Expand Down
18 changes: 15 additions & 3 deletions tests/ui/deriving/deriving-all-codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#![allow(deprecated)]
#![feature(derive_from)]

use std::from::From;

// Empty struct.
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
struct Empty;
Expand Down Expand Up @@ -51,7 +53,14 @@ struct SingleField {
// `clone` implemention that just does `*self`.
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
struct Big {
b1: u32, b2: u32, b3: u32, b4: u32, b5: u32, b6: u32, b7: u32, b8: u32,
b1: u32,
b2: u32,
b3: u32,
b4: u32,
b5: u32,
b6: u32,
b7: u32,
b8: u32,
}

// It is more efficient to compare scalar types before non-scalar types.
Expand Down Expand Up @@ -126,7 +135,7 @@ enum Enum0 {}
// A single-variant enum.
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
enum Enum1 {
Single { x: u32 }
Single { x: u32 },
}

// A C-like, fieldless enum with a single variant.
Expand All @@ -152,7 +161,10 @@ enum Mixed {
P,
Q,
R(u32),
S { d1: Option<u32>, d2: Option<i32> },
S {
d1: Option<u32>,
d2: Option<i32>,
},
}

// When comparing enum variant it is more efficient to compare scalar types before non-scalar types.
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/deriving/deriving-all-codegen.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extern crate std;
#[prelude_import]
use std::prelude::rust_2021::*;

use std::from::From;

// Empty struct.
struct Empty;
#[automatically_derived]
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/deriving/deriving-from-wrong-target.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//@ edition: 2021
//@ check-fail

#![feature(derive_from)]
#![allow(dead_code)]

use std::from::From;

#[derive(From)]
//~^ ERROR `#[derive(From)]` used on a struct with no fields
struct S1;
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/deriving/deriving-from-wrong-target.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `#[derive(From)]` used on a struct with no fields
--> $DIR/deriving-from-wrong-target.rs:7:10
--> $DIR/deriving-from-wrong-target.rs:8:10
|
LL | #[derive(From)]
| ^^^^
Expand All @@ -10,7 +10,7 @@ LL | struct S1;
= note: `#[derive(From)]` can only be used on structs with exactly one field

error: `#[derive(From)]` used on a struct with no fields
--> $DIR/deriving-from-wrong-target.rs:11:10
--> $DIR/deriving-from-wrong-target.rs:12:10
|
LL | #[derive(From)]
| ^^^^
Expand All @@ -21,7 +21,7 @@ LL | struct S2 {}
= note: `#[derive(From)]` can only be used on structs with exactly one field

error: `#[derive(From)]` used on a struct with multiple fields
--> $DIR/deriving-from-wrong-target.rs:15:10
--> $DIR/deriving-from-wrong-target.rs:16:10
|
LL | #[derive(From)]
| ^^^^
Expand All @@ -32,7 +32,7 @@ LL | struct S3(u32, bool);
= note: `#[derive(From)]` can only be used on structs with exactly one field

error: `#[derive(From)]` used on a struct with multiple fields
--> $DIR/deriving-from-wrong-target.rs:19:10
--> $DIR/deriving-from-wrong-target.rs:20:10
|
LL | #[derive(From)]
| ^^^^
Expand All @@ -43,7 +43,7 @@ LL | struct S4 {
= note: `#[derive(From)]` can only be used on structs with exactly one field

error: `#[derive(From)]` used on an enum
--> $DIR/deriving-from-wrong-target.rs:26:10
--> $DIR/deriving-from-wrong-target.rs:27:10
|
LL | #[derive(From)]
| ^^^^
Expand All @@ -54,7 +54,7 @@ LL | enum E1 {}
= note: `#[derive(From)]` can only be used on structs with exactly one field

error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/deriving-from-wrong-target.rs:30:10
--> $DIR/deriving-from-wrong-target.rs:31:10
|
LL | #[derive(From)]
| ^^^^ doesn't have a size known at compile-time
Expand All @@ -71,7 +71,7 @@ LL + struct SUnsizedField<T> {
|

error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/deriving-from-wrong-target.rs:30:10
--> $DIR/deriving-from-wrong-target.rs:31:10
|
LL | #[derive(From)]
| ^^^^ doesn't have a size known at compile-time
Expand All @@ -80,7 +80,7 @@ LL | struct SUnsizedField<T: ?Sized> {
| - this type parameter needs to be `Sized`
|
note: required because it appears within the type `SUnsizedField<T>`
--> $DIR/deriving-from-wrong-target.rs:33:8
--> $DIR/deriving-from-wrong-target.rs:34:8
|
LL | struct SUnsizedField<T: ?Sized> {
| ^^^^^^^^^^^^^
Expand All @@ -92,7 +92,7 @@ LL + struct SUnsizedField<T> {
|

error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/deriving-from-wrong-target.rs:34:11
--> $DIR/deriving-from-wrong-target.rs:35:11
|
LL | struct SUnsizedField<T: ?Sized> {
| - this type parameter needs to be `Sized`
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/deriving/deriving-from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#![feature(derive_from)]

use core::from::From;

#[derive(From)]
struct TupleSimple(u32);

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/feature-gates/feature-gate-derive-from.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ edition: 2021
use std::from::From; //~ ERROR use of unstable library feature `derive_from

#[derive(From)] //~ ERROR use of unstable library feature `derive_from`
struct Foo(u32);
Expand Down
12 changes: 11 additions & 1 deletion tests/ui/feature-gates/feature-gate-derive-from.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ LL | #[derive(From)]
= help: add `#![feature(derive_from)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 1 previous error
error[E0658]: use of unstable library feature `derive_from`
--> $DIR/feature-gate-derive-from.rs:1:5
|
LL | use std::from::From;
| ^^^^^^^^^^^^^^^
|
= note: see issue #144889 <https://github.com/rust-lang/rust/issues/144889> for more information
= help: add `#![feature(derive_from)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
Loading