Skip to content

Commit 72e371b

Browse files
committed
fix: instantiate_lmvars_fn
1 parent e9ad746 commit 72e371b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/kernel/instantiate_mvars.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ 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 *, lean_object *> m_cache;
36+
std::unordered_map<lean_object *, level> m_cache;
37+
std::vector<level> m_saved; // Helper vector to prevent values from being garbagge collected
3738

3839
inline level cache(level const & l, level r, bool shared) {
3940
if (shared) {
40-
m_cache.insert(mk_pair(l.raw(), r.raw()));
41+
m_cache.insert(mk_pair(l.raw(), r));
4142
}
4243
return r;
4344
}
@@ -50,7 +51,7 @@ class instantiate_lmvars_fn {
5051
if (is_shared(l)) {
5152
auto it = m_cache.find(l.raw());
5253
if (it != m_cache.end()) {
53-
return level(it->second, true);
54+
return it->second;
5455
}
5556
shared = true;
5657
}
@@ -72,6 +73,12 @@ class instantiate_lmvars_fn {
7273
} else {
7374
level a_new = visit(a);
7475
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);
7582
assign_lmvar(m_mctx, mvar_id(l), a_new);
7683
}
7784
return a_new;

0 commit comments

Comments
 (0)