-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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!(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
#![feature(test)] | ||
#![feature(libc)] | ||
|
||
#![cfg_attr(stage0, feature(question_mark))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that #37177 already contains this (and is in the queue) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #37202 contains it too :D |
||
|
||
#![deny(warnings)] | ||
|
||
extern crate libc; | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ofLayout
)There was a problem hiding this comment.
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 likenon_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
).There was a problem hiding this comment.
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.