Skip to content

Commit

Permalink
fix for contract-scoped constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich committed Jun 19, 2024
1 parent 2492d83 commit 9cd41fe
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/grammar/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ export type ASTReceiveType =
}
| {
kind: "internal-const-comment";
comment: ASTId;
comment: ASTLvalueRef[];
}
| {
kind: "bounce";
Expand All @@ -386,7 +386,7 @@ export type ASTReceiveType =
}
| {
kind: "external-const-comment";
comment: ASTId;
comment: ASTLvalueRef[];
};

export type ASTReceive = {
Expand Down
4 changes: 2 additions & 2 deletions src/grammar/grammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ Tact {

Receiver = receive "(" Parameter? ")" "{" Statement* "}" --regular
| receive "(" stringLiteral ")" "{" Statement* "}" --comment
| receive "(" id ")" "{" Statement* "}" --constComment
| receive "(" LValue ")" "{" Statement* "}" --constComment
| "bounced" "(" Parameter ")" "{" Statement* "}" --bounced // cannot be a reserved word because there a 'bounced' field in stdlib's 'Context' structure
| external "(" Parameter? ")" "{" Statement* "}" --externalRegular
| external "(" stringLiteral ")" "{" Statement* "}" --externalComment
| external "(" id ")" "{" Statement* "}" --constExternalComment
| external "(" LValue ")" "{" Statement* "}" --constExternalComment

Statement = StatementLet
| StatementBlock
Expand Down
4 changes: 2 additions & 2 deletions src/grammar/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ semantics.addOperation<ASTNode>("astOfItem", {
kind: "def_receive",
selector: {
kind: "internal-const-comment",
comment: comment.astOfExpression(),
comment: comment.astOfLValue(),
},
statements: receiverBody.children.map((s) => s.astOfStatement()),
ref: createRef(this),
Expand Down Expand Up @@ -430,7 +430,7 @@ semantics.addOperation<ASTNode>("astOfItem", {
kind: "def_receive",
selector: {
kind: "external-const-comment",
comment: comment.astOfExpression(),
comment: comment.astOfLValue(),
},
statements: receiverBody.children.map((s) => s.astOfStatement()),
ref: createRef(this),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ exports[`constant string receiver should implement const string receiver correct
},
"getters": [],
"receivers": [
{
"message": {
"kind": "empty",
},
"receiver": "internal",
},
{
"message": {
"kind": "text",
Expand All @@ -99,6 +105,13 @@ exports[`constant string receiver should implement const string receiver correct
},
"receiver": "internal",
},
{
"message": {
"kind": "text",
"text": "string 4",
},
"receiver": "internal",
},
],
"types": [
{
Expand Down Expand Up @@ -244,14 +257,14 @@ exports[`constant string receiver should implement const string receiver correct
},
"bounce": true,
"from": "@treasure(treasure)",
"to": "kQDg3ce4axASb2iP16WY4k7nP9uV--zZxPfmGYbm8ZF_YPj4",
"to": "kQCjCGWLilNv7PPffKoWdQHPZ-88MekCb9F_U5ddfPTsuH3_",
"type": "internal",
"value": "10",
},
},
{
"$type": "processed",
"gasUsed": 6689n,
"gasUsed": 6857n,
},
{
"$type": "sent",
Expand All @@ -262,10 +275,10 @@ exports[`constant string receiver should implement const string receiver correct
"type": "text",
},
"bounce": true,
"from": "kQDg3ce4axASb2iP16WY4k7nP9uV--zZxPfmGYbm8ZF_YPj4",
"from": "kQCjCGWLilNv7PPffKoWdQHPZ-88MekCb9F_U5ddfPTsuH3_",
"to": "@treasure(treasure)",
"type": "internal",
"value": "9.992115",
"value": "9.991947",
},
],
},
Expand All @@ -283,14 +296,14 @@ exports[`constant string receiver should implement const string receiver correct
},
"bounce": true,
"from": "@treasure(treasure)",
"to": "kQDg3ce4axASb2iP16WY4k7nP9uV--zZxPfmGYbm8ZF_YPj4",
"to": "kQCjCGWLilNv7PPffKoWdQHPZ-88MekCb9F_U5ddfPTsuH3_",
"type": "internal",
"value": "10",
},
},
{
"$type": "processed",
"gasUsed": 9300n,
"gasUsed": 9468n,
},
{
"$type": "sent",
Expand All @@ -301,10 +314,10 @@ exports[`constant string receiver should implement const string receiver correct
"type": "text",
},
"bounce": true,
"from": "kQDg3ce4axASb2iP16WY4k7nP9uV--zZxPfmGYbm8ZF_YPj4",
"from": "kQCjCGWLilNv7PPffKoWdQHPZ-88MekCb9F_U5ddfPTsuH3_",
"to": "@treasure(treasure)",
"type": "internal",
"value": "9.989504",
"value": "9.989336",
},
],
},
Expand All @@ -322,14 +335,14 @@ exports[`constant string receiver should implement const string receiver correct
},
"bounce": true,
"from": "@treasure(treasure)",
"to": "kQDg3ce4axASb2iP16WY4k7nP9uV--zZxPfmGYbm8ZF_YPj4",
"to": "kQCjCGWLilNv7PPffKoWdQHPZ-88MekCb9F_U5ddfPTsuH3_",
"type": "internal",
"value": "10",
},
},
{
"$type": "processed",
"gasUsed": 9377n,
"gasUsed": 9581n,
},
{
"$type": "sent",
Expand All @@ -340,10 +353,49 @@ exports[`constant string receiver should implement const string receiver correct
"type": "text",
},
"bounce": true,
"from": "kQDg3ce4axASb2iP16WY4k7nP9uV--zZxPfmGYbm8ZF_YPj4",
"from": "kQCjCGWLilNv7PPffKoWdQHPZ-88MekCb9F_U5ddfPTsuH3_",
"to": "@treasure(treasure)",
"type": "internal",
"value": "9.989223",
},
],
},
],
},
{
"$seq": 4,
"events": [
{
"$type": "received",
"message": {
"body": {
"text": "string 4",
"type": "text",
},
"bounce": true,
"from": "@treasure(treasure)",
"to": "kQCjCGWLilNv7PPffKoWdQHPZ-88MekCb9F_U5ddfPTsuH3_",
"type": "internal",
"value": "10",
},
},
{
"$type": "processed",
"gasUsed": 9730n,
},
{
"$type": "sent",
"messages": [
{
"body": {
"text": "string 4",
"type": "text",
},
"bounce": true,
"from": "kQCjCGWLilNv7PPffKoWdQHPZ-88MekCb9F_U5ddfPTsuH3_",
"to": "@treasure(treasure)",
"type": "internal",
"value": "9.989427",
"value": "9.989074",
},
],
},
Expand Down
1 change: 1 addition & 0 deletions src/test/e2e-emulated/const-string-receiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe("constant string receiver", () => {
await contract.send(treasure, { value: toNano("10") }, "string 1");
await contract.send(treasure, { value: toNano("10") }, "string 2");
await contract.send(treasure, { value: toNano("10") }, "string 3");
await contract.send(treasure, { value: toNano("10") }, "string 4");
await system.run();

expect(tracker.collect()).toMatchSnapshot();
Expand Down
6 changes: 6 additions & 0 deletions src/test/e2e-emulated/contracts/const-string-receiver.tact
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const string2: String = "string 2";
const string3: String = "string 3";

contract ConstStringReceiverTester {
const string4: String = "string 4";

receive() { }

receive("string 1") {
Expand All @@ -16,4 +18,8 @@ contract ConstStringReceiverTester {
receive(string3) {
self.reply(string3.asComment());
}

receive(self.string4) {
self.reply(self.string4.asComment());
}
}
70 changes: 40 additions & 30 deletions src/types/__snapshots__/resolveDescriptors.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,46 @@ Line 24, col 3:
"
`;
exports[`resolveDescriptors should fail descriptors for contract-duplicate-const-string-receiver 1`] = `
"<unknown>:20:5: Receive function for string "string 1" already exists
Line 20, col 5:
19 |
> 20 | receive(string1) {
^~~~~~~~~~~~~~~~~~
21 |
"
`;
exports[`resolveDescriptors should fail descriptors for contract-duplicate-const-string-receiver2 1`] = `
"<unknown>:15:5: Receive function for "string 1" already exists
Line 15, col 5:
14 |
> 15 | receive("string 1") {
^~~~~~~~~~~~~~~~~~~~~
16 |
"
`;
exports[`resolveDescriptors should fail descriptors for contract-duplicate-const-string-receiver3 1`] = `
"<unknown>:20:5: Constant "string3" not found
Line 20, col 5:
19 |
> 20 | receive(string3) {
^~~~~~~~~~~~~~~~~~
21 |
"
`;
exports[`resolveDescriptors should fail descriptors for contract-duplicate-const-string-receiver4 1`] = `
"<unknown>:15:5: Receive function for string "string 1" already exists
Line 15, col 5:
14 |
> 15 | receive(self.string1) {
^~~~~~~~~~~~~~~~~~~~~~~
16 |
"
`;
exports[`resolveDescriptors should fail descriptors for contract-duplicate-external-fallback-receiver 1`] = `
"<unknown>:20:5: Empty receive function already exists
Line 20, col 5:
Expand Down Expand Up @@ -90,36 +130,6 @@ Line 12, col 23:
"
`;
exports[`resolveDescriptors should fail descriptors for contract-receiver-const-string-duplicate 1`] = `
"<unknown>:20:5: Receive function for string "string 1" already exists
Line 20, col 5:
19 |
> 20 | receive(string1) {
^~~~~~~~~~~~~~~~~~
21 |
"
`;
exports[`resolveDescriptors should fail descriptors for contract-receiver-const-string-duplicate2 1`] = `
"<unknown>:15:5: Receive function for "string 1" already exists
Line 15, col 5:
14 |
> 15 | receive("string 1") {
^~~~~~~~~~~~~~~~~~~~~
16 |
"
`;
exports[`resolveDescriptors should fail descriptors for contract-receiver-const-string-duplicate3 1`] = `
"<unknown>:20:5: Constant "string3" not found
Line 20, col 5:
19 |
> 20 | receive(string3) {
^~~~~~~~~~~~~~~~~~
21 |
"
`;
exports[`resolveDescriptors should fail descriptors for contract-receiver-int 1`] = `
"<unknown>:13:3: Receive function can only accept message, Slice or String
Line 13, col 3:
Expand Down
23 changes: 19 additions & 4 deletions src/types/resolveDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,10 +1033,25 @@ export function resolveDescriptors(ctx: CompilerContext) {
const internal =
d.selector.kind === "internal-const-comment";

const commentId = d.selector.comment.value;
const commentConstant =
s.constants.find((v) => v.name === commentId) ??
staticConstants.get(commentId);
if (d.selector.comment.length > 2) {
// TEMPORARY
// to be reworked after #284 and #400 are resolved
throwSyntaxError(
"Invalid comment receiver selector",
d.ref,
);
}

const isSelf =
d.selector.comment.length === 2 &&
d.selector.comment[0].name === "self";

const commentId =
d.selector.comment[d.selector.comment.length - 1]
.name;
const commentConstant = isSelf
? s.constants.find((v) => v.name === commentId)
: staticConstants.get(commentId);

if (!commentConstant) {
throwSyntaxError(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
primitive Int;
primitive String;

trait BaseTrait {

}

contract Main {
const string1: String = "string 1";

receive("string 1") {

}

receive(self.string1) {

}
}

0 comments on commit 9cd41fe

Please sign in to comment.