Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bcfb510

Browse files
authoredAug 5, 2024
Unrolled build for rust-lang#127907
Rollup merge of rust-lang#127907 - RalfJung:byte_slice_in_packed_struct_with_derive, r=nnethercote built-in derive: remove BYTE_SLICE_IN_PACKED_STRUCT_WITH_DERIVE hack and lint Fixes rust-lang#107457 by turning the lint into a hard error. The lint has been shown in future breakage reports since Rust 1.69 (released in April 2023). Let's see (via crater) if enough time has passed since rust-lang#104429, and unicode-org/icu4x#2834 has propagated far enough to let us make this a hard error. Cc ``@nnethercote`` ``@Manishearth``
2 parents 176e545 + bda31d1 commit bcfb510

File tree

8 files changed

+41
-232
lines changed

8 files changed

+41
-232
lines changed
 

‎compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+6-48
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,10 @@ use std::{iter, vec};
181181
use rustc_ast::ptr::P;
182182
use rustc_ast::{
183183
self as ast, BindingMode, ByRef, EnumDef, Expr, GenericArg, GenericParamKind, Generics,
184-
Mutability, PatKind, TyKind, VariantData,
184+
Mutability, PatKind, VariantData,
185185
};
186186
use rustc_attr as attr;
187187
use rustc_expand::base::{Annotatable, ExtCtxt};
188-
use rustc_session::lint::builtin::BYTE_SLICE_IN_PACKED_STRUCT_WITH_DERIVE;
189188
use rustc_span::symbol::{kw, sym, Ident, Symbol};
190189
use rustc_span::{Span, DUMMY_SP};
191190
use thin_vec::{thin_vec, ThinVec};
@@ -1599,52 +1598,11 @@ impl<'a> TraitDef<'a> {
15991598
),
16001599
);
16011600
if is_packed {
1602-
// In general, fields in packed structs are copied via a
1603-
// block, e.g. `&{self.0}`. The two exceptions are `[u8]`
1604-
// and `str` fields, which cannot be copied and also never
1605-
// cause unaligned references. These exceptions are allowed
1606-
// to handle the `FlexZeroSlice` type in the `zerovec`
1607-
// crate within `icu4x-0.9.0`.
1608-
//
1609-
// Once use of `icu4x-0.9.0` has dropped sufficiently, this
1610-
// exception should be removed.
1611-
let is_simple_path = |ty: &P<ast::Ty>, sym| {
1612-
if let TyKind::Path(None, ast::Path { segments, .. }) = &ty.kind
1613-
&& let [seg] = segments.as_slice()
1614-
&& seg.ident.name == sym
1615-
&& seg.args.is_none()
1616-
{
1617-
true
1618-
} else {
1619-
false
1620-
}
1621-
};
1622-
1623-
let exception = if let TyKind::Slice(ty) = &struct_field.ty.kind
1624-
&& is_simple_path(ty, sym::u8)
1625-
{
1626-
Some("byte")
1627-
} else if is_simple_path(&struct_field.ty, sym::str) {
1628-
Some("string")
1629-
} else {
1630-
None
1631-
};
1632-
1633-
if let Some(ty) = exception {
1634-
cx.sess.psess.buffer_lint(
1635-
BYTE_SLICE_IN_PACKED_STRUCT_WITH_DERIVE,
1636-
sp,
1637-
ast::CRATE_NODE_ID,
1638-
rustc_lint_defs::BuiltinLintDiag::ByteSliceInPackedStructWithDerive {
1639-
ty: ty.to_string(),
1640-
},
1641-
);
1642-
} else {
1643-
// Wrap the expression in `{...}`, causing a copy.
1644-
field_expr = cx.expr_block(
1645-
cx.block(struct_field.span, thin_vec![cx.stmt_expr(field_expr)]),
1646-
);
1647-
}
1601+
// Fields in packed structs are wrapped in a block, e.g. `&{self.0}`,
1602+
// causing a copy instead of a (potentially misaligned) reference.
1603+
field_expr = cx.expr_block(
1604+
cx.block(struct_field.span, thin_vec![cx.stmt_expr(field_expr)]),
1605+
);
16481606
}
16491607
cx.expr_addr_of(sp, field_expr)
16501608
})

‎compiler/rustc_lint/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ fn register_builtins(store: &mut LintStore) {
543543
);
544544
store.register_removed(
545545
"suspicious_auto_trait_impls",
546-
"no longer needed, see #93367 \
546+
"no longer needed, see issue #93367 \
547547
<https://github.com/rust-lang/rust/issues/93367> for more information",
548548
);
549549
store.register_removed(
@@ -565,6 +565,11 @@ fn register_builtins(store: &mut LintStore) {
565565
"box_pointers",
566566
"it does not detect other kinds of allocations, and existed only for historical reasons",
567567
);
568+
store.register_removed(
569+
"byte_slice_in_packed_struct_with_derive",
570+
"converted into hard error, see issue #107457 \
571+
<https://github.com/rust-lang/rust/issues/107457> for more information",
572+
)
568573
}
569574

