Allow generic foreign functions.
Generic extern functions written in Rust have their names mangled, as well as their internal clownshoe __rust_abi functions. This allows e.g. specific monomorphizations of these functions to be used as callbacks. Closes #12502.
This commit is contained in:
parent
2a47fa708c
commit
e6e6ef24ab
11 changed files with 198 additions and 63 deletions
|
|
@ -8,8 +8,10 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
extern "C" fn foo<T>() {} //~ERROR foreign functions may not use type parameters
|
||||
extern {
|
||||
fn foo<T>(); //~ ERROR foreign items may not have type parameters
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = foo::<int>;
|
||||
foo::<i32>();
|
||||
}
|
||||
|
|
|
|||
8
src/test/run-make/extern-fn-generic/Makefile
Normal file
8
src/test/run-make/extern-fn-generic/Makefile
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
-include ../tools.mk
|
||||
|
||||
all:
|
||||
$(CC) -std=c99 test.c -c -o $(TMPDIR)/test.o
|
||||
$(AR) rcs $(TMPDIR)/libtest.a $(TMPDIR)/test.o
|
||||
$(RUSTC) testcrate.rs -L $(TMPDIR)
|
||||
$(RUSTC) test.rs -L $(TMPDIR)
|
||||
$(call RUN,test) || exit 1
|
||||
16
src/test/run-make/extern-fn-generic/test.c
Normal file
16
src/test/run-make/extern-fn-generic/test.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include <stdint.h>
|
||||
|
||||
typedef struct TestStruct {
|
||||
uint8_t x;
|
||||
int32_t y;
|
||||
} TestStruct;
|
||||
|
||||
typedef int callback(TestStruct s);
|
||||
|
||||
uint32_t call(callback *c) {
|
||||
TestStruct s;
|
||||
s.x = 'a';
|
||||
s.y = 3;
|
||||
|
||||
return c(s);
|
||||
}
|
||||
32
src/test/run-make/extern-fn-generic/test.rs
Normal file
32
src/test/run-make/extern-fn-generic/test.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright 2014 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 crate testcrate;
|
||||
|
||||
extern "C" fn bar<T>(ts: testcrate::TestStruct<T>) -> T { ts.y }
|
||||
|
||||
#[link(name = "test")]
|
||||
extern {
|
||||
fn call(c: extern "C" fn(testcrate::TestStruct<i32>) -> i32) -> i32;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Let's test calling it cross crate
|
||||
let back = unsafe {
|
||||
testcrate::call(testcrate::foo::<i32>)
|
||||
};
|
||||
assert_eq!(3, back);
|
||||
|
||||
// And just within this crate
|
||||
let back = unsafe {
|
||||
call(bar::<i32>)
|
||||
};
|
||||
assert_eq!(3, back);
|
||||
}
|
||||
24
src/test/run-make/extern-fn-generic/testcrate.rs
Normal file
24
src/test/run-make/extern-fn-generic/testcrate.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2014 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.
|
||||
|
||||
#![crate_type = "lib"]
|
||||
|
||||
#[repr(C)]
|
||||
pub struct TestStruct<T> {
|
||||
pub x: u8,
|
||||
pub y: T
|
||||
}
|
||||
|
||||
pub extern "C" fn foo<T>(ts: TestStruct<T>) -> T { ts.y }
|
||||
|
||||
#[link(name = "test")]
|
||||
extern {
|
||||
pub fn call(c: extern "C" fn(TestStruct<i32>) -> i32) -> i32;
|
||||
}
|
||||
16
src/test/run-pass/generic-extern-mangle.rs
Normal file
16
src/test/run-pass/generic-extern-mangle.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Copyright 2014 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 "C" fn foo<T: Int>(a: T, b: T) -> T { a + b }
|
||||
|
||||
fn main() {
|
||||
assert_eq!(99u8, foo(255u8, 100u8));
|
||||
assert_eq!(99u16, foo(65535u16, 100u16));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue