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

Ternary operator expressions and array literal indexing are not evaluated at compilation time #14129

Closed
18a6 opened this issue Apr 17, 2023 · 4 comments

Comments

@18a6
Copy link

18a6 commented Apr 17, 2023

Description

Not sure if solidity evaluates RValue at runtime or compile time.

Environment

  • Compiler version: v0.8.17.
  • Operating system: Ubuntu 22.04.

Steps to Reproduce

//throws compilation error
uint8 public a = 255 + 1;

//throws no compilation error although it's equivalent to the previous expression
uint8 public a = 255 + (true ? 1 : 0);
uint8 public a = 255 + [1][0]; 
@18a6 18a6 added the bug 🐛 label Apr 17, 2023
@jinilshah21
Copy link

//SPDX-License-Identifier: MIT
pragma solidity 0.8.17 ;
contract A {
    
    uint8 public a ;
     function s() public {//this will give compilation error
         a = 255 + 1;
     }
   
    //throws no compilation error
    function s1() public {//once called gives error
        a = 255 + (true ? 1 : 0);
    }
   
    function s2() public {//once called giver error
         a = 255 + [1][0]; 
    }
}

uint8 public a = 255 + 1; gives compilation error because a cannot store more than 255, and it is evident that it will not store 255+1 as 0.

On the other hand, other two lines do not show compilation error, but on calling the default getter function of a, it will give an outofbound error which means that a cannot store rvalue greater than 255.

@cameel
Copy link
Member

cameel commented Apr 17, 2023

This is not a bug, just a limitation of the compiler. Expressions involving ternary operator or indexing of array literals are not evaluated at compilation time and the oversized constant cannot be detected. They're still subject to runtime overflow checks though.

We won't be addressing this directly so I'm closing the issue, but we have #3157 in the pipeline which will generally enable the compiler to evaluate more kinds of expressions at compilation time and those two cases are likely to be covered by it as well.

@cameel cameel closed this as not planned Won't fix, can't repro, duplicate, stale Apr 17, 2023
@cameel cameel removed the bug 🐛 label Apr 17, 2023
@cameel cameel changed the title Solidity RValue Evaluation Ternary operator expressions and array literal indexing are not evaluated at compilation time Apr 17, 2023
@jinilshah21
Copy link

Oh, I get it. Is there any issue which I can work on? I have good experience with solidity

@shahoo
Copy link

shahoo commented Apr 17, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants