Auto merge of #26583 - eefriedman:lint-ffi, r=nrc

Makes the lint a bit more accurate, and improves the quality of the diagnostic
messages by explicitly returning an error message.
This commit is contained in:
bors 2015-07-24 02:40:42 +00:00
commit 68e0d13bfd
14 changed files with 349 additions and 181 deletions

View file

@ -37,7 +37,7 @@ struct D {
}
extern "C" {
fn foo(x: A); //~ ERROR found type without foreign-function-safe
fn foo(x: A); //~ ERROR found struct without foreign-function-safe
fn bar(x: B); //~ ERROR foreign-function-safe
fn baz(x: C);
fn qux(x: A2); //~ ERROR foreign-function-safe

View file

@ -11,7 +11,7 @@
#![deny(warnings)]
extern {
pub fn foo(x: (isize)); //~ ERROR found rust type `isize` in foreign module
pub fn foo(x: (isize)); //~ ERROR found Rust type `isize` in foreign module
}
fn main() {

View file

@ -18,9 +18,9 @@ enum T { E, F, G }
extern {
fn zf(x: Z);
fn uf(x: U); //~ ERROR found type without foreign-function-safe
fn bf(x: B); //~ ERROR found type without foreign-function-safe
fn tf(x: T); //~ ERROR found type without foreign-function-safe
fn uf(x: U); //~ ERROR found enum without foreign-function-safe
fn bf(x: B); //~ ERROR found enum without foreign-function-safe
fn tf(x: T); //~ ERROR found enum without foreign-function-safe
}
pub fn main() { }

View file

@ -13,14 +13,45 @@
extern crate libc;
trait Mirror { type It; }
impl<T> Mirror for T { type It = Self; }
#[repr(C)]
pub struct StructWithProjection(*mut <StructWithProjection as Mirror>::It);
#[repr(C)]
pub struct StructWithProjectionAndLifetime<'a>(
&'a mut <StructWithProjectionAndLifetime<'a> as Mirror>::It
);
pub type I32Pair = (i32, i32);
#[repr(C)]
pub struct ZeroSize;
pub type RustFn = fn();
pub type RustBadRet = extern fn() -> Box<u32>;
extern {
pub fn bare_type1(size: isize); //~ ERROR: found rust type
pub fn bare_type2(size: usize); //~ ERROR: found rust type
pub fn ptr_type1(size: *const isize); //~ ERROR: found rust type
pub fn ptr_type2(size: *const usize); //~ ERROR: found rust type
pub fn bare_type1(size: isize); //~ ERROR: found Rust type
pub fn bare_type2(size: usize); //~ ERROR: found Rust type
pub fn ptr_type1(size: *const isize); //~ ERROR: found Rust type
pub fn ptr_type2(size: *const usize); //~ ERROR: found Rust type
pub fn slice_type(p: &[u32]); //~ ERROR: found Rust slice type
pub fn str_type(p: &str); //~ ERROR: found Rust type
pub fn box_type(p: Box<u32>); //~ ERROR found Rust type
pub fn char_type(p: char); //~ ERROR found Rust type
pub fn trait_type(p: &Clone); //~ ERROR found Rust trait type
pub fn tuple_type(p: (i32, i32)); //~ ERROR found Rust tuple type
pub fn tuple_type2(p: I32Pair); //~ ERROR found Rust tuple type
pub fn zero_size(p: ZeroSize); //~ ERROR found zero-size struct
pub fn fn_type(p: RustFn); //~ ERROR found function pointer with Rust
pub fn fn_type2(p: fn()); //~ ERROR found function pointer with Rust
pub fn fn_contained(p: RustBadRet); //~ ERROR: found Rust type
pub fn good1(size: *const libc::c_int);
pub fn good2(size: *const libc::c_uint);
pub fn good3(fptr: Option<extern fn()>);
pub fn good4(aptr: &[u8; 4 as usize]);
pub fn good5(s: StructWithProjection);
pub fn good6(s: StructWithProjectionAndLifetime);
pub fn good7(fptr: extern fn() -> ());
pub fn good8(fptr: extern fn() -> !);
}
fn main() {

View file

@ -13,9 +13,9 @@
mod xx {
extern {
pub fn strlen(str: *const u8) -> usize; //~ ERROR found rust type `usize`
pub fn foo(x: isize, y: usize); //~ ERROR found rust type `isize`
//~^ ERROR found rust type `usize`
pub fn strlen(str: *const u8) -> usize; //~ ERROR found Rust type `usize`
pub fn foo(x: isize, y: usize); //~ ERROR found Rust type `isize`
//~^ ERROR found Rust type `usize`
}
}

View file

@ -9,7 +9,9 @@
// except according to those terms.
#![feature(rustc_private)]
#![feature(libc)]
extern crate libc;
extern crate rustc;
extern crate rustc_driver;
extern crate rustc_lint;
@ -29,6 +31,7 @@ use rustc::session::config::{self, basic_options, build_configuration, Input, Op
use rustc::session::build_session;
use rustc_driver::driver;
use rustc_resolve::MakeGlobMap;
use libc::c_void;
use syntax::diagnostics::registry::Registry;
@ -111,7 +114,7 @@ impl ExecutionEngine {
}
/// Returns a raw pointer to the named function.
pub fn get_function(&mut self, name: &str) -> Option<*const ()> {
pub fn get_function(&mut self, name: &str) -> Option<*const c_void> {
let s = CString::new(name.as_bytes()).unwrap();
for &m in &self.modules {
@ -128,7 +131,7 @@ impl ExecutionEngine {
}
/// Returns a raw pointer to the named global item.
pub fn get_global(&mut self, name: &str) -> Option<*const ()> {
pub fn get_global(&mut self, name: &str) -> Option<*const c_void> {
let s = CString::new(name.as_bytes()).unwrap();
for &m in &self.modules {