Skip to content

Commit

Permalink
Made matching machine types equal to float, int, uint (fixes #1376)
Browse files Browse the repository at this point in the history
  • Loading branch information
boggle authored and marijnh committed Dec 28, 2011
1 parent b9b9b3e commit 7fd62bb
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/comp/middle/ty.rs
Expand Up @@ -626,6 +626,25 @@ pure fn struct(cx: ctxt, typ: t) -> sty {
}
}

// Returns struact(cx, typ) but replaces all occurences of platform
// dependent primitive types with their machine type equivalent
pure fn mach_struct(cx: ctxt, cfg: @session::config, typ: t) -> sty {
alt interner::get(*cx.ts, typ).struct {
ty_named(t, _) { mach_struct(cx, cfg, t) }
s { mach_sty(cfg, s) }
}
}

// Converts s to its machine type equivalent
pure fn mach_sty(cfg: @session::config, s: sty) -> sty {
alt s {
ty_int(ast::ty_i.) { ty_int(cfg.int_type) }
ty_uint(ast::ty_u.) { ty_uint(cfg.uint_type) }
ty_float(ast::ty_f.) { ty_float(cfg.float_type) }
s { s }
}
}

pure fn ty_name(cx: ctxt, typ: t) -> option::t<@str> {
alt interner::get(*cx.ts, typ).struct {
ty_named(_, n) { some(n) }
Expand Down Expand Up @@ -1752,7 +1771,9 @@ mod unify {

// Simple structural type comparison.
fn struct_cmp(cx: @ctxt, expected: t, actual: t) -> result {
if struct(cx.tcx, expected) == struct(cx.tcx, actual) {
let tcx = cx.tcx;
let cfg = tcx.sess.get_targ_cfg();
if mach_struct(tcx, cfg, expected) == mach_struct(tcx, cfg, actual) {
ret ures_ok(expected);
}
ret ures_err(terr_mismatch);
Expand Down

3 comments on commit 7fd62bb

@nikomatsakis
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am reading this correctly, it unifies uint and u32/u64 (depending on target). Does this mean that you can have a program which typechecks when targeting 64-bit but not 32-bit? This seems... unfortunate. I would expect that if I am fastidious about using uint and int, my program will be portable.

I understand the problem of the math module, but I'd rather this unification be "opt-in", either by adding another integral type or by having an attribute on the module being type-checked.

@boggle
Copy link
Contributor Author

@boggle boggle commented on 7fd62bb Jan 1, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you see the thread on #1376? It is a trade-off and I would like to keep this patch for now, perhaps we could add an option to rustc that will produce warnings or errrors in the presence of potential overflows.

@nikomatsakis
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed that thread. ok, sounds good.

Please sign in to comment.