Skip to content

Commit

Permalink
rustc: Warn when int or uint is used in a native type decl
Browse files Browse the repository at this point in the history
  • Loading branch information
lht committed Jan 18, 2012
1 parent 94cd792 commit 6db9eed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/comp/middle/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,25 @@ fn check_method(ccx: @crate_ctxt, method: @ast::method) {
check_fn(ccx, ast::proto_bare, method.decl, method.body, method.id, none);
}

fn check_native_fn(ccx: @crate_ctxt, decl: ast::fn_decl) {
let tys = vec::map(decl.inputs) {|a| a.ty };
for ty in (tys + [decl.output]) {
alt ty.node {
ast::ty_int(ast::ty_i.) {
ccx.tcx.sess.span_warn(
ty.span, "found rust type `int` in native module, while " +
"ctypes::c_int or ctypes::long should be used");
}
ast::ty_uint(ast::ty_u.) {
ccx.tcx.sess.span_warn(
ty.span, "found rust type `uint` in native module, while " +
"ctypes::c_uint or ctypes::ulong should be used");
}
_ { }
}
}
}

fn check_item(ccx: @crate_ctxt, it: @ast::item) {
alt it.node {
ast::item_const(_, e) { check_const(ccx, it.span, e, it.id); }
Expand All @@ -2690,6 +2709,16 @@ fn check_item(ccx: @crate_ctxt, it: @ast::item) {
ast::item_res(decl, tps, body, dtor_id, _) {
check_fn(ccx, ast::proto_bare, decl, body, dtor_id, none);
}
ast::item_native_mod(nmod) {
for ni in nmod.items {
alt ni.node {
ast::native_item_fn(decl, tps) {
check_native_fn(ccx, decl);
}
_ { }
}
}
}
ast::item_impl(tps, _, ty, ms) {
ccx.self_infos += [self_impl(ast_ty_to_ty(ccx.tcx, m_check, ty))];
for m in ms { check_method(ccx, m); }
Expand Down
9 changes: 9 additions & 0 deletions src/test/compile-fail/warn-native-int-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//error-pattern:ctypes::c_int or ctypes::long should be used
native mod xx {
fn strlen(str: *u8) -> uint;
fn foo(x: int, y: uint);
}

fn main() {
"let compile fail to verify warning message" = 999;
}

0 comments on commit 6db9eed

Please sign in to comment.