570575
fn register_internals(store: &mut LintStore) {

‎compiler/rustc_lint_defs/src/builtin.rs

-34
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ declare_lint_pass! {
2626
BARE_TRAIT_OBJECTS,
2727
BINDINGS_WITH_VARIANT_NAME,
2828
BREAK_WITH_LABEL_AND_LOOP,
29-
BYTE_SLICE_IN_PACKED_STRUCT_WITH_DERIVE,
3029
CENUM_IMPL_DROP_CAST,
3130
COHERENCE_LEAK_CHECK,
3231
CONFLICTING_REPR_HINTS,
@@ -4315,39 +4314,6 @@ declare_lint! {
43154314
report_in_external_macro
43164315
}
43174316

4318-
declare_lint! {
4319-
/// The `byte_slice_in_packed_struct_with_derive` lint detects cases where a byte slice field
4320-
/// (`[u8]`) or string slice field (`str`) is used in a `packed` struct that derives one or
4321-
/// more built-in traits.
4322-
///
4323-
/// ### Example
4324-
///
4325-
/// ```rust
4326-
/// #[repr(packed)]
4327-
/// #[derive(Hash)]
4328-
/// struct FlexZeroSlice {
4329-
/// width: u8,
4330-
/// data: [u8],
4331-
/// }
4332-
/// ```
4333-
///
4334-
/// {{produces}}
4335-
///
4336-
/// ### Explanation
4337-
///
4338-
/// This was previously accepted but is being phased out, because fields in packed structs are
4339-
/// now required to implement `Copy` for `derive` to work. Byte slices and string slices are a
4340-
/// temporary exception because certain crates depended on them.
4341-
pub BYTE_SLICE_IN_PACKED_STRUCT_WITH_DERIVE,
4342-
Warn,
4343-
"`[u8]` or `str` used in a packed struct with `derive`",
4344-
@future_incompatible = FutureIncompatibleInfo {
4345-
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
4346-
reference: "issue #107457 <https://github.com/rust-lang/rust/issues/107457>",
4347-
};
4348-
report_in_external_macro
4349-
}
4350-
43514317
declare_lint! {
43524318
/// The `invalid_macro_export_arguments` lint detects cases where `#[macro_export]` is being used with invalid arguments.
43534319
///

‎tests/ui/derives/deriving-with-repr-packed.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,22 @@ struct Y(usize);
2222
struct X(Y);
2323
//~^ ERROR cannot move out of `self` which is behind a shared reference
2424

25-
// This is currently allowed, but will be phased out at some point. From
26-
// `zerovec` within icu4x-0.9.0.
2725
#[derive(Debug)]
2826
#[repr(packed)]
2927
struct FlexZeroSlice {
3028
width: u8,
3129
data: [u8],
32-
//~^ WARNING byte slice in a packed struct that derives a built-in trait
33-
//~^^ this was previously accepted
30+
//~^ ERROR cannot move
31+
//~| ERROR cannot move
3432
}
3533

36-
// Again, currently allowed, but will be phased out.
3734
#[derive(Debug)]
3835
#[repr(packed)]
3936
struct WithStr {
4037
width: u8,
4138
data: str,
42-
//~^ WARNING string slice in a packed struct that derives a built-in trait
43-
//~^^ this was previously accepted
39+
//~^ ERROR cannot move
40+
//~| ERROR cannot move
4441
}
4542

4643
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,3 @@
1-
warning: byte slice in a packed struct that derives a built-in trait
2-
--> $DIR/deriving-with-repr-packed.rs:31:5
3-
|
4-
LL | #[derive(Debug)]
5-
| ----- in this derive macro expansion
6-
...
7-
LL | data: [u8],
8-
| ^^^^^^^^^^
9-
|
10-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
11-
= note: for more information, see issue #107457 <https://github.com/rust-lang/rust/issues/107457>
12-
= help: consider implementing the trait by hand, or remove the `packed` attribute
13-
= note: `#[warn(byte_slice_in_packed_struct_with_derive)]` on by default
14-
= note: this warning originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
15-
16-
warning: string slice in a packed struct that derives a built-in trait
17-
--> $DIR/deriving-with-repr-packed.rs:41:5
18-
|
19-
LL | #[derive(Debug)]
20-
| ----- in this derive macro expansion
21-
...
22-
LL | data: str,
23-
| ^^^^^^^^^
24-
|
25-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
26-
= note: for more information, see issue #107457 <https://github.com/rust-lang/rust/issues/107457>
27-
= help: consider implementing the trait by hand, or remove the `packed` attribute
28-
= note: this warning originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
29-
301
error[E0507]: cannot move out of `self` which is behind a shared reference
312
--> $DIR/deriving-with-repr-packed.rs:22:10
323
|
@@ -47,38 +18,43 @@ LL | struct X(Y);
4718
= note: `#[derive(Debug)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
4819
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
4920

50-
error: aborting due to 1 previous error; 2 warnings emitted
21+
error[E0161]: cannot move a value of type `[u8]`
22+
--> $DIR/deriving-with-repr-packed.rs:29:5
23+
|
24+
LL | data: [u8],
25+
| ^^^^^^^^^^ the size of `[u8]` cannot be statically determined
5126

52-
For more information about this error, try `rustc --explain E0507`.
53-
Future incompatibility report: Future breakage diagnostic:
54-
warning: byte slice in a packed struct that derives a built-in trait
55-
--> $DIR/deriving-with-repr-packed.rs:31:5
27+
error[E0507]: cannot move out of `self.data` which is behind a shared reference
28+
--> $DIR/deriving-with-repr-packed.rs:29:5
5629
|
5730
LL | #[derive(Debug)]
5831
| ----- in this derive macro expansion
5932
...
6033
LL | data: [u8],
61-
| ^^^^^^^^^^
34+
| ^^^^^^^^^^ move occurs because `self.data` has type `[u8]`, which does not implement the `Copy` trait
35+
|
36+
= note: `#[derive(Debug)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
37+
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
38+
39+
error[E0161]: cannot move a value of type `str`
40+
--> $DIR/deriving-with-repr-packed.rs:38:5
6241
|
63-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
64-
= note: for more information, see issue #107457 <https://github.com/rust-lang/rust/issues/107457>
65-
= help: consider implementing the trait by hand, or remove the `packed` attribute
66-
= note: `#[warn(byte_slice_in_packed_struct_with_derive)]` on by default
67-
= note: this warning originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
42+
LL | data: str,
43+
| ^^^^^^^^^ the size of `str` cannot be statically determined
6844

69-
Future breakage diagnostic:
70-
warning: string slice in a packed struct that derives a built-in trait
71-
--> $DIR/deriving-with-repr-packed.rs:41:5
45+
error[E0507]: cannot move out of `self.data` which is behind a shared reference
46+
--> $DIR/deriving-with-repr-packed.rs:38:5
7247
|
7348
LL | #[derive(Debug)]
7449
| ----- in this derive macro expansion
7550
...
7651
LL | data: str,
77-
| ^^^^^^^^^
52+
| ^^^^^^^^^ move occurs because `self.data` has type `str`, which does not implement the `Copy` trait
7853
|
79-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
80-
= note: for more information, see issue #107457 <https://github.com/rust-lang/rust/issues/107457>
81-
= help: consider implementing the trait by hand, or remove the `packed` attribute
82-
= note: `#[warn(byte_slice_in_packed_struct_with_derive)]` on by default
83-
= note: this warning originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
54+
= note: `#[derive(Debug)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
55+
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
56+
57+
error: aborting due to 5 previous errors
8458

59+
Some errors have detailed explanations: E0161, E0507.
60+
For more information about an error, try `rustc --explain E0161`.

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

-10
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,6 @@ impl Copy for PackedManualCopy {}
7373
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
7474
struct Unsized([u32]);
7575

76-
// A packed struct with an unsized `[u8]` field. This is currently allowed, but
77-
// causes a warning and will be phased out at some point.
78-
#[derive(Debug, Hash)]
79-
#[repr(packed)]
80-
struct PackedUnsizedU8([u8]);
81-
//~^ WARNING byte slice in a packed struct that derives a built-in trait
82-
//~^^ WARNING byte slice in a packed struct that derives a built-in trait
83-
//~^^^ this was previously accepted
84-
//~^^^^ this was previously accepted
85-
8676
trait Trait {
8777
type A;
8878
}

‎tests/ui/deriving/deriving-all-codegen.stderr

-63
This file was deleted.

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

-20
Original file line numberDiff line numberDiff line change
@@ -516,26 +516,6 @@ impl ::core::cmp::Ord for Unsized {
516516
}
517517
}
518518

519-
// A packed struct with an unsized `[u8]` field. This is currently allowed, but
520-
// causes a warning and will be phased out at some point.
521-
#[repr(packed)]
522-
struct PackedUnsizedU8([u8]);
523-
#[automatically_derived]
524-
impl ::core::fmt::Debug for PackedUnsizedU8 {
525-
#[inline]
526-
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
527-
::core::fmt::Formatter::debug_tuple_field1_finish(f,
528-
"PackedUnsizedU8", &&self.0)
529-
}
530-
}
531-
#[automatically_derived]
532-
impl ::core::hash::Hash for PackedUnsizedU8 {
533-
#[inline]
534-
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
535-
::core::hash::Hash::hash(&self.0, state)
536-
}
537-
}
538-
539519
trait Trait {
540520
type A;
541521
}

0 commit comments

Comments
 (0)
Failed to load comments.