Skip to content

Commit c5b81a5

Browse files
committed
chore: safer RC assumptions at instantiate_lmvars_fn and instantiate_mvars_fn
1 parent 1e6d617 commit c5b81a5

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/kernel/instantiate_mvars.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ option_ref<level> get_lmvar_assignment(metavar_ctx & mctx, name const & mid) {
3333

3434
class instantiate_lmvars_fn {
3535
metavar_ctx & m_mctx;
36-
std::unordered_map<lean_object *, level> m_cache;
3736
std::vector<level> m_saved; // Helper vector to prevent values from being garbagge collected
37+
std::unordered_map<lean_object *, level> m_cache;
3838

3939
inline level cache(level const & l, level r, bool shared) {
4040
if (shared) {
@@ -71,14 +71,14 @@ class instantiate_lmvars_fn {
7171
if (!has_mvar(a)) {
7272
return a;
7373
} else {
74+
/*
75+
We save `a` to ensure it will not be garbage collected.
76+
This is necessary because `m_cache` may contain references
77+
to it and its subterms, **and** it does not bump their RC's
78+
*/
79+
m_saved.push_back(a);
7480
level a_new = visit(a);
7581
if (!is_eqp(a, a_new)) {
76-
/*
77-
We save `a` to ensure it will not be garbage collected
78-
after we update `mctx`. This is necessary because `m_cache`
79-
may contain references to its subterms.
80-
*/
81-
m_saved.push_back(a);
8282
assign_lmvar(m_mctx, mvar_id(l), a_new);
8383
}
8484
return a_new;
@@ -140,9 +140,9 @@ expr replace_fvars(expr const & e, array_ref<expr> const & fvars, expr const * r
140140
class instantiate_mvars_fn {
141141
metavar_ctx & m_mctx;
142142
instantiate_lmvars_fn m_level_fn;
143+
std::vector<expr> m_saved; // Helper vector to prevent values from being garbagge collected
143144
name_set m_already_normalized; // Store metavariables whose assignment has already been normalized.
144145
std::unordered_map<lean_object *, expr> m_cache;
145-
std::vector<expr> m_saved; // Helper vector to prevent values from being garbagge collected
146146

147147
level visit_level(level const & l) {
148148
return m_level_fn(l);
@@ -172,14 +172,14 @@ class instantiate_mvars_fn {
172172
return optional<expr>(a);
173173
} else {
174174
m_already_normalized.insert(mid);
175+
/*
176+
We save `a` to ensure it will not be garbage collected.
177+
This is necessary because `m_cache` may contain references to it and
178+
its subterms. Recall that `m_cache` does not bump their RC's.
179+
*/
180+
m_saved.push_back(a);
175181
expr a_new = visit(a);
176182
if (!is_eqp(a, a_new)) {
177-
/*
178-
We save `a` to ensure it will not be garbage collected
179-
after we update `mctx`. This is necessary because `m_cache`
180-
may contain references to its subterms.
181-
*/
182-
m_saved.push_back(a);
183183
assign_mvar(m_mctx, mid, a_new);
184184
}
185185
return optional<expr>(a_new);

0 commit comments

Comments
 (0)