Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: binary expr type error message locations #1139

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions kclvm/sema/src/resolver/calculation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ impl<'ctx> Resolver<'ctx> {
/// float # as defined by IEEE 754 1.0 < 2.0
/// list/config/schema # lexicographical [1] == [2]
/// iterable # 1 in [1, 2, 3], "s" in "ss", "key" in Schema
/// relation # a is True, b is Undefined
pub fn compare(
&mut self,
left: TypeRef,
Expand All @@ -264,11 +265,11 @@ impl<'ctx> Resolver<'ctx> {
if self
.ctx
.ty_ctx
.is_number_type_or_number_union_type(t1.clone())
.is_number_bool_type_or_number_bool_union_type(t1.clone())
&& self
.ctx
.ty_ctx
.is_number_type_or_number_union_type(t2.clone())
.is_number_bool_type_or_number_bool_union_type(t2.clone())
&& !matches!(op, ast::CmpOp::In | ast::CmpOp::NotIn)
{
return self.bool_ty();
Expand Down
23 changes: 6 additions & 17 deletions kclvm/sema/src/resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
fn walk_binary_expr(&mut self, binary_expr: &'ctx ast::BinaryExpr) -> Self::Result {
let left_ty = self.expr(&binary_expr.left);
let mut right_ty = self.expr(&binary_expr.right);
let range = (binary_expr.left.get_pos(), binary_expr.right.get_end_pos());
match &binary_expr.op {
ast::BinOp::As => {
if let ast::Expr::Identifier(identifier) = &binary_expr.right.node {
Expand All @@ -480,25 +481,13 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
&ty_str_replace_pkgpath(&ty_annotation_str, &self.ctx.pkgpath),
);
} else {
self.handler.add_compile_error(
"keyword 'as' right operand must be a type",
binary_expr.left.get_span_pos(),
);
self.handler
.add_compile_error("keyword 'as' right operand must be a type", range);
return left_ty;
}
self.binary(
left_ty,
right_ty,
&binary_expr.op,
binary_expr.left.get_span_pos(),
)
self.binary(left_ty, right_ty, &binary_expr.op, range)
}
_ => self.binary(
left_ty,
right_ty,
&binary_expr.op,
binary_expr.left.get_span_pos(),
),
_ => self.binary(left_ty, right_ty, &binary_expr.op, range),
}
}

Expand Down Expand Up @@ -1060,7 +1049,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
t1.clone(),
t2.clone(),
&compare.ops[0],
compare.comparators[0].get_span_pos(),
(compare.left.get_pos(), compare.comparators[0].get_end_pos()),
);
for i in 1..compare.comparators.len() - 1 {
let op = &compare.ops[i + 1];
Expand Down
8 changes: 8 additions & 0 deletions kclvm/sema/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ impl TypeContext {
self.is_kind_type_or_kind_union_type(ty, &[TypeFlags::INT, TypeFlags::FLOAT])
}

#[inline]
pub fn is_number_bool_type_or_number_bool_union_type(&self, ty: TypeRef) -> bool {
self.is_kind_type_or_kind_union_type(
ty,
&[TypeFlags::INT, TypeFlags::FLOAT, TypeFlags::BOOL],
)
}

#[inline]
pub fn is_config_type_or_config_union_type(&self, ty: TypeRef) -> bool {
self.is_kind_type_or_kind_union_type(ty, &[TypeFlags::DICT, TypeFlags::SCHEMA])
Expand Down
4 changes: 4 additions & 0 deletions test/grammar/types/binary_expr/binary_expr_3/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a: bool = True
b: bool = a is True
c: bool = a is not True
d: bool = a is not False
4 changes: 4 additions & 0 deletions test/grammar/types/binary_expr/binary_expr_3/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a: true
b: true
c: false
d: true
Loading