Skip to content

In improper-ctypes lint, handle functions which explicitly return (). #27315

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

Merged
merged 1 commit into from
Jul 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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