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

use _library_ integer constants as fixed-length array sizes #12248

Closed
benediamond opened this issue Nov 6, 2021 · 7 comments
Closed

use _library_ integer constants as fixed-length array sizes #12248

benediamond opened this issue Nov 6, 2021 · 7 comments
Labels
closed due inactivity The issue/PR was automatically closed due to inactivity. stale The issue/PR was marked as stale because it has been open for too long.

Comments

@benediamond
Copy link

Description

Integer constants can't be used as the sizes of fixed-length arrays, even when they come from libraries.

Environment

  • Compiler version: 0.8.9
  • Target EVM version (as per compiler settings): london
  • Framework/IDE (e.g. Truffle or Remix): remix, hardhat, etc.
  • EVM execution environment / backend / blockchain client:
  • Operating system: MacOS

Steps to Reproduce

A.sol:

library A {
    uint256 constant N = 42;
}

B.sol:

import './A.sol';

contract B {
    uint256[A.N] b;
}

leads to

TypeError: Invalid array length, expected integer literal or constant expression.
  --> contracts/B.sol:12:13:
   |
12 |         uint256[A.N] b;
   |                 ^^^

-->

somewhat related original issue, about constants in the same file, now fixed: #716
#1290 is not the same issue; it's about grabbing constants from other contracts which are not libraries. but the workaround suggested there also works here.

@phyBrackets
Copy link
Contributor

I think fixed size array expect compile time constant , but here A.N will be evaluated at runtime .

@chriseth
Copy link
Contributor

chriseth commented Nov 8, 2021

A workaround for now would be to move the constant to file level.

This issue could have a duplicate somewhere by the way.

@benediamond
Copy link
Author

This issue could have a duplicate somewhere

i looked carefully and didn't find one.

A workaround for now would be to move the constant to file level.

interesting, this move hadn't occurred to me. thanks. feel free to close if you want (though of course i think the "issue" is still open).

@ethereum ethereum deleted a comment Nov 8, 2021
@qwerty472123
Copy link

qwerty472123 commented Jun 15, 2022

@chriseth

For a.sol

uint constant a = 0x12345678;

b.sol

import "./a.sol";

contract Hello {
    uint[a] x;
}

is OK, but c.sol

import * as X from "./a.sol";

contract Hello {
    uint[X.a] x;
}

outputs Error: Invalid array length, expected integer literal or constant expression..

@github-actions
Copy link

This issue has been marked as stale due to inactivity for the last 90 days.
It will be automatically closed in 7 days.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label Mar 21, 2023
@github-actions
Copy link

Hi everyone! This issue has been automatically closed due to inactivity.
If you think this issue is still relevant in the latest Solidity version and you have something to contribute, feel free to reopen.
However, unless the issue is a concrete proposal that can be implemented, we recommend starting a language discussion on the forum instead.

@github-actions github-actions bot added the closed due inactivity The issue/PR was automatically closed due to inactivity. label Mar 30, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 30, 2023
@KumaCrypto
Copy link

Hey, guys! I faced the same problem with compiler version 0.8.23. Do you plan to add support for setting array length using a constant from the library?
The workaround mentioned above didn't work for me.

My simplified case:

library MyLib {
    uint256  internal  constant ARR_LENGTH =  5;
    function getArray()  internal  pure  returns(uint256[ARR_LENGTH] memory arr) {
        return arr;
    }
} 

contract MyContract {
    function useArrayFromLib()  external  {
        uint256[MyLib.ARR_LENGTH] memory arr = MyLib.getArray();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed due inactivity The issue/PR was automatically closed due to inactivity. stale The issue/PR was marked as stale because it has been open for too long.
Projects
None yet
Development

No branches or pull requests

5 participants