-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
const array indexing zeroes data in object variants at runtime #8015
Comments
* Decoupling op logic and gas - introduce gasometer, rework opcode declaration * Remove gas constants for gas opcode computation * Remove gas constants for precompiled contracts * make vm_types compile * Make opcode, call and computation compile * Distinguish between dynamic and complex gas costs, fix arithmetic * Fix context and sha3 * update memory and storage ops * Log opcode uses memory expansion code * update/stub system_ops with gas costs * Make test compile. Deactivate stub test_vm * all tests compiles, opcode fails due to nim-lang/Nim#8007 (const object variant in tables reset at runtime) * Create an enum without holes - workaround: nim-lang/Nim#8007 * Use arrays instead of tables for GasCosts, remove some unused imports - passing all basic tests! * Make test_vm_json compile * Fix test_vm_json - workaround nim-lang/Nim#8015 * fix memory expansion cost bug * Remove leftover special handling from before GckMemExpansion * cleanup outdated comment, better align = * Fix sha3 gas cost not taking memory expansion into account * Improve gas error reporting of test_vm_json * Fix gas computation regression due to mem expansion * mass replace for memExpansion->RequestedMemSize was too eager * fix log gas cost (no tests :/) * missed a static FeeSchedule * static as expression is fickle
this is the generated code for costTable NIM_CONST tyArray_WhU07q1GSgwZ2Yc60JIUlg costTable_sBUTwJc6CNeo5483rNsQhg = {{((tyEnum_CostKind_GGuYJIMY9aVFBKVS7j4CxtA) 0), ((NI) 999), NIM_NIL},
{((tyEnum_CostKind_GGuYJIMY9aVFBKVS7j4CxtA) 1), ((NI) 0), foo1_X2aQK4bCESQjARXX8182jQ},
{((tyEnum_CostKind_GGuYJIMY9aVFBKVS7j4CxtA) 1), ((NI) 0), foo2_X2aQK4bCESQjARXX8182jQ_2}}
;
why this code below not crash? because Nim compiler use literal value here, not taken from costTable echo costTable[0] # (kind: Fixed, cost: 999)
echo costTable[1].handler() # 100
echo costTable[2].handler() # 200 although the generated C code is wrong, the bug could be located somewhere else, not necessarily in codegen. But it is a starting point if someone decide to take a look. |
As reported in #9021 and https://forum.nim-lang.org/t/4773 for enums, all kind of fields lose data not only proc fields. |
Const case objects never were supposed to compile, there is logic in the compiler to detect and reject that but as we can see it's not triggered. |
Current outputs (Nim devel 1.1.1) for the two examples in the original post:
(Notice |
seems related: #13081 |
Just so people know, in many cases you can do |
even though other object variant bugs were recently fixed, this bug remains minimal repro: when true:
import unittest
type Foo = object
case b: bool
of false: v1: int
of true: v2: int
const t = [Foo(b: false, v1: 1), Foo(b: true, v2: 2)]
check $t == "[(b: false, v1: 1), (b: true, v2: 2)]"
check $t[0] == "(b: false, v1: 1)" # fails fails as of 64016dd 1.5.1
@mratsim I tried a few versions and never saw that, only Which git hash did you try it on? (this is why we need to show git hash in issue reports, always);
doesn't crash anymore |
Both original examples and the example in the @timotheecour's comment above mine seem to work fine on the latest devel. Can you confirm? |
indeed => #16782 |
Follow-up on #8007.
As a workaround to Tables losing compile-time object variant fields at runtime, I used arrays.
However after assigning a const array of object variant to a non-const variable, fields containing a proc pointer become nil.
Test case:
No issue if:
Example without object variant
The text was updated successfully, but these errors were encountered: