Skip to content

Split elided_lifetime_in_paths into finer-grained lints #120808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

shepmaster
Copy link
Member

@shepmaster shepmaster commented Feb 8, 2024

Description

Converts the existing elided_lifetime_in_paths lint into three smaller pieces:

  • hidden_lifetimes_in_input_paths — fires for fn(ContainsLifetime) -> ...
  • hidden_lifetimes_in_output_paths — fires for fn(...) -> ContainsLifetime
  • hidden_lifetimes_in_type_paths — fires for many other usages of ContainsLifetime, such as in a static or in a turbofish

A new group hidden_lifetimes_in_paths is created with the three smaller lints and places that use the old elided_lifetime_in_paths name are updated to match.

#![allow(dead_code)]
#![warn(hidden_lifetimes_in_paths)]

struct ContainsLifetime<'a>(&'a u8);

fn inputs_and_outputs(v: ContainsLifetime) -> ContainsLifetime { v }
static CL: ContainsLifetime = ContainsLifetime(&42);

fn main() {}
warning: paths containing hidden lifetime parameters are deprecated
 --> a.rs:6:26
  |
6 | fn inputs_and_outputs(v: ContainsLifetime) -> ContainsLifetime { v }
  |                          ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(hidden_lifetimes_in_input_paths)]` implied by `#[warn(hidden_lifetimes_in_paths)]`
help: indicate the anonymous lifetime
  |
6 | fn inputs_and_outputs(v: ContainsLifetime<'_>) -> ContainsLifetime { v }
  |                                          ++++

warning: paths containing hidden lifetime parameters are deprecated
 --> a.rs:6:47
  |
6 | fn inputs_and_outputs(v: ContainsLifetime) -> ContainsLifetime { v }
  |                                               ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(hidden_lifetimes_in_output_paths)]` implied by `#[warn(hidden_lifetimes_in_paths)]`
help: indicate the anonymous lifetime
  |
6 | fn inputs_and_outputs(v: ContainsLifetime) -> ContainsLifetime<'_> { v }
  |                                                               ++++

warning: paths containing hidden lifetime parameters are deprecated
 --> a.rs:7:12
  |
7 | static CL: ContainsLifetime = ContainsLifetime(&42);
  |            ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(hidden_lifetimes_in_type_paths)]` implied by `#[warn(hidden_lifetimes_in_paths)]`
help: indicate the anonymous lifetime
  |
7 | static CL: ContainsLifetime<'_> = ContainsLifetime(&42);
  |                            ++++

Background

In general, we want to discourage function signatures like fn (&T) -> ContainsLifetime, fn (ContainsLifetime) -> &T , and fn (ContainsLifetime) -> ContainsLifetime as it is not obvious that a lifetime flows through the function and back out as there is no visual indication that ContainsLifetime has a lifetime generic (such types are not usually literally called "contains lifetime" 😃).

In #120808 (comment) (and multiple followup comments, e.g. #120808 (comment)), the lang team decided on a plan where we introduce a new warn-by-default lint mismatched_lifetime_syntaxes (superseding elided_named_lifetimes) which helps insure consistency between input and output lifetime syntaxes and then split elided_lifetime_in_paths into three parts. The combination of these lints should be enough to accomplish the original goal.

History

Note that this PR has substantially changed from its original version. Check the (copious) comments below as well as the edit history of this message.

@rustbot
Copy link
Collaborator

rustbot commented Feb 8, 2024

r? @pnkfelix

rustbot has assigned @pnkfelix.
They will have a look at your PR within the next two weeks and either review your PR or
reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 8, 2024
@rust-log-analyzer

This comment has been minimized.

@shepmaster
Copy link
Member Author

As I've now tried to add this test twice and to help prevent trying to add it again... this fails because elision can't take place:

fn top_level_nested_to_top_level_nested(v: &ContainsLifetime) -> &ContainsLifetime { v }
error[E0106]: missing lifetime specifiers
 --> src/lib.rs:3:66
  |
