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

Implicit accessor function should return struct members, not struct #1374

Merged
merged 1 commit into from
Jun 24, 2023

Conversation

seanyoung
Copy link
Contributor

If the acessor function returns a single struct, then the members should be returned. If any of those members are structs, then those members will remain structs.

contract C {
	struct S { int a; bool b; }

	S public s;

	function foo() public {
		(int a, bool b) = this.s();
	}
}

Also, the accesor function should be declared external and therefore not accessible as an internal function call.

@seanyoung seanyoung force-pushed the destructure branch 6 times, most recently from 8b8dc30 to 7ea2dd3 Compare June 20, 2023 16:10
@seanyoung seanyoung marked this pull request as ready for review June 20, 2023 18:53
src/abi/tests.rs Outdated Show resolved Hide resolved
Comment on lines 515 to 524
body = vec![
Statement::VariableDecl(pt::Loc::Implicit, var_no, param, Some(expr)),
Statement::Return(
pt::Loc::Implicit,
Some(Expression::List {
loc: pt::Loc::Implicit,
list,
}),
),
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of building the function body during codegen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the argument for keeping the ast more "pure", but on the other hand I don't think it is that ugly. However, I am not sure what this would look like, code wise.

We need to build the function prototype in sema, that includes the parameters and return values. Once we've calculated all of that, we've already done almost all the work to build up the body. We would need to do the same work again in codegen to build the body, or somehow share this code.

Maybe something for another day.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle I agree with Lucas, it seems odd to sort of generate code not in codegen but in sema. However there might be more such instances. I also thought a bit about it and didn't come to an immediate conclusion or solution to that question.

src/sema/variables.rs Outdated Show resolved Hide resolved
];

returns = def.fields.clone();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two branches of if constant { .. } else {...} have nearly identical code. You can simplify most of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified it, not sure it can be simplified more

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your new version looks much cleaner. Thanks!

Comment on lines 9 to 10
// error: 5:11-14: functions declared external cannot be called via an internal function call
// note 2:22-23: declaration of function 'c'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The note points to a public variable, while the error says functions declared external. Don't we need a clearer error message for this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I've improved this.

Copy link
Contributor

@xermicus xermicus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

If the acessor function returns a single struct, then the members should be returned. If
any of those members are structs, then those members will remain structs.

	contract C {
		struct S { int a; bool b; }

		S public s;

		function foo() public {
			(int a, bool b) = this.s();
		}
	}

Also, the accesor function should be declared `external` and therefore not accessible
as an internal function call.

Signed-off-by: Sean Young <[email protected]>
@seanyoung seanyoung merged commit a84b0ad into hyperledger:main Jun 24, 2023
17 checks passed
@seanyoung seanyoung deleted the destructure branch June 24, 2023 08:23
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.

3 participants