rust-lang / rust-lang/rust

impl-trait-overcaptures cannot be applied, missing parens

Open
#130,957 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-suggestion-diagnostics D-invalid-suggestion D-papercut T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:
rustc ./tests/ui/impl-trait/dyn-trait-elided-two-inputs-ref-assoc.rs --force-warn impl-trait-overcaptures

// Test that we don't get an error with `dyn Bar` in an impl Trait
// when there are multiple inputs.  The `dyn Bar` should default to `+
// 'static`. This used to erroneously generate an error (cc #62517).
//
//@ revisions: current next
//@[next] compile-flags: -Znext-solver
//@ ignore-compare-mode-next-solver (explicit revisions)
//@ check-pass

trait Foo {
    type Item: ?Sized;

    fn item(&self) -> Box<Self::Item> { panic!() }
}

trait Bar { }

impl<T> Foo for T {
    type Item = dyn Bar;
}

fn is_static<T>(_: T) where T: 'static { }

fn bar(x: &str) -> &impl Foo<Item = dyn Bar> { &() }

fn main() {
    let s = format!("foo");
    let r = bar(&s);
    is_static(r.item());
}

warning: `impl Foo<Item = (dyn Bar + 'static)>` will capture more lifetimes than possibly intended in edition 2024
  --> ./tests/ui/impl-trait/dyn-trait-elided-two-inputs-ref-assoc.rs:24:21
   |
24 | fn bar(x: &str) -> &impl Foo<Item = dyn Bar> { &() }
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = warning: this changes meaning in Rust 2024
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html>
note: specifically, this lifetime is in scope but not mentioned in the type's bounds
  --> ./tests/ui/impl-trait/dyn-trait-elided-two-inputs-ref-assoc.rs:24:11
   |
24 | fn bar(x: &str) -> &impl Foo<Item = dyn Bar> { &() }
   |           ^
   = note: all lifetimes in scope will be captured by `impl Trait`s in edition 2024
   = note: requested on the command line with `--force-warn impl-trait-overcaptures`
help: use the precise capturing `use<...>` syntax to make the captures explicit
   |
24 | fn bar(x: &str) -> &impl Foo<Item = dyn Bar> + use<> { &() }
   |                                              +++++++

this does not build:

error: ambiguous `+` in a type
  --> ./tests/ui/impl-trait/dyn-trait-elided-two-inputs-ref-assoc.rs:24:21
   |
24 | fn bar(x: &str) -> &impl Foo<Item = dyn Bar> + use<> { &() }
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: try adding parentheses
   |
24 | fn bar(x: &str) -> &(impl Foo<Item = dyn Bar> + use<>) { &() }
   |                     +                                +
Meta

rustc --version --verbose:

rustc 1.83.0-nightly (2bd1e894e 2024-09-26)
binary: rustc
commit-hash: 2bd1e894efde3b6be857ad345914a3b1cea51def
commit-date: 2024-09-26
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

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 with tests/ui/impl-trait/dyn-trait-elided-two-inputs-ref-assoc.rs and run the rustc command shown in the report. Inspect the overcapture warning's suggested use<> syntax for the impl Foo<Item = dyn Bar> return type. Done means the suggested form parses with the required parentheses and the regression test passes.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.