Skip to content

Commit 151de90

Browse files
committed
rustc: Do less work still in typestate
1 parent 02dde78 commit 151de90

File tree

4 files changed

+52
-46
lines changed

4 files changed

+52
-46
lines changed

src/rustc/middle/tstate/auxiliary.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ type fn_info =
224224
{constrs: constr_map,
225225
num_constraints: uint,
226226
cf: ret_style,
227-
used_vars: @mut [node_id]};
227+
used_vars: @mut [node_id],
228+
ignore: bool};
228229

229230
/* mapping from node ID to typestate annotation */
230231
type node_ann_table = @mut [mut ts_ann];

src/rustc/middle/tstate/ck.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ fn fn_states(fk: visit::fn_kind, f_decl: ast::fn_decl, f_body: ast::blk,
116116
sp: span, id: node_id,
117117
ccx: crate_ctxt, v: visit::vt<crate_ctxt>) {
118118

119+
visit::visit_fn(fk, f_decl, f_body, sp, id, ccx, v);
120+
119121
// We may not care about typestate for this function if it contains
120122
// no constrained calls
121-
if ccx.fm.contains_key(id) {
122-
visit::visit_fn(fk, f_decl, f_body, sp, id, ccx, v);
123+
if !ccx.fm.get(id).ignore {
123124
/* Look up the var-to-bit-num map for this function */
124125

125126
let f_info = ccx.fm.get(id);

src/rustc/middle/tstate/collect_locals.rs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ fn add_constraint(tcx: ty::ctxt, c: sp_constr, next: uint, tbl: constr_map) ->
6969
ret next + 1u;
7070
}
7171

72+
fn contains_constrained_calls(tcx: ty::ctxt, body: blk) -> bool {
73+
type cx = @{
74+
tcx: ty::ctxt,
75+
mut has: bool
76+
};
77+
let cx = @{
78+
tcx: tcx,
79+
mut has: false
80+
};
81+
let vtor = visit::default_visitor::<cx>();
82+
let vtor = @{visit_expr: visit_expr with *vtor};
83+
visit::visit_block(body, cx, visit::mk_vt(vtor));
84+
ret cx.has;
85+
86+
fn visit_expr(e: @expr, &&cx: cx, v: visit::vt<cx>) {
87+
import syntax::print::pprust;
88+
#debug("visiting %?", pprust::expr_to_str(e));
89+
90+
visit::visit_expr(e, cx, v);
91+
92+
if constraints_expr(cx.tcx, e).is_not_empty() {
93+
#debug("has constraints");
94+
cx.has = true;
95+
} else {
96+
#debug("has not constraints");
97+
}
98+
}
99+
}
72100

73101
/* builds a table mapping each local var defined in f
74102
to a bit number in the precondition/postcondition vectors */
@@ -86,24 +114,29 @@ fn mk_fn_info(ccx: crate_ctxt,
86114
/* now we have to add bit nums for both the constraints
87115
and the variables... */
88116

89-
let mut i = 0u, l = vec::len(*cx.cs);
90-
while i < l {
91-
next = add_constraint(cx.tcx, copy cx.cs[i], next, res_map);
92-
i += 1u;
93-
}
94-
/* if this function has any constraints, instantiate them to the
95-
argument names and add them */
96-
for f_decl.constraints.each {|c|
97-
let sc = ast_constr_to_sp_constr(cx.tcx, f_decl.inputs, c);
98-
next = add_constraint(cx.tcx, sc, next, res_map);
117+
let ignore = !contains_constrained_calls(ccx.tcx, f_body);
118+
119+
if !ignore {
120+
let mut i = 0u, l = vec::len(*cx.cs);
121+
while i < l {
122+
next = add_constraint(cx.tcx, copy cx.cs[i], next, res_map);
123+
i += 1u;
124+
}
125+
/* if this function has any constraints, instantiate them to the
126+
argument names and add them */
127+
for f_decl.constraints.each {|c|
128+
let sc = ast_constr_to_sp_constr(cx.tcx, f_decl.inputs, c);
129+
next = add_constraint(cx.tcx, sc, next, res_map);
130+
}
99131
}
100132

101133
let v: @mut [node_id] = @mut [];
102134
let rslt =
103135
{constrs: res_map,
104136
num_constraints: next,
105137
cf: f_decl.cf,
106-
used_vars: v};
138+
used_vars: v,
139+
ignore: ignore};
107140
ccx.fm.insert(id, rslt);
108141
#debug("%s has %u constraints", name, num_constraints(rslt));
109142
}

src/rustc/middle/tstate/pre_post_conditions.rs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -618,42 +618,13 @@ fn find_pre_post_fn(fcx: fn_ctxt, body: blk) {
618618
}
619619
}
620620

621-
fn contains_constrained_calls(tcx: ty::ctxt, body: blk) -> bool {
622-
type cx = @{
623-
tcx: ty::ctxt,
624-
mut has: bool
625-
};
626-
let cx = @{
627-
tcx: tcx,
628-
mut has: false
629-
};
630-
let vtor = visit::default_visitor::<cx>();
631-
let vtor = @{visit_expr: visit_expr with *vtor};
632-
visit::visit_block(body, cx, visit::mk_vt(vtor));
633-
ret cx.has;
634-
635-
fn visit_expr(e: @expr, &&cx: cx, v: visit::vt<cx>) {
636-
import syntax::print::pprust;
637-
#debug("visiting %?", pprust::expr_to_str(e));
638-
639-
visit::visit_expr(e, cx, v);
640-
641-
if constraints_expr(cx.tcx, e).is_not_empty() {
642-
#debug("has constraints");
643-
cx.has = true;
644-
} else {
645-
#debug("has not constraints");
646-
}
647-
}
648-
}
649-
650621
fn fn_pre_post(fk: visit::fn_kind, decl: fn_decl, body: blk, sp: span,
651622
id: node_id,
652623
ccx: crate_ctxt, v: visit::vt<crate_ctxt>) {
653624

654-
if contains_constrained_calls(ccx.tcx, body) {
655-
visit::visit_fn(fk, decl, body, sp, id, ccx, v);
656-
assert (ccx.fm.contains_key(id));
625+
visit::visit_fn(fk, decl, body, sp, id, ccx, v);
626+
assert (ccx.fm.contains_key(id));
627+
if !ccx.fm.get(id).ignore {
657628
let fcx =
658629
{enclosing: ccx.fm.get(id),
659630
id: id,

0 commit comments

Comments
 (0)