Skip to content

Commit 2e23d81

Browse files
committed
tests for lint that warns about mixing #[repr(C)] with Drop.
THis includes tests for struct and enum. (I suspect the closure case is actually unreachable, but i see no harm in including it.)
1 parent 78130a4 commit 2e23d81

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2015 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+
// Check we reject structs that mix a `Drop` impl with `#[repr(C)]`.
12+
//
13+
// As a special case, also check that we do not warn on such structs
14+
// if they also are declared with `#[unsafe_no_drop_flag]`
15+
16+
#![feature(unsafe_no_drop_flag)]
17+
#![deny(drop_with_repr_extern)]
18+
19+
#[repr(C)] struct As { x: Box<i8> }
20+
#[repr(C)] enum Ae { Ae(Box<i8>), _None }
21+
22+
struct Bs { x: Box<i8> }
23+
enum Be { Be(Box<i8>), _None }
24+
25+
#[repr(C)] struct Cs { x: Box<i8> }
26+
//~^ NOTE the `#[repr(C)]` attribute is attached here
27+
28+
impl Drop for Cs { fn drop(&mut self) { } }
29+
//~^ ERROR implementing Drop adds hidden state to types, possibly conflicting with `#[repr(C)]`
30+
31+
#[repr(C)] enum Ce { Ce(Box<i8>), _None }
32+
//~^ NOTE the `#[repr(C)]` attribute is attached here
33+
34+
impl Drop for Ce { fn drop(&mut self) { } }
35+
//~^ ERROR implementing Drop adds hidden state to types, possibly conflicting with `#[repr(C)]`
36+
37+
#[unsafe_no_drop_flag]
38+
#[repr(C)] struct Ds { x: Box<i8> }
39+
40+
impl Drop for Ds { fn drop(&mut self) { } }
41+
42+
#[unsafe_no_drop_flag]
43+
#[repr(C)] enum De { De(Box<i8>), _None }
44+
45+
impl Drop for De { fn drop(&mut self) { } }
46+
47+
fn main() {
48+
let a = As { x: Box::new(3) };
49+
let b = Bs { x: Box::new(3) };
50+
let c = Cs { x: Box::new(3) };
51+
let d = Ds { x: Box::new(3) };
52+
53+
println!("{:?}", (*a.x, *b.x, *c.x, *d.x));
54+
55+
let _a = Ae::Ae(Box::new(3));
56+
let _b = Be::Be(Box::new(3));
57+
let _c = Ce::Ce(Box::new(3));
58+
let _d = De::De(Box::new(3));
59+
}

0 commit comments

Comments
 (0)