Skip to content

Commit 617b413

Browse files
arielb1Ariel Ben-Yehuda
authored and
Ariel Ben-Yehuda
committed
limit packed copy-out to non-generic Copy structs
1 parent dee8a71 commit 617b413

File tree

7 files changed

+123
-15
lines changed

7 files changed

+123
-15
lines changed

src/librustc_mir/transform/check_unsafety.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,22 @@ fn report_unused_unsafe(tcx: TyCtxt, used_unsafe: &FxHashSet<ast::NodeId>, id: a
373373
db.emit();
374374
}
375375

376+
fn builtin_derive_def_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Option<DefId> {
377+
debug!("builtin_derive_def_id({:?})", def_id);
378+
if let Some(impl_def_id) = tcx.impl_of_method(def_id) {
379+
if tcx.has_attr(impl_def_id, "automatically_derived") {
380+
debug!("builtin_derive_def_id({:?}) - is {:?}", def_id, impl_def_id);
381+
Some(impl_def_id)
382+
} else {
383+
debug!("builtin_derive_def_id({:?}) - not automatically derived", def_id);
384+
None
385+
}
386+
} else {
387+
debug!("builtin_derive_def_id({:?}) - not a method", def_id);
388+
None
389+
}
390+
}
391+
376392
pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
377393
debug!("check_unsafety({:?})", def_id);
378394

@@ -386,6 +402,7 @@ pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
386402
unsafe_blocks
387403
} = tcx.unsafety_check_result(def_id);
388404

