wasm-tools icon indicating copy to clipboard operation
wasm-tools copied to clipboard

wast: How do you look up function indices when `Id::new(...)` is not public?

Open matthew-a-thomas opened this issue 5 years ago • 1 comments

If the following code is the idiomatic way to look up a function index then there's a problem. I can't create an Id:

let wat = "...";
let module = parse::<Wat>(&wat)?.module;
let names = module.resolve()?;
names.resolve_func(&mut Index::Id(
    Id::new( // associated function `new` is private
        "$my-name",
        Span::from_offset(0)
    )
));

Is there a better way to look up the $my-name function index?

matthew-a-thomas avatar Nov 10 '20 16:11 matthew-a-thomas

Currently this was intended to help resolve other nodes that already had an Id, but exposing other methods seems fine by me!

alexcrichton avatar Nov 10 '20 20:11 alexcrichton

Just stumbled over the same thing - I want to create AST nodes programatically, i.e. without parsing.

I see a couple of options:

  • pub new(name: &'a str, span: Span)->Id<'a> without any checks. This could cause downstream bugs, because i could e.g. provide an invalid name $foo or foo bar.
  • pub new(name: &'a str, span: Span)->Id<'a> and internally parse the name, panic if the name is not a valid identifier. However I think that might not be possible due to requirements with the parser buffer / parser.
  • pub new(name: &'a str, span: Span)->Result<Id<'a>> as above, but instead of panic return the parse error.

martinitus avatar Jan 12 '24 15:01 martinitus

I think this can be closed :) Thanks again @alexcrichton

martinitus avatar Jan 18 '24 16:01 martinitus