Skip to content

Commit 3e94144

Browse files
committed
auto merge of #18746 : nikomatsakis/rust/issue-17322-unsized_ty, r=ncr
Pass the unadjusted type into the unsize_info function, which seems to be what it expects. Fixes #17322. r? @nick29581 Full disclosure: still running make check locally ;)
2 parents cf7df1e + ee9a7b6 commit 3e94144

File tree

4 files changed

+100
-21
lines changed

4 files changed

+100
-21
lines changed

Diff for: src/librustc/middle/ty.rs

+56-8
Original file line numberDiff line numberDiff line change
@@ -3396,14 +3396,6 @@ pub fn deref<'tcx>(ty: Ty<'tcx>, explicit: bool) -> Option<mt<'tcx>> {
33963396
}
33973397
}
33983398

3399-
pub fn deref_or_dont<'tcx>(ty: Ty<'tcx>) -> Ty<'tcx> {
3400-
match ty.sty {
3401-
ty_uniq(ty) => ty,
3402-
ty_rptr(_, mt) | ty_ptr(mt) => mt.ty,
3403-
_ => ty
3404-
}
3405-
}
3406-
34073399
pub fn close_type<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
34083400
match ty.sty {
34093401
ty_open(ty) => mk_rptr(cx, ReStatic, mt {ty: ty, mutbl:ast::MutImmutable}),
@@ -5989,3 +5981,59 @@ impl DebruijnIndex {
59895981
DebruijnIndex { depth: self.depth + amount }
59905982
}
59915983
}
5984+
5985+
impl<'tcx> Repr<'tcx> for AutoAdjustment<'tcx> {
5986+
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
5987+
match *self {
5988+
AdjustAddEnv(ref trait_store) => {
5989+
format!("AdjustAddEnv({})", trait_store)
5990+
}
5991+
AdjustDerefRef(ref data) => {
5992+
data.repr(tcx)
5993+
}
5994+
}
5995+
}
5996+
}
5997+
5998+
impl<'tcx> Repr<'tcx> for UnsizeKind<'tcx> {
5999+
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
6000+
match *self {
6001+
UnsizeLength(n) => format!("UnsizeLength({})", n),
6002+
UnsizeStruct(ref k, n) => format!("UnsizeStruct({},{})", k.repr(tcx), n),
6003+
UnsizeVtable(ref a, ref b) => format!("UnsizeVtable({},{})", a.repr(tcx), b.repr(tcx)),
6004+
}
6005+
}
6006+
}
6007+
6008+
impl<'tcx> Repr<'tcx> for AutoDerefRef<'tcx> {
6009+
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
6010+
format!("AutoDerefRef({}, {})", self.autoderefs, self.autoref.repr(tcx))
6011+
}
6012+
}
6013+
6014+
impl<'tcx> Repr<'tcx> for AutoRef<'tcx> {
6015+
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
6016+
match *self {
6017+
AutoPtr(a, b, ref c) => {
6018+
format!("AutoPtr({},{},{})", a.repr(tcx), b, c.repr(tcx))
6019+
}
6020+
AutoUnsize(ref a) => {
6021+
format!("AutoUnsize({})", a.repr(tcx))
6022+
}
6023+
AutoUnsizeUniq(ref a) => {
6024+
format!("AutoUnsizeUniq({})", a.repr(tcx))
6025+
}
6026+
AutoUnsafe(ref a, ref b) => {
6027+
format!("AutoUnsafe({},{})", a, b.repr(tcx))
6028+
}
6029+
}
6030+
}
6031+
}
6032+
6033+
impl<'tcx> Repr<'tcx> for TyTrait<'tcx> {
6034+
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
6035+
format!("TyTrait({},{})",
6036+
self.principal.repr(tcx),
6037+
self.bounds.repr(tcx))
6038+
}
6039+
}

Diff for: src/librustc/middle/typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17071707
node_id: ast::NodeId,
17081708
span: Span,
17091709
adj: ty::AutoAdjustment<'tcx>) {
1710-
debug!("write_adjustment(node_id={}, adj={})", node_id, adj);
1710+
debug!("write_adjustment(node_id={}, adj={})", node_id, adj.repr(self.tcx()));
17111711

