Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

item.ident for ItemKind::Impl #106

Open
bruno-medeiros opened this issue Nov 22, 2016 · 6 comments
Open

item.ident for ItemKind::Impl #106

bruno-medeiros opened this issue Nov 22, 2016 · 6 comments

Comments

@bruno-medeiros
Copy link

It used to be with syntax_syntex version 0.28 or that for item: &Item that were of kind ItemKind::Impl , the item.ident would have the identifier that the impl applied to. However, in recent versions of syntax_syntex, that no longer seems to be the case, the ident is just the empty string. Is there still some straightforward way to obtain that information?

The ident for impls was used by the Rainicorn tool to provide Outline/Structure view for IDES.

@dtolnay
Copy link
Contributor

dtolnay commented Nov 22, 2016

This isn't up to Syntex, we just copy the parser out of the Rust compiler. I tracked this down to the following pull request from March: rust-lang/rust#32570. @eddyb can I interest you in reintroducing impl_pretty_name?

Failing that, you can always build the ident in Rainicorn. The ItemKind::Impl variant looks like:

Impl(Unsafety, ImplPolarity, Generics, Option<TraitRef>, P<Ty>, Vec<ImplItem>)

The Option<TraitRef> and P<Ty> were used to build the name. The function was:

pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: Option<&Ty>) -> Ident {
    let mut pretty = match ty {
        Some(t) => pprust::ty_to_string(t),
        None => String::from("..")
    };

    match *trait_ref {
        Some(ref trait_ref) => {
            pretty.push('.');
            pretty.push_str(&pprust::path_to_string(&trait_ref.path));
        }
        None => {}
    }
    token::gensym_ident(&pretty[..])
}

@eddyb
Copy link

eddyb commented Nov 22, 2016

@dtolnay No, it was a horrible hack. Usually if you want that kind of information, you have a tcx: TyCtxt and can use tcx.item_path_str, which does it in a more principled way (assuming you can't do better).

@bruno-medeiros
Copy link
Author

What's a TyCtxt and how do I get one?

I should note that I pretty much just want the identifier from an impl definition and not do any semantic analysis or anything. That is, for impl Foo { }, I don't even need a type Foo to be defined anywhere, since Foo will just be used for display purposes in my tool.

@erickt
Copy link

erickt commented Dec 20, 2016

@eddyb: this is in syntex, so we don't have a TyCtxt. Is there another way we can get this purely through libsyntax?

@eddyb
Copy link

eddyb commented Dec 20, 2016

Not really, no, you can't get anything semantically interesting in that case, so the hack is one of the better options.

@bruno-medeiros
Copy link
Author

Not really, no, you can't get anything semantically interesting in that case, so the hack is one of the better options.

Like I said, for this use case I don't want anything semantic, just pure syntax. I will give @dtolnay code a try then, and report back 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

4 participants