Skip to content

Commit c3516f0

Browse files
committed
rustc: Allow the addresses of rvalues to be taken
1 parent ffc1d0a commit c3516f0

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/rustc/middle/region.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ type region_map = {
4040
/* Mapping from an AST type node to the region that `&` resolves to. */
4141
ast_type_to_inferred_region: hashmap<ast::node_id,ty::region>,
4242
/* Mapping from a call site (or `bind` site) to its containing block. */
43-
call_site_to_block: hashmap<ast::node_id,ast::node_id>
43+
call_site_to_block: hashmap<ast::node_id,ast::node_id>,
44+
/*
45+
* Mapping from an address-of operator or alt expression to its containing
46+
* block. This is used as the region if the operand is an rvalue.
47+
*/
48+
rvalue_to_block: hashmap<ast::node_id,ast::node_id>
4449
};
4550

4651
type ctxt = {
@@ -249,6 +254,17 @@ fn resolve_expr(expr: @ast::expr, cx: ctxt, visitor: visit::vt<ctxt>) {
249254
}
250255
visit::visit_expr(expr, cx, visitor);
251256
}
257+
ast::expr_addr_of(_, _) | ast::expr_alt(_, _, _) {
258+
// Record the block that this expression appears in, in case the
259+
// operand is an rvalue.
260+
alt cx.parent {
261+
pa_block(blk_id) {
262+
cx.region_map.rvalue_to_block.insert(expr.id, blk_id);
263+
}
264+
_ { cx.sess.span_bug(expr.span, "expr outside of block?!"); }
265+
}
266+
visit::visit_expr(expr, cx, visitor);
267+
}
252268
_ { visit::visit_expr(expr, cx, visitor); }
253269
}
254270
}
@@ -276,7 +292,8 @@ fn resolve_crate(sess: session, def_map: resolve::def_map, crate: @ast::crate)
276292
region_name_to_fn: new_def_hash(),
277293
ast_type_to_inferred_region:
278294
map::new_int_hash(),
279-
call_site_to_block: map::new_int_hash()},
295+
call_site_to_block: map::new_int_hash(),
296+
rvalue_to_block: map::new_int_hash()},
280297
mut bindings: @list::nil,
281298
mut queued_locals: [],
282299
parent: pa_crate,

src/rustc/middle/typeck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,8 +1931,8 @@ fn region_of(fcx: @fn_ctxt, expr: @ast::expr) -> ty::region {
19311931
"index, or deref operations");
19321932
}
19331933
_ {
1934-
fcx.ccx.tcx.sess.span_err(expr.span, "not an lvalue");
1935-
ret ty::re_block(0);
1934+
let blk_id = fcx.ccx.tcx.region_map.rvalue_to_block.get(expr.id);
1935+
ret ty::re_block(blk_id);
19361936
}
19371937
}
19381938
}

0 commit comments

Comments
 (0)