rust-lang / rust-lang/rust

impl block on a type defined with type_alias_impl_trait can conflict with other unrelated impl

Open
#139,583 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-coherence A-hygiene C-bug F-type_alias_impl_trait T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![feature(type_alias_impl_trait)]

use std::any::TypeId;

pub trait Trait {}
impl<T: ?Sized> Trait for T {}

pub trait Other {}

macro_rules! mymac {
    () => {{
        struct S;
        type Foo = impl Trait;
        let x: Foo = S;
        println!("{:?}", TypeId::of::<Foo>());
        impl Foo {
            fn foo() -> () {}
        }
        impl Other for Foo {}
    }};
}

fn main() {
    mymac!();
    mymac!();
}

I expected to see this happen: compiles without error

Instead, this happened:

error[E0119]: conflicting implementations of trait `Other` for type `main::Foo`
  --> src/main.rs:19:9
   |
19 |         impl Other for Foo {}
   |         ^^^^^^^^^^^^^^^^^^
   |         |
   |         first implementation here
   |         conflicting implementation for `main::Foo`
...
25 |     mymac!();
   |     -------- in this macro invocation
   |
   = note: this error originates in the macro `mymac` (in Nightly builds, run with -Z macro-backtrace for more info)
Meta

Rust nightly version on the playground as of 9th of April 2025.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied src/main.rs on the stated Rust nightly, focusing on the two mymac! invocations and the conflicting Other implementation diagnostic. Done means the reproducer compiles without treating the two macro expansions as the same Foo, with coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.