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

Using global #12288

Merged
merged 1 commit into from
Mar 14, 2022
Merged

Using global #12288

merged 1 commit into from
Mar 14, 2022

Conversation

chriseth
Copy link
Contributor

@chriseth chriseth commented Nov 16, 2021

The alternative syntax for this is

struct X { ... } using {f, mul as *};
type MyFloat is uint using {f};

The disadvantage is that you can only have one using statement per type and you cannot "mix" e.g. multiple modules, i.e. you cannot do export using Modulename for MyFloat; export using {f, g, h} for MyFloat;

We can of course change the using statement so that using {f, M} for MyFloat; is equivalent to using f for MyFloat; using M for MyFloat - currently modules and libraries cannot be used inside the braces.

@chriseth
Copy link
Contributor Author

This is the "export using" statement and actually much easier to implement than I thought.

@chriseth
Copy link
Contributor Author

What about using {...} for MyType attached; or something like that?

@chriseth chriseth force-pushed the extend-using-statement branch 4 times, most recently from 2323490 to 67739e9 Compare November 22, 2021 15:52
@chriseth chriseth force-pushed the extend-using-statement branch from 67739e9 to af6dd26 Compare December 1, 2021 10:17
@cameel cameel added the has dependencies The PR depends on other PRs that must be merged first label Dec 10, 2021
@chriseth chriseth force-pushed the extend-using-statement branch 3 times, most recently from 4ee70a9 to 849e8ed Compare February 7, 2022 16:39
@chriseth chriseth force-pushed the exportUsing branch 2 times, most recently from 980f3ad to 10e1dbc Compare February 8, 2022 17:19
@chriseth chriseth marked this pull request as ready for review February 8, 2022 17:20
@chriseth
Copy link
Contributor Author

chriseth commented Feb 8, 2022

This is ready for review, the name of the keyword is still up for debate.

@chriseth
Copy link
Contributor Author

chriseth commented Feb 8, 2022

Idea for test:
file A: define type X, function f and using... global
file B: import X from A and define a function g that returns X
file C: import only g from B and use g().f().

It should work even though neither f nor X are available in C by name.

@ekpyron
Copy link
Member

ekpyron commented Feb 9, 2022

Maybe

==== Source: A ====
using {f} for S global;
using {g} for S;
struct S { uint x; }
function gen() pure returns (S memory) {}
function f(S memory _x) pure { _x.g(); }
function g(S memory _x) pure { }
==== Source: B ====
import "A";
function test() pure
{
    gen().f(); // fine
    gen().g(); // some error
}

or something similar to check global and non-global using together?

In general this looks good, but may need a few more tests.

@chriseth chriseth changed the title Export using. Using global Feb 15, 2022
@chriseth chriseth force-pushed the extend-using-statement branch 4 times, most recently from 6f3398a to 1a1ca44 Compare March 9, 2022 15:59
@chriseth chriseth force-pushed the exportUsing branch 2 times, most recently from 80052b6 to 2e3394d Compare March 9, 2022 16:18
@chriseth chriseth force-pushed the extend-using-statement branch from 0723c25 to 4794861 Compare March 14, 2022 10:59
@chriseth chriseth force-pushed the extend-using-statement branch from 4794861 to 672951c Compare March 14, 2022 11:33
@ekpyron
Copy link
Member

ekpyron commented Mar 14, 2022

If I see it correctly, the global thing still needs to be exported to and imported from the json AST.

Copy link
Member

@ekpyron ekpyron left a comment

Choose a reason for hiding this comment

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

Looks good in general apart from the missing json ast part - but I also need another look after a rebase.

Base automatically changed from extend-using-statement to develop March 14, 2022 13:10
@ekpyron
Copy link
Member

ekpyron commented Mar 14, 2022

AST Json tests need updating. Looking at the rest regardless.

@@ -359,7 +359,8 @@ ASTPointer<UsingForDirective> ASTJsonImporter::createUsingForDirective(Json::Val
_node,
move(functions),
!_node.isMember("libraryName"),
_node["typeName"].isNull() ? nullptr : convertJsonToASTNode<TypeName>(_node["typeName"])
_node["typeName"].isNull() ? nullptr : convertJsonToASTNode<TypeName>(_node["typeName"]),
_node.isMember("global") && _node["global"].asBool()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_node.isMember("global") && _node["global"].asBool()
_node.isMember("global") && memberAsBool(_node, "global")

?
Or only memberAsBool or is the idea to default to false instead of raising an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is to default to false.

Copy link
Member

Choose a reason for hiding this comment

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

_node.isMember("global") && memberAsBool(_node, "global") would still be more graceful I guess, if global is there and not a boolean, but yeah, fine either way.

Copy link
Member

Choose a reason for hiding this comment

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

Now it won't default to false anymore, though, will it :-)? But yeah, it also doesn't have to, so also fine.

@ekpyron
Copy link
Member

ekpyron commented Mar 14, 2022

Just for the sake of completeness, it could have some tests for global as identifier or type name and type global is uint; using { f } for global; using { g } for global global; - but on the other hand all that should work just fine...

@chriseth
Copy link
Contributor Author

Added the test.

@ekpyron
Copy link
Member

ekpyron commented Mar 14, 2022

I'm mainly wondering if we need more tests at this point...
But even weirdest stuff like this should just work:

==== Source: A ====
import {T as U} from "A";
import "A" as X;

type T is uint;
function f(T) pure {}
using { f } for X.X.U global;
using { f } for T global;

function g() pure {
        U t;
        t.f();
}

So maybe coverage is already fine...

ekpyron
ekpyron previously approved these changes Mar 14, 2022
Copy link
Member

@ekpyron ekpyron left a comment

Choose a reason for hiding this comment

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

Looks good to me - should be squashed before merging, though.

@chriseth
Copy link
Contributor Author

Added another semantic test and squashed.

@chriseth chriseth merged commit e154d43 into develop Mar 14, 2022
@chriseth chriseth deleted the exportUsing branch March 14, 2022 17:57
Sticknmove

This comment was marked as spam.

@Sticknmove

This comment was marked as spam.

Sticknmove

This comment was marked as spam.

Sticknmove

This comment was marked as spam.

Xanewok added a commit to Xanewok/slang that referenced this pull request Sep 19, 2023
Xanewok added a commit to Xanewok/slang that referenced this pull request Sep 21, 2023
…8.13

See ethereum/solidity#12288

It's impossible to specify a newly introduced keyword optionally in the
YML spec directly or even as an additional inline parser (`null`
definitions are treated as an undefined parser and error out), so this
adds versioning to the UsingDirective to add the new `global` contextual
keyword.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has dependencies The PR depends on other PRs that must be merged first
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants