Skip to content

Commit ae92dfa

Browse files
author
Robin Kruppe
committed
[improper_ctypes] Stop complaining about repr(usize) and repr(isize) enums
This dates back to at least rust-lang#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 { ... }`.
1 parent 7ac5e96 commit ae92dfa

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

src/librustc_lint/types.rs

-29
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
2626

2727
use syntax::ast;
2828
use syntax::abi::Abi;
29-
use syntax::attr;
3029
use syntax_pos::Span;
3130
use syntax::codemap;
3231

@@ -402,17 +401,6 @@ fn is_repr_nullable_ptr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
402401
false
403402
}
404403

405-
fn is_ffi_safe(ty: attr::IntType) -> bool {
406-
match ty {
407-
attr::SignedInt(ast::IntTy::I8) | attr::UnsignedInt(ast::UintTy::U8) |
408-
attr::SignedInt(ast::IntTy::I16) | attr::UnsignedInt(ast::UintTy::U16) |
409-
attr::SignedInt(ast::IntTy::I32) | attr::UnsignedInt(ast::UintTy::U32) |
410-
attr::SignedInt(ast::IntTy::I64) | attr::UnsignedInt(ast::UintTy::U64) |
411-
attr::SignedInt(ast::IntTy::I128) | attr::UnsignedInt(ast::UintTy::U128) => true,
412-
attr::SignedInt(ast::IntTy::Isize) | attr::UnsignedInt(ast::UintTy::Usize) => false
413-
}
414-
}
415-
416404
impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
417405
/// Check if the given type is "ffi-safe" (has a stable, well-defined
418406
/// representation which can be exported to C code).
@@ -546,23 +534,6 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
546534
}
547535
}
548536

549-
if let Some(int_ty) = def.repr.int {
550-
if !is_ffi_safe(int_ty) {
551-
// FIXME: This shouldn't be reachable: we should check
552-
// this earlier.
553-
return FfiUnsafe(FfiError {
554-
message: "enum has unexpected #[repr(...)] attribute",
555-
help: None,
556-
});
557-
}
558-
559-
// Enum with an explicitly sized discriminant; either
560-
// a C-style enum or a discriminated union.
561-
562-
// The layout of enum variants is implicitly repr(C).
563-
// FIXME: Is that correct?
564-
}
565-
566537
// Check the contained variants.
567538
for variant in &def.variants {
568539
for field in &variant.fields {

src/test/compile-fail/lint-ctypes-enum.rs

+12
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,23 @@ enum U { A }
1616
enum B { C, D }
1717
enum T { E, F, G }
1818

19+
#[repr(C)]
20+
enum ReprC { A, B, C }
21+
22+
#[repr(u8)]
23+
enum U8 { A, B, C }
24+
25+
#[repr(isize)]
26+
enum Isize { A, B, C }
27+
1928
extern {
2029
fn zf(x: Z);
2130
fn uf(x: U); //~ ERROR found enum without foreign-function-safe
2231
fn bf(x: B); //~ ERROR found enum without foreign-function-safe
2332
fn tf(x: T); //~ ERROR found enum without foreign-function-safe
33+
fn reprc(x: ReprC);
34+
fn u8(x: U8);
35+
fn isize(x: Isize);
2436
}
2537

2638
pub fn main() { }

0 commit comments

Comments
 (0)