From 21b514ff3040527e290d79a27d591b1b31301f68 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Sun, 26 Jul 2015 17:51:03 -0700 Subject: [PATCH] In improper-ctypes lint, handle functions which explicitly return `()`. Fixes issue #27302. --- src/librustc_lint/builtin.rs | 21 +++++++++++++++------ src/test/compile-fail/lint-ctypes.rs | 3 +++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 6289d50588104..751224e72864b 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -436,6 +436,16 @@ fn is_repr_nullable_ptr<'tcx>(variants: &Vec>>) -> bool false } +fn ast_ty_to_normalized<'tcx>(tcx: &ty::ctxt<'tcx>, + id: ast::NodeId) + -> Ty<'tcx> { + let tty = match tcx.ast_ty_to_ty_cache.borrow().get(&id) { + Some(&t) => t, + None => panic!("ast_ty_to_ty_cache was incomplete after typeck!") + }; + infer::normalize_associated_type(tcx, &tty) +} + 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). @@ -638,11 +648,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { } fn check_def(&mut self, sp: Span, id: ast::NodeId) { - let tty = match self.cx.tcx.ast_ty_to_ty_cache.borrow().get(&id) { - Some(&t) => t, - None => panic!("ast_ty_to_ty_cache was incomplete after typeck!") - }; - let tty = infer::normalize_associated_type(self.cx.tcx, &tty); + let tty = ast_ty_to_normalized(self.cx.tcx, id); match ImproperCTypesVisitor::check_type_for_ffi(self, &mut FnvHashSet(), tty) { FfiResult::FfiSafe => {} @@ -707,7 +713,10 @@ impl LintPass for ImproperCTypes { check_ty(cx, &*input.ty); } if let ast::Return(ref ret_ty) = decl.output { - check_ty(cx, &**ret_ty); + let tty = ast_ty_to_normalized(cx.tcx, ret_ty.id); + if !tty.is_nil() { + check_ty(cx, &ret_ty); + } } } diff --git a/src/test/compile-fail/lint-ctypes.rs b/src/test/compile-fail/lint-ctypes.rs index 614f8e6fde81a..4daba86679d5d 100644 --- a/src/test/compile-fail/lint-ctypes.rs +++ b/src/test/compile-fail/lint-ctypes.rs @@ -26,6 +26,7 @@ pub type I32Pair = (i32, i32); pub struct ZeroSize; pub type RustFn = fn(); pub type RustBadRet = extern fn() -> Box; +pub type CVoidRet = (); extern { pub fn bare_type1(size: isize); //~ ERROR: found Rust type @@ -52,6 +53,8 @@ extern { pub fn good6(s: StructWithProjectionAndLifetime); pub fn good7(fptr: extern fn() -> ()); pub fn good8(fptr: extern fn() -> !); + pub fn good9() -> (); + pub fn good10() -> CVoidRet; } fn main() {