Skip to content

Fix increments and assignments to enums #602

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jul 19, 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
65 changes: 44 additions & 21 deletions src/parser/cxx/type_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ struct TypeChecker::Visitor {
return check.unit_->control();
}

[[nodiscard]] auto is_parsing_c() const {
return check.unit_->language() == LanguageKind::kC;
}

[[nodiscard]] auto is_parsing_cxx() const {
return check.unit_->language() == LanguageKind::kCXX;
}

void error(SourceLocation loc, std::string message) {
if (!check.reportErrors_) return;
check.unit_->error(loc, std::move(message));
Expand Down Expand Up @@ -498,7 +506,16 @@ void TypeChecker::Visitor::operator()(PostIncrExpressionAST* ast) {
}

auto incr_arithmetic = [&]() {
if (!control()->is_arithmetic(ast->baseExpression->type)) return false;
if (control()->is_const(ast->baseExpression->type)) return false;

if (is_parsing_cxx() &&
!control()->is_arithmetic(ast->baseExpression->type))
return false;

if (is_parsing_c() &&
!control()->is_arithmetic_or_unscoped_enum(ast->baseExpression->type))
return false;

auto ty = control()->remove_cv(ast->baseExpression->type);
if (type_cast<BoolType>(ty)) return false;

Expand Down Expand Up @@ -798,20 +815,23 @@ void TypeChecker::Visitor::operator()(UnaryExpressionAST* ast) {
break;
}

auto ty = ast->expression->type;
if (!control()->is_const(ast->expression->type)) {
const auto ty = ast->expression->type;

if (control()->is_arithmetic(ty) && !control()->is_const(ty)) {
ast->type = ty;
ast->valueCategory = ValueCategory::kLValue;
break;
}

if (auto ptrTy = type_cast<PointerType>(ty)) {
if (!control()->is_void(ptrTy->elementType())) {
ast->type = ptrTy;
if (is_parsing_cxx() ? control()->is_arithmetic(ty)
: control()->is_arithmetic_or_unscoped_enum(ty)) {
ast->type = ty;
ast->valueCategory = ValueCategory::kLValue;
break;
}

if (auto ptrTy = type_cast<PointerType>(ty)) {
if (!control()->is_void(ptrTy->elementType())) {
ast->type = ptrTy;
ast->valueCategory = ValueCategory::kLValue;
break;
}
}
}

error(ast->opLoc, std::format("cannot increment a value of type '{}'",
Expand All @@ -826,20 +846,23 @@ void TypeChecker::Visitor::operator()(UnaryExpressionAST* ast) {
break;
}

auto ty = ast->expression->type;

if (control()->is_arithmetic(ty) && !control()->is_const(ty)) {
ast->type = ty;
ast->valueCategory = ValueCategory::kLValue;
break;
}
if (!control()->is_const(ast->expression->type)) {
auto ty = ast->expression->type;

if (auto ptrTy = type_cast<PointerType>(ty)) {
if (ptrTy && !control()->is_void(ptrTy->elementType())) {
ast->type = ptrTy;
if (is_parsing_cxx() ? control()->is_arithmetic(ty)
: control()->is_arithmetic_or_unscoped_enum(ty)) {
ast->type = ty;
ast->valueCategory = ValueCategory::kLValue;
break;
}

if (auto ptrTy = type_cast<PointerType>(ty)) {
if (ptrTy && !control()->is_void(ptrTy->elementType())) {
ast->type = ptrTy;
ast->valueCategory = ValueCategory::kLValue;
break;
}
}
}

error(ast->opLoc, std::format("cannot decrement a value of type '{}'",
Expand Down
22 changes: 22 additions & 0 deletions tests/unit_tests/sema/incr_c_01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %cxx -verify -fcheck -xc %s

enum E { a, b, c };

int main() {
enum E e = a;
++e;
--e;
e++;
e--;

const E ce = a;
// expected-error@1 {{cannot increment a value of type 'const E'}}
++ce;
// expected-error@1 {{cannot decrement a value of type 'const E'}}
--ce;
// expected-error@1 {{cannot increment a value of type 'const E'}}
ce++;
// expected-error@1 {{cannot decrement a value of type 'const E'}}
ce--;
return 0;
}