# General lifetime elision of values possible or even desired?

**URL:** <https://internals.rust-lang.org/t/general-lifetime-elision-of-values-possible-or-even-desired/13692>\
**Category:** Uncategorized\
**Created:** [December 29, 2020, 3:57pm UTC](https://internals.rust-lang.org/t/general-lifetime-elision-of-values-possible-or-even-desired/13692 "2020-12-29T15:57:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sighoya](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@sighoya](https://internals.rust-lang.org/u/sighoya)\
**Post date:** [December 29, 2020, 3:57pm UTC](https://internals.rust-lang.org/t/general-lifetime-elision-of-values-possible-or-even-desired/13692/1 "2020-12-29T15:57:59Z")

</div>

Following my question from [https://users.rust-lang.org/t/inferred-lifetimes/53396/5](https://users.rust-lang.org/t/inferred-lifetimes/53396/5).

Is it generally possible to infer lifetimes from function definitions (not from struct/function signatures) by simply inferring the worst case scenario?

Did I miss something?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 29, 2020, 4:02pm UTC](https://internals.rust-lang.org/t/general-lifetime-elision-of-values-possible-or-even-desired/13692/2 "2020-12-29T16:02:33Z")

</div>

Inferring function signatures from function definitions (and be it only the lifetimes in a function signature), aka some form of global type inference is technically possible, however not desirable in Rust. If you don’t write a function signature explicitly it is way easier to accidentally break semver-compatibility due to implicitly changing function signatures. Also type-checking partially complete programs becomes more painful if you have a setting where everything still compiles since the compiler derived very lax lifetimes for a function whose implementation is still [`todo!()`](https://doc.rust-lang.org/std/macro.todo.html), but suddently once you implement that function all the places where the function is _used_ don’t compile anymore.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [December 29, 2020, 4:02pm UTC](https://internals.rust-lang.org/t/general-lifetime-elision-of-values-possible-or-even-desired/13692/3 "2020-12-29T16:02:35Z")

</div>

> [@sighoya](#):
>
> Is it generally possible to infer lifetimes from function definitions

Yes, it is, but then this is yet another level of magic that you are proposing. This would make practically every function that uses references into a leaky abstraction, by allowing the public interface (the signature) to silently and invisibly change based on the implementation (the body). This in turn breaks every conceivable piece of good practice around abstraction, and as such is _strongly_ undesirable.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 29, 2020, 4:07pm UTC](https://internals.rust-lang.org/t/general-lifetime-elision-of-values-possible-or-even-desired/13692/4 "2020-12-29T16:07:15Z")

</div>

Something that IMO _should_ be discussed though _(at least in case it hasn’t already been discussed)_ is the possibility suggesting inferred maximally lax, or what you might have called "worst case" lifetimes in compiler error messages in cases where lifetimes mismatch or are elided in "illegal" ways, yet a function definition is already present. And similarly offering such a lifetime inference as an IDE-autocompletion step could be valuable.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 29, 2021, 4:07pm UTC](https://internals.rust-lang.org/t/general-lifetime-elision-of-values-possible-or-even-desired/13692/5 "2021-03-29T16:07:20Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
