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

Implement constant string receivers #401

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Gusarich
Copy link
Member

Closes #51

  • I have updated CHANGELOG.md
  • I have added tests to demonstrate the contribution is correctly implemented: this usually includes both positive and negative tests, showing the happy path(s) and featuring intentionally broken cases
  • I have run all the tests locally and no test failure was reported
  • I did not do unrelated and/or undiscussed refactorings

@Gusarich Gusarich added this to the v1.4.0 milestone Jun 11, 2024
@Gusarich
Copy link
Member Author

just found out it doesn't work correctly for contract-scoped constants. I'll fix that

@Gusarich
Copy link
Member Author

Fixed.
However, there's a temporary limitation on lvalue paths. I had to add it because #284 and #400 aren't resolved yet.
We'll be able to support strings from constant structs after these are resolved.

@anton-trunov anton-trunov self-assigned this Jun 12, 2024
@Gusarich
Copy link
Member Author

Rebased, added more tests and implemented handling constant values from structs.
Also found #439 during testing.

Copy link
Member

@anton-trunov anton-trunov left a 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.

Copy link
Member

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;
Copy link
Member

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?

Copy link
Member

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:

export function evalConstantExpression(
. So, after typechecking the receiver constant string, you just run const-eval on it and you should get your string. This way you will also extend the supported language fragment for const-eval and resolve issue #229.

Copy link
Member

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

Copy link
Member Author

@Gusarich Gusarich Jun 20, 2024

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...

Copy link
Member

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

Copy link
Member Author

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:

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.

Copy link
Member

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
Copy link
Member

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())
  }
}

Copy link
Member

@novusnota novusnota Jun 19, 2024

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 :)

Copy link
Member

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

@Gusarich Gusarich modified the milestones: v1.4.0, v1.5.0 Jun 20, 2024
@anton-trunov anton-trunov modified the milestones: v1.5.0, v1.6.0 Sep 15, 2024
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

Successfully merging this pull request may close these issues.

Use of const String in receive parameter
3 participants