Skip to content

fix: Replace quote! with quote_spanned! in #[program] macro#4032

Open
AvhiMaz wants to merge 5 commits intosolana-foundation:masterfrom
AvhiMaz:fix/quote-spans-program-macro
Open

fix: Replace quote! with quote_spanned! in #[program] macro#4032
AvhiMaz wants to merge 5 commits intosolana-foundation:masterfrom
AvhiMaz:fix/quote-spans-program-macro

Conversation

@AvhiMaz
Copy link
Copy Markdown

@AvhiMaz AvhiMaz commented Oct 29, 2025

Replaces all uses of quote!{} with quote_spanned! in the program macro
code generation to ensure generated code corresponds to correct source
code locations. This enables the Rust compiler to report error messages
at the actual problematic code instead of the macro invocation.

Changes:

  • Applied quote_spanned! to all 9 codegen files
  • Added spanned variants of helper functions
  • Proper span extraction at instruction, program, and module levels

This addresses the requirements from issue #4015.

@vercel
Copy link
Copy Markdown

vercel bot commented Oct 29, 2025

@AvhiMaz is attempting to deploy a commit to the Solana Foundation Team on Vercel.

A member of the Team first needs to authorize it.

@AvhiMaz AvhiMaz force-pushed the fix/quote-spans-program-macro branch 2 times, most recently from 4281950 to 0f32682 Compare October 29, 2025 04:21
@AvhiMaz AvhiMaz changed the title Fix/quote spans program macro fix: Replace quote! with quote_spanned! in #[program] macro Oct 29, 2025
@AvhiMaz AvhiMaz marked this pull request as ready for review October 29, 2025 04:24
@jamie-osec
Copy link
Copy Markdown
Collaborator

Please note that quote_spanned is not the correct solution in every case; it adds noise to the code so it should be used when it will improve developer UX - in particular consider when user code/identifiers (with potential errors) will be reflected in macro output and should be highlighted in error messages, or when a generated function will appear in a backtrace.

@AvhiMaz
Copy link
Copy Markdown
Author

AvhiMaz commented Oct 29, 2025

Please note that quote_spanned is not the correct solution in every case; it adds noise to the code so it should be used when it will improve developer UX - in particular consider when user code/identifiers (with potential errors) will be reflected in macro output and should be highlighted in error messages, or when a generated function will appear in a backtrace.

yaa true, i actually put quote_spanned in every file so I have gone through files and kept quote_spanned where it actually helps:

  • instruction definitions and their trait impls - users need to see errors there
  • handler wrappers and cpi methods - these show up in backtraces
  • dispatch and entry functions - they're in the execution stack
  • user's fallback handler - it's their code

