Merge pull request #846 from Zokrates/fix-struct-member-type-mismatch
Fix struct member type mismatch crash
This commit is contained in:
commit
401a77bdbe
4 changed files with 51 additions and 0 deletions
1
changelogs/unreleased/846-schaeff
Normal file
1
changelogs/unreleased/846-schaeff
Normal file
|
@ -0,0 +1 @@
|
|||
Fix crash on struct member type mismatch
|
|
@ -0,0 +1,17 @@
|
|||
struct Foo {
|
||||
field[2] values
|
||||
}
|
||||
|
||||
struct Bar {
|
||||
Foo foo
|
||||
field bar
|
||||
}
|
||||
|
||||
def main():
|
||||
Bar s = Bar {
|
||||
foo: Foo { values: [1] },
|
||||
bar: 0,
|
||||
}
|
||||
field b = s.bar
|
||||
|
||||
return
|
|
@ -0,0 +1,17 @@
|
|||
struct Foo {
|
||||
u8[2] values
|
||||
}
|
||||
|
||||
struct Bar {
|
||||
Foo foo
|
||||
u8 bar
|
||||
}
|
||||
|
||||
def main():
|
||||
Bar s = Bar {
|
||||
foo: Foo { values: [1] }, // notice the size mismatch here
|
||||
bar: 0,
|
||||
}
|
||||
u8 b = s.bar
|
||||
|
||||
return
|
|
@ -1409,6 +1409,22 @@ impl<'ast, 'a, T: Field> ResultFolder<'ast, T> for Propagator<'ast, 'a, T> {
|
|||
)),
|
||||
}
|
||||
}
|
||||
StructExpressionInner::Value(v) => {
|
||||
let v = v.into_iter().zip(ty.iter()).map(|(v, member)|
|
||||
match self.fold_expression(v) {
|
||||
Ok(v) => match (ConcreteType::try_from(v.get_type().clone()), ConcreteType::try_from(*member.ty.clone())) {
|
||||
(Ok(t1), Ok(t2)) => if t1 == t2 { Ok(v) } else { Err(Error::Type(format!(
|
||||
"Struct member `{}` in struct `{}/{}` expected to have type `{}`, found type `{}`",
|
||||
member.id, ty.canonical_location.clone().module.display(), ty.canonical_location.clone().name, t2, t1
|
||||
))) },
|
||||
_ => Ok(v)
|
||||
}
|
||||
e => e
|
||||
}
|
||||
).collect::<Result<_, _>>()?;
|
||||
|
||||
Ok(StructExpressionInner::Value(v))
|
||||
}
|
||||
e => fold_struct_expression_inner(self, ty, e),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue