General lifetime elision of values possible or even desired?

Following my question from 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?

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!(), but suddently once you implement that function all the places where the function is used don’t compile anymore.

4 Likes

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.

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.

3 Likes

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