405+
let mut emitted_derive_error = false;
389406
for &UnsafetyViolation {
390407
source_info, description, kind
391408
} in violations.iter() {
@@ -406,11 +423,29 @@ pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
406423
block (error E0133)", description));
407424
}
408425
UnsafetyViolationKind::BorrowPacked(lint_node_id) => {
426+
if emitted_derive_error {
427+
continue
428+
}
429+
430+
let message = if let Some(impl_def_id) = builtin_derive_def_id(tcx, def_id) {
431+
emitted_derive_error = true;
432+
// FIXME: when we make this a hard error, this should have its
433+
// own error code.
434+
if !tcx.generics_of(impl_def_id).types.is_empty() {
435+
format!("#[derive] can't be used on a #[repr(packed)] struct with \
436+
type parameters (error E0133)")
437+
} else {
438+
format!("#[derive] can't be used on a non-Copy #[repr(packed)] struct \
439+
(error E0133)")
440+
}
441+
} else {
442+
format!("{} requires unsafe function or \
443+
block (error E0133)", description)
444+
};
409445
tcx.lint_node(SAFE_PACKED_BORROWS,
410446
lint_node_id,
411447
source_info.span,
412-
&format!("{} requires unsafe function or \
413-
block (error E0133)", description));
448+
&message);
414449
}
415450
}
416451
}

src/libsyntax/ext/derive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn add_derived_markers<T>(cx: &mut ExtCtxt, span: Span, traits: &[ast::Path]
7474
let meta = cx.meta_word(span, Symbol::intern("structural_match"));
7575
attrs.push(cx.attribute(span, meta));
7676
}
77-
if names.contains(&Symbol::intern("Copy")) && names.contains(&Symbol::intern("Clone")) {
77+
if names.contains(&Symbol::intern("Copy")) {
7878
let meta = cx.meta_word(span, Symbol::intern("rustc_copy_clone_marker"));
7979
attrs.push(cx.attribute(span, meta));
8080
}

src/libsyntax_ext/deriving/generic/mod.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,24 @@ impl<'a> TraitDef<'a> {
413413
attr::find_repr_attrs(&cx.parse_sess.span_diagnostic, attr)
414414
.contains(&attr::ReprPacked)
415415
});
416-
let use_temporaries = is_packed;
416+
let has_no_type_params = match item.node {
417+
ast::ItemKind::Struct(_, ref generics) |
418+
ast::ItemKind::Enum(_, ref generics) |
419+
ast::ItemKind::Union(_, ref generics) => {
420+
generics.ty_params.is_empty()
421+
}
422+
_ => {
423+
// Non-ADT derive is an error, but it should have been
424+
// set earlier; see
425+
// libsyntax/ext/expand.rs:MacroExpander::expand()
426+
return;
427+
}
428+
};
429+
let is_always_copy =
430+
attr::contains_name(&item.attrs, "rustc_copy_clone_marker") &&
431+
has_no_type_params;
432+
let use_temporaries = is_packed && is_always_copy;
433+
417434
let newitem = match item.node {
418435
ast::ItemKind::Struct(ref struct_def, ref generics) => {
419436
self.expand_struct_def(cx, &struct_def, item.ident, generics, from_scratch,
@@ -440,12 +457,7 @@ impl<'a> TraitDef<'a> {
440457
return;
441458
}
442459
}
443-
_ => {
444-
// Non-ADT derive is an error, but it should have been
445-
// set earlier; see
446-
// libsyntax/ext/expand.rs:MacroExpander::expand()
447-
return;
448-
}
460+
_ => unreachable!(),
449461
};
450462
// Keep the lint attributes of the previous item to control how the
451463
// generated implementations are linted

src/test/compile-fail/deriving-with-repr-packed-not-copy.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![deny(safe_packed_borrows)]
12+
1113
// check that derive on a packed struct with non-Copy fields
1214
// correctly. This can't be made to work perfectly because
1315
// we can't just use the field from the struct as it might
@@ -17,12 +19,10 @@
1719
struct Y(usize);
1820

1921
#[derive(PartialEq)]
20-
//~^ ERROR cannot move out of borrowed
21-
//~| ERROR cannot move out of borrowed
22-
//~| ERROR cannot move out of borrowed
23-
//~| ERROR cannot move out of borrowed
2422
#[repr(packed)]
2523
struct X(Y);
24+
//~^ ERROR #[derive] can't be used on a non-Copy #[repr(packed)]
25+
//~| hard error
2626

2727
fn main() {
2828
}

src/test/run-pass/deriving-with-repr-packed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl PartialEq for Aligned {
3131
}
3232

3333
#[repr(packed)]
34-
#[derive(PartialEq)]
34+
#[derive(Copy, Clone, PartialEq)]
3535
struct Packed(Aligned, Aligned);
3636

3737
#[derive(PartialEq)]
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(safe_packed_borrows)]
12+
13+
// check that deriving a non-Copy packed struct is an error.
14+
#[derive(Copy, Clone, PartialEq, Eq)]
15+
#[repr(packed)]
16+
pub struct Foo<T>(T, T, T);
17+
//~^ ERROR #[derive] can't be used
18+
//~| hard error
19+
//~^^^ ERROR #[derive] can't be used
20+
//~| hard error
21+
#[derive(PartialEq, Eq)]
22+
#[repr(packed)]
23+
pub struct Bar(u32, u32, u32);
24+
//~^ ERROR #[derive] can't be used
25+
//~| hard error
26+
27+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: #[derive] can't be used on a #[repr(packed)] struct with type parameters (error E0133)
2+
--> $DIR/deriving-with-repr-packed.rs:16:19
3+
|
4+
16 | pub struct Foo<T>(T, T, T);
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/deriving-with-repr-packed.rs:11:9
9+
|
10+
11 | #![deny(safe_packed_borrows)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13+
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
14+
15+
error: #[derive] can't be used on a #[repr(packed)] struct with type parameters (error E0133)
16+
--> $DIR/deriving-with-repr-packed.rs:16:19
17+
|
18+
16 | pub struct Foo<T>(T, T, T);
19+
| ^^
20+
|
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22+
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
23+
24+
error: #[derive] can't be used on a non-Copy #[repr(packed)] struct (error E0133)
25+
--> $DIR/deriving-with-repr-packed.rs:23:16
26+
|
27+
23 | pub struct Bar(u32, u32, u32);
28+
| ^^^^
29+
|
30+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
31+
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
32+
33+
error: aborting due to 5 previous errors
34+

0 commit comments

Comments
 (0)