Skip to content

Commit 37a2985

Browse files
committed
rustc: Warn when int or uint is used in a native type decl
Issue rust-lang#1403
1 parent e1c50c4 commit 37a2985

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
@@ -2678,6 +2678,25 @@ fn check_method(ccx: @crate_ctxt, method: @ast::method) {
26782678
check_fn(ccx, ast::proto_bare, method.decl, method.body, method.id, none);
26792679
}
26802680

2681+
fn check_native_fn(ccx: @crate_ctxt, decl: ast::fn_decl) {
2682+
let tys = vec::map(decl.inputs) {|a| a.ty };
2683+
for ty in (tys + [decl.output]) {
2684+
alt ty.node {
2685+
ast::ty_int(ast::ty_i.) {
2686+
ccx.tcx.sess.span_warn(
2687+
ty.span, "found rust type `int` in native module, while " +
2688+
"ctypes::c_int or ctypes::long should be used");
2689+
}
2690+
ast::ty_uint(ast::ty_u.) {
2691+
ccx.tcx.sess.span_warn(
2692+
ty.span, "found rust type `uint` in native module, while " +
2693+
"ctypes::c_uint or ctypes::ulong should be used");
2694+
}
2695+
_ { }
2696+
}
2697+
}
2698+
}
2699+
26812700
fn check_item(ccx: @crate_ctxt, it: @ast::item) {
26822701
alt it.node {
26832702
ast::item_const(_, e) { check_const(ccx, it.span, e, it.id); }
@@ -2688,6 +2707,16 @@ fn check_item(ccx: @crate_ctxt, it: @ast::item) {
26882707
ast::item_res(decl, tps, body, dtor_id, _) {
26892708
check_fn(ccx, ast::proto_bare, decl, body, dtor_id, none);
26902709
}
2710+
ast::item_native_mod(nmod) {
2711+
for ni in nmod.items {
2712+
alt ni.node {
2713+
ast::native_item_fn(decl, tps) {
2714+
check_native_fn(ccx, decl);
2715+
}
2716+
_ { }
2717+
}
2718+
}
2719+
}
26912720
ast::item_impl(tps, _, ty, ms) {
26922721
ccx.self_infos += [self_impl(ast_ty_to_ty(ccx.tcx, m_check, ty))];
26932722
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)