Skip to content

Ban #[unsafe_no_drop_flag] on zero-sized types. #33238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,13 +882,19 @@ impl<'a, 'gcx, 'tcx> Layout {
// won't actually be in the location we say it is because it'll be after
// the unsized field. Several other pieces of code assume that the unsized
// field is definitely the last one.
if def.dtor_kind().has_drop_flag() &&
let dtor_kind = def.dtor_kind();
if dtor_kind.has_drop_flag() &&
ty.is_sized(tcx, &infcx.parameter_environment, DUMMY_SP) {
st.extend(dl, Some(Ok(&Scalar {
value: Int(I8),
non_zero: false
})).into_iter(), ty)?;
}
if dtor_kind == ty::TraitDtor(false) && st.min_size().bytes() == 0 {
let span = tcx.map.def_id_span(def.did, DUMMY_SP);
tcx.sess.span_fatal(span, &format!(
"cannot have a destructor for zero-sized type `{}`", ty));
}
Univariant {
variant: st,
non_zero: Some(def.did) == tcx.lang_items.non_zero()
Expand All @@ -898,7 +904,8 @@ impl<'a, 'gcx, 'tcx> Layout {
let hint = *tcx.lookup_repr_hints(def.did).get(0)
.unwrap_or(&attr::ReprAny);

let dtor = def.dtor_kind().has_drop_flag();
let dtor_kind = def.dtor_kind();
let dtor = dtor_kind.has_drop_flag();
let drop_flag = if dtor {
Some(Scalar { value: Int(I8), non_zero: false })
} else {
Expand Down Expand Up @@ -953,6 +960,11 @@ impl<'a, 'gcx, 'tcx> Layout {
});
let mut st = Struct::new(dl, false);
st.extend(dl, fields.chain(drop_flag.iter().map(Ok)), ty)?;
if dtor_kind == ty::TraitDtor(false) && st.min_size().bytes() == 0 {
let span = tcx.map.def_id_span(def.did, DUMMY_SP);
tcx.sess.span_fatal(span, &format!(
"cannot have a destructor for zero-sized type `{}`", ty));
}
return Ok(Univariant { variant: st, non_zero: false });
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub struct CrateAnalysis<'a> {
pub glob_map: Option<hir::GlobMap>,
}

#[derive(Copy, Clone)]
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum DtorKind {
NoDtor,
TraitDtor(bool)
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_trans/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ pub fn in_memory_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) ->
return llty;
}

// FIXME(eddyb) use the layout to actually compute the LLVM type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I said "r=me" but maybe this needs to actually be addressed first?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a long term goal that is blocked on removing old trans.

let _layout = cx.tcx().normalizing_infer_ctxt(ProjectionMode::Any).enter(|infcx| {
t.layout(&infcx)
});

let mut llty = match t.sty {
ty::TyBool => Type::bool(cx),
ty::TyChar => Type::char(cx),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(rustc_attrs, unsafe_no_drop_flag)]

// ignore-pretty : (#23623) problems when ending with // comments
#![feature(unsafe_no_drop_flag)]

static mut destructions : isize = 3;

#[rustc_no_mir] // FIXME #29855 MIR doesn't handle all drops correctly.
pub fn foo() {
#[unsafe_no_drop_flag]
struct Foo;
//~^ ERROR cannot have a destructor for zero-sized type `foo::Foo`

impl Drop for Foo {
fn drop(&mut self) {
Expand Down
22 changes: 0 additions & 22 deletions src/test/run-pass/auxiliary/issue-10028.rs

This file was deleted.

29 changes: 0 additions & 29 deletions src/test/run-pass/issue-10028.rs

This file was deleted.