Skip to content

Commit 6db9eed

Browse files
committed
rustc: Warn when int or uint is used in a native type decl
Issue rust-lang#1403
1 parent 94cd792 commit 6db9eed

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/comp/middle/typeck.rs

+29
Original file line numberDiff line numberDiff line change
@@ -2680,6 +2680,25 @@ fn check_method(ccx: @crate_ctxt, method: @ast::method) {
26802680
check_fn(ccx, ast::proto_bare, method.decl, method.body, method.id, none);
26812681
}
26822682

2683+
fn check_native_fn(ccx: @crate_ctxt, decl: ast::fn_decl) {
2684+
let tys = vec::map(decl.inputs) {|a| a.ty };
2685+
for ty in (tys + [decl.output]) {
2686+
alt ty.node {
2687+
ast::ty_int(ast::ty_i.) {
2688+
ccx.tcx.sess.span_warn(
2689+
ty.span, "found rust type `int` in native module, while " +
2690+
"ctypes::c_int or ctypes::long should be used");
2691+
}
2692+
ast::ty_uint(ast::ty_u.) {
2693+
ccx.tcx.sess.span_warn(
2694+
ty.span, "found rust type `uint` in native module, while " +
2695+
"ctypes::c_uint or ctypes::ulong should be used");
2696+
}
2697+
_ { }
2698+
}
2699+
}
2700+
}
2701+
26832702
fn check_item(ccx: @crate_ctxt, it: @ast::item) {
26842703
alt it.node {
26852704
ast::item_const(_, e) { check_const(ccx, it.span, e, it.id); }
@@ -2690,6 +2709,16 @@ fn check_item(ccx: @crate_ctxt, it: @ast::item) {
26902709
ast::item_res(decl, tps, body, dtor_id, _) {
26912710
check_fn(ccx, ast::proto_bare, decl, body, dtor_id, none);
26922711
}
2712+
ast::item_native_mod(nmod) {
2713+
for ni in nmod.items {
2714+
alt ni.node {
2715+
ast::native_item_fn(decl, tps) {
2716+
check_native_fn(ccx, decl);
2717+
}
2718+
_ { }
2719+
}
2720+
}
2721+
}
26932722
ast::item_impl(tps, _, ty, ms) {
26942723
ccx.self_infos += [self_impl(ast_ty_to_ty(ccx.tcx, m_check, ty))];
26952724
for m in ms { check_method(ccx, m); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//error-pattern:ctypes::c_int or ctypes::long should be used
2+
native mod xx {
3+
fn strlen(str: *u8) -> uint;
4+
fn foo(x: int, y: uint);
5+
}
6+
7+
fn main() {
8+
"let compile fail to verify warning message" = 999;
9+
}

0 commit comments

Comments
 (0)