Skip to content

Commit

Permalink
Blacklist fn item types from being used with variadic functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Apr 8, 2016
1 parent 7979dd6 commit 942d4c7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
21 changes: 15 additions & 6 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -2578,24 +2578,33 @@ fn check_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
ty::TyFloat(ast::FloatTy::F32) => {
fcx.type_error_message(arg.span,
|t| {
format!("can't pass an {} to variadic \
function, cast to c_double", t)
format!("can't pass an `{}` to variadic \
function, cast to `c_double`", t)
}, arg_ty, None);
}
ty::TyInt(ast::IntTy::I8) | ty::TyInt(ast::IntTy::I16) | ty::TyBool => {
fcx.type_error_message(arg.span, |t| {
format!("can't pass {} to variadic \
function, cast to c_int",
format!("can't pass `{}` to variadic \
function, cast to `c_int`",
t)
}, arg_ty, None);
}
ty::TyUint(ast::UintTy::U8) | ty::TyUint(ast::UintTy::U16) => {
fcx.type_error_message(arg.span, |t| {
format!("can't pass {} to variadic \
function, cast to c_uint",
format!("can't pass `{}` to variadic \
function, cast to `c_uint`",
t)
}, arg_ty, None);
}
ty::TyFnDef(_, _, f) => {
let ptr_ty = fcx.tcx().mk_ty(ty::TyFnPtr(f));
let ptr_ty = fcx.infcx().resolve_type_vars_if_possible(&ptr_ty);
fcx.type_error_message(arg.span,
|t| {
format!("can't pass `{}` to variadic \
function, cast to `{}`", t, ptr_ty)
}, arg_ty, None);
}
_ => {}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/compile-fail/issue-32201.rs
@@ -0,0 +1,22 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern {
fn foo(a: i32, ...);
}

fn bar(_: *const u8) {}

fn main() {
unsafe {
foo(0, bar);
//~^ ERROR can't pass `fn(*const u8) {bar}` to variadic function, cast to `fn(*const u8)`
}
}
12 changes: 6 additions & 6 deletions src/test/compile-fail/variadic-ffi-3.rs
Expand Up @@ -33,11 +33,11 @@ fn main() {
//~| expected variadic fn
//~| found non-variadic function

foo(1, 2, 3f32); //~ ERROR: can't pass an f32 to variadic function, cast to c_double
foo(1, 2, true); //~ ERROR: can't pass bool to variadic function, cast to c_int
foo(1, 2, 1i8); //~ ERROR: can't pass i8 to variadic function, cast to c_int
foo(1, 2, 1u8); //~ ERROR: can't pass u8 to variadic function, cast to c_uint
foo(1, 2, 1i16); //~ ERROR: can't pass i16 to variadic function, cast to c_int
foo(1, 2, 1u16); //~ ERROR: can't pass u16 to variadic function, cast to c_uint
foo(1, 2, 3f32); //~ ERROR: can't pass an `f32` to variadic function, cast to `c_double`
foo(1, 2, true); //~ ERROR: can't pass `bool` to variadic function, cast to `c_int`
foo(1, 2, 1i8); //~ ERROR: can't pass `i8` to variadic function, cast to `c_int`
foo(1, 2, 1u8); //~ ERROR: can't pass `u8` to variadic function, cast to `c_uint`
foo(1, 2, 1i16); //~ ERROR: can't pass `i16` to variadic function, cast to `c_int`
foo(1, 2, 1u16); //~ ERROR: can't pass `u16` to variadic function, cast to `c_uint`
}
}

0 comments on commit 942d4c7

Please sign in to comment.