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
Many Context methods return types with unnecessarily short lifetimes #243
Comments
impl<'ctx> Context<'ctx> {
pub fn const_string<'a>(&'a self, string: &'a [u8], null_terminated: bool) -> VectorValue<'a> {
...
}
}
I think You may have actually meant |
Yeah, oops.
Yup! |
Ok, then I am in favor of this change. Good catch |
fn f<'ctx>(ctx: &'ctx Context, v: i32) -> VectorValue<'ctx> {
ctx.const_string(v.to_string().as_bytes(), false)
}
fn f<'ctx>(ctx: &'ctx Context, v: &[u8]) -> VectorValue<'ctx> {
ctx.const_string(v, false)
}
fn f<'a, 'ctx>(ctx: &'ctx Context, v: &'a [u8]) -> VectorValue<'ctx> {
ctx.const_string(v, false)
} all compile fine as is. Do you have an actual non-compiling example I can verify changes against? |
ah, turns out i misunderstood the lifetime elision rules:
The first and third rules mean the function signature actually becomes: fn const_string<'a, 'b>(&'a self, string: &'b [u8], null_terminated: bool) -> VectorValue<'a> |
In:
inkwell/src/context.rs
Line 950 in f768691
the lifetimes are left implicit, which unnecessarily shortens the lifetime of the return value. Making the lifetimes explicit:
The problem is that
VectorValue<'a>
should actually live as long as'ctx
(or at least as long asself
), instead of being shortened to the lifetime ofstring
, sincestring
is internally cloned rather than borrowed.Example:
There are several other functions with similar lifetime issues, such as
Context::ptr_sized_int_type
The text was updated successfully, but these errors were encountered: