Skip to content

Remove import warnings caused by macros #11863

Closed

Description

@nventuro

Some macros inject code that calls trait functions, which issues a warning unless the user has imported the trait. This should not be the case, using a macro should require nothing other than importing it. Macros can't inject imports since those have already been processed at macro resolution, so we need an alternative strategy.

What we discussed with Jake from Noir is to have the resolution happen inside the macro by injecting resolved functions instead of plain quoted tokens. This makes the macros a bit harder to parse, but much more robust.

Instead of e.g.

quote {
  let note_type_id = $typ.get_note_type_id(); // Requires the NoteInterface trait to be imported
}

we'd do

use crate::note::note_interface::NoteInterface;

let get_note_type_id = $typ.as_trait_impl(NoteInterface).methods.get_note_type_id; // This is not the actual Noir API
quote {
  let note_type_id = $get_note_type_id();

Some work remains on Noir to achieve this (I think noir-lang/noir#7315 and/or noir-lang/noir#7314) - we need to coordinate with them to ensure this is possible.

Activity

  1. self-assigned this
    on Feb 10, 2025
  2. asterite commented on Feb 13, 2025

    @asterite
    Contributor

    In Noir master this should now be possible:

    use crate::note::note_interface::NoteInterface;
    
    let note_interface = quote { NoteInterface }.as_trait_constraint();
    let get_note_type_id = $typ.as_trait_impl(note_interface).methods().filter(|m| m.name() == quote { get_note_type_id })[0];
    quote {
        let note_type_id = $get_note_type_id();
  3. benesjan commented on Feb 13, 2025

    @benesjan
    Contributor

    Thanks @asterite . Will give it a try once we get a Noir sync.

  4. TomAFrench commented on Feb 14, 2025

    @TomAFrench
    Member

    This is now synced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-aztec.nrComponent: Aztec smart contract frameworkteam-fairiesNico's team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions