RFR: 8345580: Remove const from Node::_idx which is modified
Emanuel Peter
epeter at openjdk.org
Tue Dec 17 17:35:40 UTC 2024
On Wed, 11 Dec 2024 13:11:01 GMT, Julian Waters <jwaters at openjdk.org> wrote:
>> `Node::_idx` is declared as `const`, however, it is modified by `Node::set_idx`. Please see: https://github.com/openjdk/jdk/blob/166c12771d9d8c466e73a9490c4eb1fc9a5f6c24/src/hotspot/share/opto/node.hpp#L588
>>
>> As already stated in [JDK-8345580](https://bugs.openjdk.org/browse/JDK-8345580) issue, this behavior is counterintuitive because a const variable is expected to remain unmodified throughout its lifetime. Additionally, C++ International Standard states that "_...any attempt to modify a `const` object during its lifetime results in undefined behavior._ ".
>> To address this, `const` should be removed from the declaration of `Node::_idx` to align with its intended use and avoid violating the C++ standard. Tested with tier1,2,3,4,5.
>
> I do wonder why this compiles fine since compilers should reject code that modifies a const
@TheShermanTanker
void set_idx(uint new_idx) {
const node_idx_t* ref = &_idx;
*(node_idx_t*)ref = new_idx;
}
The const-ness is simply cast away. That is possible with C++.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/22646#issuecomment-2549121068
More information about the hotspot-compiler-dev
mailing list