Skip to content

Commit 8467279

Browse files
committed
Add a new AST node for unsuffixed integer types.
1 parent baf58a7 commit 8467279

File tree

9 files changed

+63
-36
lines changed

9 files changed

+63
-36
lines changed

src/libsyntax/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ enum lit_ {
402402
lit_str(str),
403403
lit_int(i64, int_ty),
404404
lit_uint(u64, uint_ty),
405+
lit_int_unsuffixed(i64, int_ty),
405406
lit_float(str, float_ty),
406407
lit_nil,
407408
lit_bool(bool),

src/libsyntax/parse/classify.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ fn need_parens(expr: @ast::expr, outer_prec: uint) -> bool {
5151

5252
fn ends_in_lit_int(ex: @ast::expr) -> bool {
5353
alt ex.node {
54-
ast::expr_lit(@{node: ast::lit_int(_, ast::ty_i), _}) { true }
54+
ast::expr_lit(node) {
55+
alt node {
56+
@{node: ast::lit_int(_, ast::ty_i), _} |
57+
@{node: ast::lit_int_unsuffixed(_, ast::ty_i), _}
58+
{ true }
59+
_ { false }
60+
}
61+
}
5562
ast::expr_binary(_, _, sub) | ast::expr_unary(_, sub) |
5663
ast::expr_move(_, sub) | ast::expr_copy(sub) |
5764
ast::expr_assign(_, sub) |

src/libsyntax/parse/lexer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,10 @@ fn scan_number(c: char, rdr: reader) -> token::token {
282282
rdr.fatal("no valid digits found for number");
283283
}
284284
let parsed = option::get(u64::from_str_radix(num_str, base as u64));
285-
ret token::LIT_INT(parsed as i64, ast::ty_i);
285+
286+
#debug["lexing %s as an unsuffixed integer literal",
287+
num_str];
288+
ret token::LIT_INT_UNSUFFIXED(parsed as i64, ast::ty_i);
286289
}
287290
}
288291

src/libsyntax/parse/parser.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ class parser {
507507
let lo = self.span.lo;
508508
self.bump();
509509
alt copy self.token {
510-
token::LIT_INT(num, ty_i) {
510+
token::LIT_INT_UNSUFFIXED(num, _) {
511511
self.bump();
512512
some(mac_var(num as uint))
513513
}
@@ -519,7 +519,7 @@ class parser {
519519
some(mac_aq(mk_sp(lo,hi), e))
520520
}
521521
_ {
522-
self.fatal("expected `(` or integer literal");
522+
self.fatal("expected `(` or unsuffixed integer literal");
523523
}
524524
}
525525
}
@@ -540,7 +540,7 @@ class parser {
540540
token::UNDERSCORE {
541541
self.bump(); some(vstore_fixed(none))
542542
}
543-
token::LIT_INT(i, ty_i) if i >= 0i64 {
543+
token::LIT_INT_UNSUFFIXED(i, _) if i >= 0i64 {
544544
self.bump(); some(vstore_fixed(some(i as uint)))
545545
}
546546
token::BINOP(token::AND) {
@@ -559,6 +559,7 @@ class parser {
559559
alt tok {
560560
token::LIT_INT(i, it) { lit_int(i, it) }
561561
token::LIT_UINT(u, ut) { lit_uint(u, ut) }
562+
token::LIT_INT_UNSUFFIXED(i, it) { lit_int_unsuffixed(i, it) }
562563
token::LIT_FLOAT(s, ft) { lit_float(self.get_str(s), ft) }
563564
token::LIT_STR(s) { lit_str(self.get_str(s)) }
564565
token::LPAREN { self.expect(token::RPAREN); lit_nil }

src/libsyntax/parse/token.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ enum token {
5858
/* Literals */
5959
LIT_INT(i64, ast::int_ty),
6060
LIT_UINT(u64, ast::uint_ty),
61+
LIT_INT_UNSUFFIXED(i64, ast::int_ty),
6162
LIT_FLOAT(str_num, ast::float_ty),
6263
LIT_STR(str_num),
6364

@@ -132,6 +133,9 @@ fn to_str(in: interner<@str>, t: token) -> str {
132133
LIT_UINT(u, t) {
133134
ret uint::to_str(u as uint, 10u) + ast_util::uint_ty_to_str(t);
134135
}
136+
LIT_INT_UNSUFFIXED(i, t) {
137+
ret int::to_str(i as int, 10u) + ast_util::int_ty_to_str(t);
138+
}
135139
LIT_FLOAT(s, t) {
136140
ret *interner::get(in, s) +
137141
ast_util::float_ty_to_str(t);
@@ -161,6 +165,7 @@ pure fn can_begin_expr(t: token) -> bool {
161165
TILDE { true }
162166
LIT_INT(_, _) { true }
163167
LIT_UINT(_, _) { true }
168+
LIT_INT_UNSUFFIXED(_, _) { true }
164169
LIT_FLOAT(_, _) { true }
165170
LIT_STR(_) { true }
166171
POUND { true }
@@ -178,6 +183,7 @@ fn is_lit(t: token::token) -> bool {
178183
ret alt t {
179184
token::LIT_INT(_, _) { true }
180185
token::LIT_UINT(_, _) { true }
186+
token::LIT_INT_UNSUFFIXED(_, _) { true }
181187
token::LIT_FLOAT(_, _) { true }
182188
token::LIT_STR(_) { true }
183189
_ { false }

src/libsyntax/print/pprust.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,17 @@ fn print_literal(s: ps, &&lit: @ast::lit) {
16281628
u64::to_str(u, 10u)
16291629
+ ast_util::uint_ty_to_str(t));
16301630
}
1631+
ast::lit_int_unsuffixed(i, t) {
1632+
if i < 0_i64 {
1633+
word(s.s,
1634+
"-" + u64::to_str(-i as u64, 10u)
1635+
+ ast_util::int_ty_to_str(t));
1636+
} else {
1637+
word(s.s,
1638+
u64::to_str(i as u64, 10u)
1639+
+ ast_util::int_ty_to_str(t));
1640+
}
1641+
}
16311642
ast::lit_float(f, t) {
16321643
word(s.s, f + ast_util::float_ty_to_str(t));
16331644
}

src/rustc/middle/const_eval.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ fn lit_to_const(lit: @lit) -> const_val {
111111
lit_str(s) { const_str(s) }
112112
lit_int(n, _) { const_int(n) }
113113
lit_uint(n, _) { const_uint(n) }
114+
lit_int_unsuffixed(n, _) { const_int(n) }
114115
lit_float(n, _) { const_float(option::get(float::from_str(n)) as f64) }
115116
lit_nil { const_int(0i64) }
116117
lit_bool(b) { const_int(b as i64) }

src/rustc/middle/trans/base.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,11 @@ fn trans_crate_lit(cx: @crate_ctxt, lit: ast::lit) -> ValueRef {
14831483
alt lit.node {
14841484
ast::lit_int(i, t) { C_integral(T_int_ty(cx, t), i as u64, True) }
14851485
ast::lit_uint(u, t) { C_integral(T_uint_ty(cx, t), u, False) }
1486+
ast::lit_int_unsuffixed(i, t) {
1487+
// FIXME (#1425): should we be using cx.fcx.infcx to figure out what
1488+
// to actually generate from this?
1489+
C_integral(T_int_ty(cx, t), i as u64, True)
1490+
}
14861491
ast::lit_float(fs, t) { C_floating(fs, T_float_ty(cx, t)) }
14871492
ast::lit_bool(b) { C_bool(b) }
14881493
ast::lit_nil { C_nil() }

src/rustc/middle/typeck/check.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -629,40 +629,32 @@ fn check_lit(fcx: @fn_ctxt, lit: @ast::lit) -> ty::t {
629629

630630
alt lit.node {
631631
ast::lit_str(_) { ty::mk_str(tcx) }
632-
ast::lit_int(v, t) {
633-
alt t {
634-
ty_char | ty_i8 | ty_i16 | ty_i32 | ty_i64 {
635-
// If it's a char or has an explicit suffix, give it the
636-
// appropriate integral type.
637-
ty::mk_mach_int(tcx, t)
638-
}
639-
ty_i {
640-
// Otherwise, an unsuffixed integer literal parses to a
641-
// `ty_i`. In that case, it could have any integral type,
642-
// so create an integral type variable for it.
643-
let vid = fcx.infcx.next_ty_var_integral_id();
644-
645-
// We need to sniff at the value `v` provided and figure
646-
// out how big of an int it is; that determines the set of
647-
// possibly types it could take on.
648-
let possible_types = alt v {
649-
0i64 to 127i64 { min_8bit_tys() }
650-
128i64 to 65535i64 { min_16bit_tys() }
651-
65536i64 to 4294967295i64 { min_32bit_tys() }
652-
_ { min_64bit_tys() }
653-
};
632+
ast::lit_int(_, t) { ty::mk_mach_int(tcx, t) }
633+
ast::lit_uint(_, t) { ty::mk_mach_uint(tcx, t) }
634+
ast::lit_int_unsuffixed(v, t) {
635+
// An unsuffixed integer literal could have any integral type,
636+
// so we create an integral type variable for it.
637+
let vid = fcx.infcx.next_ty_var_integral_id();
638+
639+
// We need to sniff at the value `v` and figure out how big of
640+
// an int it is; that determines the range of possible types
641+
// that the integral type variable could take on.
642+
let possible_types = alt v {
643+
0i64 to 127i64 { min_8bit_tys() }
644+
128i64 to 65535i64 { min_16bit_tys() }
645+
65536i64 to 4294967295i64 { min_32bit_tys() }
646+
_ { min_64bit_tys() }
647+
};
654648

655-
// Store the set of possible types
656-
fcx.infcx.set(fcx.infcx.tvib, vid,
657-
root(possible_types));
658-
ty::mk_var_integral(tcx, vid);
649+
// Store the set of possible types and return the integral
650+
// type variable.
651+
fcx.infcx.set(fcx.infcx.tvib, vid,
652+
root(possible_types));
653+
ty::mk_var_integral(tcx, vid);
659654

660-
// FIXME: remove me when #1425 is finished.
661-
ty::mk_mach_int(tcx, t)
662-
}
663-
}
655+
// FIXME: remove me when #1425 is finished.
656+
ty::mk_mach_int(tcx, t)
664657
}
665-
ast::lit_uint(_, t) { ty::mk_mach_uint(tcx, t) }
666658
ast::lit_float(_, t) { ty::mk_mach_float(tcx, t) }
667659
ast::lit_nil { ty::mk_nil(tcx) }
668660
ast::lit_bool(_) { ty::mk_bool(tcx) }

0 commit comments

Comments
 (0)