-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Lifetime bound "forgotten" in a function #10703
Comments
cc @nikomatsakis, it seems like if we're able to have shadow type parameters you should be able to have shadow lifetime parameters. |
This has come up before though I'm not sure if there's another issue open for it. The case when this last came up was dealing with symbols from a dynamically loaded extension. It would be useful for, as @honzasp says, exposing lifetime information from external libraries. |
cc me |
this is a dup of #5922 -- I will comment there. |
Rustup r? `@ghost` changelog: none
Hi,
if a struct is bound by a lifetime which is not used in the body of the struct, this bound seems to be excluded from typechecking.
Consider following functions (not valid Rust, because the function and struct bodies are omitted):
Banana
s are created fromBananaTree
s and their lifetime must not exceed the lifetime of their tree.banana_calories
expects the passed banana to be alive, andtransform_banana
creates a banana from another one, preserving the lifetime information.The lifetimes should cause the following function to be rejected by the compiler, because the result of
transform_banana
has lifetime bound to the lifetime oftree
, which is tied to the enclosing block.leaked_banana
and the following call tobanana_calories
should then be invalid.If the bananas and functions are defined like this:
The lifetime bound
'a
inBanana<'a>
is used in the fieldcalories
, forcing the borrow checks and making the invalid use shown above fail with appropriate error message (tree
does not live long enough):On the other hand, if the implementation is like:
Then the lifetime bound on
Banana<'a>
is not used in the member fields at all, but it still should be tied to the lifetime of theBananaTree
passed tocreate_banana
and it should limit the use of the created banana. It doesn't work like that, though, because the following function compiles:The lifetime of banana from
create_banana
is clearly not tied to lifetime oftree
, because the compiler allows us to use the banana when its mother tree went out of scope.This is in fact safe, because
Banana
didn't contain any reference to theBananaTree
. The point is that I would like to use Rust's lifetimes in a C library bindings, where theBanana
values live as long as their respectiveBananaTree
s (as a matter of fact, the library is LLVM, banana trees are modules/functions and bananas are values).The text was updated successfully, but these errors were encountered: