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

未关闭
#166,483 2 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
compilers

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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

主要语言
Rust
星标
123k
派生
16.6k
平均合并
2 天 17 小时
30 天内合并 PR
532

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/rust 的其他 Issue

查看 rust-lang/rust 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。