Skip to content

Emit a type error for integer literals where the expected type is char #4268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ export kind_is_durable;
export meta_kind, kind_lteq, type_kind, type_kind_ext;
export operators;
export type_err, terr_vstore_kind;
export terr_mismatch, terr_onceness_mismatch;
export terr_integer_as_char, terr_mismatch, terr_onceness_mismatch;
export type_err_to_str, note_and_explain_type_err;
export expected_found;
export type_needs_drop;
export type_is_char;
export type_is_empty;
export type_is_integral;
export type_is_numeric;
Expand Down Expand Up @@ -696,6 +697,7 @@ enum type_err {
terr_in_field(@type_err, ast::ident),
terr_sorts(expected_found<t>),
terr_self_substs,
terr_integer_as_char,
terr_no_integral_type,
terr_no_floating_point_type,
}
Expand Down Expand Up @@ -2520,6 +2522,13 @@ fn type_is_integral(ty: t) -> bool {
}
}

fn type_is_char(ty: t) -> bool {
match get(ty).sty {
ty_int(ty_char) => true,
_ => false
}
}

fn type_is_fp(ty: t) -> bool {
match get(ty).sty {
ty_infer(FloatVar(_)) | ty_float(_) => true,
Expand Down Expand Up @@ -3489,6 +3498,10 @@ fn type_err_to_str(cx: ctxt, err: &type_err) -> ~str {
~"couldn't determine an appropriate integral type for integer \
literal"
}
terr_integer_as_char => {
~"integer literals can't be inferred to char type \
(try an explicit cast)"
}
terr_no_floating_point_type => {
~"couldn't determine an appropriate floating point type for \
floating point literal"
Expand Down
4 changes: 4 additions & 0 deletions src/librustc/middle/typeck/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ impl infer_ctxt {
}

fn int_var_sub_t(a_id: ty::IntVid, b: ty::t) -> ures {
if ty::type_is_char(b) {
return Err(ty::terr_integer_as_char);
}

assert ty::type_is_integral(b);

let vb = &self.int_var_bindings;
Expand Down
3 changes: 3 additions & 0 deletions src/test/compile-fail/issue-3477.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let _p: char = 100; //~ ERROR mismatched types: expected `char` but found
}