Skip to content

Commit bca7078

Browse files
committed
ASR: Error for ~ operation on unsigned ints
1 parent 9106711 commit bca7078

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3482,15 +3482,17 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
34823482
tmp = ASR::make_IntegerBitNot_t(al, x.base.base.loc, operand, dest_type, value);
34833483
return;
34843484
} else if (ASRUtils::is_unsigned_integer(*operand_type)) {
3485-
if (ASRUtils::expr_value(operand) != nullptr) {
3486-
int64_t op_value = ASR::down_cast<ASR::UnsignedIntegerConstant_t>(
3487-
ASRUtils::expr_value(operand))->m_n;
3488-
uint64_t val = ~uint64_t(op_value);
3489-
value = ASR::down_cast<ASR::expr_t>(ASR::make_UnsignedIntegerConstant_t(
3490-
al, x.base.base.loc, val, operand_type));
3491-
}
3492-
tmp = ASR::make_UnsignedIntegerBitNot_t(al, x.base.base.loc, operand, dest_type, value);
3493-
return;
3485+
int kind = ASRUtils::extract_kind_from_ttype_t(operand_type);
3486+
int signed_promote_kind = (kind < 8) ? kind * 2 : kind;
3487+
diag.add(diag::Diagnostic(
3488+
"The result of the bitnot ~ operation is negative, thus out of range for u" + std::to_string(kind * 8),
3489+
diag::Level::Error, diag::Stage::Semantic, {
3490+
diag::Label("use ~i" + std::to_string(signed_promote_kind * 8)
3491+
+ "(u) for signed result or bitnot_u" + std::to_string(kind * 8) + "(u) for unsigned result",
3492+
{x.base.base.loc})
3493+
})
3494+
);
3495+
throw SemanticAbort();
34943496
} else if (ASRUtils::is_real(*operand_type)) {
34953497
throw SemanticError("Unary operator '~' not supported for floats",
34963498
x.base.base.loc);

0 commit comments

Comments
 (0)