Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Using
try_from
in the instruction handler:results in:
Details
Ideally, the fix would be limited to Anchor internals without needing a breaking change to the users. However, this is difficult to achieve because #2656 added the same lifetime requirement for the
AccountInfo
references:anchor/lang/src/accounts/account.rs
Lines 226 to 229 in c93b33a
The
info: &'info AccountInfo<'info>
part is incompatible with Anchor's codegen because accounts passed to the context has a shorter lifetime than theAccountInfo
's internal lifetime. While the lifetime annotations are incorrect based on Anchor's codegen, it's also not accidental. To properly annotate these lifetimes, we'd have to add another lifetime to theAccount
type(and all other account types) which would significantly hinder the developer experience since it's a public API.In short, this problem is caused by annoting the same lifetimes in places where it's technically incorrect but we're doing so in order to not have multiple lifetimes in account types.
One possible solution for this problem is to elide the lifetimes by using
core::mem::transmute
. Usingtransmute
is heavily discouraged but my understanding is, this should be safe to do in this case because bothaccounts
lifetime and theAccountInfo
's internal lifetime live longer than the user's instruction handler.Usage
Summary of changes
try_from
usagetry_from!
macro