From 20f02079039a814b1220ead53c1ec5744645cd05 Mon Sep 17 00:00:00 2001 From: Eric Lau Date: Tue, 16 Apr 2024 15:46:24 -0400 Subject: [PATCH] Leave constants as is when making namespaced input (#1009) --- packages/core/CHANGELOG.md | 3 ++- .../contracts/test/NamespacedToModify.sol | 15 ++++++++++++++ packages/core/package.json | 2 +- .../core/src/utils/make-namespaced.test.ts.md | 19 ++++++++++++++++-- .../src/utils/make-namespaced.test.ts.snap | Bin 1450 -> 1644 bytes packages/core/src/utils/make-namespaced.ts | 10 +++++++-- 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index a99227aca..7ca075f52 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -1,8 +1,9 @@ # Changelog -## Unreleased +## 1.32.6 (2024-04-16) - This plugin is now compiled with TypeScript v5. ([#760](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/760)) +- Fix Hardhat compile error when referencing a constant within a struct definition. ([#1009](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/1009)) ## 1.32.5 (2024-02-21) diff --git a/packages/core/contracts/test/NamespacedToModify.sol b/packages/core/contracts/test/NamespacedToModify.sol index d30458f8b..d1d43977f 100644 --- a/packages/core/contracts/test/NamespacedToModify.sol +++ b/packages/core/contracts/test/NamespacedToModify.sol @@ -168,3 +168,18 @@ enum FreeEnum { MyEnum } * @param example example parameter */ error CustomErrorOutsideContract(Example example); + +contract StructArrayUsesConstant { + uint16 private constant MAX_SIZE = 10; + + struct NotNamespaced { + uint16 a; + uint256[MAX_SIZE] b; + } + + /// @custom:storage-location erc7201:uses.constant + struct MainStorage { + uint256 x; + uint256[MAX_SIZE] y; + } +} \ No newline at end of file diff --git a/packages/core/package.json b/packages/core/package.json index 1f38bd9d2..eb11bf55a 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@openzeppelin/upgrades-core", - "version": "1.32.5", + "version": "1.32.6", "description": "", "repository": "https://github.com/OpenZeppelin/openzeppelin-upgrades/tree/master/packages/core", "license": "MIT", diff --git a/packages/core/src/utils/make-namespaced.test.ts.md b/packages/core/src/utils/make-namespaced.test.ts.md index fc7020329..d134cb685 100644 --- a/packages/core/src/utils/make-namespaced.test.ts.md +++ b/packages/core/src/utils/make-namespaced.test.ts.md @@ -56,7 +56,8 @@ Generated by [AVA](https://avajs.dev). struct MyStruct { uint b; }␊ ␊ // keccak256(abi.encode(uint256(keccak256("example.main")) - 1)) & ~bytes32(uint256(0xff));␊ - enum $astId_id_random { dummy }␊ + bytes32 private constant MAIN_STORAGE_LOCATION =␊ + 0x183a6125c38840424c4a85fa12bab2ab606c4b6d0e7cc73c0c06ba5300eab500;␊ ␊ enum $astId_id_random { dummy }␊ ␊ @@ -171,7 +172,21 @@ Generated by [AVA](https://avajs.dev). * @param example example parameter␊ */␊ enum CustomErrorOutsideContract { dummy }␊ - `, + ␊ + contract StructArrayUsesConstant {␊ + uint16 private constant MAX_SIZE = 10;␊ + ␊ + struct NotNamespaced {␊ + uint16 a;␊ + uint256[MAX_SIZE] b;␊ + }␊ + ␊ + /// @custom:storage-location erc7201:uses.constant␊ + struct MainStorage {␊ + uint256 x;␊ + uint256[MAX_SIZE] y;␊ + } MainStorage $MainStorage_random;␊ + }`, }, 'contracts/test/NamespacedToModifyImported.sol': { content: `// SPDX-License-Identifier: MIT␊ diff --git a/packages/core/src/utils/make-namespaced.test.ts.snap b/packages/core/src/utils/make-namespaced.test.ts.snap index f9bc10b39386695ead9717189196f7430e285830..9877bb71b6e8492592b60ff2f64c45b70e7a1c9c 100644 GIT binary patch literal 1644 zcmV-y29xMoHRQN*sz) zR6e8G-ElI+yKBwNrZ$m_;D|VZgg^qJBJnREaX<(-09Q`EgM?HGgg9~J?d-01v$kWq zN+qx~$;`ap``Gtp-n^On6}{>h`~2N6h@hfMTnq0LWMhl)8a9ZDs9kdfkPeqGkb; zdeZ9+pI1k#*xq&VE@@4a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$(r_@`m1>Py3Es>I#G@}|f1*Gymd z-glrM>9-~H0I_^3`&I|SG~}irBx2M;gKsdnBIP)ZT-)8L?A_Se+rM4e+xY;@dl@IPf7v3upO{+QAw)-?1SPeB8XB1B^8qayQzhdZb!f4RGQ^LGa5%P!-KIC% zHBmFM;ymAJKw`xXU*{IYkE!6}*}+kPf*R-kx|ov8mEdBX3crawCh`(_G(-;fAt9BB=5!S=ijf&J6%z3xUIJ7$cl|?T93Z@&pUF<>Z=f_zx%Wh13tHtr5)B zF{sRKg)CMSejelh`jjXl5`Al0*oH4_HxRWJ`8n~+h5E%Gjfd|Sscy^BNteI>FJ=)rA zL%07i9DN!I9JnC_ZYED-V`Cch4FJB)fCgJYMIDC6{V>ED@{rG+DI;MUyM>#OCgI%M zKph8PXq~_@7FsARx6i>C93sQXO&nb~8e z4aTnTQ?O@y4yOyt@mTshFrL90>OGcD0pz*fW9iEPS?@8HUf#TUE8q9L`CH%h19G*r z1U>!#s29`0mEfEvtWWY6tT12YGZp5id|QS21CIX8_oy&t!7v63kE+7_3w=36U1Jjf E02F`9CIA2c diff --git a/packages/core/src/utils/make-namespaced.ts b/packages/core/src/utils/make-namespaced.ts index d6481f4dd..9cf5c334f 100644 --- a/packages/core/src/utils/make-namespaced.ts +++ b/packages/core/src/utils/make-namespaced.ts @@ -60,12 +60,18 @@ export function makeNamespacedInput(input: SolcInput, output: SolcOutput): SolcI const contractNodes = contractDef.nodes; for (const contractNode of contractNodes) { switch (contractNode.nodeType) { + case 'VariableDeclaration': { + // If variable is a constant, keep it since it may be referenced in a struct + if (contractNode.constant) { + break; + } + // Otherwise, fall through to convert to dummy enum + } case 'ErrorDefinition': case 'EventDefinition': case 'FunctionDefinition': case 'ModifierDefinition': - case 'UsingForDirective': - case 'VariableDeclaration': { + case 'UsingForDirective': { // Replace with an enum based on astId (the original name is not needed, since nothing should reference it) modifications.push(makeReplace(contractNode, orig, toDummyEnumWithAstId(contractNode.id))); break;