Giter Club home page Giter Club logo

Comments (6)

emilio avatar emilio commented on May 26, 2024

I think it's not about being smarter about type parameters, but about being replacement-aware. That's partially why the replaces annotation sucks quite a bit (and it'd come a bit more handy to have a replaced_by annotation or something, but that's way more annoying).

Right now the amount of code that is replacement-aware is minimal. Probably we should make Context::resolve_item replacement-aware by default (it's not hard, just slightly annoying), and then have another function to bypass this in the appropriate places.

from rust-bindgen.

fitzgen avatar fitzgen commented on May 26, 2024

After digging in more, this is not what we thought this was. Code generation, which is when we get the named types we need to emit, happens after we have already processed replacements, so the issue is not that we are failing to be replacement aware.

In fact, it looks like we end up replacing the wrong items, and then end up ignoring the replacements or something.

I'm digging in further...

from rust-bindgen.

fitzgen avatar fitzgen commented on May 26, 2024

First, we parse the replaces_MaybeWrapped template alias, which has the use_instead_of (né replaces) annotation, and it ends up with an id of ItemId(13):

DEBUG:bindgen::ir::context: BindgenContext::add_item(Item { id: ItemId(13), local_id: Cell { value: None }, next_child_local_id: Cell { value: 1 }, canonical_name_cache: RefCell { value: None }, comment: Some("/// But the replacement type does use T!\n///\n/// <div rustbindgen replaces=\"MaybeWrapped\" />"), annotations: Annotations { opaque: false, hide: false, use_instead_of: Some("MaybeWrapped"), disallow_copy: false, private_fields: None, accessor_kind: None }, parent_id: ItemId(0), kind: Type(Type { name: None, layout: None, kind: TemplateAlias(ItemId(16), [ItemId(15)]), is_const: false }) }, declaration: Some(Cursor( kind: NoDeclFound, loc: builtin definitions, usr: None)), loc: Some(Cursor(replaces_MaybeWrapped kind: TypeAliasTemplateDecl, loc: tests/headers/replace_template_alias.hpp:23:1, usr: Some("c:@replaces_MaybeWrapped")))

Then, we define the replacement for MaybeWrapped (ie a call to BindgenContext::replace) as ItemId(18)(!!) and not the template alias above which is ItemId(13):

DEBUG:bindgen::ir::context: Defining replacement for MaybeWrapped as ItemId(18)

Finally, we add the item with id itemId(18), which is the named template type argument T:

DEBUG:bindgen::ir::context: BindgenContext::add_item(Item { id: ItemId(18), local_id: Cell { value: None }, next_child_local_id: Cell { value: 1 }, canonical_name_cache: RefCell { value: None }, comment: None, annotations: Annotations { opaque: false, hide: false, use_instead_of: None, disallow_copy: false, private_fields: None, accessor_kind: None }, parent_id: ItemId(13), kind: Type(Type { name: Some("T"), layout: None, kind: Named("T", None), is_const: false }) }, declaration: None, loc: None

So that seems to be the bug: we should be defining the replacement as ItemId(13) not ItemId(18).

Still figuring out why this is happening in the first place.

from rust-bindgen.

emilio avatar emilio commented on May 26, 2024

I bet we're inserting it twice in the hashmap?

from rust-bindgen.

fitzgen avatar fitzgen commented on May 26, 2024

I bet we're inserting it twice in the hashmap?

Great guess! ;)

DEBUG:bindgen::ir::context: Defining replacement for MaybeWrapped as ItemId(13)
DEBUG:bindgen::ir::context: Defining replacement for MaybeWrapped as ItemId(16)
DEBUG:bindgen::ir::context: Defining replacement for MaybeWrapped as ItemId(18)

from rust-bindgen.

fitzgen avatar fitzgen commented on May 26, 2024

I guess we can assume the first is the correct replacement and then warn for duplicate replacement definition attempts.

Fix incoming.

from rust-bindgen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.