Skip to content

Commit ac50b8f

Browse files
authored
fix: handle missing type invariants error (#2172)
1 parent c780c07 commit ac50b8f

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

source/vir/src/user_defined_type_invariants.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ pub(crate) fn annotate_user_defined_invariants(
4242
{
4343
let fun =
4444
typ_get_user_defined_type_invariant(datatypes, &expr.typ).unwrap();
45-
let function = functions.get(&fun).unwrap();
45+
let Some(function) = functions.get(&fun) else {
46+
return Err(internal_error(
47+
&expr.span,
48+
"missing type invariant function",
49+
));
50+
};
4651
Ok(assert_and_return(&expr, function, module)?)
4752
} else {
4853
Ok(expr.clone())
@@ -84,7 +89,12 @@ pub(crate) fn annotate_user_defined_invariants(
8489
return Err(error(&expr.span, "this type is not a datatype"));
8590
}
8691
if let Some(fun) = typ_get_user_defined_type_invariant(datatypes, &typ) {
87-
let function = functions.get(&fun).unwrap();
92+
let Some(function) = functions.get(&fun) else {
93+
return Err(internal_error(
94+
&expr.span,
95+
"missing type invariant function",
96+
));
97+
};
8898
if !crate::ast_util::is_visible_to(&function.x.visibility, module) {
8999
return Err(error(
90100
&expr.span,
@@ -129,7 +139,9 @@ pub(crate) fn annotate_one(
129139
&& typ_has_user_defined_type_invariant(datatypes, &expr.typ)
130140
{
131141
let fun = typ_get_user_defined_type_invariant(datatypes, &expr.typ).unwrap();
132-
let function = functions.get(&fun).unwrap();
142+
let Some(function) = functions.get(&fun) else {
143+
return Err(internal_error(&expr.span, "missing type invariant function"));
144+
};
133145
Ok(assert_and_return(&expr, function, module)?)
134146
} else {
135147
Ok(expr.clone())
@@ -143,7 +155,9 @@ pub(crate) fn annotate_one(
143155
return Err(error(&expr.span, "this type is not a datatype"));
144156
}
145157
if let Some(fun) = typ_get_user_defined_type_invariant(datatypes, &typ) {
146-
let function = functions.get(&fun).unwrap();
158+
let Some(function) = functions.get(&fun) else {
159+
return Err(internal_error(&expr.span, "missing type invariant function"));
160+
};
147161
if !crate::ast_util::is_visible_to(&function.x.visibility, module) {
148162
return Err(error(
149163
&expr.span,
@@ -288,7 +302,12 @@ fn asserts_for_lhs(
288302
if info.field_loc_needs_check[&cur.span.id] {
289303
if let Some(fun) = typ_get_user_defined_type_invariant(datatypes, &inner.typ) {
290304
let expr = loc_to_normal_expr(inner);
291-
let function = functions.get(&fun).unwrap();
305+
let Some(function) = functions.get(&fun) else {
306+
return Err(internal_error(
307+
&inner.span,
308+
"missing type invariant function",
309+
));
310+
};
292311
stmts.push(mk_assert_stmt(&expr, function, module)?);
293312
}
294313
}

0 commit comments

Comments
 (0)