rust-lang / rust-lang/rust

Inconsistent lifetime specifier requirements depending on whether `self` is defined directly or using a `type`

Open
#153,148 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lifetimes C-bug F-arbitrary_self_types needs-triage T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

When providing a custom type of self, Rust inconsistently requires lifetime specifiers. When self is defined directly using &Arc<Demo>, no lifetime specifiers are required by Rust, which is expected behavior. However, when self is defined using a type: &DemoPtr (where DemoPtr is just Arc<Demo>), for some reason, Rust requires lifetime specifiers to be present. Additionally, it causes clippy to emit needless_lifetimes warning, which is a false positive, as when applied, the code no longer compiles.

use std::{collections::HashMap, sync::Arc};

type DemoPtr = Arc<Demo>;

struct Demo {
	test: HashMap<String, String>,
}

impl Demo {
	// Compiles, expected behavior.
	pub fn a(self: &Arc<Demo>) -> &HashMap<String, String> {
		&self.test
	}

	// Compiles, unexpected behavior as lifetime specifiers are required.
	// Additionally it emits `clippy::needless_lifetimes` warning.
	pub fn b<'a>(self: &'a DemoPtr) -> &'a HashMap<String, String> {
		&self.test
	}

	// Does not compile, unexpected behavior as lifetime specifiers shouldn't be required.
	// This is the change suggested by `clippy` which breaks the build when applied.
	pub fn c(self: &DemoPtr) -> &HashMap<String, String> {
		&self.test
	}
}

I expected to be able to compile function c (self: &DemoPtr), without having to define lifetime specifiers.

Instead, I have to define lifetime specifiers as in function b (self: &'a DemoPtr), which also emits clippy lints.

rustc --version --verbose:

rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: aarch64-apple-darwin
release: 1.91.1
LLVM version: 21.1.2

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 the supplied Rust reproducer, compiling functions a, b, and c with rustc 1.91.1 and checking the corresponding clippy output. Investigate lifetime elision for method receivers involving type aliases, then add or update a compiler regression test so c compiles without explicit lifetimes and the needless_lifetimes warning is not emitted.

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.