3 | fn top_level_nested_to_top_level_nested(v: &ContainsLifetime) -> &ContainsLifetime {
  |                                            -----------------     ^^^^^^^^^^^^^^^^^ expected named lifetime parameter
  |                                                                  |
  |                                                                  expected named lifetime parameter
  |

@shepmaster shepmaster force-pushed the split-elided-lifetimes-in-paths branch from eaf0446 to 8f5390c Compare February 9, 2024 19:40
@rust-log-analyzer

This comment has been minimized.

@shepmaster shepmaster force-pushed the split-elided-lifetimes-in-paths branch from 8f5390c to f1f5c32 Compare February 9, 2024 22:34
@shepmaster shepmaster force-pushed the split-elided-lifetimes-in-paths branch from f1f5c32 to cc85718 Compare February 11, 2024 16:15
@pnkfelix
Copy link
Member

pnkfelix commented Feb 12, 2024

This generally looks fine. I had a few questions about what we expect to happen in a corner case.

r=me once those questions are addressed in some way (update: well, maybe the r=me is premature given the list of questions that @shepmaster included in the PR description. But I don't think the PR is waiting on review at this point.)

@rustbot label: +S-waiting-on-author -S-waiting-on-review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 12, 2024
@pnkfelix
Copy link
Member

Oh, I suppose there is still an open question about the use of the "tied"/"untied" terminology, which I admit threw me for a loop at first. I'm not sure which group is the best to handle resolving that question, though. And I'm also not entirely sure that resolving that question should block landing this work.

Is resolving a question like that a matter for WG-diagnostics, or for T-lang?

@shepmaster
Copy link
Member Author

Is resolving a question like that a matter for WG-diagnostics, or for T-lang?

That's a great question that I don't have an answer for. I posed it in the Zulip thread hoping there was some existing terminology. Unfortunately, no one seemed aware of one. "Tied" made some intuitive sense for the small handful of people I asked one-on-one.

It feels like this is something that we must have talked about before and potentially even documented somewhere, but 🤷

@shepmaster shepmaster force-pushed the split-elided-lifetimes-in-paths branch 2 times, most recently from f6d8513 to da16b9b Compare February 13, 2024 15:21
Copy link
Contributor

@danielhenrymantilla danielhenrymantilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pushing this forward! ❤️

@bors

This comment was marked as resolved.

@shepmaster shepmaster force-pushed the split-elided-lifetimes-in-paths branch 2 times, most recently from 57a0a90 to 88dd6fc Compare March 11, 2024 20:18
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jun 6, 2025
… r=traviscross,jieyouxu

Add a new `mismatched-lifetime-syntaxes` lint

The lang-team [discussed this](https://hackmd.io/nf4ZUYd7Rp6rq-1svJZSaQ) and I attempted to [summarize](rust-lang/rust#120808 (comment)) their decision. The summary-of-the-summary is:

- Using two different kinds of syntax for elided lifetimes is confusing. In rare cases, it may even [lead to unsound code](rust-lang/rust#48686)! Some examples:

    ```rust
    // Lint will warn about these
    fn(v: ContainsLifetime) -> ContainsLifetime<'_>;
    fn(&'static u8) -> &u8;
    ```

- Matching up references with no lifetime syntax, references with anonymous lifetime syntax, and paths with anonymous lifetime syntax is an exception to the simplest possible rule:

    ```rust
    // Lint will not warn about these
    fn(&u8) -> &'_ u8;
    fn(&'_ u8) -> &u8;
    fn(&u8) -> ContainsLifetime<'_>;
    ```

- Having a lint for consistent syntax of elided lifetimes will make the [future goal](rust-lang/rust#91639) of warning-by-default for paths participating in elision much simpler.

---

This new lint attempts to accomplish the goal of enforcing consistent syntax. In the process, it supersedes and replaces the existing `elided-named-lifetimes` lint, which means it starts out life as warn-by-default.
@shepmaster shepmaster force-pushed the split-elided-lifetimes-in-paths branch from 1304850 to 4a41f53 Compare June 6, 2025 14:10
@rust-log-analyzer

This comment has been minimized.

@shepmaster shepmaster force-pushed the split-elided-lifetimes-in-paths branch from 4a41f53 to 22cfbc6 Compare June 6, 2025 16:00
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jun 9, 2025
… r=traviscross,jieyouxu

Add a new `mismatched-lifetime-syntaxes` lint

The lang-team [discussed this](https://hackmd.io/nf4ZUYd7Rp6rq-1svJZSaQ) and I attempted to [summarize](rust-lang/rust#120808 (comment)) their decision. The summary-of-the-summary is:

- Using two different kinds of syntax for elided lifetimes is confusing. In rare cases, it may even [lead to unsound code](rust-lang/rust#48686)! Some examples:

    ```rust
    // Lint will warn about these
    fn(v: ContainsLifetime) -> ContainsLifetime<'_>;
    fn(&'static u8) -> &u8;
    ```

- Matching up references with no lifetime syntax, references with anonymous lifetime syntax, and paths with anonymous lifetime syntax is an exception to the simplest possible rule:

    ```rust
    // Lint will not warn about these
    fn(&u8) -> &'_ u8;
    fn(&'_ u8) -> &u8;
    fn(&u8) -> ContainsLifetime<'_>;
    ```

- Having a lint for consistent syntax of elided lifetimes will make the [future goal](rust-lang/rust#91639) of warning-by-default for paths participating in elision much simpler.

---

This new lint attempts to accomplish the goal of enforcing consistent syntax. In the process, it supersedes and replaces the existing `elided-named-lifetimes` lint, which means it starts out life as warn-by-default.
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Jun 9, 2025
… r=traviscross,jieyouxu

Add a new `mismatched-lifetime-syntaxes` lint

The lang-team [discussed this](https://hackmd.io/nf4ZUYd7Rp6rq-1svJZSaQ) and I attempted to [summarize](rust-lang/rust#120808 (comment)) their decision. The summary-of-the-summary is:

- Using two different kinds of syntax for elided lifetimes is confusing. In rare cases, it may even [lead to unsound code](rust-lang/rust#48686)! Some examples:

    ```rust
    // Lint will warn about these
    fn(v: ContainsLifetime) -> ContainsLifetime<'_>;
    fn(&'static u8) -> &u8;
    ```

- Matching up references with no lifetime syntax, references with anonymous lifetime syntax, and paths with anonymous lifetime syntax is an exception to the simplest possible rule:

    ```rust
    // Lint will not warn about these
    fn(&u8) -> &'_ u8;
    fn(&'_ u8) -> &u8;
    fn(&u8) -> ContainsLifetime<'_>;
    ```

- Having a lint for consistent syntax of elided lifetimes will make the [future goal](rust-lang/rust#91639) of warning-by-default for paths participating in elision much simpler.

---

This new lint attempts to accomplish the goal of enforcing consistent syntax. In the process, it supersedes and replaces the existing `elided-named-lifetimes` lint, which means it starts out life as warn-by-default.
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Jun 13, 2025
… r=traviscross,jieyouxu

Add a new `mismatched-lifetime-syntaxes` lint

The lang-team [discussed this](https://hackmd.io/nf4ZUYd7Rp6rq-1svJZSaQ) and I attempted to [summarize](rust-lang/rust#120808 (comment)) their decision. The summary-of-the-summary is:

- Using two different kinds of syntax for elided lifetimes is confusing. In rare cases, it may even [lead to unsound code](rust-lang/rust#48686)! Some examples:

    ```rust
    // Lint will warn about these
    fn(v: ContainsLifetime) -> ContainsLifetime<'_>;
    fn(&'static u8) -> &u8;
    ```

- Matching up references with no lifetime syntax, references with anonymous lifetime syntax, and paths with anonymous lifetime syntax is an exception to the simplest possible rule:

    ```rust
    // Lint will not warn about these
    fn(&u8) -> &'_ u8;
    fn(&'_ u8) -> &u8;
    fn(&u8) -> ContainsLifetime<'_>;
    ```

- Having a lint for consistent syntax of elided lifetimes will make the [future goal](rust-lang/rust#91639) of warning-by-default for paths participating in elision much simpler.

---

This new lint attempts to accomplish the goal of enforcing consistent syntax. In the process, it supersedes and replaces the existing `elided-named-lifetimes` lint, which means it starts out life as warn-by-default.
@bors

This comment was marked as resolved.

Removing the `issue-91763` test as the implementation is completely
different now.

Bootstrap forces `rust_2018_idioms` to the warning level in the
rustc_lint doctests using `-Zcrate-attr`. This overrides the doctest's
crate-level `deny` attributes, so I've changed those to be
statement-level attributes.
@shepmaster shepmaster force-pushed the split-elided-lifetimes-in-paths branch from 22cfbc6 to b0e622e Compare June 14, 2025 17:30
@shepmaster shepmaster changed the title Split elided_lifetime_in_paths into tied and untied Split elided_lifetime_in_paths into finer-grained lints Jun 14, 2025
@shepmaster
Copy link
Member Author

r? @jieyouxu

@rustbot rustbot assigned jieyouxu and unassigned davidtwco Jun 14, 2025
@shepmaster shepmaster marked this pull request as ready for review June 14, 2025 21:16
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 14, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 14, 2025

Changes to the size of AST and/or HIR nodes.

cc @nnethercote

@@ -4996,7 +5012,7 @@ mod size_asserts {
static_assert_size!(StmtKind<'_>, 16);
static_assert_size!(TraitItem<'_>, 88);
static_assert_size!(TraitItemKind<'_>, 48);
static_assert_size!(Ty<'_>, 48);
static_assert_size!(Ty<'_>, 56);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is unfortunate. Ty is a very common type, an extra 8 bytes just to fit a boolean?

@jieyouxu

This comment was marked as outdated.

@rustbot rustbot removed the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label Jun 16, 2025
Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! The broad implementation looks good to me modulo some nits, however I have two implementation specifics that I'm not very sure about.

Here's my initial review pass, but I would like to ask for a second opinion from @oli-obk (or @nnethercote) regarding two questions:

  1. The "overlapping" mismatched_lifetime_syntaxes and hidden_lifetimes_in_output_paths situation, and whether to perform some kind of "suppression" to have mismatched_lifetime_syntaxes take precedence over hidden_lifetimes_in_output_paths unless hidden_lifetimes_in_output_paths have a stronger lint level. And if that is desired, the more important question is how, since these two lints run in their own lint passes. AFAICT, it doesn't seem easy to stash both lint diagnostics and their respective effective lint levels, then perform some kind of post-diagnostics-construction precedence filter that accounts for the relative lint levels between the two lints.
  2. I'm also not very sure about the increase in size of Ty<'_>, since this is a very commonly used type, as @nnethercote pointed out in an earlier review comment. It seems like tracking source of the type is desirable, but I'm not too sure about an alternative scheme that doesn't involve increasing size of Ty<'_>.

I'm going to hand this review off to oli for a second opinion.

Comment on lines 4 to 6
// signatures, but they can also appear in other places! The original
// version of this lint handled all these cases in one location, but
// it's desired that the one lint actually be multiple.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (wording): maybe for clarity (I was reading this, and it wasn't immediately clear)

Most of the time, we focus on elided lifetimes in function signatures, but they can also appear in other places! The original lint elided_lifetime_in_paths handled all these cases in one location, but it's more desirable to split it into 3 more fine-grained lints under a new hidden_lifetimes_in_paths lint group umbrella:

  1. hidden_lifetimes_in_input_paths
  2. hidden_lifetimes_in_output_paths
  3. hidden_lifetimes_in_type_paths

Comment on lines +289 to +290
lint_hidden_lifetime_in_path =
paths containing hidden lifetime parameters are deprecated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion [WORDING-STRENGTH 1/2]: I think I agree with @tmandry's comment earlier

hidden lifetime parameters in types are deprecated

I also think we should remove this wording at least until we upgrade some lint to warn-by-default.

where if this set of 3 lints are initially allow-by-default in this PR, we should not use "deprecated" in this PR, but instead add "deprecated" in the follow-up PR that bumps these three lints and the hidden_lifetimes_in_paths to warn-by-default, so that the deprecation is part of the follow-up decision to explicitly deprecate this form.

In this PR, I would reword this less strong initially, maybe something like

paths containing hidden lifetime parameters can be confusing

Comment on lines +100 to +101
/// explicitly stated, even if it is the `'_` [placeholder
/// lifetime].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (wording): To make it easier to search for '_, maybe

This lint ensures that lifetime parameters are always explicitly stated, even if it is the placeholder lifetime '_.

Actually hm, this is more so trying to express

This lint ensures that lifetime parameters are explicit in types occuring as function arguments. Where lifetime elision may occur, the placeholder lifetime '_ may be used to make the presence of lifetime constraints explicit.

Comment on lines +134 to +138
/// glance that borrowing is occurring. This is especially true
/// when a type is used as a function's return value: lifetime
/// elision will link the return value's lifetime to an argument's
/// lifetime, but no syntax in the function signature indicates
/// that.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussion: right, I see what you meant by having both mismatched_lifetime_syntaxes and hidden_lifetimes_in_output_paths firing against the same return position type-with-hidden-lifetime.

For reference, the discussion about what a reasonable scheme is to suppress the lower-lint-level of the two lints, or have mismatched_lifetime_syntaxes "shadow" hidden_lifetimes_in_output_paths when both are at the same lint level is at #t-compiler/diagnostics > Avoiding emitting one lint if another has been emitted.

@oli-obk suggested in that thread something among the lines of

Since we already have stashed errors we could have lint marking where we can explicitly mark spans as having linted and other lints can check if spans have been linted

Tho I'm not sure how to cleanly massage e.g. the stashed diagnostics mechanism and StashKey to indicate how to resolve if both lints are to be emitted (since they'd be ran in separate lint passes). I'll hand the review over to oli for this part.

/// [placeholder lifetime]: https://doc.rust-lang.org/reference/lifetime-elision.html#lifetime-elision-in-functions
pub HIDDEN_LIFETIMES_IN_TYPE_PATHS,
Allow,
"hidden lifetime parameters in types outside function signatures are discouraged"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion [WORDING-STRENGTH 2/2]: same here, maybe lessen the "strength" conveyed in this initial PR adding this lint as allow-by-default, and defer the lint level bump + wording strengthening (to "deprecate" and "discourage") in the follow-up upgrade-to-warn-by-default PR. I.e. maybe

hidden lifetime parameters in types outside function signatures can be confusing

But I don't feel too strongly about this.

Comment on lines +5 to +9
#[deny(hidden_lifetimes_in_paths)]
mod w {
fn test2(_: super::Foo) {}
//~^ ERROR paths containing hidden lifetime parameters are deprecated
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: can you add a little more elaboration on what this test intends to exercise? Is it to check that we recurse into items with mods, and/or the lint level can be properly controlled through lint level attributes?

@jieyouxu
Copy link
Member

r? @oli-obk (for a second opinion, unless you're busy)

@rustbot rustbot assigned oli-obk and unassigned jieyouxu Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tidy Area: The tidy tool A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. L-elided_lifetimes_in_paths Lint: elided_lifetimes_in_paths S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-lang Relevant to the language team
Projects
None yet
Development

Successfully merging this pull request may close these issues.