-
Notifications
You must be signed in to change notification settings - Fork 110
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
Implement constant string receivers #401
base: main
Are you sure you want to change the base?
Conversation
just found out it doesn't work correctly for contract-scoped constants. I'll fix that |
dd066dc
to
9f1a72d
Compare
Rebased, added more tests and implemented handling constant values from structs. |
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.
Great progress!
Let's add some more tests:
- a positive test: a constant definition in a trait that is used as a receiver name;
- a negative test with a trait constant declaration, so it does not have a body.
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 is a case of "receiver not found", right? Not a duplicate one, because string3 is missing
const internal = | ||
d.selector.kind === "internal-const-comment"; | ||
|
||
let path = d.selector.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.
It feels like the following constant look-up code should be factored out into a helper function. This also should be implemented for expressions as well, can't we reuse that code here?
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.
Btw, the constant lookup you have implemented here, should go to the constant evaluator:
Line 521 in 9ed9193
export function evalConstantExpression( |
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 could even allow arbitrary expressions in receivers as long as those are string-valued constant expressions
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 way we'll try to evaluate a constant that might depend on the resolved contract during resolving that same contract. With static constants it'll work fine, but with cases like receive(self.something)
it won't work...
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.
Why not? We can first process all constants and then process receivers
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.
Because these are stored in CompilerContext with the contract itself in the very end of the resolving:
tact/src/types/resolveDescriptors.ts
Lines 1622 to 1624 in 9ed9193
for (const [k, t] of types) { | |
ctx = store.set(ctx, k, t); | |
} |
So I can't get contract-scoped constants by calling getAllTypes
or getType
in constant evaluator.
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.
if we cannot move it before receiver processing stage, let's just push this PR to v1.5 and solve this properly
@@ -85,9 +85,11 @@ Tact { | |||
|
|||
Receiver = receive "(" Parameter? ")" "{" Statement* "}" --regular | |||
| receive "(" stringLiteral ")" "{" Statement* "}" --comment | |||
| receive "(" LValue ")" "{" Statement* "}" --constComment |
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.
Let's actually change stringLiteral
to Expression
here (and for externals) to support arbitrary expressions: it should work as long as it's a string-valued constant expression. In the future we might have some compile-time string builtins or even allow integers, so instead of doing this:
message(1) OpCodeOneNoData {}
contract Contract {
// would be nice to support underscores here too, since OpCodeOneNoData does not carry any further info
receive(f: OpCodeOneNoData) {
self.reply("OpCodeOneNoData".asComment())
}
}
one could do
contract Contract {
receive(1) {
self.reply("OpCodeOneNoData".asComment())
}
}
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.
Hmm, interestingly enough it would somewhat change receivers towards #433. I'd propose having arbitrary string-valued expressions specified in constant declarations somewhere on top, or generally just prior to receivers and not embedded in. Motivation here reflects what you've said there: #433 (comment)
That said, @Gusarich would have the last word here :)
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.
yeah, it's a bit too late to have a consistent design for Tact v1.x :) given that we already have messages and strings
Closes #51