Skip to content

Commit

Permalink
In improper-ctypes lint, handle functions which explicitly return ().
Browse files Browse the repository at this point in the history
Fixes issue #27302.
  • Loading branch information
eefriedman committed Jul 27, 2015
1 parent 82d40cb commit 21b514f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/librustc_lint/builtin.rs
Expand Up @@ -436,6 +436,16 @@ fn is_repr_nullable_ptr<'tcx>(variants: &Vec<Rc<ty::VariantInfo<'tcx>>>) -> 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).
Expand Down Expand Up @@ -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 => {}
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/compile-fail/lint-ctypes.rs
Expand Up @@ -26,6 +26,7 @@ pub type I32Pair = (i32, i32);
pub struct ZeroSize;
pub type RustFn = fn();
pub type RustBadRet = extern fn() -> Box<u32>;
pub type CVoidRet = ();

extern {
pub fn bare_type1(size: isize); //~ ERROR: found Rust type
Expand All @@ -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() {
Expand Down

0 comments on commit 21b514f

Please sign in to comment.