Skip to content

Commit f6efb54

Browse files
author
Jakub Wieczorek
committed
Fix an ICE on a cast from an inferred nil to uint
Fixes #10991.
1 parent 6d8342f commit f6efb54

File tree

2 files changed

+29
-57
lines changed

2 files changed

+29
-57
lines changed

src/librustc/middle/typeck/check/mod.rs

+15-57
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,9 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
30233023
ty::ty_trait(..) => (),
30243024

30253025
_ => {
3026+
let t_1 = structurally_resolved_type(fcx, e.span, t_1);
3027+
let t_e = structurally_resolved_type(fcx, e.span, t_e);
3028+
30263029
if ty::type_is_nil(t_e) {
30273030
fcx.type_error_message(expr.span, |actual| {
30283031
format!("cast from nil: `{}` as `{}`",
@@ -3037,21 +3040,14 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
30373040
}, t_e, None);
30383041
}
30393042

3040-
let t1 = structurally_resolved_type(fcx, e.span, t_1);
3041-
let te = structurally_resolved_type(fcx, e.span, t_e);
3042-
let t_1_is_scalar = type_is_scalar(fcx, expr.span, t_1);
3043-
let t_1_is_char = type_is_char(fcx, expr.span, t_1);
3044-
let t_1_is_bare_fn = type_is_bare_fn(fcx, expr.span, t_1);
3045-
let t_1_is_float = type_is_floating_point(fcx,
3046-
expr.span,
3047-
t_1);
3043+
let t_1_is_scalar = ty::type_is_scalar(t_1);
3044+
let t_1_is_char = ty::type_is_char(t_1);
3045+
let t_1_is_bare_fn = ty::type_is_bare_fn(t_1);
3046+
let t_1_is_float = ty::type_is_floating_point(t_1);
30483047

30493048
// casts to scalars other than `char` and `bare fn` are trivial
3050-
let t_1_is_trivial = t_1_is_scalar &&
3051-
!t_1_is_char && !t_1_is_bare_fn;
3052-
3053-
if type_is_c_like_enum(fcx, expr.span, t_e) &&
3054-
t_1_is_trivial {
3049+
let t_1_is_trivial = t_1_is_scalar && !t_1_is_char && !t_1_is_bare_fn;
3050+
if ty::type_is_c_like_enum(fcx.tcx(), t_e) && t_1_is_trivial {
30553051
if t_1_is_float {
30563052
fcx.type_error_message(expr.span, |actual| {
30573053
format!("illegal cast; cast through an \
@@ -3062,22 +3058,20 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
30623058
}
30633059
// casts from C-like enums are allowed
30643060
} else if t_1_is_char {
3065-
let te = fcx.infcx().resolve_type_vars_if_possible(te);
3066-
if ty::get(te).sty != ty::ty_uint(ast::TyU8) {
3061+
let t_e = fcx.infcx().resolve_type_vars_if_possible(t_e);
3062+
if ty::get(t_e).sty != ty::ty_uint(ast::TyU8) {
30673063
fcx.type_error_message(expr.span, |actual| {
30683064
format!("only `u8` can be cast as \
30693065
`char`, not `{}`", actual)
30703066
}, t_e, None);
30713067
}
3072-
} else if ty::get(t1).sty == ty::ty_bool {
3068+
} else if ty::get(t_1).sty == ty::ty_bool {
30733069
fcx.tcx()
30743070
.sess
30753071
.span_err(expr.span,
30763072
"cannot cast as `bool`, compare with \
30773073
zero instead");
3078-
} else if type_is_region_ptr(fcx, expr.span, t_e) &&
3079-
type_is_unsafe_ptr(fcx, expr.span, t_1) {
3080-
3074+
} else if ty::type_is_region_ptr(t_e) && ty::type_is_unsafe_ptr(t_1) {
30813075
fn is_vec(t: ty::t) -> bool {
30823076
match ty::get(t).sty {
30833077
ty::ty_vec(..) => true,
@@ -3110,7 +3104,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
31103104

31113105
/* this cast is only allowed from &[T] to *T or
31123106
&T to *T. */
3113-
match (&ty::get(te).sty, &ty::get(t_1).sty) {
3107+
match (&ty::get(t_e).sty, &ty::get(t_1).sty) {
31143108
(&ty::ty_rptr(_, ty::mt { ty: mt1, mutbl: ast::MutImmutable }),
31153109
&ty::ty_ptr(ty::mt { ty: mt2, mutbl: ast::MutImmutable }))
31163110
if types_compatible(fcx, e.span, mt1, mt2) => {
@@ -3120,8 +3114,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
31203114
demand::coerce(fcx, e.span, t_1, &**e);
31213115
}
31223116
}
3123-
} else if !(type_is_scalar(fcx,expr.span,t_e)
3124-
&& t_1_is_trivial) {
3117+
} else if !(ty::type_is_scalar(t_e) && t_1_is_trivial) {
31253118
/*
31263119
If more type combinations should be supported than are
31273120
supported here, then file an enhancement issue and
@@ -4203,41 +4196,6 @@ pub fn type_is_uint(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
42034196
return ty::type_is_uint(typ_s);
42044197
}
42054198

4206-
pub fn type_is_scalar(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
4207-
let typ_s = structurally_resolved_type(fcx, sp, typ);
4208-
return ty::type_is_scalar(typ_s);
4209-
}
4210-
4211-
pub fn type_is_char(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
4212-
let typ_s = structurally_resolved_type(fcx, sp, typ);
4213-
return ty::type_is_char(typ_s);
4214-
}
4215-
4216-
pub fn type_is_bare_fn(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
4217-
let typ_s = structurally_resolved_type(fcx, sp, typ);
4218-
return ty::type_is_bare_fn(typ_s);
4219-
}
4220-
4221-
pub fn type_is_floating_point(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
4222-
let typ_s = structurally_resolved_type(fcx, sp, typ);
4223-
return ty::type_is_floating_point(typ_s);
4224-
}
4225-
4226-
pub fn type_is_unsafe_ptr(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
4227-
let typ_s = structurally_resolved_type(fcx, sp, typ);
4228-
return ty::type_is_unsafe_ptr(typ_s);
4229-
}
4230-
4231-
pub fn type_is_region_ptr(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
4232-
let typ_s = structurally_resolved_type(fcx, sp, typ);
4233-
return ty::type_is_region_ptr(typ_s);
4234-
}
4235-
4236-
pub fn type_is_c_like_enum(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
4237-
let typ_s = structurally_resolved_type(fcx, sp, typ);
4238-
return ty::type_is_c_like_enum(fcx.ccx.tcx, typ_s);
4239-
}
4240-
42414199
pub fn ast_expr_vstore_to_ty(fcx: &FnCtxt,
42424200
e: &ast::Expr,
42434201
v: ast::ExprVstore,

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let nil = ();
13+
let _t = nil as uint; //~ ERROR: cast from nil: `()` as `uint`
14+
}

0 commit comments

Comments
 (0)