Skip to content

typeck: handle unsized structs in type hints by recursing into their last field. #27294

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
Jul 27, 2015
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
10 changes: 5 additions & 5 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2490,7 +2490,7 @@ fn check_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
// The special-cased logic below has three functions:
// 1. Provide as good of an expected type as possible.
let expected = expected_arg_tys.get(i).map(|&ty| {
Expectation::rvalue_hint(ty)
Expectation::rvalue_hint(fcx.tcx(), ty)
});

check_expr_with_unifier(fcx, &**arg,
Expand Down Expand Up @@ -3268,7 +3268,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
match unop {
ast::UnUniq => match ty.sty {
ty::TyBox(ty) => {
Expectation::rvalue_hint(ty)
Expectation::rvalue_hint(tcx, ty)
}
_ => {
NoExpectation
Expand Down Expand Up @@ -3345,7 +3345,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
// the last field of a struct can be unsized.
ExpectHasType(mt.ty)
} else {
Expectation::rvalue_hint(mt.ty)
Expectation::rvalue_hint(tcx, mt.ty)
}
}
_ => NoExpectation
Expand Down Expand Up @@ -3982,8 +3982,8 @@ impl<'tcx> Expectation<'tcx> {
/// which still is useful, because it informs integer literals and the like.
/// See the test case `test/run-pass/coerce-expect-unsized.rs` and #20169
/// for examples of where this comes up,.
fn rvalue_hint(ty: Ty<'tcx>) -> Expectation<'tcx> {
match ty.sty {
fn rvalue_hint(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Expectation<'tcx> {
match tcx.struct_tail(ty).sty {
ty::TySlice(_) | ty::TyTrait(..) => {
ExpectRvalueLikeUnsized(ty)
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/run-pass/coerce-expect-unsized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#![allow(unknown_features)]
#![feature(box_syntax)]

use std::cell::RefCell;
use std::fmt::Debug;
use std::rc::Rc;

// Check that coercions apply at the pointer level and don't cause
// rvalue expressions to be unsized. See #20169 for more information.
Expand Down Expand Up @@ -45,6 +47,9 @@ pub fn main() {
let _: Box<[isize]> = Box::new([1, 2, 3]);
let _: Box<Fn(isize) -> _> = Box::new(|x| (x as u8));

let _: Rc<RefCell<[isize]>> = Rc::new(RefCell::new([1, 2, 3]));
let _: Rc<RefCell<FnMut(isize) -> _>> = Rc::new(RefCell::new(|x| (x as u8)));

let _: Vec<Box<Fn(isize) -> _>> = vec![
Box::new(|x| (x as u8)),
Box::new(|x| (x as i16 as u8)),
Expand Down