Repository navigation
Fix invalid conversion between library public or external function with different state mutability - #17064
Fix invalid conversion between library public or external function with different state mutability#17064matheusaaguiar wants to merge 2 commits into
Conversation
blishko
left a comment
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
I was a bit confused at first, but the docs state that (emphasis by me): The last phrase comes after a warning and note block, so they may pass unnoticed.
If the types match exactly, then there is no conversion involved.
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." I think that at least for consistency with codegen and docs, analysis should not allow library public and external function conversions. |
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. |
29f9a86 to
2e53bbb
Compare
|
@cameel I have pushed the changes implementing the conversion as discussed in the issue. |
555772e to
c6bb3aa
Compare
blishko
left a comment
There was a problem hiding this comment.
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; } | |||
There was a problem hiding this comment.
| 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?
| function g(uint y) public view returns (uint) { | ||
| return y + (block.number / block.number); | ||
| } |
There was a problem hiding this comment.
This is not used. Can we remove it?
|
Also, what about the docs? Should we clarify the documentation to cover the library functions properly? |
c6bb3aa to
2ca92b4
Compare
I updated the PR description and adjusted the tests as you suggested, and also added a couple of new ones.
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 |
There was a problem hiding this comment.
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.
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.