17121712
if adj.is_identity() {
17131713
return;

Diff for: src/librustc_trans/trans/expr.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
194194
}
195195
Some(adj) => { adj }
196196
};
197-
debug!("unadjusted datum for expr {}: {}",
198-
expr.id, datum.to_string(bcx.ccx()));
197+
debug!("unadjusted datum for expr {}: {}, adjustment={}",
198+
expr.repr(bcx.tcx()),
199+
datum.to_string(bcx.ccx()),
200+
adjustment.repr(bcx.tcx()));
199201
match adjustment {
200202
AdjustAddEnv(..) => {
201203
datum = unpack_datum!(bcx, add_env(bcx, expr, datum));
@@ -265,9 +267,10 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
265267
&AutoPtr(_, _, ref a) | &AutoUnsafe(_, ref a) => {
266268
debug!(" AutoPtr");
267269
match a {
268-
&Some(box ref a) => datum = unpack_datum!(bcx,
269-
apply_autoref(a, bcx, expr, datum)),
270-
_ => {}
270+
&Some(box ref a) => {
271+
datum = unpack_datum!(bcx, apply_autoref(a, bcx, expr, datum));
272+
}
273+
&None => {}
271274
}
272275
unpack_datum!(bcx, ref_ptr(bcx, expr, datum))
273276
}
@@ -293,6 +296,10 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
293296
expr: &ast::Expr,
294297
datum: Datum<'tcx, Expr>)
295298
-> DatumBlock<'blk, 'tcx, Expr> {
299+
debug!("ref_ptr(expr={}, datum={})",
300+
expr.repr(bcx.tcx()),
301+
datum.to_string(bcx.ccx()));
302+
296303
if !ty::type_is_sized(bcx.tcx(), datum.ty) {
297304
debug!("Taking address of unsized type {}",
298305
bcx.ty_to_string(datum.ty));
@@ -307,34 +314,36 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
307314
// Retrieve the information we are losing (making dynamic) in an unsizing
308315
// adjustment.
309316
// When making a dtor, we need to do different things depending on the
310-
// ownership of the object.. mk_ty is a function for turning unsized_type
317+
// ownership of the object.. mk_ty is a function for turning `unadjusted_ty`
311318
// into a type to be destructed. If we want to end up with a Box pointer,
312319
// then mk_ty should make a Box pointer (T -> Box<T>), if we want a
313320
// borrowed reference then it should be T -> &T.
314321
fn unsized_info<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
315322
kind: &ty::UnsizeKind<'tcx>,
316323
id: ast::NodeId,
317-
unsized_ty: Ty<'tcx>,
324+
unadjusted_ty: Ty<'tcx>,
318325
mk_ty: |Ty<'tcx>| -> Ty<'tcx>) -> ValueRef {
326+
debug!("unsized_info(kind={}, id={}, unadjusted_ty={})",
327+
kind, id, unadjusted_ty.repr(bcx.tcx()));
319328
match kind {
320329
&ty::UnsizeLength(len) => C_uint(bcx.ccx(), len),
321-
&ty::UnsizeStruct(box ref k, tp_index) => match unsized_ty.sty {
330+
&ty::UnsizeStruct(box ref k, tp_index) => match unadjusted_ty.sty {
322331
ty::ty_struct(_, ref substs) => {
323332
let ty_substs = substs.types.get_slice(subst::TypeSpace);
324333
// The dtor for a field treats it like a value, so mk_ty
325334
// should just be the identity function.
326335
unsized_info(bcx, k, id, ty_substs[tp_index], |t| t)
327336
}
328337
_ => bcx.sess().bug(format!("UnsizeStruct with bad sty: {}",
329-
bcx.ty_to_string(unsized_ty)).as_slice())
338+
bcx.ty_to_string(unadjusted_ty)).as_slice())
330339
},
331340
&ty::UnsizeVtable(ty::TyTrait { ref principal, .. }, _) => {
332-
let substs = principal.substs.with_self_ty(unsized_ty).erase_regions();
341+
let substs = principal.substs.with_self_ty(unadjusted_ty).erase_regions();
333342
let trait_ref =
334343
Rc::new(ty::TraitRef { def_id: principal.def_id,
335344
substs: substs });
336345
let trait_ref = trait_ref.subst(bcx.tcx(), bcx.fcx.param_substs);
337-
let box_ty = mk_ty(unsized_ty);
346+
let box_ty = mk_ty(unadjusted_ty);
338347
PointerCast(bcx,
339348
meth::get_vtable(bcx, box_ty, trait_ref),
340349
Type::vtable_ptr(bcx.ccx()))
@@ -350,7 +359,9 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
350359
let tcx = bcx.tcx();
351360
let datum_ty = datum.ty;
352361
let unsized_ty = ty::unsize_ty(tcx, datum_ty, k, expr.span);
362+
debug!("unsized_ty={}", unsized_ty.repr(bcx.tcx()));
353363
let dest_ty = ty::mk_open(tcx, unsized_ty);
364+
debug!("dest_ty={}", unsized_ty.repr(bcx.tcx()));
354365
// Closures for extracting and manipulating the data and payload parts of
355366
// the fat pointer.
356367
let base = match k {
@@ -366,7 +377,7 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
366377
let info = |bcx, _val| unsized_info(bcx,
367378
k,
368379
expr.id,
369-
ty::deref_or_dont(datum_ty),
380+
datum_ty,
370381
|t| ty::mk_rptr(tcx,
371382
ty::ReStatic,
372383
ty::mt{

Diff for: src/test/run-pass/issue-17322.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
use std::io;
12+
13+
fn f(wr: &mut Writer) {
14+
wr.write_str("hello").ok().expect("failed");
15+
}
16+
17+
fn main() {
18+
let mut wr = box io::stdout() as Box<Writer + 'static>;
19+
f(&mut wr);
20+
}

0 commit comments

Comments
 (0)