Skip to content

Improve checking for subtraction operator #534

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

Merged
merged 2 commits into from
Apr 26, 2025
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
29 changes: 26 additions & 3 deletions src/parser/cxx/ast_interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ struct ASTInterpreter::NewPlacementResult {};

struct ASTInterpreter::NestedNamespaceSpecifierResult {};

struct ASTInterpreter::ToBool {
ASTInterpreter& interp;

auto operator()(const StringLiteral*) const -> std::optional<bool> {
return true;
}

auto operator()(const auto& value) const -> std::optional<bool> {
return bool(value);
}
};

struct ASTInterpreter::UnitVisitor {
ASTInterpreter& accept;

Expand Down Expand Up @@ -2193,10 +2205,17 @@ auto ASTInterpreter::ExpressionVisitor::operator()(BinaryExpressionAST* ast)
auto ASTInterpreter::ExpressionVisitor::operator()(
ConditionalExpressionAST* ast) -> ExpressionResult {
auto conditionResult = accept(ast->condition);
auto iftrueExpressionResult = accept(ast->iftrueExpression);
auto iffalseExpressionResult = accept(ast->iffalseExpression);

return ExpressionResult{std::nullopt};
if (!conditionResult.has_value()) return std::nullopt;

if (accept.toBool(conditionResult.value())) {
auto result = accept(ast->iftrueExpression);
return result;
}

auto result = accept(ast->iffalseExpression);

return result;
}

auto ASTInterpreter::ExpressionVisitor::operator()(YieldExpressionAST* ast)
Expand Down Expand Up @@ -3176,4 +3195,8 @@ auto ASTInterpreter::evaluate(ExpressionAST* ast) -> std::optional<ConstValue> {
return result;
}

auto ASTInterpreter::toBool(const ConstValue& value) -> std::optional<bool> {
return std::visit(ToBool{*this}, value);
}

} // namespace cxx
5 changes: 5 additions & 0 deletions src/parser/cxx/ast_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class ASTInterpreter {

[[nodiscard]] auto evaluate(ExpressionAST* ast) -> std::optional<ConstValue>;

[[nodiscard]] auto toBool(const ConstValue& value) -> std::optional<bool>;

private:
using ExpressionResult = std::optional<ConstValue>;

Expand Down Expand Up @@ -116,6 +118,9 @@ class ASTInterpreter {
struct AttributeSpecifierVisitor;
struct AttributeTokenVisitor;

// ops
struct ToBool;

// run on the base nodes
[[nodiscard]] auto operator()(UnitAST* ast) -> UnitResult;
[[nodiscard]] auto operator()(DeclarationAST* ast) -> DeclarationResult;
Expand Down
13 changes: 9 additions & 4 deletions src/parser/cxx/type_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1624,8 +1624,6 @@ auto TypeChecker::Visitor::usual_arithmetic_conversion(ExpressionAST*& expr,
(void)lvalue_to_rvalue_conversion(other);
adjust_cv(other);

if (control()->is_same(expr->type, other->type)) return expr->type;

ExpressionAST* savedExpr = expr;
ExpressionAST* savedOther = other;

Expand Down Expand Up @@ -1991,8 +1989,15 @@ void TypeChecker::Visitor::check_subtraction(BinaryExpressionAST* ast) {
}

if (control()->is_pointer(ast->rightExpression->type)) {
if (control()->is_same(ast->leftExpression->type,
ast->rightExpression->type)) {
auto leftElementType =
control()->get_element_type(ast->leftExpression->type);
(void)strip_cv(leftElementType);

auto rightElementType =
control()->get_element_type(ast->rightExpression->type);
(void)strip_cv(rightElementType);

if (control()->is_same(leftElementType, rightElementType)) {
ast->type = control()->getLongIntType(); // TODO: ptrdiff_t
} else {
error(ast->opLoc,
Expand Down
33 changes: 33 additions & 0 deletions tests/unit_tests/sema/subtraction_01.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %cxx -verify -fcheck %s

auto main() -> int {
char* p;
const char* cp;
void* vp;
int i = 0;
short s = 0;

static_assert(__is_same(int, decltype('a' - '0')));
static_assert(__is_same(int, decltype('a' - 0)));
static_assert(__is_same(int, decltype('a' - s)));
static_assert(__is_same(long, decltype('a' - 1l)));
static_assert(__is_same(unsigned, decltype('a' - 1u)));

static_assert(__is_same(decltype(p), decltype(p - 0)));
static_assert(__is_same(decltype(p), decltype(p - i)));

static_assert(__is_same(long, decltype(p - p)));
static_assert(__is_same(long, decltype(p - cp)));

// clang-format off

// expected-error@1 {{'char*' and 'void*' are not pointers to compatible types}}
p - vp;

// expected-error@1 {{invalid operands to binary expression 'int' and 'char*'}}
0 - p;

// clang-format on

return 0;
}