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

[move 2024][alpha] Method syntax #13933

Merged
merged 18 commits into from
Sep 29, 2023
Merged

[move 2024][alpha] Method syntax #13933

merged 18 commits into from
Sep 29, 2023

Conversation

tnowacki
Copy link
Contributor

@tnowacki tnowacki commented Sep 22, 2023

Description

See 'Release Notes' for feature description.

This PR:

  • Resurrects original method style syntax
  • Adds a defines_primitive annotation so that method style syntax can be used with primitive/builtin types
  • Adds use fun declarations

Some notes about implementation details

  • A normal use alias, use a::m::f as g introduces an implicit use fun a::m::f as a::m::S.g in situations where the first argument of a::m::f is a::m::S. This ensures that there isn't any weird precedence/shadowing between use and use fun in a given scope.
    • There are some nuances here, but the rules are ensuring that once you have a module that calls x.foo(), it always resolves to the same function a::m::bar, even if new public use funs are added elsewhere.
  • I had to move ProgramInfo into the naming::ast::program and typing::ast::program to properly resolve use funs (particularly with this implicit setup described above)

TODO:

  • use fun tests
  • gating use fun declarations
  • Finalize wording in error messages. I don't love "method style" but I'm scared of just a a bare "method" without qualification (as there is no dynamic dispatch, which I feel is closely associated)

Follow up

  • let mut and mut variable modifiers
  • I think I will rewrite alias maps in expansion to reduce clones (as I did with use fun declarations

Test Plan

How did you test the new or updated feature?


If your changes are not user-facing and not a breaking change, you can skip the following section. Otherwise, please indicate what changed, and then add to the Release Notes section as highlighted during the release process.

Type of Change (Check all that apply)

  • protocol change
  • user-visible impact
  • breaking change for a client SDKs
  • breaking change for FNs (FN binary must upgrade)
  • breaking change for validators or node operators (must upgrade binaries)
  • breaking change for on-chain data layout
  • necessitate either a data wipe or data migration

Release notes

Method style syntax has been added to Move 2024.alpha. This means functions can now be invoked with the syntax, e.foo(arg1, arg2). Assuming e: a::m::S (or &a::m::S or &mut a::m::S), e.foo(arg1, arg2) will resolve to a::m::foo(e, arg1, arg2). Additionally, the compiler will borrow e when necessary.
For instances where the desired target function is not declared in the same module as the type (or is not declared with the desired name), a use fun alias can be used instead. For example, use b::n::bar as S.baz will allow e.baz() to resolve as b::n::bar(e). A public use fun can be declared in the types defining module, allowing the alias to be used outside of the module.

@vercel
Copy link

vercel bot commented Sep 22, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mysten-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 28, 2023 10:09pm
sui-typescript-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 28, 2023 10:09pm
3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
explorer ⬜️ Ignored (Inspect) Visit Preview Sep 28, 2023 10:09pm
multisig-toolkit ⬜️ Ignored (Inspect) Visit Preview Sep 28, 2023 10:09pm
sui-kiosk ⬜️ Ignored (Inspect) Visit Preview Sep 28, 2023 10:09pm

Copy link
Contributor

@tzakian tzakian left a comment

Choose a reason for hiding this comment

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

Just a bunch of smaller things -- nothing major.

Can't wait for this to be enabled!

@@ -250,6 +251,7 @@ codes!(
(NOTE: this may become an error in the future)",
severity: Warning
},
InvalidMethodCall: { msg: "invalid method style call", severity: BlockingError },
Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely prefer this over "invalid method call" and I think this conveys that this isn't an actual method call. One other option might be "invalid method-style function call" the addition of function helps disambiguate that this isn't an actual method you're calling I think.

Copy link
Contributor

Choose a reason for hiding this comment

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

🚲 "invalid method-style invocation"

@@ -227,6 +247,7 @@ pub fn program(
keyed
};

super::primitive_definers::determine(context.env, pre_compiled_lib, &module_map);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a gigantic name of determine for this function since this validates them as opposed to determining them. Thoughts something more like validate?

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 shall give it an even more cryptic name! (Seriously I think modules is more in line with the rest of the compiler, but I'll add a comment)

@tnowacki tnowacki changed the title [move 2024][alpha] Method style syntax [move 2024][alpha] Method syntax Sep 26, 2023
Copy link
Contributor

@tzakian tzakian left a comment

Choose a reason for hiding this comment

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

Looks great! This is amazing amazing amazing.

One little small change needed in an error message as far as I could see, but otherwise I'm happy :)

2 │ fun foo<T>(x: T) {
│ - But 'T' was declared as a type parameter here
3 │ use fun foo as T.foo;
│ ^^^^^^^^^^^^^^^^^^^^^ Invalid 'use fun'. Cannot associate a function as a method a type parameter
Copy link
Contributor

Choose a reason for hiding this comment

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

This error message is a little wonky

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