Skip to content

Commit a75c0b3

Browse files
committed
Merge pull request #4268 from catamorphism/issue-3477
Emit a type error for integer literals where the expected type is char
2 parents 13879d8 + 65839fa commit a75c0b3

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/librustc/middle/ty.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ export kind_is_durable;
162162
export meta_kind, kind_lteq, type_kind, type_kind_ext;
163163
export operators;
164164
export type_err, terr_vstore_kind;
165-
export terr_mismatch, terr_onceness_mismatch;
165+
export terr_integer_as_char, terr_mismatch, terr_onceness_mismatch;
166166
export type_err_to_str, note_and_explain_type_err;
167167
export expected_found;
168168
export type_needs_drop;
169+
export type_is_char;
169170
export type_is_empty;
170171
export type_is_integral;
171172
export type_is_numeric;
@@ -714,6 +715,7 @@ enum type_err {
714715
terr_in_field(@type_err, ast::ident),
715716
terr_sorts(expected_found<t>),
716717
terr_self_substs,
718+
terr_integer_as_char,
717719
terr_no_integral_type,
718720
terr_no_floating_point_type,
719721
}
@@ -2541,6 +2543,13 @@ fn type_is_integral(ty: t) -> bool {
25412543
}
25422544
}
25432545
2546+
fn type_is_char(ty: t) -> bool {
2547+
match get(ty).sty {
2548+
ty_int(ty_char) => true,
2549+
_ => false
2550+
}
2551+
}
2552+
25442553
fn type_is_fp(ty: t) -> bool {
25452554
match get(ty).sty {
25462555
ty_infer(FloatVar(_)) | ty_float(_) => true,
@@ -3510,6 +3519,10 @@ fn type_err_to_str(cx: ctxt, err: &type_err) -> ~str {
35103519
~"couldn't determine an appropriate integral type for integer \
35113520
literal"
35123521
}
3522+
terr_integer_as_char => {
3523+
~"integer literals can't be inferred to char type \
3524+
(try an explicit cast)"
3525+
}
35133526
terr_no_floating_point_type => {
35143527
~"couldn't determine an appropriate floating point type for \
35153528
floating point literal"

src/librustc/middle/typeck/infer/unify.rs

+4
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ impl infer_ctxt {
366366
}
367367

368368
fn int_var_sub_t(a_id: ty::IntVid, b: ty::t) -> ures {
369+
if ty::type_is_char(b) {
370+
return Err(ty::terr_integer_as_char);
371+
}
372+
369373
assert ty::type_is_integral(b);
370374

371375
let vb = &self.int_var_bindings;

src/test/compile-fail/issue-3477.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
let _p: char = 100; //~ ERROR mismatched types: expected `char` but found
3+
}

0 commit comments

Comments
 (0)