Skip to content

Commit c18deb0

Browse files
committed
rustc: Implement let assignability
1 parent fe6e0ba commit c18deb0

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/librustc/middle/typeck/check.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,10 +1088,12 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
10881088
}
10891089
10901090
// A generic function for checking assignment expressions
1091-
fn check_assignment(fcx: @fn_ctxt, _sp: span, lhs: @ast::expr,
1091+
fn check_assignment(fcx: @fn_ctxt, sp: span, lhs: @ast::expr,
10921092
rhs: @ast::expr, id: ast::node_id) -> bool {
10931093
let mut bot = check_expr(fcx, lhs, None);
1094-
bot |= check_expr_with(fcx, rhs, fcx.expr_ty(lhs));
1094+
let lhs_type = fcx.expr_ty(lhs);
1095+
let unifier = || demand::assign(fcx, sp, lhs_type, rhs);
1096+
bot |= check_expr_with_unifier(fcx, rhs, Some(lhs_type), unifier);
10951097
fcx.write_ty(id, ty::mk_nil(fcx.ccx.tcx));
10961098
return bot;
10971099
}
@@ -2247,7 +2249,8 @@ fn require_integral(fcx: @fn_ctxt, sp: span, t: ty::t) {
22472249
fn check_decl_initializer(fcx: @fn_ctxt, nid: ast::node_id,
22482250
init: @ast::expr) -> bool {
22492251
let lty = ty::mk_var(fcx.ccx.tcx, lookup_local(fcx, init.span, nid));
2250-
return check_expr_with(fcx, init, lty);
2252+
let unifier = || demand::assign(fcx, init.span, lty, init);
2253+
return check_expr_with_unifier(fcx, init, Some(lty), unifier);
22512254
}
22522255

22532256
fn check_decl_local(fcx: @fn_ctxt, local: @ast::local) -> bool {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fn f() {
2+
let a = ~"hello";
3+
let b: &str = a;
4+
io::println(b);
5+
}
6+
7+
fn g() {
8+
let c = ~"world";
9+
let d: &str;
10+
d = c;
11+
io::println(d);
12+
}
13+
14+
fn main() {
15+
f();
16+
g();
17+
}
18+

0 commit comments

Comments
 (0)