Is there a preparation for default lifetimes?

Some time ago this warning appeared:

warning: hiding a lifetime that's elided elsewhere is confusing
 --> src/main.rs:5:16
  |
5 | fn foo<T>(foo: &()) -> Foo<T> {
  |                ^^^     ^^^^^^ the same lifetime is hidden here
  |                |
  |                the lifetime is elided here
  |
  = help: the same lifetime is referred to in inconsistent ways, making the signature confusing
  = note: `#[warn(mismatched_lifetime_syntaxes)]` on by default

After reading threads on default lifetimes, it seems to address some issues that were raised earlier, regarding compatibility with pre-existing defaults and inference? And generally, can you please point out is there some work on default lifetimes going on?

Previous threads:

1 Like

I don't know what you mean by "default" lifetimes.

The lint just wants you to write

fn foo<T>(foo: &()) -> Foo<'_, T> {

so it's clear that there is a lifetime in Foo (rather than, say, Vec<T> that doesn't have one there).

It is a shame that the lint doesn't have a structured suggestion.

2 Likes

Just to clarify, I understand what the lint is telling, just noticing that it may be related to the discussion with default lifetimes. If this lint becomes a hard error in future, it may be easier to integrate default lifetimes.

I don't think those lints were enabled with the intention of making the annotation always required in the future (so that default lifetimes would be feasible). For example this triggers no warning:

fn hidden_in_hidden_out<T>(_: std::cell::Ref<T>) -> Foo<T> {

Last time I checked it had four structured suggestions (for every combination of named and anonymous lifetimes), and the problem was that the error reporting data format doesn't correctly support more than one.

1 Like

Oh, I made the mistake of thinking that the quoted lint was the full output, but it isn't:

warning: hiding a lifetime that's elided elsewhere is confusing
 --> src/lib.rs:2:16
  |
2 | fn foo<T>(foo: &()) -> Foo<T> {
  |                ^^^     ^^^^^^ the same lifetime is hidden here
  |                |
  |                the lifetime is elided here
  |
  = help: the same lifetime is referred to in inconsistent ways, making the signature confusing
  = note: `#[warn(mismatched_lifetime_syntaxes)]` on by default
help: use `'_` for type paths
  |
2 | fn foo<T>(foo: &()) -> Foo<'_, T> {
  |                            +++

I'm happy with the output as is, tbqh.

3 Likes

(“What’s a ‘type path’?” But generally yes, especially since you don’t need to read the message to accept the suggestion.)