-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Support constants in custom storage layout expression #15944
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
base: develop
Are you sure you want to change the base?
Conversation
944dc30
to
059349f
Compare
4aae044
to
00ee32e
Compare
"The base slot of the storage layout must evaluate to an integer." | ||
); | ||
return; | ||
if (integerType->isSigned()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could also be removed from here and then replace the assert in line 173
as suggested in a previous PR #15528 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depends. The range check should catch it anyway and it provides a message that will be clearer to the user so I'd rather not check it. Removing this and adding an assert would be a better choice.
But I'm not sure if we will actually reach that check with constants. What happens in this case if you remove the isSigned()
. Will it fail in constant evaluator instead?
int constant X = -1;
contract C layout at X {}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It still fails the range check and the message is indeed clearer as you said:
Error: The base slot of the storage layout evaluates to -1, which is outside the range of type uint256.
thank you for supporting constants! this is a huge composability unlock! |
Would this also apply to immutables? |
No, this only targets constants. Immutables are a little bit different and demand a different handling. |
We can't really support immutables here, because the layout must already be defined at creation time (constructor can initialize state variables), but their values can still change at that point. In case where they do not, the immutable can be replaced by a constant anyway. @matheusaaguiar Speaking of immutables, please make sure you have a test case against #15989. I.e. one showing that immutables are rejected even if they end up being treated as pure by the compiler. |
ec9785d
to
d1dbd2e
Compare
{ | ||
m_errorReporter.typeError( | ||
6396_error, | ||
baseSlotExpression.location(), | ||
"The base slot of the storage layout must evaluate to a rational number." | ||
"The base slot of the storage layout must evaluate to a rational integer number." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The base slot of the storage layout must evaluate to a rational integer number." | |
"The base slot of the storage layout must evaluate to an integer." |
We could also say a rational number or an integer
, but not sure if the user will get the distinction. Unfortunately, out terminology here is a bit ambiguous.
@@ -4,4 +4,4 @@ contract A { | |||
|
|||
contract C is A layout at A.x { } | |||
// ---- | |||
// TypeError 6396: (68-71): The base slot of the storage layout must evaluate to a rational number. | |||
// TypeError 1505: (68-71): The base slot expression cannot be evaluated during compilation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is weird. Why are we getting this and not something like:
Error: Member "x" not found or not visible after argument-dependent lookup in type(contract A).
Is ConstantEvaluator
silencing this error and just returning nullopt
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, ConstantEvaluator
can't handle member access. At the moment it can only process, literals, variables with no member acess from modules or other contracts and most of binary/unary operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. So apparently it's not just layuouts, but ConstantEvaluator
does not support such constants in general. It does not seem hard to add that though. Looking at ConstantEvaluator
, it may be as simple as defining endVisit(MemberAccess)
similar to endVisit(Identifier)
it already has. I think we should do it: #16055.
This should not block this PR though. It's fine to leave it unsupported. You could add an explicit check for MemberAccess
to PostTypeContractLevelChecker
to issue a better message, but it will only catch cases where it's the top-level expression, so it's a half-measure.
import "A" as M; | ||
contract C layout at M.x{ } | ||
// ---- | ||
// TypeError 1505: (B:38-41): The base slot expression cannot be evaluated during compilation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one should work without errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for constant_initialized_from_cast.sol
address immutable a = 0x0000000000000000000000000000000000000001; | ||
uint immutable x = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add an immutable that's not pure (i.e. one that's not initialized with a literal) since that's a separate case. Also a comment explaining how they differ.
@@ -1,3 +1,3 @@ | |||
contract C layout at ~uint(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) {} | |||
// ---- | |||
// TypeError 6396: (21-94): The base slot of the storage layout must evaluate to a rational number. | |||
// TypeError 1505: (21-94): The base slot expression cannot be evaluated during compilation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that users will be confused about why we can't evaluate this, because fundamentally it should be possible. It's documented, but maybe it would not hurt to just say that the constant evaluator is just too limited for this?
// TypeError 1505: (21-94): The base slot expression cannot be evaluated during compilation. | |
// TypeError 1505: (21-94): The base slot expression contains elements that are not yet supported by the internal constant evaluator and therefore cannot be evaluated at compilation time. |
contract at layout at type(uint).max { } | ||
// ---- | ||
// TypeError 6396: (22-36): The base slot of the storage layout must evaluate to a rational number. | ||
// TypeError 1505: (22-36): The base slot expression cannot be evaluated during compilation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hoping this would work already in this PR, but apparently it's another thing that requires proper compile-time evaluation... #11183 (comment)
d1dbd2e
to
0d68b8c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should mention in the docs that constants are now supported.
Just replace an integer literal expression
with an integer literal expression or a constant
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature also needs a changelog entry.
ced73ae
to
d19c5dc
Compare
part of #15727.