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

upgrade errors advanced function signatures #20663

Conversation

jordanjennings-mysten
Copy link
Contributor

Description

function signature upgrade errors test and improve formatting for:

  • type params in functions
  • vectors
  • &
  • mut
  • structs

Test plan

How did you test the new or updated feature?


Release notes

Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required.

For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates.

  • Protocol:
  • Nodes (Validators and Full nodes):
  • Indexer:
  • JSON-RPC:
  • GraphQL:
  • CLI:
  • Rust SDK:
  • REST API:

Copy link

vercel bot commented Dec 18, 2024

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

Name Status Preview Comments Updated (UTC)
sui-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 18, 2024 0:12am
3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
multisig-toolkit ⬜️ Ignored (Inspect) Visit Preview Dec 18, 2024 0:12am
sui-kiosk ⬜️ Ignored (Inspect) Visit Preview Dec 18, 2024 0:12am
sui-typescript-docs ⬜️ Ignored (Inspect) Visit Preview Dec 18, 2024 0:12am

Copy link
Contributor

@stefan-mysten stefan-mysten left a comment

Choose a reason for hiding this comment

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

This looks sweet, thanks @jordanjennings-mysten.

┌─ /fixtures/upgrade_errors/function_errors_v2/sources/UpgradeErrors.move:42:45
42 │ public fun func_with_wrong_struct_param(a: StructB): u64 {
│ ^ Unexpected parameter '0x0::upgrades::StructB', expected '0x0::upgrades::StructA'
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe?

Suggested change
^ Unexpected parameter '0x0::upgrades::StructB', expected '0x0::upgrades::StructA'
^ Unexpected parameter with type '0x0::upgrades::StructB', expected '0x0::upgrades::StructA'

Copy link
Member

Choose a reason for hiding this comment

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

+1 to this -- in the binary representation, all we see is a type, but we should clarify for the sake of people reading source code.

@jordanjennings-mysten jordanjennings-mysten merged commit 4767a0b into MystenLabs:main Dec 19, 2024
48 checks passed
Copy link
Member

@amnn amnn left a comment

Choose a reason for hiding this comment

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

Sorry to leave post-landing notes -- I think we still need to handle the following cases:

  • generic structs and enums in function signatures
  • type parameters in struct and variant fields.

I've also left a suggestion to clean-up the allocation pattern for format_params. Not a big deal in this setting, but something we generally want to be careful about when dealing with recursive structures like types.

┌─ /fixtures/upgrade_errors/function_errors_v2/sources/UpgradeErrors.move:42:45
42 │ public fun func_with_wrong_struct_param(a: StructB): u64 {
│ ^ Unexpected parameter '0x0::upgrades::StructB', expected '0x0::upgrades::StructA'
Copy link
Member

Choose a reason for hiding this comment

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

+1 to this -- in the binary representation, all we see is a type, but we should clarify for the sake of people reading source code.

Type::Reference(t) => {
format!("&{}", format_param(t, type_params, secondary)?)
}
_ => format!("{}", param),
Copy link
Member

Choose a reason for hiding this comment

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

Note that this does not handle generic structs and enums (if they contain type parameters, you won't find them and replace them with their type param name). Sorry in advance if I'm pointing something out that you addressed in some later PR, I know this is something we talked about on Wednesday!

@@ -895,6 +896,36 @@ fn function_lost_public(
Ok(diags)
}

fn format_param(
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, this function will behave more nicely when it comes to allocations if you structure it to accept a buffer to write into, rather than return a string:

fn format_params(
    param: &Type,
    type_params: Vec<SourceName>,
    output: &mut String,
    secondary: &mut Vec<(Loc, String)>,
) -> Result<(), Error>;

Instead of format!(...) you can use write!(output, ...).unwrap() -- it is safe to unwrap because writing to a string doesn't panic. With this pattern, you don't end up allocating small temporary strings and then adding them onto the larger main output string.

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