Skip to content

rustc_typeck: unify expected return types with formal return types to propagate coercions through calls of generic functions. #20415

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 4 commits into from
Jan 12, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rustc_typeck: unify expected return types with formal return types to…
… propagate coercions through calls of generic functions.
  • Loading branch information
eddyb committed Jan 11, 2015
commit e73fbc69cd85ccd7e444929b7b7aa0ae74ee6198
33 changes: 33 additions & 0 deletions src/librustc/middle/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,39 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
self.commit_unconditionally(move || self.try(move |_| f()))
}

/// Execute `f` and commit only the region bindings if successful.
/// The function f must be very careful not to leak any non-region
/// variables that get created.
pub fn commit_regions_if_ok<T, E, F>(&self, f: F) -> Result<T, E> where
F: FnOnce() -> Result<T, E>
{
debug!("commit_regions_if_ok()");
let CombinedSnapshot { type_snapshot,
int_snapshot,
float_snapshot,
region_vars_snapshot } = self.start_snapshot();

let r = self.try(move |_| f());

// Roll back any non-region bindings - they should be resolved
// inside `f`, with, e.g. `resolve_type_vars_if_possible`.
self.type_variables
.borrow_mut()
.rollback_to(type_snapshot);
self.int_unification_table
.borrow_mut()
.rollback_to(int_snapshot);
self.float_unification_table
.borrow_mut()
.rollback_to(float_snapshot);

// Commit region vars that may escape through resolved types.
self.region_vars
.commit(region_vars_snapshot);

r
}

