Skip to content

Commit bac6fd8

Browse files
committed
---
yaml --- r: 14842 b: refs/heads/try c: 715d199 h: refs/heads/master v: v3
1 parent ed1d0eb commit bac6fd8

File tree

2 files changed

+92
-42
lines changed

2 files changed

+92
-42
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: 3a45f87620eb04242a63544b872ed1807cd38c72
5+
refs/heads/try: 715d1995d7fdb28caeaa5401e1dbdbb3751bee60
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/rustc/middle/typeck.rs

Lines changed: 91 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,25 +1442,49 @@ fn valid_range_bounds(tcx: ty::ctxt, from: @ast::expr, to: @ast::expr)
14421442
ast_util::compare_lit_exprs(tcx, from, to) <= 0
14431443
}
14441444

1445-
fn check_pat_variant(fcx: @fn_ctxt, map: pat_util::pat_id_map,
1446-
pat: @ast::pat, path: @ast::path, subpats: [@ast::pat],
1447-
expected: ty::t) {
1445+
type pat_ctxt = {
1446+
fcx: @fn_ctxt,
1447+
map: pat_util::pat_id_map,
1448+
alt_region: ty::region,
1449+
block_region: ty::region,
1450+
/* Equal to either alt_region or block_region. */
1451+
pat_region: ty::region
1452+
};
1453+
1454+
fn instantiate_self_regions(pcx: pat_ctxt, args: [ty::t]) -> [ty::t] {
1455+
vec::map(args, {|arg_ty|
1456+
if ty::type_has_rptrs(arg_ty) {
1457+
ty::fold_ty(pcx.fcx.ccx.tcx, ty::fm_rptr({|r|
1458+
alt r {
1459+
ty::re_inferred | ty::re_caller(_) { pcx.pat_region }
1460+
_ { r }
1461+
}
1462+
}), arg_ty)
1463+
} else {
1464+
arg_ty
1465+
}
1466+
})
1467+
}
1468+
1469+
fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
1470+
subpats: [@ast::pat], expected: ty::t) {
14481471
// Typecheck the path.
1449-
let tcx = fcx.ccx.tcx;
1450-
let v_def = lookup_def(fcx, path.span, pat.id);
1472+
let tcx = pcx.fcx.ccx.tcx;
1473+
let v_def = lookup_def(pcx.fcx, path.span, pat.id);
14511474
let v_def_ids = ast_util::variant_def_ids(v_def);
14521475
let ctor_tpt = ty::lookup_item_type(tcx, v_def_ids.enm);
1453-
instantiate_path(fcx, path, ctor_tpt, pat.span, pat.id);
1476+
instantiate_path(pcx.fcx, path, ctor_tpt, pat.span, pat.id);
14541477

14551478
// Take the enum type params out of `expected`.
1456-
alt structure_of(fcx, pat.span, expected) {
1479+
alt structure_of(pcx.fcx, pat.span, expected) {
14571480
ty::ty_enum(_, expected_tps) {
14581481
let ctor_ty = ty::node_id_to_type(tcx, pat.id);
1459-
demand::with_substs(fcx, pat.span, expected, ctor_ty,
1482+
demand::with_substs(pcx.fcx, pat.span, expected, ctor_ty,
14601483
expected_tps);
14611484
// Get the number of arguments in this enum variant.
1462-
let arg_types = variant_arg_types(fcx.ccx, pat.span,
1485+
let arg_types = variant_arg_types(pcx.fcx.ccx, pat.span,
14631486
v_def_ids.var, expected_tps);
1487+
arg_types = instantiate_self_regions(pcx, arg_types);
14641488
let subpats_len = subpats.len(), arg_len = arg_types.len();
14651489
if arg_len > 0u {
14661490
// N-ary variant.
@@ -1475,7 +1499,7 @@ fn check_pat_variant(fcx: @fn_ctxt, map: pat_util::pat_id_map,
14751499
}
14761500

14771501
vec::iter2(subpats, arg_types) {|subpat, arg_ty|
1478-
check_pat(fcx, map, subpat, arg_ty);
1502+
check_pat(pcx, subpat, arg_ty);
14791503
}
14801504
} else if subpats_len > 0u {
14811505
tcx.sess.span_err
@@ -1497,23 +1521,23 @@ fn check_pat_variant(fcx: @fn_ctxt, map: pat_util::pat_id_map,
14971521

14981522
// Pattern checking is top-down rather than bottom-up so that bindings get
14991523
// their types immediately.
1500-
fn check_pat(fcx: @fn_ctxt, map: pat_util::pat_id_map, pat: @ast::pat,
1501-
expected: ty::t) {
1502-
let tcx = fcx.ccx.tcx;
1524+
fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
1525+
let tcx = pcx.fcx.ccx.tcx;
15031526
alt pat.node {
15041527
ast::pat_wild {
15051528
write_ty(tcx, pat.id, expected);
15061529
}
15071530
ast::pat_lit(lt) {
1508-
check_expr_with(fcx, lt, expected);
1531+
check_expr_with(pcx.fcx, lt, expected);
15091532
write_ty(tcx, pat.id, expr_ty(tcx, lt));
15101533
}
15111534
ast::pat_range(begin, end) {
1512-
check_expr_with(fcx, begin, expected);
1513-
check_expr_with(fcx, end, expected);
1514-
let b_ty = resolve_type_vars_if_possible(fcx, expr_ty(tcx, begin));
1535+
check_expr_with(pcx.fcx, begin, expected);
1536+
check_expr_with(pcx.fcx, end, expected);
1537+
let b_ty = resolve_type_vars_if_possible(pcx.fcx,
1538+
expr_ty(tcx, begin));
15151539
if !ty::same_type(tcx, b_ty, resolve_type_vars_if_possible(
1516-
fcx, expr_ty(tcx, end))) {
1540+
pcx.fcx, expr_ty(tcx, end))) {
15171541
tcx.sess.span_err(pat.span, "mismatched types in range");
15181542
} else if !ty::type_is_numeric(b_ty) {
15191543
tcx.sess.span_err(pat.span, "non-numeric type used in range");
@@ -1525,29 +1549,30 @@ fn check_pat(fcx: @fn_ctxt, map: pat_util::pat_id_map, pat: @ast::pat,
15251549
}
15261550
ast::pat_ident(name, sub)
15271551
if !pat_util::pat_is_variant(tcx.def_map, pat) {
1528-
let vid = lookup_local(fcx, pat.span, pat.id);
1552+
let vid = lookup_local(pcx.fcx, pat.span, pat.id);
15291553
let typ = ty::mk_var(tcx, vid);
1530-
typ = demand::simple(fcx, pat.span, expected, typ);
1531-
let canon_id = map.get(path_to_ident(name));
1554+
typ = demand::simple(pcx.fcx, pat.span, expected, typ);
1555+
let canon_id = pcx.map.get(path_to_ident(name));
15321556
if canon_id != pat.id {
1533-
let ct = ty::mk_var(tcx, lookup_local(fcx, pat.span, canon_id));
1534-
typ = demand::simple(fcx, pat.span, ct, typ);
1557+
let tv_id = lookup_local(pcx.fcx, pat.span, canon_id);
1558+
let ct = ty::mk_var(tcx, tv_id);
1559+
typ = demand::simple(pcx.fcx, pat.span, ct, typ);
15351560
}
15361561
write_ty(tcx, pat.id, typ);
15371562
alt sub {
1538-
some(p) { check_pat(fcx, map, p, expected); }
1563+
some(p) { check_pat(pcx, p, expected); }
15391564
_ {}
15401565
}
15411566
}
15421567
ast::pat_ident(path, _) {
1543-
check_pat_variant(fcx, map, pat, path, [], expected);
1568+
check_pat_variant(pcx, pat, path, [], expected);
15441569
}
15451570
ast::pat_enum(path, subpats) {
1546-
check_pat_variant(fcx, map, pat, path, subpats, expected);
1571+
check_pat_variant(pcx, pat, path, subpats, expected);
15471572
}
15481573
ast::pat_rec(fields, etc) {
15491574
let ex_fields;
1550-
alt structure_of(fcx, pat.span, expected) {
1575+
alt structure_of(pcx.fcx, pat.span, expected) {
15511576
ty::ty_rec(fields) { ex_fields = fields; }
15521577
_ {
15531578
tcx.sess.span_fatal
@@ -1570,7 +1595,9 @@ fn check_pat(fcx: @fn_ctxt, map: pat_util::pat_id_map, pat: @ast::pat,
15701595
}
15711596
for f: ast::field_pat in fields {
15721597
alt vec::find(ex_fields, bind matches(f.ident, _)) {
1573-
some(field) { check_pat(fcx, map, f.pat, field.mt.ty); }
1598+
some(field) {
1599+
check_pat(pcx, f.pat, field.mt.ty);
1600+
}
15741601
none {
15751602
tcx.sess.span_fatal(pat.span,
15761603
#fmt["mismatched types: did not \
@@ -1583,7 +1610,7 @@ fn check_pat(fcx: @fn_ctxt, map: pat_util::pat_id_map, pat: @ast::pat,
15831610
}
15841611
ast::pat_tup(elts) {
15851612
let ex_elts;
1586-
alt structure_of(fcx, pat.span, expected) {
1613+
alt structure_of(pcx.fcx, pat.span, expected) {
15871614
ty::ty_tup(elts) { ex_elts = elts; }
15881615
_ {
15891616
tcx.sess.span_fatal
@@ -1600,13 +1627,17 @@ fn check_pat(fcx: @fn_ctxt, map: pat_util::pat_id_map, pat: @ast::pat,
16001627
fields", vec::len(ex_elts), e_count]);
16011628
}
16021629
let i = 0u;
1603-
for elt in elts { check_pat(fcx, map, elt, ex_elts[i]); i += 1u; }
1630+
for elt in elts {
1631+
check_pat(pcx, elt, ex_elts[i]);
1632+
i += 1u;
1633+
}
1634+
16041635
write_ty(tcx, pat.id, expected);
16051636
}
16061637
ast::pat_box(inner) {
1607-
alt structure_of(fcx, pat.span, expected) {
1638+
alt structure_of(pcx.fcx, pat.span, expected) {
16081639
ty::ty_box(e_inner) {
1609-
check_pat(fcx, map, inner, e_inner.ty);
1640+
check_pat(pcx, inner, e_inner.ty);
16101641
write_ty(tcx, pat.id, expected);
16111642
}
16121643
_ {
@@ -1618,9 +1649,9 @@ fn check_pat(fcx: @fn_ctxt, map: pat_util::pat_id_map, pat: @ast::pat,
16181649
}
16191650
}
16201651
ast::pat_uniq(inner) {
1621-
alt structure_of(fcx, pat.span, expected) {
1652+
alt structure_of(pcx.fcx, pat.span, expected) {
16221653
ty::ty_uniq(e_inner) {
1623-
check_pat(fcx, map, inner, e_inner.ty);
1654+
check_pat(pcx, inner, e_inner.ty);
16241655
write_ty(tcx, pat.id, expected);
16251656
}
16261657
_ {
@@ -2454,16 +2485,25 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
24542485
write_ty(tcx, id, ty::mk_nil(tcx));
24552486
bot = !may_break(body);
24562487
}
2457-
ast::expr_alt(expr, arms, _) {
2458-
bot = check_expr(fcx, expr);
2488+
ast::expr_alt(discrim, arms, _) {
2489+
bot = check_expr(fcx, discrim);
2490+
2491+
let parent_block = tcx.region_map.rvalue_to_block.get(expr.id);
24592492

24602493
// Typecheck the patterns first, so that we get types for all the
24612494
// bindings.
2462-
let pattern_ty = ty::expr_ty(tcx, expr);
2495+
let pattern_ty = ty::expr_ty(tcx, discrim);
24632496
for arm: ast::arm in arms {
2464-
let id_map = pat_util::pat_id_map(tcx.def_map, arm.pats[0]);
2497+
let pcx = {
2498+
fcx: fcx,
2499+
map: pat_util::pat_id_map(tcx.def_map, arm.pats[0]),
2500+
alt_region: ty::re_block(parent_block),
2501+
block_region: ty::re_block(arm.body.node.id),
2502+
pat_region: ty::re_block(parent_block)
2503+
};
2504+
24652505
for p: @ast::pat in arm.pats {
2466-
check_pat(fcx, id_map, p, pattern_ty);
2506+
check_pat(pcx, p, pattern_ty);
24672507
}
24682508
}
24692509
// Now typecheck the blocks.
@@ -2797,8 +2837,18 @@ fn check_decl_local(fcx: @fn_ctxt, local: @ast::local) -> bool {
27972837
}
27982838
_ {/* fall through */ }
27992839
}
2800-
let id_map = pat_util::pat_id_map(fcx.ccx.tcx.def_map, local.node.pat);
2801-
check_pat(fcx, id_map, local.node.pat, t);
2840+
2841+
let block_id = fcx.ccx.tcx.region_map.rvalue_to_block.get(local.node.id);
2842+
let region = ty::re_block(block_id);
2843+
let pcx = {
2844+
fcx: fcx,
2845+
map: pat_util::pat_id_map(fcx.ccx.tcx.def_map, local.node.pat),
2846+
alt_region: region,
2847+
block_region: region,
2848+
pat_region: region
2849+
};
2850+
2851+
check_pat(pcx, local.node.pat, t);
28022852
ret bot;
28032853
}
28042854

0 commit comments

Comments
 (0)