Skip to content

Commit

Permalink
[improper_ctypes] Stop complaining about repr(usize) and repr(isize) …
Browse files Browse the repository at this point in the history
…enums

This dates back to at least #26583. At the time, usize and isize were considered ffi-unsafe to nudge people away from them, but this changed in the aforementioned PR, making it inconsistent to complain about it in enum discriminants. In fact, repr(usize) is probably the best way to interface with `enum Foo : size_t { ... }`.
  • Loading branch information
Robin Kruppe committed Feb 15, 2018
1 parent 7ac5e96 commit ae92dfa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
29 changes: 0 additions & 29 deletions src/librustc_lint/types.rs
Expand Up @@ -26,7 +26,6 @@ use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};

use syntax::ast;
use syntax::abi::Abi;
use syntax::attr;
use syntax_pos::Span;
use syntax::codemap;

Expand Down Expand Up @@ -402,17 +401,6 @@ fn is_repr_nullable_ptr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
false
}

fn is_ffi_safe(ty: attr::IntType) -> bool {
match ty {
attr::SignedInt(ast::IntTy::I8) | attr::UnsignedInt(ast::UintTy::U8) |
attr::SignedInt(ast::IntTy::I16) | attr::UnsignedInt(ast::UintTy::U16) |
attr::SignedInt(ast::IntTy::I32) | attr::UnsignedInt(ast::UintTy::U32) |
attr::SignedInt(ast::IntTy::I64) | attr::UnsignedInt(ast::UintTy::U64) |
attr::SignedInt(ast::IntTy::I128) | attr::UnsignedInt(ast::UintTy::U128) => true,
attr::SignedInt(ast::IntTy::Isize) | attr::UnsignedInt(ast::UintTy::Usize) => false
}
}

impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
/// Check if the given type is "ffi-safe" (has a stable, well-defined
/// representation which can be exported to C code).
Expand Down Expand Up @@ -546,23 +534,6 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
}
}

if let Some(int_ty) = def.repr.int {
if !is_ffi_safe(int_ty) {
// FIXME: This shouldn't be reachable: we should check
// this earlier.
return FfiUnsafe(FfiError {
message: "enum has unexpected #[repr(...)] attribute",
help: None,
});
}

// Enum with an explicitly sized discriminant; either
// a C-style enum or a discriminated union.

// The layout of enum variants is implicitly repr(C).
// FIXME: Is that correct?
}

// Check the contained variants.
for variant in &def.variants {
for field in &variant.fields {
Expand Down
12 changes: 12 additions & 0 deletions src/test/compile-fail/lint-ctypes-enum.rs
Expand Up @@ -16,11 +16,23 @@ enum U { A }
enum B { C, D }
enum T { E, F, G }

#[repr(C)]
enum ReprC { A, B, C }

#[repr(u8)]
enum U8 { A, B, C }

#[repr(isize)]
enum Isize { A, B, C }

extern {
fn zf(x: Z);
fn uf(x: U); //~ ERROR found enum without foreign-function-safe
fn bf(x: B); //~ ERROR found enum without foreign-function-safe
fn tf(x: T); //~ ERROR found enum without foreign-function-safe
fn reprc(x: ReprC);
fn u8(x: U8);
fn isize(x: Isize);
}

pub fn main() { }

0 comments on commit ae92dfa

Please sign in to comment.