Skip to content

librustc: Apply coercions to return expressions. #14735

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

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 7 additions & 7 deletions src/librustc/middle/typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use middle::ty;
use middle::typeck::check::FnCtxt;
use middle::typeck::infer;
use middle::typeck::infer::resolve_type;
use middle::typeck::infer::resolve::try_resolve_tvar_shallow;

use std::result::{Err, Ok};
use std::result;
Expand Down Expand Up @@ -60,18 +58,20 @@ pub fn eqtype(fcx: &FnCtxt, sp: Span, expected: ty::t, actual: ty::t) {

// Checks that the type `actual` can be coerced to `expected`.
pub fn coerce(fcx: &FnCtxt, sp: Span, expected: ty::t, expr: &ast::Expr) {
coerce_with_fn(fcx, sp, expected, expr,
|sp, a, e, err| fcx.report_mismatched_types(sp, e, a, err))
}

pub fn coerce_with_fn(fcx: &FnCtxt, sp: Span, expected: ty::t,
expr: &ast::Expr, handle_err: |Span, ty::t, ty::t, &ty::type_err|) {
let expr_ty = fcx.expr_ty(expr);
debug!("demand::coerce(expected = {}, expr_ty = {})",
expected.repr(fcx.ccx.tcx),
expr_ty.repr(fcx.ccx.tcx));
let expected = if ty::type_needs_infer(expected) {
resolve_type(fcx.infcx(), expected,
try_resolve_tvar_shallow).unwrap_or(expected)
} else { expected };
match fcx.mk_assignty(expr, expr_ty, expected) {
result::Ok(()) => { /* ok */ }
result::Err(ref err) => {
fcx.report_mismatched_types(sp, expected, expr_ty, err);
handle_err(sp, expr_ty, expected, err);
}
}
}
57 changes: 33 additions & 24 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@ fn check_fn<'a>(ccx: &'a CrateCtxt<'a>,
Some(tail_expr) => {
// Special case: we print a special error if there appears
// to be do-block/for-loop confusion
demand::suptype_with_fn(&fcx, tail_expr.span, false,
fcx.ret_ty, fcx.expr_ty(tail_expr),
|sp, e, a, s| {
demand::coerce_with_fn(&fcx, tail_expr.span,
fcx.ret_ty, tail_expr,
|sp, a, e, s| {
fcx.report_mismatched_return_types(sp, e, a, s);
});
}
Expand Down Expand Up @@ -1722,7 +1722,11 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
match ty::get(method_fn_ty).sty {
ty::ty_bare_fn(ref fty) => {
// HACK(eddyb) ignore self in the definition (see above).
check_argument_types(fcx, sp, fty.sig.inputs.slice_from(1),
let arg_tys = fty.sig.inputs.slice_from(1).iter()
.map(|t| {
fcx.infcx().resolve_type_vars_if_possible(*t)
}).collect::<Vec<ty::t>>();
check_argument_types(fcx, sp, arg_tys.as_slice(),
callee_expr, args, deref_args,
fty.sig.variadic);
fty.sig.output
Expand Down Expand Up @@ -2958,7 +2962,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
}
},
Some(e) => {
check_expr_has_type(fcx, e, ret_ty);
check_expr_coercable_to_type(fcx, e, ret_ty);
}
}
fcx.write_bot(id);
Expand Down Expand Up @@ -3500,26 +3504,31 @@ pub fn check_block_with_expected(fcx: &FnCtxt,
else {
fcx.write_nil(blk.id);
},
Some(e) => {
if any_bot && !warned {
fcx.ccx
.tcx
.sess
.add_lint(UnreachableCode,
e.id,
e.span,
"unreachable expression".to_string());
Some(e) => {
if any_bot && !warned {
fcx.ccx.tcx.sess
.add_lint(UnreachableCode,
e.id,
e.span,
"unreachable expression".to_string());
}
let ety = match expected {
Some(ety) => {
check_expr_coercable_to_type(fcx, e, ety);
ety
},
None => {
check_expr(fcx, e);
fcx.expr_ty(e)
}
};
fcx.write_ty(blk.id, ety);
if any_err {
fcx.write_error(blk.id);
} else if any_bot {
fcx.write_bot(blk.id);
}
}
check_expr_with_opt_hint(fcx, e, expected);
let ety = fcx.expr_ty(e);
fcx.write_ty(blk.id, ety);
if any_err {
fcx.write_error(blk.id);
}
else if any_bot {
fcx.write_bot(blk.id);
}
}
};
});

Expand Down
2 changes: 1 addition & 1 deletion src/librustrt/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn abort(args: &fmt::Arguments) -> ! {
let mut w = BufWriter { buf: msg, pos: 0 };
let _ = write!(&mut w, "{}", args);
let msg = str::from_utf8(w.buf.slice_to(w.pos)).unwrap_or("aborted");
let msg = if msg.is_empty() {"aborted"} else {msg};
let msg = if !msg.is_empty() {msg} else {"aborted"};

// Give some context to the message
let hash = msg.bytes().fold(0, |accum, val| accum + (val as uint) );
Expand Down
29 changes: 29 additions & 0 deletions src/test/run-pass/issue-12755.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Make sure we apply the usual coercions to return expressions

pub struct Foo<'a> {
a: &'a mut int,
b: int,
take_a: bool
}

impl<'a> Foo<'a> {
fn take(&'a mut self) -> &'a mut int {
if self.take_a {
self.a
} else {
&mut self.b
}
}
}

fn main() {}