Skip to content

Commit efc7f82

Browse files
committed
Revamp foreign code not to consider the Rust modes. This requires
adjusting a few foreign functions that were declared with by-ref mode. This also allows us to remove by-val mode in the near future. With copy mode, though, we have to be careful because Rust will implicitly pass somethings by pointer but this may not be the C ABI rules. For example, rust will pass a struct Foo as a Foo*. So I added some code into the adapters to fix this (though the C ABI rules may put the pointer back, oh well). This patch also includes a lint mode for the use of by-ref mode in foreign functions as the semantics of this have changed.
1 parent 4d8ddff commit efc7f82

18 files changed

+492
-161
lines changed

src/libcore/rt/thread.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct Thread {
2222
impl Thread {
2323
static fn start(main: ~fn()) -> Thread {
2424
fn substart(main: &fn()) -> *raw_thread {
25-
unsafe { rust_raw_thread_start(main) }
25+
unsafe { rust_raw_thread_start(&main) }
2626
}
2727
let raw = substart(main);
2828
Thread {
@@ -39,6 +39,6 @@ impl Drop for Thread {
3939
}
4040

4141
extern {
42-
pub unsafe fn rust_raw_thread_start(f: &fn()) -> *raw_thread;
42+
pub unsafe fn rust_raw_thread_start(f: &(&fn())) -> *raw_thread;
4343
pub unsafe fn rust_raw_thread_join_delete(thread: *raw_thread);
4444
}

src/libcore/unstable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod rustrt {
4747
pub unsafe fn rust_lock_little_lock(lock: rust_little_lock);
4848
pub unsafe fn rust_unlock_little_lock(lock: rust_little_lock);
4949

50-
pub unsafe fn rust_raw_thread_start(f: &fn()) -> *raw_thread;
50+
pub unsafe fn rust_raw_thread_start(f: &(&fn())) -> *raw_thread;
5151
pub unsafe fn rust_raw_thread_join_delete(thread: *raw_thread);
5252
}
5353
}
@@ -72,7 +72,7 @@ pub fn run_in_bare_thread(f: ~fn()) {
7272
let closure: &fn() = || {
7373
f()
7474
};
75-
let thread = rustrt::rust_raw_thread_start(closure);
75+
let thread = rustrt::rust_raw_thread_start(&closure);
7676
rustrt::rust_raw_thread_join_delete(thread);
7777
chan.send(());
7878
}

src/librustc/lib/llvm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ pub mod llvm {
14431443
/** Prepares inline assembly. */
14441444
pub unsafe fn LLVMInlineAsm(Ty: TypeRef, AsmString: *c_char,
14451445
Constraints: *c_char, SideEffects: Bool,
1446-
AlignStack: Bool, Dialect: AsmDialect)
1446+
AlignStack: Bool, Dialect: c_uint)
14471447
-> ValueRef;
14481448
}
14491449
}

src/librustc/middle/lint.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub enum lint {
7878
deprecated_self,
7979
deprecated_mutable_fields,
8080
deprecated_drop,
81+
foreign_mode,
8182

8283
managed_heap_memory,
8384
owned_heap_memory,
@@ -182,6 +183,13 @@ pub fn get_lint_dict() -> LintDict {
182183
default: warn
183184
}),
184185

186+
(@~"foreign_mode",
187+
@LintSpec {
188+
lint: foreign_mode,
189+
desc: "warn about deprecated uses of modes in foreign fns",
190+
default: warn
191+
}),
192+
185193
(@~"deprecated_pattern",
186194
@LintSpec {
187195
lint: deprecated_pattern,
@@ -753,6 +761,20 @@ fn check_item_ctypes(cx: ty::ctxt, it: @ast::item) {
753761

754762
fn check_foreign_fn(cx: ty::ctxt, fn_id: ast::node_id,
755763
decl: &ast::fn_decl) {
764+
// warn about `&&` mode on foreign functions, both because it is
765+
// deprecated and because its semantics have changed recently:
766+
for decl.inputs.eachi |i, arg| {
767+
match ty::resolved_mode(cx, arg.mode) {
768+
ast::by_val | ast::by_copy => {}
769+
ast::by_ref => {
770+
cx.sess.span_lint(
771+
foreign_mode, fn_id, fn_id, arg.ty.span,
772+
fmt!("foreign function uses `&&` mode \
773+
on argument %u", i));
774+
}
775+
}
776+
}
777+
756778
let tys = vec::map(decl.inputs, |a| a.ty );
757779
for vec::each(vec::append_one(tys, decl.output)) |ty| {
758780
match ty.node {
@@ -785,7 +807,7 @@ fn check_item_ctypes(cx: ty::ctxt, it: @ast::item) {
785807
if attr::foreign_abi(it.attrs) !=
786808
either::Right(ast::foreign_abi_rust_intrinsic) => {
787809
for nmod.items.each |ni| {
788-
match /*bad*/copy ni.node {
810+
match ni.node {
789811
ast::foreign_item_fn(ref decl, _, _) => {
790812
check_foreign_fn(cx, it.id, decl);
791813
}

src/librustc/middle/trans/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ pub fn InlineAsmCall(cx: block, asm: *c_char, cons: *c_char,
885885
886886
let llfty = T_fn(~[], T_void());
887887
let v = llvm::LLVMInlineAsm(llfty, asm, cons, volatile,
888-
alignstack, dia);
888+
alignstack, dia as c_uint);
889889
890890
Call(cx, v, ~[])
891891
}

src/librustc/middle/trans/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ pub fn trans_arg_expr(bcx: block,
757757

758758
if formal_ty.ty != arg_datum.ty {
759759
// this could happen due to e.g. subtyping
760-
let llformal_ty = type_of::type_of_explicit_arg(ccx, formal_ty);
760+
let llformal_ty = type_of::type_of_explicit_arg(ccx, &formal_ty);
761761
debug!("casting actual type (%s) to match formal (%s)",
762762
bcx.val_str(val), bcx.llty_str(llformal_ty));
763763
val = PointerCast(bcx, val, llformal_ty);

0 commit comments

Comments
 (0)