Skip to content

Commit 21b514f

Browse files
committed
In improper-ctypes lint, handle functions which explicitly return ().
Fixes issue #27302.
1 parent 82d40cb commit 21b514f

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/librustc_lint/builtin.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ fn is_repr_nullable_ptr<'tcx>(variants: &Vec<Rc<ty::VariantInfo<'tcx>>>) -> bool
436436
false
437437
}
438438

439+
fn ast_ty_to_normalized<'tcx>(tcx: &ty::ctxt<'tcx>,
440+
id: ast::NodeId)
441+
-> Ty<'tcx> {
442+
let tty = match tcx.ast_ty_to_ty_cache.borrow().get(&id) {
443+
Some(&t) => t,
444+
None => panic!("ast_ty_to_ty_cache was incomplete after typeck!")
445+
};
446+
infer::normalize_associated_type(tcx, &tty)
447+
}
448+
439449
impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
440450
/// Check if the given type is "ffi-safe" (has a stable, well-defined
441451
/// representation which can be exported to C code).
@@ -638,11 +648,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
638648
}
639649

640650
fn check_def(&mut self, sp: Span, id: ast::NodeId) {
641-
let tty = match self.cx.tcx.ast_ty_to_ty_cache.borrow().get(&id) {
642-
Some(&t) => t,
643-
None => panic!("ast_ty_to_ty_cache was incomplete after typeck!")
644-
};
645-
let tty = infer::normalize_associated_type(self.cx.tcx, &tty);
651+
let tty = ast_ty_to_normalized(self.cx.tcx, id);
646652

647653
match ImproperCTypesVisitor::check_type_for_ffi(self, &mut FnvHashSet(), tty) {
648654
FfiResult::FfiSafe => {}
@@ -707,7 +713,10 @@ impl LintPass for ImproperCTypes {
707713
check_ty(cx, &*input.ty);
708714
}
709715
if let ast::Return(ref ret_ty) = decl.output {
710-
check_ty(cx, &**ret_ty);
716+
let tty = ast_ty_to_normalized(cx.tcx, ret_ty.id);
717+
if !tty.is_nil() {
718+
check_ty(cx, &ret_ty);
719+
}
711720
}
712721
}
713722

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

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub type I32Pair = (i32, i32);
2626
pub struct ZeroSize;
2727
pub type RustFn = fn();
2828
pub type RustBadRet = extern fn() -> Box<u32>;
29+
pub type CVoidRet = ();
2930

3031
extern {
3132
pub fn bare_type1(size: isize); //~ ERROR: found Rust type
@@ -52,6 +53,8 @@ extern {
5253
pub fn good6(s: StructWithProjectionAndLifetime);
5354
pub fn good7(fptr: extern fn() -> ());
5455
pub fn good8(fptr: extern fn() -> !);
56+
pub fn good9() -> ();
57+
pub fn good10() -> CVoidRet;
5558
}
5659

5760
fn main() {

0 commit comments

Comments
 (0)