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

[clang] Invalid rejection of enum arithmetic #84712

Closed
davidstone opened this issue Mar 11, 2024 · 4 comments · Fixed by #84068
Closed

[clang] Invalid rejection of enum arithmetic #84712

davidstone opened this issue Mar 11, 2024 · 4 comments · Fixed by #84068
Labels
c++26 clang:frontend Language frontend issues, e.g. anything involving "Sema" rejects-valid

Comments

@davidstone
Copy link
Contributor

The following valid translation unit

enum e1 {
	value = 0
};

enum e2 {
	a = 0,
	b = e1::value,
	c = a + b
};

is incorrectly rejected by clang when compiled in C++26 mode with

<source>:8:8: error: invalid arithmetic between different enumeration types ('e2' and 'e1')
    8 |         c = a + b
      |             ~ ^ ~
1 error generated.
Compiler returned: 1

See it live: https://godbolt.org/z/v4sG9YqMb

In earlier versions of C++, it incorrectly warns saying the conversion is deprecated.

This was accepted in clang 18.1.0, but is failing on current trunk (15e9478).

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Mar 11, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" rejects-valid c++26 and removed clang Clang issues not falling into any other category labels Mar 11, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 11, 2024

@llvm/issue-subscribers-clang-frontend

Author: David Stone (davidstone)

The following valid translation unit
enum e1 {
	value = 0
};

enum e2 {
	a = 0,
	b = e1::value,
	c = a + b
};

is incorrectly rejected by clang when compiled in C++26 mode with

&lt;source&gt;:8:8: error: invalid arithmetic between different enumeration types ('e2' and 'e1')
    8 |         c = a + b
      |             ~ ^ ~
1 error generated.
Compiler returned: 1

See it live: https://godbolt.org/z/v4sG9YqMb

In earlier versions of C++, it incorrectly warns saying the conversion is deprecated.

This was accepted in clang 18.1.0, but is failing on current trunk (15e9478).

@Sirraide
Copy link
Contributor

This seems to be a bug, yeah. The type of a within e2 should be int; b is initialised by an expression of unscoped enumeration type, for which the standard specifies that it should have the underlying type of that enumeration [dcl.enum].

@Sirraide Sirraide self-assigned this Mar 11, 2024
@shafik
Copy link
Collaborator

shafik commented Mar 11, 2024

Related: #84068

@Sirraide
Copy link
Contributor

Related: #84068

Ah, I somehow was under the impression that that had already been merged.

@Sirraide Sirraide removed their assignment Mar 11, 2024
Kupa-Martin added a commit to Kupa-Martin/llvm-project that referenced this issue Mar 11, 2024
Enumerators dont have the type of their enumeration before the
closing brace. In these cases Expr::getEnumCoercedType()
incorrectly returned the enumeration type.

Introduced in PR llvm#81418
Fixes llvm#84712
Kupa-Martin added a commit to Kupa-Martin/llvm-project that referenced this issue Mar 11, 2024
Enumerators dont have the type of their enumeration before the
closing brace. In these cases Expr::getEnumCoercedType()
incorrectly returned the enumeration type.

Introduced in PR llvm#81418
Fixes llvm#84712
AaronBallman pushed a commit that referenced this issue Mar 12, 2024
)

Enumerators dont have the type of their enumeration before the closing
brace. In these cases Expr::getEnumCoercedType() incorrectly returned
the enumeration type.

Introduced in PR #81418
Fixes #84712
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++26 clang:frontend Language frontend issues, e.g. anything involving "Sema" rejects-valid
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants