# Implicit use of a trait in its implementation leaks through a module

**URL:** <https://internals.rust-lang.org/t/implicit-use-of-a-trait-in-its-implementation-leaks-through-a-module/19724>\
**Category:** documentation\
**Created:** [October 17, 2023, 10:03am UTC](https://internals.rust-lang.org/t/implicit-use-of-a-trait-in-its-implementation-leaks-through-a-module/19724 "2023-10-17T10:03:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GoldsteinE](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/goldsteine/32/13212_2.png) [@GoldsteinE](https://internals.rust-lang.org/u/GoldsteinE)\
**Post date:** [October 17, 2023, 10:03am UTC](https://internals.rust-lang.org/t/implicit-use-of-a-trait-in-its-implementation-leaks-through-a-module/19724/1 "2023-10-17T10:03:14Z")

</div>

Consider this code ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=52773fec135018e6bfae307a4ec5c4c1)):

```rust
mod a {
    pub trait Foo {
        fn bar(&self);
    }
}

impl a::Foo for () {
    fn bar(&self) {
        mod hide { // We're in a new module...
            pub fn baz() {
                // ...so why is `Foo` in scope here?
                ().bar()
            }
        }
        hide::baz();
    }    
}

```

`impl a::Trait for ()` implicitly brings `Foo` into scope. Weirdly enough, it stays in scope even when we’re in a nested module inside of a trait function. I have a few questions related to this snippet:

1. Is this behaviour (implicit use leaking through a module) expected? Is it desired? Edition 2024 is coming, so we could change it if it’s accidental.
2. Is it documented anywhere?
3. In fact, is the implicit use itself documented anywhere?
4. If I were to make PR into reference + book, where do you think it should be documented? Reference’s section on scopes is currently just a stub, and I don’t feel like I’m able to write a whole section.

---

<div class="post-metadata">

**Author:** ![GoldsteinE](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/goldsteine/32/13212_2.png) [@GoldsteinE](https://internals.rust-lang.org/u/GoldsteinE)\
**Post date:** [October 17, 2023, 10:16am UTC](https://internals.rust-lang.org/t/implicit-use-of-a-trait-in-its-implementation-leaks-through-a-module/19724/2 "2023-10-17T10:16:35Z")

</div>

I feel like an appropriate place in the Book to document it is somewhere near here: [https://doc.rust-lang.org/stable/book/ch10-02-traits.html#implementing-a-trait-on-a-type](https://doc.rust-lang.org/stable/book/ch10-02-traits.html#implementing-a-trait-on-a-type)

I’m not sure what the appropriate place in the Reference is. Probably here: [Implementations - The Rust Reference](https://doc.rust-lang.org/reference/items/implementations.html) and preferably also in Scopes section, but it doesn’t actually exist yet. There’re other possible places though, so I’m not sure.

---

<div class="post-metadata">

**Author:** ![GoldsteinE](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/goldsteine/32/13212_2.png) [@GoldsteinE](https://internals.rust-lang.org/u/GoldsteinE)\
**Post date:** [October 17, 2023, 10:30am UTC](https://internals.rust-lang.org/t/implicit-use-of-a-trait-in-its-implementation-leaks-through-a-module/19724/3 "2023-10-17T10:30:25Z")

</div>

Related: this code compiles

```rust
mod a {
    pub trait Foo {
        fn bar(&self);
    }
}

pub fn baz<T: a::Foo>(y: T) {
    y.bar();
}

```

but this doesn’t:

```rust
mod a {
    pub trait Foo {
        fn bar(&self);
    }
}

impl a::Foo for () {
    fn bar(&self) {}
}

pub fn baz<T: a::Foo>(y: T) {
    ().bar();
}

```

so any generic bound brings its trait in scope, but only for the bound parameter? Reference says that

> In the body of a generic function, methods from `Trait` can be called on `Ty` values. Likewise associated constants on the `Trait` can be used.

in [Trait and lifetime bounds - The Rust Reference](https://doc.rust-lang.org/reference/trait-bounds.html), but I’d add something like “even if the trait isn’t in scope”, because it’s non-obvious IMHO.

---

<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:** [January 15, 2024, 10:30am UTC](https://internals.rust-lang.org/t/implicit-use-of-a-trait-in-its-implementation-leaks-through-a-module/19724/4 "2024-01-15T10:30:42Z")

</div>

Closed on January 15, 2024, 10:30am UTC

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