Skip to content

Commit 373d620

Browse files
committed
rollup merge of rust-lang#24935: pnkfelix/lint-repr-c-drop
Lint: warn when mixing `#[repr(C)]` with Drop Fix rust-lang#24585
2 parents 0cca577 + 2e23d81 commit 373d620

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

src/librustc_lint/builtin.rs

+54
Original file line numberDiff line numberDiff line change
@@ -2142,3 +2142,57 @@ impl LintPass for UnstableFeatures {
21422142
}
21432143
}
21442144
}
2145+
2146+
/// Lints for attempts to impl Drop on types that have `#[repr(C)]`
2147+
/// attribute (see issue #24585).
2148+
#[derive(Copy, Clone)]
2149+
pub struct DropWithReprExtern;
2150+
2151+
declare_lint! {
2152+
DROP_WITH_REPR_EXTERN,
2153+
Warn,
2154+
"use of #[repr(C)] on a type that implements Drop"
2155+
}
2156+
2157+
impl LintPass for DropWithReprExtern {
2158+
fn get_lints(&self) -> LintArray {
2159+
lint_array!(DROP_WITH_REPR_EXTERN)
2160+
}
2161+
fn check_crate(&mut self, ctx: &Context, _: &ast::Crate) {
2162+
for dtor_did in ctx.tcx.destructors.borrow().iter() {
2163+
let (drop_impl_did, dtor_self_type) =
2164+
if dtor_did.krate == ast::LOCAL_CRATE {
2165+
let impl_did = ctx.tcx.map.get_parent_did(dtor_did.node);
2166+
let ty = ty::lookup_item_type(ctx.tcx, impl_did).ty;
2167+
(impl_did, ty)
2168+
} else {
2169+
continue;
2170+
};
2171+
2172+
match dtor_self_type.sty {
2173+
ty::ty_enum(self_type_did, _) |
2174+
ty::ty_struct(self_type_did, _) |
2175+
ty::ty_closure(self_type_did, _) => {
2176+
let hints = ty::lookup_repr_hints(ctx.tcx, self_type_did);
2177+
if hints.iter().any(|attr| *attr == attr::ReprExtern) &&
2178+
ty::ty_dtor(ctx.tcx, self_type_did).has_drop_flag() {
2179+
let drop_impl_span = ctx.tcx.map.def_id_span(drop_impl_did,
2180+
codemap::DUMMY_SP);
2181+
let self_defn_span = ctx.tcx.map.def_id_span(self_type_did,
2182+
codemap::DUMMY_SP);
2183+
ctx.span_lint(DROP_WITH_REPR_EXTERN,
2184+
drop_impl_span,
2185+
"implementing Drop adds hidden state to types, \
2186+
possibly conflicting with `#[repr(C)]`");
2187+
// FIXME #19668: could be span_lint_note instead of manual guard.
2188+
if ctx.current_level(DROP_WITH_REPR_EXTERN) != Level::Allow {
2189+
ctx.sess().span_note(self_defn_span,
2190+
"the `#[repr(C)]` attribute is attached here");
2191+
}
2192+
}
2193+
}
2194+
_ => {}
2195+
}
2196+
}
2197+
}
2198+
}

src/librustc_lint/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
108108
UnconditionalRecursion,
109109
InvalidNoMangleItems,
110110
PluginAsLibrary,
111+
DropWithReprExtern,
111112
);
112113

113114
add_builtin_with_new!(sess,
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)