mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-23 15:15:47 +03:00
Merge pull request #1185 from AleoHQ/bug/ternary-different-types
[Bugfix] Ternary Type Check
This commit is contained in:
commit
49a51d6026
@ -204,6 +204,12 @@ impl AsgConvertError {
|
|||||||
Self::new_from_span(format!("array index out of bounds: '{}'", index), span)
|
Self::new_from_span(format!("array index out of bounds: '{}'", index), span)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn ternary_different_types(left: &str, right: &str, span: &Span) -> Self {
|
||||||
|
let message = format!("ternary sides had different types: left {}, right {}", left, right);
|
||||||
|
|
||||||
|
Self::new_from_span(message, span)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn unknown_array_size(span: &Span) -> Self {
|
pub fn unknown_array_size(span: &Span) -> Self {
|
||||||
Self::new_from_span("array size cannot be inferred, add explicit types".to_string(), span)
|
Self::new_from_span("array size cannot be inferred, add explicit types".to_string(), span)
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,24 @@ impl<'a> FromAst<'a, leo_ast::TernaryExpression> for TernaryExpression<'a> {
|
|||||||
value: &leo_ast::TernaryExpression,
|
value: &leo_ast::TernaryExpression,
|
||||||
expected_type: Option<PartialType<'a>>,
|
expected_type: Option<PartialType<'a>>,
|
||||||
) -> Result<TernaryExpression<'a>, AsgConvertError> {
|
) -> Result<TernaryExpression<'a>, AsgConvertError> {
|
||||||
|
let if_true = Cell::new(<&Expression<'a>>::from_ast(
|
||||||
|
scope,
|
||||||
|
&*value.if_true,
|
||||||
|
expected_type.clone(),
|
||||||
|
)?);
|
||||||
|
let left: PartialType = if_true.get().get_type().unwrap().into();
|
||||||
|
|
||||||
|
let if_false = Cell::new(<&Expression<'a>>::from_ast(scope, &*value.if_false, expected_type)?);
|
||||||
|
let right = if_false.get().get_type().unwrap().into();
|
||||||
|
|
||||||
|
if left != right {
|
||||||
|
return Err(AsgConvertError::ternary_different_types(
|
||||||
|
&left.to_string(),
|
||||||
|
&right.to_string(),
|
||||||
|
&value.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
Ok(TernaryExpression {
|
Ok(TernaryExpression {
|
||||||
parent: Cell::new(None),
|
parent: Cell::new(None),
|
||||||
span: Some(value.span.clone()),
|
span: Some(value.span.clone()),
|
||||||
@ -87,12 +105,8 @@ impl<'a> FromAst<'a, leo_ast::TernaryExpression> for TernaryExpression<'a> {
|
|||||||
&*value.condition,
|
&*value.condition,
|
||||||
Some(Type::Boolean.partial()),
|
Some(Type::Boolean.partial()),
|
||||||
)?),
|
)?),
|
||||||
if_true: Cell::new(<&Expression<'a>>::from_ast(
|
if_true,
|
||||||
scope,
|
if_false,
|
||||||
&*value.if_true,
|
|
||||||
expected_type.clone(),
|
|
||||||
)?),
|
|
||||||
if_false: Cell::new(<&Expression<'a>>::from_ast(scope, &*value.if_false, expected_type)?),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
9
tests/compiler/statements/assign_ternary.leo
Normal file
9
tests/compiler/statements/assign_ternary.leo
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
/*
|
||||||
|
namespace: Compile
|
||||||
|
expectation: Fail
|
||||||
|
input_file: inputs/u32_3.in
|
||||||
|
*/
|
||||||
|
|
||||||
|
function main(x: u32) {
|
||||||
|
let x = true ? x: true;
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
namespace: Compile
|
||||||
|
expectation: Fail
|
||||||
|
outputs:
|
||||||
|
- " --> compiler-test:4:13\n |\n 4 | let x = true ? x: true;\n | ^^^^^^^^^^^^^^\n |\n = ternary sides had different types: left u32, right bool"
|
Loading…
Reference in New Issue
Block a user