Error fix suggestion request: replace `Self` with more lifetime lenient self type name

Aperta
#166,483 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
45/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
compilers

Direzione di ricerca

The payload gives a minimal SliceMut reproducer and the current and desired lifetime may not live long enough output. Start by finding where that borrowck message is emitted and where the existing Self suggestion logic lives in rustc. Done means the note replace Self with SliceMut appears for this case, with a UI test added under tests/ui that matches the desired output.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A-borrow-checker A-diagnostics A-lifetimes D-lack-of-suggestion T-compiler
Code
pub struct SliceMut<'a, T>(&'a mut [T]);

impl<'a, T> SliceMut<'a, T> {
    pub fn subslice_ok(&mut self, start: usize, end: usize) -> SliceMut<'_, T> {
        SliceMut(&mut self.0[start .. end])
    }
    
    pub fn subslice_fail(&mut self, start: usize, end: usize) -> SliceMut<'_, T> {
        Self(&mut self.0[start .. end])
    }
}
Current output
error: lifetime may not live long enough
  --> src/lib.rs:10:14
   |
 4 | impl<'a, T> SliceMut<'a, T> {
   |      -- lifetime `'a` defined here
...
 9 |     pub fn subslice_fail(&mut self, start: usize, end: usize) -> SliceMut<'_, T> {
   |                          - let's call the lifetime of this reference `'1`
10 |         Self(&mut self.0[start .. end])
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^ this usage requires that `'1` must outlive `'a`
Desired output
error: lifetime may not live long enough
  --> src/lib.rs:10:14
   |
 4 | impl<'a, T> SliceMut<'a, T> {
   |      -- lifetime `'a` defined here
...
 9 |     pub fn subslice_fail(&mut self, start: usize, end: usize) -> SliceMut<'_, T> {
   |                          - let's call the lifetime of this reference `'1`
10 |         Self(&mut self.0[start .. end])
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^ this usage requires that `'1` must outlive `'a`
     note: replace `Self` with `SliceMut`
Rationale and extra context

rustc should suggest replacing concrete type Self with the not-lifetime-bound self type name when those lifetimes differ

Other cases

Rust Version
rustc 1.101.0-nightly (1a2c4797e 2026-10-21)
binary: rustc
commit-hash: 1a2c4797ef2570bd76ca5dee34449169bc7dc457
commit-date: 2026-10-21
host: x86_64-unknown-linux-gnu
release: 1.101.0-nightly
LLVM version: 23.1.4
Anything else?

No response

Lingua principale
Rust
Stelle
123k
Fork
16.6k
Merge medio
2g 17h
PR unite (30g)
532

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/rust

Tutte le issue di rust-lang/rust

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.