Skip to content

Commit b79d717

Browse files
committed
fix borrowing pats---the id field of cmt was assoc with wrong pat
1 parent 19ec5a4 commit b79d717

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/rustc/middle/borrowck.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,27 @@ impl methods for gather_loan_ctxt {
351351
// been built up and pass it off to guarantee_valid() so that
352352
// we can be sure that the binding will remain valid for the
353353
// duration of the arm.
354+
//
355+
// The correspondence between the id in the cmt and which
356+
// pattern is being referred to is somewhat...subtle. In
357+
// general, the id of the cmt is the id of the node that
358+
// produces the value. For patterns, that's actually the
359+
// *subpattern*, generally speaking.
360+
//
361+
// To see what I mean about ids etc, consider:
362+
//
363+
// let x = @@3;
364+
// alt x {
365+
// @@y { ... }
366+
// }
367+
//
368+
// Here the cmt for `y` would be something like
369+
//
370+
// local(x)->@->@
371+
//
372+
// where the id of `local(x)` is the id of the `x` that appears
373+
// in the alt, the id of `local(x)->@` is the `@y` pattern,
374+
// and the id of `local(x)->@->@` is the id of the `y` pattern.
354375

355376
#debug["gather_pat: id=%d pat=%s cmt=%s arm_id=%d alt_id=%d",
356377
pat.id, pprust::pat_to_str(pat),
@@ -369,7 +390,7 @@ impl methods for gather_loan_ctxt {
369390
ast::pat_enum(_, some(subpats)) {
370391
// variant(x, y, z)
371392
for subpats.each { |subpat|
372-
let subcmt = self.bccx.cat_variant(pat, cmt, subpat);
393+
let subcmt = self.bccx.cat_variant(subpat, cmt);
373394
self.gather_pat(subcmt, subpat, arm_id, alt_id);
374395
}
375396
}
@@ -396,23 +417,22 @@ impl methods for gather_loan_ctxt {
396417
ast::pat_rec(field_pats, _) {
397418
// {f1: p1, ..., fN: pN}
398419
for field_pats.each { |fp|
399-
let cmt_field = self.bccx.cat_field(pat, cmt, fp.ident,
400-
tcx.ty(fp.pat));
420+
let cmt_field = self.bccx.cat_field(fp.pat, cmt, fp.ident);
401421
self.gather_pat(cmt_field, fp.pat, arm_id, alt_id);
402422
}
403423
}
404424

405425
ast::pat_tup(subpats) {
406426
// (p1, ..., pN)
407427
for subpats.each { |subpat|
408-
let subcmt = self.bccx.cat_tuple_elt(pat, cmt, subpat);
428+
let subcmt = self.bccx.cat_tuple_elt(subpat, cmt);
409429
self.gather_pat(subcmt, subpat, arm_id, alt_id);
410430
}
411431
}
412432

413433
ast::pat_box(subpat) | ast::pat_uniq(subpat) {
414434
// @p1, ~p1
415-
alt self.bccx.cat_deref(pat, cmt, 0u, true) {
435+
alt self.bccx.cat_deref(subpat, cmt, 0u, true) {
416436
some(subcmt) {
417437
self.gather_pat(subcmt, subpat, arm_id, alt_id);
418438
}
@@ -998,7 +1018,7 @@ impl categorize_methods for borrowck_ctxt {
9981018

9991019
ast::expr_field(base, f_name, _) {
10001020
let base_cmt = self.cat_autoderef(base);
1001-
self.cat_field(expr, base_cmt, f_name, expr_ty)
1021+
self.cat_field(expr, base_cmt, f_name)
10021022
}
10031023

10041024
ast::expr_index(base, _) {
@@ -1033,8 +1053,7 @@ impl categorize_methods for borrowck_ctxt {
10331053
ret @{cat:cat_discr(cmt, alt_id) with *cmt};
10341054
}
10351055

1036-
fn cat_field<N:ast_node>(node: N, base_cmt: cmt,
1037-
f_name: str, f_ty: ty::t) -> cmt {
1056+
fn cat_field<N:ast_node>(node: N, base_cmt: cmt, f_name: str) -> cmt {
10381057
let f_mutbl = alt field_mutbl(self.tcx, base_cmt.ty, f_name) {
10391058
some(f_mutbl) { f_mutbl }
10401059
none {
@@ -1053,7 +1072,7 @@ impl categorize_methods for borrowck_ctxt {
10531072
};
10541073
@{id: node.id(), span: node.span(),
10551074
cat: cat_comp(base_cmt, comp_field(f_name)), lp:lp,
1056-
mutbl: m, ty: f_ty}
1075+
mutbl: m, ty: self.tcx.ty(node)}
10571076
}
10581077

10591078
fn cat_deref<N:ast_node>(node: N, base_cmt: cmt, derefs: uint,
@@ -1141,16 +1160,16 @@ impl categorize_methods for borrowck_ctxt {
11411160
mutbl:mt.mutbl, ty:mt.ty}
11421161
}
11431162

1144-
fn cat_variant<N: ast_node>(variant: N, cmt: cmt, arg: N) -> cmt {
1145-
@{id: variant.id(), span: variant.span(),
1163+
fn cat_variant<N: ast_node>(arg: N, cmt: cmt) -> cmt {
1164+
@{id: arg.id(), span: arg.span(),
11461165
cat: cat_comp(cmt, comp_variant),
11471166
lp: cmt.lp.map { |l| @lp_comp(l, comp_variant) },
11481167
mutbl: cmt.mutbl, // imm iff in an immutable context
11491168
ty: self.tcx.ty(arg)}
11501169
}
11511170

1152-
fn cat_tuple_elt<N: ast_node>(pat: N, cmt: cmt, elt: N) -> cmt {
1153-
@{id: pat.id(), span: pat.span(),
1171+
fn cat_tuple_elt<N: ast_node>(elt: N, cmt: cmt) -> cmt {
1172+
@{id: elt.id(), span: elt.span(),
11541173
cat: cat_comp(cmt, comp_tuple),
11551174
lp: cmt.lp.map { |l| @lp_comp(l, comp_tuple) },
11561175
mutbl: cmt.mutbl, // imm iff in an immutable context

0 commit comments

Comments
 (0)