# Accessing the \`DefId\` of a Trait Implementation

**URL:** <https://internals.rust-lang.org/t/accessing-the-defid-of-a-trait-implementation/17001>\
**Category:** compiler\
**Created:** [July 12, 2022, 6:43pm UTC](https://internals.rust-lang.org/t/accessing-the-defid-of-a-trait-implementation/17001 "2022-07-12T18:43:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ranjitjhala](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ranjitjhala/32/8844_2.png) [@ranjitjhala](https://internals.rust-lang.org/u/ranjitjhala)\
**Post date:** [July 12, 2022, 6:43pm UTC](https://internals.rust-lang.org/t/accessing-the-defid-of-a-trait-implementation/17001/1 "2022-07-12T18:43:31Z")

</div>

Hi all,

Suppose I define my own type and implement a trait for it e.g.

```rust
pub struct MyType { ... } 

impl Iterator for MyType { 
  type Item = MyItem;
  fn next(...) -> MyItem { ... } // can we get this DefId at the usage site?
} 

```

Now suppose I have some client code of the form

```rust
let my_typ_inst = MyType::new(...):
let foo = my_typ_inst.next();

```

The MIR for the above call looks something like

```rust
<MyType as std::iter::Iterator>::next(...)

```

I have two questions:

1. Is there some way to get the `DefId` for the `MyType` implementation of `next()` ?

2. Is there some way to recover the fact that the signature of `next` at the above site is `fn (&Self) -> Option<MyItem>` ?

Should I be looking around here for example?

> **[TyCtxt in rustc\_middle::ty::context - Rust](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.try_expand_impl_trait_type)**
>
> The central data structure of the compiler. It stores references to the various arenas and also houses the results of the various compiler queries that have been performed. See the rustc dev guide for more details.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [July 12, 2022, 8:35pm UTC](https://internals.rust-lang.org/t/accessing-the-defid-of-a-trait-implementation/17001/2 "2022-07-12T20:35:05Z")

</div>

What you want is some sort of `normalize` to resolve the projection; perhaps [`rustc_trait_selection::traits::normalize`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_trait_selection/traits/fn.normalize.html)? The raw query is on tcx but you're supposed to call it through a wrapper `normalize` rather than directly and I can't quickly find the one you probably want in rustdoc.

---

<div class="post-metadata">

**Author:** ![ranjitjhala](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ranjitjhala/32/8844_2.png) [@ranjitjhala](https://internals.rust-lang.org/u/ranjitjhala)\
**Post date:** [July 13, 2022, 3:08am UTC](https://internals.rust-lang.org/t/accessing-the-defid-of-a-trait-implementation/17001/3 "2022-07-13T03:08:30Z")

</div>

Thanks @CAD97 ! Digging around I tried to use the functions here

> **[normalize\_erasing\_regions.rs - source](https://doc.rust-lang.org/beta/nightly-rustc/src/rustc_middle/ty/normalize_erasing_regions.rs.html#39https://doc.rust-lang.org/beta/nightly-rustc/src/rustc_middle/ty/normalize_erasing_regions.rs.html%2339)**
>
> Source of the Rust file \`compiler/rustc\_middle/src/ty/normalize\_erasing\_regions.rs\`.

(because the `normalize` you linked to requires an `InferCtx` among other things, which I don't know how to get a hold of yet.)

However, I was surprised that all those functions (in `normalize_erasing_regions`) have no effect. In particular, I am puzzled by this:

```rust
println!("has_projections `{exp_ty:?}` = {:?}", exp_ty.has_projections());

```

yields the following output

```rust
has_projections `for<'r> fn(&'r mut range::Rng) -> std::option::Option<<range::Rng as std::iter::Iterator>::Item> {<range::Rng as std::iter::Iterator>::next}` = false

```

all the rest of the code in `normalize_erasing_regions` skips normalization when the above is `false`. Is the idea that the normalization doesn't happen for a whole function signature but must be done separately on each component?

Thanks in advance for any pointers!

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [July 13, 2022, 5:51am UTC](https://internals.rust-lang.org/t/accessing-the-defid-of-a-trait-implementation/17001/4 "2022-07-13T05:51:14Z")

</div>

I'm not super familiar with this part of rustc, but yeah, you'll have the most luck if you can drill down to the part of the type with the projection you care about and then normalize/resolve that.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 11, 2022, 5:51am UTC](https://internals.rust-lang.org/t/accessing-the-defid-of-a-trait-implementation/17001/5 "2022-10-11T05:51:46Z")

</div>

Closed on October 11, 2022, 5:51am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
