Giter VIP home page Giter VIP logo

Comments (2)

Veetaha avatar Veetaha commented on May 26, 2024

The problem occurs because the code tries to look up the definition of the item in HIR, but that item comes from a foreign crate.

When the TraitRef's ItemId is created here


The type of the ID that is transmuted into ItemId is rustc_span::def_id::DefId. But when the ctx.ast().item() is called the ItemId is converted into rustc_hir::hir::ItemId. You may notice that the method in the snippet below doesn't use the layout.krate field at all. I guess it assumes that this method is always called with local items (of the current crate).
pub fn to_item_id(&self, api_id: ItemId) -> hir::ItemId {
let layout = transmute_id!(ItemId as DefIdLayout = api_id);
hir::ItemId {
owner_id: hir::OwnerId {
def_id: hir::def_id::LocalDefId {
local_def_index: hir::def_id::DefIndex::from_u32(layout.index),
},
},
}
}

So this results in an invalid rustc_hir::hir::ItemId. You'll get a panic even if you try to debug-print this ID.


I'm not sure what the best solution here should be. Maybe the to_item_id(ItemId) -> hir::ItemId method should check if layout.krate is not a local crate. I suppose there is a special sentinel value for krate that identifies it as the current crate. Then if the given item is not from the current crate it should return None.

But how could users query the info about which trait is implemented? Maybe if there was at least the path to the implemented trait in the TraitRef that would allow to check the syntactic path to the trait.

from marker.

xFrednet avatar xFrednet commented on May 26, 2024

Ahh, I think the actual problem is the type. TraitRef should hold a TyDefId instead, and Marker should probably have a function to convert the TyDefId into an ItemId if the item is available.

Thinking about it, this will not resolve all issues though 🤔 Users can still retrieve ItemIds when AstPaths are resolved. So this case definitely need to be handled on Marker's side.

Rustc splits these IDs into a general DefId which can be used across crate boundaries, but is not guaranteed to have an ast item, and a LocalDefId which is only defined for items from the current crate, where an ast representation exists. I wonder if it would make sense to adapt a similar model 🤔

from marker.

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.