removed it from the scaffolding stuff:

  • module wrappers (accounts, cpi modules)
  • idl dispatch (that's anchor internals)
  • generated helper code with no user identifiers

should fix the issue now.

@AvhiMaz AvhiMaz force-pushed the fix/quote-spans-program-macro branch from e8e4c9a to 1e9fe84 Compare October 29, 2025 14:50
Copy link
Copy Markdown
Collaborator

@0x4ka5h 0x4ka5h left a comment

Choose a reason for hiding this comment

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

could you please make changes for cli's saftey checks?

  • can you keep this issue (#3915) in mind so that changes should solve that issue too?
  • can you also implement some tests to showcase the working? also I would recommend to add a CI workflow constrains to prove the changes are working as needed.

@AvhiMaz
Copy link
Copy Markdown
Author

AvhiMaz commented Oct 31, 2025

hi @0x4ka5h added fix for issue #3915 - safety checks now show struct names and correct line numbers. tests added and ci configured to validate it works. can you review it ? thanks

@AvhiMaz AvhiMaz requested a review from 0x4ka5h October 31, 2025 09:45
@Otter-0x4ka5h
Copy link
Copy Markdown
Contributor

instead of pointing out to both#[program] and issue line, cant we just directly poke to the line where incident happens?
for eg:
instead

error: custom attribute panicked
 --> programs/account-info/src/lib.rs:5:1
  |
5 | #[program]
  | ^^^^^^^^^^
  |
  = help: message: Safety checks failed: 
                  /Users/0x4ka5h/Documents/intern/osec/development/anchor/tests/safety-checks/programs/account-info/src/lib.rs:21:0
                  Struct "Initialize1" field "unchecked" is unsafe, but is not documented.
                  Please add a `/// CHECK:` doc comment explaining why no checks through types are necessary.
                  Alternatively, for reasons like quick prototyping, you may disable the safety checks
                  by using the `skip-lint` option.
                  See https://www.anchor-lang.com/docs/the-accounts-struct#safety-checks for more information.
                              

vs

error: custom attribute panicked
 --> programs/account-info/src/lib.rs:21:0
     |
21 |  unchecked: AccountInfo<'info>,
     | ^^^^^^^^^^^^^^^^^^^^^^^^
     |
  = help: message: Safety checks failed: 
                  /Users/0x4ka5h/Documents/intern/osec/development/anchor/tests/safety-checks/programs/account-info/src/lib.rs:21:0
                  Struct "Initialize1" field "unchecked" is unsafe, but is not documented.
                  Please add a `/// CHECK:` doc comment explaining why no checks through types are necessary.
                  Alternatively, for reasons like quick prototyping, you may disable the safety checks
                  by using the `skip-lint` option.
                  See https://www.anchor-lang.com/docs/the-accounts-struct#safety-checks for more information.

@Otter-0x4ka5h
Copy link
Copy Markdown
Contributor

Otter-0x4ka5h commented Oct 31, 2025

Closes #3915 as well

@AvhiMaz
Copy link
Copy Markdown
Author

AvhiMaz commented Oct 31, 2025

instead of pointing out to both#[program] and issue line, cant we just directly poke to the line where incident happens? for eg: instead

error: custom attribute panicked
 --> programs/account-info/src/lib.rs:5:1
  |
5 | #[program]
  | ^^^^^^^^^^
  |
  = help: message: Safety checks failed: 
                  /Users/0x4ka5h/Documents/intern/osec/development/anchor/tests/safety-checks/programs/account-info/src/lib.rs:21:0
                  Struct "Initialize1" field "unchecked" is unsafe, but is not documented.
                  Please add a `/// CHECK:` doc comment explaining why no checks through types are necessary.
                  Alternatively, for reasons like quick prototyping, you may disable the safety checks
                  by using the `skip-lint` option.
                  See https://www.anchor-lang.com/docs/the-accounts-struct#safety-checks for more information.
                              

vs

error: custom attribute panicked
 --> programs/account-info/src/lib.rs:21:0
     |
21 |  unchecked: AccountInfo<'info>,
     | ^^^^^^^^^^^^^^^^^^^^^^^^
     |
  = help: message: Safety checks failed: 
                  /Users/0x4ka5h/Documents/intern/osec/development/anchor/tests/safety-checks/programs/account-info/src/lib.rs:21:0
                  Struct "Initialize1" field "unchecked" is unsafe, but is not documented.
                  Please add a `/// CHECK:` doc comment explaining why no checks through types are necessary.
                  Alternatively, for reasons like quick prototyping, you may disable the safety checks
                  by using the `skip-lint` option.
                  See https://www.anchor-lang.com/docs/the-accounts-struct#safety-checks for more information.

make sense, gotcha! i will update the implementation to point directly to the problematic field instead.

@jamie-osec
Copy link
Copy Markdown
Collaborator

While you're making changes, please address the earlier issues I raised with program_mod.span and Span::call_site

Copy link
Copy Markdown
Contributor

@Otter-0x4ka5h Otter-0x4ka5h left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Copy Markdown
Collaborator

@jamie-osec jamie-osec left a comment

Choose a reason for hiding this comment

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

Also LGTM - would you mind squashing your changes into one commit so git history is a little cleaner? Thank you!

@AvhiMaz AvhiMaz force-pushed the fix/quote-spans-program-macro branch from 26cd733 to 6d2c957 Compare November 6, 2025 13:04
@AvhiMaz
Copy link
Copy Markdown
Author

AvhiMaz commented Nov 6, 2025

Also LGTM - would you mind squashing your changes into one commit so git history is a little cleaner? Thank you!

done!

@AvhiMaz AvhiMaz force-pushed the fix/quote-spans-program-macro branch from fe95928 to 6d2c957 Compare November 6, 2025 13:12
@Otter-0x4ka5h
Copy link
Copy Markdown
Contributor

@AvhiMaz can you makesure all tests should be passed

@AvhiMaz
Copy link
Copy Markdown
Author

AvhiMaz commented Nov 6, 2025

@AvhiMaz can you makesure all tests should be passed

yaa checking

@AvhiMaz
Copy link
Copy Markdown
Author

AvhiMaz commented Nov 7, 2025

hi @Otter-0x4ka5h, can you please trigger the CI again? hopefully, all test cases pass this time.

@AvhiMaz
Copy link
Copy Markdown
Author

AvhiMaz commented Nov 7, 2025

done, thanks !

Copy link
Copy Markdown
Collaborator

@0x4ka5h 0x4ka5h left a comment

Choose a reason for hiding this comment

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

lgtm @jamie-osec but i'm not sure by using Span::call_site()

@AvhiMaz
Copy link
Copy Markdown
Author

AvhiMaz commented Nov 7, 2025

lgtm @jamie-osec but i'm not sure by using Span::call_site()

I used Span::call_site() here because this is a macro-time error that occurs during the #[program] macro expansion. It points the user to the exact place where the macro was invoked, making it easier to debug. Am I wrong here?

what i thought if we use mixed_site() or def_site() would point inside the macro definition, which wouldn't be as helpful for end users, lmk if you'd prefer a different approach

@jamie-osec
Copy link
Copy Markdown
Collaborator

If you can address the latest comment and fix the merge conflicts this should be good to land

@AvhiMaz AvhiMaz force-pushed the fix/quote-spans-program-macro branch from f4b03b3 to 266da38 Compare December 3, 2025 15:34
@AvhiMaz
Copy link
Copy Markdown
Author

AvhiMaz commented Dec 3, 2025

If you can address the latest comment and fix the merge conflicts this should be good to land

done

@AvhiMaz AvhiMaz force-pushed the fix/quote-spans-program-macro branch from 266da38 to e6c666b Compare December 3, 2025 15:43
AvhiMaz added 4 commits March 12, 2026 09:37
…tter error messages

Signed-off-by: AvhiMaz <avhimazumder5@outlook.com>
Signed-off-by: AvhiMaz <avhimazumder5@outlook.com>
Signed-off-by: AvhiMaz <avhimazumder5@outlook.com>
Signed-off-by: AvhiMaz <avhimazumder5@outlook.com>
@AvhiMaz AvhiMaz force-pushed the fix/quote-spans-program-macro branch from e6c666b to 00c41d2 Compare March 12, 2026 04:07
@AvhiMaz AvhiMaz requested a review from jamie-osec March 12, 2026 04:12
Signed-off-by: AvhiMaz <avhimazumder5@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improved spans for generated code and error messages

5 participants