@@ -1848,6 +1848,45 @@ fn lookup_field_ty(cx: ty::ctxt, items:[@ast::class_item],
1848
1848
cx. sess . span_fatal ( sp, #fmt ( "unbound field %s" , fieldname) ) ;
1849
1849
}
1850
1850
1851
+ /*
1852
+ * Returns the region that the value named by the given expression lives in.
1853
+ * If the expression is not an lvalue, reports an error and returns the block
1854
+ * region.
1855
+ *
1856
+ * Note that borrowing is not detected here, because we would have to
1857
+ * immediately structurally resolve too many types otherwise. Thus the
1858
+ * reference-counted heap and exchange heap regions will be reported as block
1859
+ * regions instead. This is cleaned up in the region checking pass.
1860
+ */
1861
+ fn region_of ( fcx : @fn_ctxt , expr : @ast:: expr ) -> ty:: region {
1862
+ alt expr. node {
1863
+ ast:: expr_path ( path) {
1864
+ let defn = lookup_def ( fcx, path. span , expr. id ) ;
1865
+ alt defn {
1866
+ ast : : def_local ( local_id, _) |
1867
+ ast:: def_upvar ( local_id, _, _) {
1868
+ let local_blocks = fcx. ccx . tcx . region_map . local_blocks ;
1869
+ let local_block_id = local_blocks. get ( local_id) ;
1870
+ ret ty:: re_block ( local_block_id) ;
1871
+ }
1872
+ _ {
1873
+ fcx. ccx . tcx . sess . span_unimpl ( expr. span ,
1874
+ "immortal region" ) ;
1875
+ }
1876
+ }
1877
+ }
1878
+ ast:: expr_field ( base, _, _) | ast:: expr_index ( base, _) |
1879
+ ast:: expr_unary ( ast:: deref, base) {
1880
+ fcx. ccx . tcx . sess . span_unimpl ( expr. span , "regions of field, " +
1881
+ "index, or deref operations" ) ;
1882
+ }
1883
+ _ {
1884
+ fcx. ccx . tcx . sess . span_err ( expr. span , "not an lvalue" ) ;
1885
+ ret ty:: re_block ( 0 ) ;
1886
+ }
1887
+ }
1888
+ }
1889
+
1851
1890
fn check_expr_fn_with_unifier ( fcx : @fn_ctxt ,
1852
1891
expr : @ast:: expr ,
1853
1892
proto : ast:: proto ,
@@ -2202,9 +2241,9 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
2202
2241
bot = check_expr ( fcx, oper) ;
2203
2242
let oper_t = expr_ty ( tcx, oper) ;
2204
2243
2205
- // FIXME: This is incorrect. Infer the proper region.
2244
+ let region = region_of ( fcx , oper ) ;
2206
2245
let tm = { ty: oper_t, mutbl: mutbl } ;
2207
- oper_t = ty:: mk_rptr ( tcx, ty :: re_block ( 0 ) , tm) ;
2246
+ oper_t = ty:: mk_rptr ( tcx, region , tm) ;
2208
2247
write_ty ( tcx, id, oper_t) ;
2209
2248
}
2210
2249
ast:: expr_path ( pth) {
0 commit comments