Document that enum-to-integer casts are not allowed for signed integer targets
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 21
Description
The Solidity [documentation](https://docs.solidity.org/en/latest/types.html#enums) says:
> Enums are one way to create a user-defined type in Solidity. They are explicitly convertible to and from all integer types but implicit conversion is not allowed.
However, casting an enum value to an unsigned integer is accepted, and casting a signed integer to an enum is accepted and checked at runtime, but casting an enum value to a signed integer is rejected during type checking. This seems inconsistent because enum values are non-negative small integer values, so converting an enum to a sufficiently wide signed integer such as `int256` should be representable.
Expected behavior: either `int256(e)` should compile when the target signed integer type can represent all enum values, or the documentation should clarify that enums are not explicitly convertible to signed integer types.
## Environment
- Compiler version: `0.8.35-develop.2026.5.5+commit.47b9dedd.Linux.g++`
- Compilation pipeline (legacy, IR, EOF): default
- Target EVM version (as per compiler settings): default
- Framework/IDE (e.g. Foundry, Hardhat, Remix): solc CLI
- EVM execution environment / backend / blockchain client: N/A
- Operating system: Ubuntu Jammy on Linux `5.15.0-173-generic` x86_64
## Steps to Reproduce
```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract C {
enum E {
A,
B
}
function enumToUint(E e) external pure returns (uint256) {
return uint256(e); // OK
}
function enumToSignedInt(E e) external pure returns (int256) {
return int256(e); // Rejected
}
function signedIntToEnum(int256 x) external pure returns (E) {
return E(x); // OK, runtime-checked
}
}
```
Actual result:
```text
TypeError: Explicit type conversion not allowed from "enum C.E" to "int256".
```
The current workaround is an intermediate unsigned cast:
```solidity
return int256(uint256(e));
```
That workaround compiles, but the need for it is not apparent from the documented enum conversion rule.
Contributor guide
Research direction
Open the linked Solidity Types documentation and read the Enums section, then compare its conversion wording with the supplied solc CLI reproduction, including int256(e) and the unsigned-cast workaround. Done means the documentation clearly states whether enum-to-signed-integer casts are allowed and explains the relevant restriction or workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- solidity
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 72/100