/// Execute `f`, unroll bindings on panic
pub fn try<T, E, F>(&self, f: F) -> Result<T, E> where
F: FnOnce(&CombinedSnapshot) -> Result<T, E>
Expand Down
26 changes: 19 additions & 7 deletions src/librustc_typeck/check/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use super::check_argument_types;
use super::check_expr;
use super::check_method_argument_types;
use super::err_args;
use super::Expectation;
use super::expected_types_for_fn_args;
use super::FnCtxt;
use super::LvaluePreference;
use super::method;
Expand Down Expand Up @@ -65,7 +67,8 @@ pub fn check_legal_trait_for_method_call(ccx: &CrateCtxt, span: Span, trait_id:
pub fn check_call<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
call_expr: &ast::Expr,
callee_expr: &ast::Expr,
arg_exprs: &[P<ast::Expr>])
arg_exprs: &[P<ast::Expr>],
expected: Expectation<'tcx>)
{
check_expr(fcx, callee_expr);
let original_callee_ty = fcx.expr_ty(callee_expr);
Expand All @@ -84,15 +87,15 @@ pub fn check_call<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
match result {
None => {
// this will report an error since original_callee_ty is not a fn
confirm_builtin_call(fcx, call_expr, original_callee_ty, arg_exprs);
confirm_builtin_call(fcx, call_expr, original_callee_ty, arg_exprs, expected);
}

Some(CallStep::Builtin) => {
confirm_builtin_call(fcx, call_expr, callee_ty, arg_exprs);
confirm_builtin_call(fcx, call_expr, callee_ty, arg_exprs, expected);
}

Some(CallStep::Overloaded(method_callee)) => {
confirm_overloaded_call(fcx, call_expr, arg_exprs, method_callee);
confirm_overloaded_call(fcx, call_expr, arg_exprs, method_callee, expected);
}
}
}
Expand Down Expand Up @@ -153,7 +156,8 @@ fn try_overloaded_call_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
fn confirm_builtin_call<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
call_expr: &ast::Expr,
callee_ty: Ty<'tcx>,
arg_exprs: &[P<ast::Expr>])
arg_exprs: &[P<ast::Expr>],
expected: Expectation<'tcx>)
{
let error_fn_sig;

Expand Down Expand Up @@ -192,9 +196,15 @@ fn confirm_builtin_call<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
fcx.normalize_associated_types_in(call_expr.span, &fn_sig);

// Call the generic checker.
let expected_arg_tys = expected_types_for_fn_args(fcx,
call_expr.span,
expected,
fn_sig.output,
fn_sig.inputs.as_slice());
check_argument_types(fcx,
call_expr.span,
fn_sig.inputs.as_slice(),
&expected_arg_tys[],
arg_exprs,
AutorefArgs::No,
fn_sig.variadic,
Expand All @@ -206,15 +216,17 @@ fn confirm_builtin_call<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
fn confirm_overloaded_call<'a,'tcx>(fcx: &FnCtxt<'a, 'tcx>,
call_expr: &ast::Expr,
arg_exprs: &[P<ast::Expr>],
method_callee: ty::MethodCallee<'tcx>)
method_callee: ty::MethodCallee<'tcx>,
expected: Expectation<'tcx>)
{
let output_type = check_method_argument_types(fcx,
call_expr.span,
method_callee.ty,
call_expr,
arg_exprs,
AutorefArgs::No,
TupleArgumentsFlag::TupleArguments);
TupleArgumentsFlag::TupleArguments,
expected);
let method_call = ty::MethodCall::expr(call_expr.id);
fcx.inh.method_map.borrow_mut().insert(method_call, method_callee);
write_call(fcx, call_expr, output_type);
Expand Down
115 changes: 99 additions & 16 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2559,7 +2559,8 @@ fn lookup_method_for_for_loop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
iterator_expr,
&[],
AutorefArgs::No,
DontTupleArguments);
DontTupleArguments,
NoExpectation);

match method {
Some(method) => {
Expand Down Expand Up @@ -2601,7 +2602,8 @@ fn check_method_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
callee_expr: &ast::Expr,
args_no_rcvr: &[P<ast::Expr>],
autoref_args: AutorefArgs,
tuple_arguments: TupleArgumentsFlag)
tuple_arguments: TupleArgumentsFlag,
expected: Expectation<'tcx>)
-> ty::FnOutput<'tcx> {
if ty::type_is_error(method_fn_ty) {
let err_inputs = err_args(fcx.tcx(), args_no_rcvr.len());
Expand All @@ -2614,6 +2616,7 @@ fn check_method_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
check_argument_types(fcx,
sp,
&err_inputs[],
&[],
args_no_rcvr,
autoref_args,
false,
Expand All @@ -2623,9 +2626,15 @@ fn check_method_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
match method_fn_ty.sty {
ty::ty_bare_fn(_, ref fty) => {
// HACK(eddyb) ignore self in the definition (see above).
let expected_arg_tys = expected_types_for_fn_args(fcx,
sp,
expected,
fty.sig.0.output,
&fty.sig.0.inputs[1..]);
check_argument_types(fcx,
sp,
&fty.sig.0.inputs[1..],
&expected_arg_tys[],
args_no_rcvr,
autoref_args,
fty.sig.0.variadic,
Expand All @@ -2645,6 +2654,7 @@ fn check_method_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
fn check_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
sp: Span,
fn_inputs: &[Ty<'tcx>],
expected_arg_tys: &[Ty<'tcx>],
args: &[P<ast::Expr>],
autoref_args: AutorefArgs,
variadic: bool,
Expand All @@ -2659,6 +2669,7 @@ fn check_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
1
};

let mut expected_arg_tys = expected_arg_tys;
let expected_arg_count = fn_inputs.len();
let formal_tys = if tuple_arguments == TupleArguments {
let tuple_type = structurally_resolved_type(fcx, sp, fn_inputs[0]);
Expand All @@ -2671,23 +2682,32 @@ fn check_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
if arg_types.len() == 1 {""} else {"s"},
args.len(),
if args.len() == 1 {" was"} else {"s were"});
expected_arg_tys = &[][];
err_args(fcx.tcx(), args.len())
} else {
expected_arg_tys = match expected_arg_tys.get(0) {
Some(&ty) => match ty.sty {
ty::ty_tup(ref tys) => &**tys,
_ => &[]
},
None => &[]
};
(*arg_types).clone()
}
}
_ => {
span_err!(tcx.sess, sp, E0059,
"cannot use call notation; the first type parameter \
for the function trait is neither a tuple nor unit");
expected_arg_tys = &[][];
err_args(fcx.tcx(), args.len())
}
}
} else if expected_arg_count == supplied_arg_count {
fn_inputs.iter().map(|a| *a).collect()
fn_inputs.to_vec()
} else if variadic {
if supplied_arg_count >= expected_arg_count {
fn_inputs.iter().map(|a| *a).collect()
fn_inputs.to_vec()
} else {
span_err!(tcx.sess, sp, E0060,
"this function takes at least {} parameter{} \
Expand All @@ -2696,6 +2716,7 @@ fn check_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
if expected_arg_count == 1 {""} else {"s"},
supplied_arg_count,
if supplied_arg_count == 1 {" was"} else {"s were"});
expected_arg_tys = &[][];
err_args(fcx.tcx(), supplied_arg_count)
}
} else {
Expand All @@ -2705,6 +2726,7 @@ fn check_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
if expected_arg_count == 1 {""} else {"s"},
supplied_arg_count,
if supplied_arg_count == 1 {" was"} else {"s were"});
expected_arg_tys = &[][];
err_args(fcx.tcx(), supplied_arg_count)
};

Expand Down Expand Up @@ -2768,7 +2790,25 @@ fn check_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
AutorefArgs::No => {}
}

check_expr_coercable_to_type(fcx, &**arg, formal_ty);
// 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)
});

check_expr_with_unifier(fcx, &**arg,
expected.unwrap_or(ExpectHasType(formal_ty)),
NoPreference, || {
// 2. Coerce to the most detailed type that could be coerced
// to, which is `expected_ty` if `rvalue_hint` returns an
// `ExprHasType(expected_ty)`, or the `formal_ty` otherwise.
let coerce_ty = expected.and_then(|e| e.only_has_type(fcx));
demand::coerce(fcx, arg.span, coerce_ty.unwrap_or(formal_ty), &**arg);

// 3. Relate the expected type and the formal one,
// if the expected type was used for the coercion.
coerce_ty.map(|ty| demand::suptype(fcx, arg.span, formal_ty, ty));
});
}
}
}
Expand Down Expand Up @@ -3008,6 +3048,45 @@ enum TupleArgumentsFlag {
TupleArguments,
}

/// Unifies the return type with the expected type early, for more coercions
/// and forward type information on the argument expressions.
fn expected_types_for_fn_args<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
call_span: Span,
expected_ret: Expectation<'tcx>,
formal_ret: ty::FnOutput<'tcx>,
formal_args: &[Ty<'tcx>])
-> Vec<Ty<'tcx>> {
let expected_args = expected_ret.only_has_type(fcx).and_then(|ret_ty| {
if let ty::FnConverging(formal_ret_ty) = formal_ret {
fcx.infcx().commit_regions_if_ok(|| {
// Attempt to apply a subtyping relationship between the formal
// return type (likely containing type variables if the function
// is polymorphic) and the expected return type.
// No argument expectations are produced if unification fails.
let origin = infer::Misc(call_span);
let ures = fcx.infcx().sub_types(false, origin, formal_ret_ty, ret_ty);
// FIXME(#15760) can't use try! here, FromError doesn't default
// to identity so the resulting type is not constrained.
if let Err(e) = ures {
return Err(e);
}

// Record all the argument types, with the substitutions
// produced from the above subtyping unification.
Ok(formal_args.iter().map(|ty| {
fcx.infcx().resolve_type_vars_if_possible(ty)
}).collect())
}).ok()
} else {
None
}
}).unwrap_or(vec![]);
debug!("expected_types_for_fn_args(formal={} -> {}, expected={} -> {})",
formal_args.repr(fcx.tcx()), formal_ret.repr(fcx.tcx()),
expected_args.repr(fcx.tcx()), expected_ret.repr(fcx.tcx()));
expected_args
}

/// Invariant:
/// If an expression has any sub-expressions that result in a type error,
/// inspecting that expression's type with `ty::type_is_error` will return
Expand All @@ -3029,12 +3108,13 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
expr.repr(fcx.tcx()), expected.repr(fcx.tcx()));

// Checks a method call.
fn check_method_call(fcx: &FnCtxt,
expr: &ast::Expr,
method_name: ast::SpannedIdent,
args: &[P<ast::Expr>],
tps: &[P<ast::Ty>],
lvalue_pref: LvaluePreference) {
fn check_method_call<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
expr: &ast::Expr,
method_name: ast::SpannedIdent,
args: &[P<ast::Expr>],
tps: &[P<ast::Ty>],
expected: Expectation<'tcx>,
lvalue_pref: LvaluePreference) {
let rcvr = &*args[0];
check_expr_with_lvalue_pref(fcx, &*rcvr, lvalue_pref);

Expand Down Expand Up @@ -3071,7 +3151,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
expr,
&args[1..],
AutorefArgs::No,
DontTupleArguments);
DontTupleArguments,
expected);

write_call(fcx, expr, ret_ty);
}
Expand Down Expand Up @@ -3182,7 +3263,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
op_ex,
args,
autoref_args,
DontTupleArguments) {
DontTupleArguments,
NoExpectation) {
ty::FnConverging(result_type) => result_type,
ty::FnDiverging => fcx.tcx().types.err
}
Expand All @@ -3198,7 +3280,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
op_ex,
args,
autoref_args,
DontTupleArguments);
DontTupleArguments,
NoExpectation);
fcx.tcx().types.err
}
}
Expand Down Expand Up @@ -4045,10 +4128,10 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
fcx.write_ty(id, fcx.node_ty(b.id));
}
ast::ExprCall(ref callee, ref args) => {
callee::check_call(fcx, expr, &**callee, &args[]);
callee::check_call(fcx, expr, &**callee, &args[], expected);
}
ast::ExprMethodCall(ident, ref tps, ref args) => {
check_method_call(fcx, expr, ident, &args[], &tps[], lvalue_pref);
check_method_call(fcx, expr, ident, &args[], &tps[], expected, lvalue_pref);
let arg_tys = args.iter().map(|a| fcx.expr_ty(&**a));
let args_err = arg_tys.fold(false,
|rest_err, a| {
Expand Down
3 changes: 3 additions & 0 deletions src/test/run-pass/coerce-expect-unsized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ pub fn main() {
let _: &Fn(int) -> _ = &{ |x| (x as u8) };
let _: &Show = &if true { false } else { true };
let _: &Show = &match true { true => 'a', false => 'b' };

let _: Box<[int]> = Box::new([1, 2, 3]);
let _: Box<Fn(int) -> _> = Box::new(|x| (x as u8));
}
Loading