-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Allow enum discriminants to not be uint, and use smallest possible size by default. #9613
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
Changes from 1 commit
f1124a2
01740ac
25f9534
ac311ec
c8c0876
a027f16
01097cb
727731f
92109b1
fcfbfde
afab330
de9bb97
472d798
ac4644d
49f851c
c0190a9
86a710e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,22 +145,8 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr { | |
return Univariant(mk_struct(cx, ftys, packed), dtor) | ||
} | ||
ty::ty_enum(def_id, ref substs) => { | ||
struct Case { discr: Disr, tys: ~[ty::t] }; | ||
impl Case { | ||
fn is_zerolen(&self, cx: &mut CrateContext) -> bool { | ||
mk_struct(cx, self.tys, false).size == 0 | ||
} | ||
fn find_ptr(&self) -> Option<uint> { | ||
self.tys.iter().position(|&ty| mono_data_classify(ty) == MonoNonNull) | ||
} | ||
} | ||
|
||
let cases = do ty::enum_variants(cx.tcx, def_id).map |vi| { | ||
let arg_tys = do vi.args.map |&raw_ty| { | ||
ty::subst(cx.tcx, substs, raw_ty) | ||
}; | ||
Case { discr: vi.disr_val, tys: arg_tys } | ||
}; | ||
let cases = get_cases(cx.tcx, def_id, substs); | ||
let hint = ty::lookup_repr_hint(cx.tcx, def_id); | ||
|
||
if cases.len() == 0 { | ||
// Uninhabitable; represent as unit | ||
|
@@ -170,7 +156,6 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr { | |
if cases.iter().all(|c| c.tys.len() == 0) { | ||
// All bodies empty -> intlike | ||
let discrs = cases.map(|c| c.discr); | ||
let hint = ty::lookup_repr_hint(cx.tcx, def_id); | ||
let bounds = IntBounds { | ||
ulo: *discrs.iter().min().unwrap(), | ||
uhi: *discrs.iter().max().unwrap(), | ||
|
@@ -232,6 +217,56 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr { | |
} | ||
} | ||
|
||
/// Determine, without doing translation, whether an ADT must be FFI-safe. | ||
/// For use in lint or similar, where being sound but slightly incomplete is acceptable. | ||
pub fn is_ffi_safe(tcx: ty::ctxt, def_id: ast::DefId) -> bool { | ||
match ty::get(ty::lookup_item_type(tcx, def_id).ty).sty { | ||
ty::ty_enum(def_id, ref substs) => { | ||
let cases = get_cases(tcx, def_id, substs); | ||
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. Problem: these seem to be the wrong substs. Maybe I'm getting the formal paramters of the enum instead of the actual parameters of the use? Do I need to actually resolve the type here? (Edit: or, if not here, then in lint.rs.) Alternately, I could just allow any non-C-like enums (ones where any variant has a non-zero number of fields), because those are less likely to be used accidentally, and file an issue to tighten up the warning in the future. Thoughts? |
||
// Univariant => like struct/tuple. | ||
if cases.len() <= 2 { | ||
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. Is this really univariant? (Should it be 1 rather than 2?) 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. Yes, it should be 1 (or |
||
return true; | ||
} | ||
let hint = ty::lookup_repr_hint(tcx, def_id); | ||
// Appropriate representation explicitly selected? | ||
if hint.is_ffi_safe() { | ||
return true; | ||
} | ||
// Conservative approximation of nullable pointers, for Option<~T> etc. | ||
if cases.len() == 2 && hint == attr::ReprAny && | ||
(cases[0].tys.is_empty() && cases[1].find_ptr().is_some() || | ||
cases[1].tys.is_empty() && cases[0].find_ptr().is_some()) { | ||
return true; | ||
} | ||
false | ||
} | ||
// struct, tuple, etc. | ||
// (is this right in the present of typedefs?) | ||
_ => true | ||
} | ||
} | ||
|
||
// NOTE this should probably all be in ty | ||
struct Case { discr: Disr, tys: ~[ty::t] } | ||
impl Case { | ||
fn is_zerolen(&self, cx: &mut CrateContext) -> bool { | ||
mk_struct(cx, self.tys, false).size == 0 | ||
} | ||
fn find_ptr(&self) -> Option<uint> { | ||
self.tys.iter().position(|&ty| mono_data_classify(ty) == MonoNonNull) | ||
} | ||
} | ||
|
||
fn get_cases(tcx: ty::ctxt, def_id: ast::DefId, substs: &ty::substs) -> ~[Case] { | ||
do ty::enum_variants(tcx, def_id).map |vi| { | ||
let arg_tys = do vi.args.map |&raw_ty| { | ||
ty::subst(tcx, substs, raw_ty) | ||
}; | ||
Case { discr: vi.disr_val, tys: arg_tys } | ||
} | ||
} | ||
|
||
|
||
fn mk_struct(cx: &mut CrateContext, tys: &[ty::t], packed: bool) -> Struct { | ||
let lltys = tys.map(|&ty| type_of::sizing_type_of(cx, ty)); | ||
let llty_rec = Type::struct_(lltys, packed); | ||
|
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.
It seems like this is definitely worthy of a
span_note
explaining a little bit aboutrepr
, although that may require reorganization of linting somewhat to pass through the note.