Skip to content
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

Mark enums with non-zero discriminant as non-zero #37224

Merged
merged 1 commit into from
Oct 19, 2016
Merged
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
32 changes: 17 additions & 15 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ impl<'a, 'gcx, 'tcx> Struct {
-> Result<Option<FieldPath>, LayoutError<'gcx>> {
let tcx = infcx.tcx.global_tcx();
match (ty.layout(infcx)?, &ty.sty) {
(&Scalar { non_zero: true, .. }, _) => Ok(Some(vec![])),
(&Scalar { non_zero: true, .. }, _) |
(&CEnum { non_zero: true, .. }, _) => Ok(Some(vec![])),
(&FatPointer { non_zero: true, .. }, _) => {
Ok(Some(vec![FAT_PTR_ADDR as u32]))
}
Expand Down Expand Up @@ -769,6 +770,7 @@ pub enum Layout {
CEnum {
discr: Integer,
signed: bool,
non_zero: bool,
Copy link
Member

Choose a reason for hiding this comment

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

You already have the range (see rustc_trans::adt for a way to determine if a value is in the range), why would this additional field be needed?

Copy link
Contributor Author

@petrochenkov petrochenkov Oct 16, 2016

Choose a reason for hiding this comment

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

enum E {
    A = -1,
    B = 1,
}

0 is in the range, but the enum is still non-zero.
(Also, non_zero: bool fits into previous padding so it doesn't increase the size of Layout)

Copy link
Member

Choose a reason for hiding this comment

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

That's annoying. What I expect to eventually have would use 2 there, that's why I don't like non_zero: it's a special-case that makes sense mostly on pointers.
If you want to discuss the full niche optimization, I'd be down for it (IRC?).
If you don't want to change this implementation I'll r+ and take out this later (and/or switch to multi-ranges - LLVM actually supports them for !range).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't plan to implement the full optimization so far.

// Inclusive discriminant range.
// If min > max, it represents min...u64::MAX followed by 0...max.
// FIXME(eddyb) always use the shortest range, e.g. by finding
Expand Down Expand Up @@ -1002,9 +1004,10 @@ impl<'a, 'gcx, 'tcx> Layout {

if def.is_enum() && def.variants.iter().all(|v| v.fields.is_empty()) {
// All bodies empty -> intlike
let (mut min, mut max) = (i64::MAX, i64::MIN);
let (mut min, mut max, mut non_zero) = (i64::MAX, i64::MIN, true);
for v in &def.variants {
let x = v.disr_val.to_u64_unchecked() as i64;
if x == 0 { non_zero = false; }
if x < min { min = x; }
if x > max { max = x; }
}
Expand All @@ -1013,6 +1016,7 @@ impl<'a, 'gcx, 'tcx> Layout {
return success(CEnum {
discr: discr,
signed: signed,
non_zero: non_zero,
min: min as u64,
max: max as u64
});
Expand Down Expand Up @@ -1069,19 +1073,17 @@ impl<'a, 'gcx, 'tcx> Layout {

// FIXME(eddyb) should take advantage of a newtype.
if path == &[0] && variants[discr].len() == 1 {
match *variants[discr][0].layout(infcx)? {
Scalar { value, .. } => {
return success(RawNullablePointer {
nndiscr: discr as u64,
value: value
});
}
_ => {
bug!("Layout::compute: `{}`'s non-zero \
`{}` field not scalar?!",
ty, variants[discr][0])
}
}
let value = match *variants[discr][0].layout(infcx)? {
Scalar { value, .. } => value,
CEnum { discr, .. } => Int(discr),
_ => bug!("Layout::compute: `{}`'s non-zero \
`{}` field not scalar?!",
ty, variants[discr][0])
};
return success(RawNullablePointer {
nndiscr: discr as u64,
value: value,
});
}

path.push(0); // For GEP through a pointer.
Expand Down
39 changes: 39 additions & 0 deletions src/test/run-pass/nonzero-enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::mem::size_of;

enum E {
A = 1,
B = 2,
C = 3,
}

struct S {
a: u16,
b: u8,
e: E,
}

fn main() {
assert_eq!(size_of::<E>(), 1);
assert_eq!(size_of::<Option<E>>(), 1);
assert_eq!(size_of::<Result<E, ()>>(), 1);
assert_eq!(size_of::<S>(), 4);
assert_eq!(size_of::<Option<S>>(), 4);
let enone = None::<E>;
let esome = Some(E::A);
if let Some(..) = enone {
panic!();
}
if let None = esome {
panic!();
}
}
2 changes: 2 additions & 0 deletions src/tools/compiletest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#![feature(test)]
#![feature(libc)]

#![cfg_attr(stage0, feature(question_mark))]
Copy link
Member

Choose a reason for hiding this comment

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

A few people hit this, I guess it got removed by accident. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Note that #37177 already contains this (and is in the queue)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#37202 contains it too :D
The sooner it lands the better.


#![deny(warnings)]

extern crate libc;
Expand Down