skip to content

After a dependency update, a Rust call `email.retry("disk full", 3)` that compiled before now fails with E0034 — what changed, and how do you fix it?

level: seniorimportance: nice to knowfreq 22%

answer

  1. a second candidate appeared
  2. upstream trait gained a default method
  3. both traits in scope at the call
  4. qualify the call by trait path

basics

~20 s

A trait that is in scope at the call site gained a retry method, typically a new default method upstream, so two trait methods now match. Disambiguate with RetryExt::retry(&email, ...) or Notifier::retry(&email, ...), or rename your own trait's method.

solid answer

~50 s

E0034, `multiple applicable items in scope`, means two trait methods now match the call at the same receiver step with equal priority. The usual cause is an upstream trait such as `Notifier` gaining a **defaulted** `retry` in a minor release: every type that implements `Notifier` gets the method without changing its impl, so it collides with the application's own extension trait `RetryExt`. The fix is to say which one you mean, `RetryExt::retry(&email, "disk full", 3)`, or to rename the method in the trait you own. Removing the `Notifier` import from that module also works if nothing else there needs it, but `use ... as _` does not, because it still puts the methods in scope. The case to fear more is an upstream **inherent** method with the same signature: inherent methods win without any error, so the call silently switches implementation.

code

rust · 32 lines
rust
mod alerts {
    // upstream crate, new minor version: `retry` gained a default body
    pub trait Notifier {
        fn send(&self, msg: &str) -> bool;
        fn retry(&self, msg: &str, attempts: u32) -> bool {
            (0..attempts).any(|_| self.send(msg))
        }
    }
    pub struct EmailNotifier;
    impl Notifier for EmailNotifier {
        fn send(&self, _msg: &str) -> bool { true }
    }
}

// the application's own extension trait, written before the upgrade
trait RetryExt {
    fn retry(&self, msg: &str, attempts: u32) -> bool;
}
impl RetryExt for alerts::EmailNotifier {
    fn retry(&self, msg: &str, attempts: u32) -> bool {
        println!("app retry x{attempts}: {msg}");
        true
    }
}

use alerts::{EmailNotifier, Notifier};

fn main() {
    let email = EmailNotifier;
    email.send("hello");
    email.retry("disk full", 3);
}

go deeper

for a junior

Recall that two traits in scope with the same method name make a plain method call ambiguous, and that Trait::method(&value) picks one.

for a middle

Explain how a default method added upstream reaches every existing implementor, why that creates E0034 only where both traits are visible, and how qualification fixes it.

for a senior

Diagnose from the candidate list, choose between qualifying, renaming and narrowing imports, and flag the silent inherent-method case that compiles but changes behaviour.

for a principal

Set naming and path-call conventions for extension traits so dependency updates cannot capture calls, and decide how updates are reviewed when you extend foreign types.

## The symptom A service has compiled for months. Someone runs `cargo update`, and a line that never changed now fails: ```plaintext error[E0034]: multiple applicable items in scope --> q5.rs:31:11 | 31 | email.retry("disk full", 3); | ^^^^^ multiple `retry` found ``` rustc 1.99.1 lists two candidates, one from the trait `Notifier` (defined by the upstream crate) and one from `RetryExt` (defined in the application), and suggests `Notifier::retry(&email, ...)` or `RetryExt::retry(&email, ...)`. ## What changed For a method call, rustc walks candidate receiver types in order (`EmailNotifier`, then `&EmailNotifier`, then `&mut EmailNotifier`, and so on). At each step it checks inherent methods first, then methods of **visible** traits implemented by the type. If more than one trait method matches at the same step, there is no priority between them, and the Rust Reference says this is a compiler error that needs disambiguating call syntax. Before the update only `RetryExt::retry` matched. After it, the upstream `Notifier` trait has a new method: ```rust pub trait Notifier { fn send(&self, msg: &str) -> bool; fn retry(&self, msg: &str, attempts: u32) -> bool { (0..attempts).any(|_| self.send(msg)) } } ``` Because the new method has a **default body**, every existing implementor, including `EmailNotifier`, gets it without editing a line. The module imports `Notifier` (it calls `send`), so both traits are visible, both `retry` methods take `&self`, and both match at `&EmailNotifier`. Cargo's SemVer reference lists exactly this under "Possibly-breaking: adding a defaulted trait item": usually safe, but it can introduce an ambiguity if a method of the same name exists in another trait. The classification of such changes is covered with API evolution; the diagnosis and repair are the trait-definition part. ## How to fix the call site 1. **Qualify the call** with the trait you mean: `RetryExt::retry(&email, "disk full", 3)`. The receiver becomes the first argument and its type selects the impl. `<EmailNotifier as RetryExt>::retry(&email, "disk full", 3)` is the fully spelled form. 2. **Rename the method in the trait you own**, for example `retry_with_policy`. This removes the collision in every module at once. 3. **Narrow what is in scope**: if a module does not need `Notifier`'s methods, drop the import there. Changing it to `use alerts::Notifier as _;` does **not** help; an underscore import still makes the trait's methods candidates, and the call stays ambiguous. 4. **Decide whether you still need your own `retry`.** If the upstream default now does the same job, deleting the extension method may be the cleanest result. ## The quieter variant: an inherent method E0034 is the lucky outcome, because the build fails. Suppose instead the upstream crate added an **inherent** method, `impl EmailNotifier { pub fn retry(&self, msg: &str, attempts: u32) -> bool }`. Inherent methods are searched before trait methods at the same receiver step, so `email.retry("disk full", 3)` now calls the upstream method: - with the same signature, it compiles cleanly and behaves differently (checked with rustc 1.99.1: the call switched to the inherent method, and in a small binary the only hint was a `dead_code` warning that the application trait is never used; a public trait in a library gets not even that); - with a different signature, the build fails with an argument or type error instead. Cargo's SemVer reference makes the same point under "Possibly-breaking change: adding any inherent items": matching signatures produce no compile error but possibly a silent change in run-time behaviour. ## Comparing the outcomes | Upstream change | Your extension trait method | Result at `email.retry(..)` | |---|---|---| | New defaulted trait method, trait in scope | same name, same receiver | E0034, build fails | | New defaulted trait method, trait not in scope | same name | still calls your method | | New inherent method, same signature | same name | silently calls upstream | | New inherent method, different signature | same name | type or argument error | ## Hardening against it - Give extension-trait methods **distinctive names** rather than generic verbs like `retry`, `send` or `len`. - In code where the exact implementation matters, call extension methods by path (`RetryExt::retry(&email, ..)`), so neither a new inherent method nor a new trait method can capture the call. - Read changelogs for added methods on traits and types you extend, and keep `Cargo.lock` committed for applications so such changes arrive only when you choose to update.

  • Why does `use alerts::Notifier as _;` not remove the E0034 ambiguity?
    An underscore import binds no name but still brings the trait into scope for method resolution; that is its whole purpose. Both `retry` methods remain visible candidates at the same receiver step, so rustc still reports E0034. Only removing `Notifier` from scope in that module, or qualifying the call, changes the outcome.
  • If upstream had added an inherent `retry` with the same signature instead, what would you see?
    Probably nothing at compile time. Inherent methods are checked before trait methods at the same receiver step, so `email.retry(..)` would quietly call the upstream method. Cargo's SemVer guide warns that matching signatures give no error but can change run-time behaviour; a path call such as `RetryExt::retry(&email, ..)` protects against it.
  • Can a trait method still beat an inherent method of the same name?
    Yes, when it is found at an earlier receiver step. rustc tries the receiver type itself before `&T`, so a trait method taking `self` by value on a `Copy` type is chosen over an inherent `&self` method. Inherent-first only orders candidates within one step.

  • E0034 after an update means the dependency removed a method.
  • Changing the import to use Trait as _ resolves the ambiguity.
  • Adding a method with a default body can never break downstream code.
  • An upstream inherent method with the same name always causes a compile error.
  • rustc picks the trait from the crate you depend on most directly.