Skip to content

Commit 6ca454f

Browse files
committed
auto merge of #14854 : jakub-/rust/issue-10991, r=pcwalton
Fixes #10991.
2 parents 557b9e7 + f6efb54 commit 6ca454f

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
@@ -3025,6 +3025,9 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
30253025
_ if ty::type_is_trait(t_1) => {},
30263026

30273027
_ => {
3028+
let t_1 = structurally_resolved_type(fcx, e.span, t_1);
3029+
let t_e = structurally_resolved_type(fcx, e.span, t_e);
3030+
30283031
if ty::type_is_nil(t_e) {
30293032
fcx.type_error_message(expr.span, |actual| {
30303033
format!("cast from nil: `{}` as `{}`",
@@ -3039,21 +3042,14 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
30393042
}, t_e, None);
30403043
}
30413044

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

30513050
// casts to scalars other than `char` and `bare fn` are trivial
3052-
let t_1_is_trivial = t_1_is_scalar &&
3053-
!t_1_is_char && !t_1_is_bare_fn;
3054-
3055-
if type_is_c_like_enum(fcx, expr.span, t_e) &&
3056-
t_1_is_trivial {
3051+
let t_1_is_trivial = t_1_is_scalar && !t_1_is_char && !t_1_is_bare_fn;
3052+
if ty::type_is_c_like_enum(fcx.tcx(), t_e) && t_1_is_trivial {
30573053
if t_1_is_float {
30583054
fcx.type_error_message(expr.span, |actual| {
30593055
format!("illegal cast; cast through an \
@@ -3064,22 +3060,20 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
30643060
}
30653061
// casts from C-like enums are allowed
30663062
} else if t_1_is_char {
3067-
let te = fcx.infcx().resolve_type_vars_if_possible(te);
3068-
if ty::get(te).sty != ty::ty_uint(ast::TyU8) {
3063+
let t_e = fcx.infcx().resolve_type_vars_if_possible(t_e);
3064+
if ty::get(t_e).sty != ty::ty_uint(ast::TyU8) {
30693065
fcx.type_error_message(expr.span, |actual| {
30703066
format!("only `u8` can be cast as \
30713067
`char`, not `{}`", actual)
30723068
}, t_e, None);
30733069
}
3074-
} else if ty::get(t1).sty == ty::ty_bool {
3070+
} else if ty::get(t_1).sty == ty::ty_bool {
30753071
fcx.tcx()
30763072
.sess
30773073
.span_err(expr.span,
30783074
"cannot cast as `bool`, compare with \
30793075
zero instead");
3080-
} else if type_is_region_ptr(fcx, expr.span, t_e) &&
3081-
type_is_unsafe_ptr(fcx, expr.span, t_1) {
3082-
3076+
} else if ty::type_is_region_ptr(t_e) && ty::type_is_unsafe_ptr(t_1) {
30833077
fn is_vec(t: ty::t) -> bool {
30843078
match ty::get(t).sty {
30853079
ty::ty_vec(..) => true,
@@ -3112,7 +3106,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
31123106

31133107
/* this cast is only allowed from &[T] to *T or
31143108
&T to *T. */
3115-
match (&ty::get(te).sty, &ty::get(t_1).sty) {
3109+
match (&ty::get(t_e).sty, &ty::get(t_1).sty) {
31163110
(&ty::ty_rptr(_, ty::mt { ty: mt1, mutbl: ast::MutImmutable }),
31173111
&ty::ty_ptr(ty::mt { ty: mt2, mutbl: ast::MutImmutable }))
31183112
if types_compatible(fcx, e.span, mt1, mt2) => {
@@ -3122,8 +3116,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
31223116
demand::coerce(fcx, e.span, t_1, &**e);
31233117
}
31243118
}
3125-
} else if !(type_is_scalar(fcx,expr.span,t_e)
3126-
&& t_1_is_trivial) {
3119+
} else if !(ty::type_is_scalar(t_e) && t_1_is_trivial) {
31273120
/*
31283121
If more type combinations should be supported than are
31293122
supported here, then file an enhancement issue and
@@ -4205,41 +4198,6 @@ pub fn type_is_uint(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
42054198
return ty::type_is_uint(typ_s);
42064199
}
42074200

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