Skip to content

Commit c6ed4b1

Browse files
authored
Unrolled build for #145563
Rollup merge of #145563 - Kobzol:remove-from-from-prelude, r=petrochenkov Remove the `From` derive macro from prelude The new `#[derive(From)]` functionality (implemented in #144922) caused name resolution ambiguity issues (#145524). The reproducer looks e.g. like this: ```rust mod foo { pub use derive_more::From; } use foo::*; #[derive(From)] // ERROR: `From` is ambiguous struct S(u32); ``` It's pretty unfortunate that it works like this, but I guess that there's not much to be done here, and we'll have to wait for the next edition to put the `From` macro into the prelude. That will probably require #139493 to land. I created a new module in core (and re-exported it in std) called `from`, where I re-exported the `From` macro. I *think* that since this is a new module, it should not have the same backwards incompatibility issue. Happy to hear suggestions about the naming - maybe it would make sense as `core::macros::from::From`? But we already had a precedent in the `core::assert_matches` module, so I just followed suit. Fixes: #145524 r? ``@petrochenkov``
2 parents b96868f + a6a760e commit c6ed4b1

File tree

10 files changed

+57
-22
lines changed

10 files changed

+57
-22
lines changed

library/core/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ pub mod assert_matches {
226226
pub use crate::macros::{assert_matches, debug_assert_matches};
227227
}
228228

229+
#[unstable(feature = "derive_from", issue = "144889")]
230+
/// Unstable module containing the unstable `From` derive macro.
231+
pub mod from {
232+
#[unstable(feature = "derive_from", issue = "144889")]
233+
pub use crate::macros::builtin::From;
234+
}
235+
229236
// We don't export this through #[macro_export] for now, to avoid breakage.
230237
#[unstable(feature = "autodiff", issue = "124509")]
231238
/// Unstable module containing the unstable `autodiff` macro.

library/core/src/prelude/v1.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,3 @@ pub use crate::macros::builtin::deref;
117117
reason = "`type_alias_impl_trait` has open design concerns"
118118
)]
119119
pub use crate::macros::builtin::define_opaque;
120-
121-
#[unstable(
122-
feature = "derive_from",
123-
issue = "144889",
124-
reason = "`derive(From)` is unstable"
125-
)]
126-
pub use crate::macros::builtin::From;

library/std/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,14 @@ pub use core::{
738738
unreachable, write, writeln,
739739
};
740740

741+
// Re-export unstable derive macro defined through core.
742+
#[unstable(feature = "derive_from", issue = "144889")]
743+
/// Unstable module containing the unstable `From` derive macro.
744+
pub mod from {
745+
#[unstable(feature = "derive_from", issue = "144889")]
746+
pub use core::from::From;
747+
}
748+
741749
// Include a number of private modules that exist solely to provide
742750
// the rustdoc documentation for primitive types. Using `include!`
743751
// because rustdoc only looks for these modules at the crate level.

tests/ui/deriving/deriving-all-codegen.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#![allow(deprecated)]
1919
#![feature(derive_from)]
2020

21+
use std::from::From;
22+
2123
// Empty struct.
2224
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
2325
struct Empty;
@@ -51,7 +53,14 @@ struct SingleField {
5153
// `clone` implemention that just does `*self`.
5254
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
5355
struct Big {
54-
b1: u32, b2: u32, b3: u32, b4: u32, b5: u32, b6: u32, b7: u32, b8: u32,
56+
b1: u32,
57+
b2: u32,
58+
b3: u32,
59+
b4: u32,
60+
b5: u32,
61+
b6: u32,
62+
b7: u32,
63+
b8: u32,
5564
}
5665

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

132141
// A C-like, fieldless enum with a single variant.
@@ -152,7 +161,10 @@ enum Mixed {
152161
P,
153162
Q,
154163
R(u32),
155-
S { d1: Option<u32>, d2: Option<i32> },
164+
S {
165+
d1: Option<u32>,
166+
d2: Option<i32>,
167+
},
156168
}
157169

158170
// When comparing enum variant it is more efficient to compare scalar types before non-scalar types.

tests/ui/deriving/deriving-all-codegen.stdout

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ extern crate std;
2323
#[prelude_import]
2424
use std::prelude::rust_2021::*;
2525

26+
use std::from::From;
27+
2628
// Empty struct.
2729
struct Empty;
2830
#[automatically_derived]

tests/ui/deriving/deriving-from-wrong-target.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
//@ edition: 2021
21
//@ check-fail
32

43
#![feature(derive_from)]
54
#![allow(dead_code)]
65

6+
use std::from::From;
7+
78
#[derive(From)]
89
//~^ ERROR `#[derive(From)]` used on a struct with no fields
910
struct S1;

tests/ui/deriving/deriving-from-wrong-target.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: `#[derive(From)]` used on a struct with no fields
2-
--> $DIR/deriving-from-wrong-target.rs:7:10
2+
--> $DIR/deriving-from-wrong-target.rs:8:10
33
|
44
LL | #[derive(From)]
55
| ^^^^
@@ -10,7 +10,7 @@ LL | struct S1;
1010
= note: `#[derive(From)]` can only be used on structs with exactly one field
1111

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

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

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

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

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

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

9494
error[E0277]: the size for values of type `T` cannot be known at compilation time
95-
--> $DIR/deriving-from-wrong-target.rs:34:11
95+
--> $DIR/deriving-from-wrong-target.rs:35:11
9696
|
9797
LL | struct SUnsizedField<T: ?Sized> {
9898
| - this type parameter needs to be `Sized`

tests/ui/deriving/deriving-from.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#![feature(derive_from)]
55

6+
use core::from::From;
7+
68
#[derive(From)]
79
struct TupleSimple(u32);
810

tests/ui/feature-gates/feature-gate-derive-from.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ edition: 2021
1+
use std::from::From; //~ ERROR use of unstable library feature `derive_from
22

33
#[derive(From)] //~ ERROR use of unstable library feature `derive_from`
44
struct Foo(u32);

tests/ui/feature-gates/feature-gate-derive-from.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ LL | #[derive(From)]
88
= help: add `#![feature(derive_from)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

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

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

0 commit comments

Comments
 (0)