Skip to content

Commit 5c505b7

Browse files
committed
Permit DST types to unify like other types.
Also: 1. stop eagerly coercing from `[T, ..n]` to `[T]` only do so if requested. 2. permit error to be interact more freely. Fixes rust-lang#17178.
1 parent e6a1936 commit 5c505b7

File tree

3 files changed

+15
-45
lines changed

3 files changed

+15
-45
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ use middle::typeck::infer::{CoerceResult, resolve_type, Coercion};
7272
use middle::typeck::infer::combine::{CombineFields, Combine};
7373
use middle::typeck::infer::sub::Sub;
7474
use middle::typeck::infer::resolve::try_resolve_tvar_shallow;
75-
use util::common::indenter;
7675
use util::ppaux;
7776
use util::ppaux::Repr;
7877

@@ -93,7 +92,6 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
9392
debug!("Coerce.tys({} => {})",
9493
a.repr(self.get_ref().infcx.tcx),
9594
b.repr(self.get_ref().infcx.tcx));
96-
let _indent = indenter();
9795

9896
// Special case: if the subtype is a sized array literal (`[T, ..n]`),
9997
// then it would get auto-borrowed to `&[T, ..n]` and then DST-ified
@@ -411,7 +409,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
411409

412410
self.unpack_actual_value(ty_b, |sty_b|
413411
match (sty_a, sty_b) {
414-
(&ty::ty_vec(t_a, Some(len)), _) => {
412+
(&ty::ty_vec(t_a, Some(len)), &ty::ty_vec(_, None)) => {
415413
let ty = ty::mk_vec(tcx, t_a, None);
416414
Some((ty, ty::UnsizeLength(len)))
417415
}

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

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -363,28 +363,6 @@ pub fn super_fn_sigs<'tcx, C: Combine<'tcx>>(this: &C,
363363

364364
pub fn super_tys<'tcx, C: Combine<'tcx>>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
365365

366-
// This is a horrible hack - historically, [T] was not treated as a type,
367-
// so, for example, &T and &[U] should not unify. In fact the only thing
368-
// &[U] should unify with is &[T]. We preserve that behaviour with this
369-
// check.
370-
fn check_ptr_to_unsized<'tcx, C: Combine<'tcx>>(this: &C,
371-
a: ty::t,
372-
b: ty::t,
373-
a_inner: ty::t,
374-
b_inner: ty::t,
375-
result: ty::t) -> cres<ty::t> {
376-
match (&ty::get(a_inner).sty, &ty::get(b_inner).sty) {
377-
(&ty::ty_vec(_, None), &ty::ty_vec(_, None)) |
378-
(&ty::ty_str, &ty::ty_str) |
379-
(&ty::ty_trait(..), &ty::ty_trait(..)) => Ok(result),
380-
(&ty::ty_vec(_, None), _) | (_, &ty::ty_vec(_, None)) |
381-
(&ty::ty_str, _) | (_, &ty::ty_str) |
382-
(&ty::ty_trait(..), _) | (_, &ty::ty_trait(..))
383-
=> Err(ty::terr_sorts(expected_found(this, a, b))),
384-
_ => Ok(result),
385-
}
386-
}
387-
388366
let tcx = this.infcx().tcx;
389367
let a_sty = &ty::get(a).sty;
390368
let b_sty = &ty::get(b).sty;
@@ -402,6 +380,10 @@ pub fn super_tys<'tcx, C: Combine<'tcx>>(this: &C, a: ty::t, b: ty::t) -> cres<t
402380
b.repr(this.infcx().tcx)).as_slice());
403381
}
404382

383+
(&ty::ty_err, _) | (_, &ty::ty_err) => {
384+
Ok(ty::mk_err())
385+
}
386+
405387
// Relate integral variables to other types
406388
(&ty::ty_infer(IntVar(a_id)), &ty::ty_infer(IntVar(b_id))) => {
407389
try!(this.infcx().simple_vars(this.a_is_expected(),
@@ -442,8 +424,7 @@ pub fn super_tys<'tcx, C: Combine<'tcx>>(this: &C, a: ty::t, b: ty::t) -> cres<t
442424
(&ty::ty_bool, _) |
443425
(&ty::ty_int(_), _) |
444426
(&ty::ty_uint(_), _) |
445-
(&ty::ty_float(_), _) |
446-
(&ty::ty_err, _) => {
427+
(&ty::ty_float(_), _) => {
447428
if ty::get(a).sty == ty::get(b).sty {
448429
Ok(a)
449430
} else {
@@ -494,13 +475,13 @@ pub fn super_tys<'tcx, C: Combine<'tcx>>(this: &C, a: ty::t, b: ty::t) -> cres<t
494475
}
495476

496477
(&ty::ty_uniq(a_inner), &ty::ty_uniq(b_inner)) => {
497-
let typ = try!(this.tys(a_inner, b_inner));
498-
check_ptr_to_unsized(this, a, b, a_inner, b_inner, ty::mk_uniq(tcx, typ))
478+
let typ = try!(this.tys(a_inner, b_inner));
479+
Ok(ty::mk_uniq(tcx, typ))
499480
}
500481

501482
(&ty::ty_ptr(ref a_mt), &ty::ty_ptr(ref b_mt)) => {
502-
let mt = try!(this.mts(a_mt, b_mt));
503-
check_ptr_to_unsized(this, a, b, a_mt.ty, b_mt.ty, ty::mk_ptr(tcx, mt))
483+
let mt = try!(this.mts(a_mt, b_mt));
484+
Ok(ty::mk_ptr(tcx, mt))
504485
}
505486

506487
(&ty::ty_rptr(a_r, ref a_mt), &ty::ty_rptr(b_r, ref b_mt)) => {
@@ -516,7 +497,7 @@ pub fn super_tys<'tcx, C: Combine<'tcx>>(this: &C, a: ty::t, b: ty::t) -> cres<t
516497
}
517498
_ => try!(this.mts(a_mt, b_mt))
518499
};
519-
check_ptr_to_unsized(this, a, b, a_mt.ty, b_mt.ty, ty::mk_rptr(tcx, r, mt))
500+
Ok(ty::mk_rptr(tcx, r, mt))
520501
}
521502

522503
(&ty::ty_vec(a_t, sz_a), &ty::ty_vec(b_t, sz_b)) => {

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,30 +139,21 @@ impl<'f, 'tcx> Combine<'tcx> for Sub<'f, 'tcx> {
139139
.relate_vars(a_id, SubtypeOf, b_id);
140140
Ok(a)
141141
}
142-
// The vec/str check here and below is so that we don't unify
143-
// T with [T], this is necessary so we reflect subtyping of references
144-
// (&T does not unify with &[T]) where that in turn is to reflect
145-
// the historical non-typedness of [T].
146-
(&ty::ty_infer(TyVar(_)), &ty::ty_str) |
147-
(&ty::ty_infer(TyVar(_)), &ty::ty_vec(_, None)) => {
148-
Err(ty::terr_sorts(expected_found(self, a, b)))
149-
}
150142
(&ty::ty_infer(TyVar(a_id)), _) => {
151143
try!(self.fields
152144
.switch_expected()
153145
.instantiate(b, SupertypeOf, a_id));
154146
Ok(a)
155147
}
156-
157-
(&ty::ty_str, &ty::ty_infer(TyVar(_))) |
158-
(&ty::ty_vec(_, None), &ty::ty_infer(TyVar(_))) => {
159-
Err(ty::terr_sorts(expected_found(self, a, b)))
160-
}
161148
(_, &ty::ty_infer(TyVar(b_id))) => {
162149
try!(self.fields.instantiate(a, SubtypeOf, b_id));
163150
Ok(a)
164151
}
165152

153+
(&ty::ty_err, _) | (_, &ty::ty_err) => {
154+
Ok(ty::mk_err())
155+
}
156+
166157
(_, &ty::ty_bot) => {
167158
Err(ty::terr_sorts(expected_found(self, a, b)))
168159
}

0 commit comments

Comments
 (0)