Skip to content

Fix invalid conversion between library public or external function with different state mutability - #17064

Open
matheusaaguiar wants to merge 2 commits into
developfrom
fix-function-library-invalid-conversion
Open

matheusaaguiar wants to merge 2 commits into
developfrom
fix-function-library-invalid-conversion

Conversation

@matheusaaguiar

@matheusaaguiar matheusaaguiar commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Fix #16906.

Libraries public and external functions cannot be converted even when the conversion is valid.
These functions cannot be named, so currently the only way to generate a conversion between library public/external functions is by using a ternary expression or a try...catch block.

Both codegen pipelines asserts against any conversion from/to public and external library functions, albeit analysis allows it.

This PR lifts the restriction in codegen and makes it consistent with analysis.

@matheusaaguiar matheusaaguiar self-assigned this Oct 1, 2026
blishko
blishko previously approved these changes Oct 1, 2026

@blishko blishko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am approving, since this fixes the ICE.
Though I am not sure if I understand the docs correctly, or if maybe some clarification in the docs would help.

Is it the case that for library functions the types must match exactly?

@cameel cameel left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am approving, since this fixes the ICE.

@blishko I don't think that's the right approach to ICEs. An ICE is the compiler reaching a contradiction due to something that was not fully thought through. The value of a fix is not in removing the ICE (that's trivial - we could just remove the assert if we wanted that) but in making the compiler behave correctly. As long as there are any doubts regarding what the correct behavior is, it's far safer not to remove it.

A bad fix can enable a new behavior that we will have to support in the future. In this particular case the PR is replacing the ICE with an error message so at least that does not constrain us (removing an error later is not backwards-incompatible), but I still think we should not go forward with it if there are any doubts just because it removes the ICE. We should resolve all the questions before we approve it. There's an unanswered one from @matheusaaguiar in the issue:

I am not sure if we should allow this conversion even though both sides are library public/external functions.

@matheusaaguiar

matheusaaguiar commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Though I am not sure if I understand the docs correctly, or if maybe some clarification in the docs would help.

I was a bit confused at first, but the docs state that (emphasis by me):
"External function types, on the other hand, are only compatible with public and external contract functions.
Libraries are excluded because they require a delegatecall and use a different ABI convention for their selectors."

The last phrase comes after a warning and note block, so they may pass unnoticed.

Is it the case that for library functions the types must match exactly?

If the types match exactly, then there is no conversion involved.

There's an unanswered one from @matheusaaguiar in the issue:

I am not sure if we should allow this conversion even though both sides are library public/external functions.

I mentioned that because the reporter claimed the "conversion is legal, and every other construct that performs it compiles: an inline array literal [H.f, H.g], a tuple assignment, assignment through a variable."
However, the docs confirm that it's not legal and also I could not reproduce the cases that were supposed to compile.

I think that at least for consistency with codegen and docs, analysis should not allow library public and external function conversions.
If that is changed, I guess that it is also a breaking change, right?

@cameel

cameel commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator

I mentioned that because the reporter claimed the "conversion is legal

There's a small nuance here: conversion to what. They are not convertible to external contract function types. But being convertible to external library function types seems quite natural and it's currently allowed. It's only this minor corner case of mutability conversion that's in question. See #16906 (comment).

In any case, I think we should be continuing that discussion in the issue.

@blishko
blishko dismissed their stale review October 3, 2026 11:40

Apparently, my approval was premature.

@matheusaaguiar
matheusaaguiar marked this pull request as draft October 5, 2026 18:08
@matheusaaguiar
matheusaaguiar force-pushed the fix-function-library-invalid-conversion branch from 29f9a86 to 2e53bbb Compare November 3, 2026 18:42
@matheusaaguiar
matheusaaguiar marked this pull request as ready for review November 4, 2026 14:03
@matheusaaguiar

Copy link
Copy Markdown
Contributor Author

@cameel I have pushed the changes implementing the conversion as discussed in the issue.

@matheusaaguiar
matheusaaguiar force-pushed the fix-function-library-invalid-conversion branch 2 times, most recently from 555772e to c6bb3aa Compare November 17, 2026 20:52

@blishko blishko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the fix is correct now.

I just have some comments about the tests.
Also, the PR description is out of date now?

@@ -0,0 +1,17 @@
library L {
struct S { uint x; }
function pure_sum(S storage s, uint v) public view returns (uint) { return s.x + v; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
function pure_sum(S storage s, uint v) public view returns (uint) { return s.x + v; }
function view_sum(S storage s, uint v) public view returns (uint) { return s.x + v; }

It's not pure, no?

Comment on lines +5 to +7
function g(uint y) public view returns (uint) {
return y + (block.number / block.number);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is not used. Can we remove it?

@blishko

blishko commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Also, what about the docs? Should we clarify the documentation to cover the library functions properly?

@matheusaaguiar
matheusaaguiar force-pushed the fix-function-library-invalid-conversion branch from c6bb3aa to 2ca92b4 Compare November 18, 2026 19:45
@matheusaaguiar

Copy link
Copy Markdown
Contributor Author

I just have some comments about the tests. Also, the PR description is out of date now?

I updated the PR description and adjusted the tests as you suggested, and also added a couple of new ones.

Also, what about the docs? Should we clarify the documentation to cover the library functions properly?

I clarified that in the docs.

Such comparisons are not advised, and will lead to the compiler issuing a warning, until the next breaking release (0.9.0),
when the warning will be upgraded to an error, thereby making such comparisons disallowed.

Libraries are excluded because they require a ``delegatecall`` and use :ref:`a different ABI

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I deliberately moved this because the note might "hide" it...at the first time I read this section, I didn't notice this paragraph at the end.

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.

[ICE] Conditional expression selecting between two library functions with different state mutability aborts when called

3 participants