Repository navigation
Borrowed pointers inside enums #3166
Description
Activity
I believe the problem is that the field
short_nameis given the strings, which is only valid during the function call toshort_name()itself. In other words, all the lifetimes are not the same. You can make the compile like so:mod argparse { use std; import std::map; import either::{either, left, right}; struct Flag { name: &str; desc: &str; short_name: option<&str>; max_count: uint; banner: option<&str>; mut value: uint; } fn flag(name: &str, desc: &str) -> Flag { Flag { name: name, desc: desc, short_name: none, max_count: 1, banner: none, value: 0 } } impl Flag { fn short_name(self, s: &self/str) -> Flag/&self { let new_banner : option<&str> = match self.banner { none => { none } some(value) => { some(value) } }; Flag { name: self.name, desc: self.desc, short_name: some(s), max_count: self.max_count, banner: match self.banner { none => { none } some(value) => { some(value) } }, value: self.value } } } } fn main () { let f : argparse::Flag = argparse::flag(&"flag", &"My flag"); }all the changes are in the declaration of
short_name(), which becomes:fn short_name(self, s: &self/str) -> Flag/&self {The first change is that
sbecomes&self/strinstead of&str. Basically this means "I need a str that lives as long as the borrowed pointers within self".&strjust means "lives as long as this function call".The return type also has to change to
Flag/&self. This notation is not well publicized or known, but it means "an instance of Foo containing borrowed pointers with lifetime&self". As before, the default is to return an instance ofFlagcontaining borrowed pointers that are valid only for the call itself.Thanks for the detailed explanation and workaround. However, it seems to me that the actual issue is how the borrowed pointer inside self.banner is handled, as it is treated differently from the other fields.
If you take this implementation of short_name() (this is the one I should have posted earlier, instead of the version with 'match'), this fails with the "mismatched type" error:
fn short_name(self, s: &str) ⟶ Flag { Flag { name: self.name, desc: self.desc, short_name: some(s), max_count: self.max_count, banner: self.banner, value: self.value } }
However, this works:
fn short_name(self, s: &str) ⟶ Flag { Flag { name: self.name, desc: self.desc, short_name: some(s), max_count: self.max_count, banner: none, value: self.value } }
One would expect to be able to copy optional fields the same way as mandatory fields. Is it an issue with rust expecting non-wrapped borrowed pointers to have a lifetime at least equal to that of their container, but not so with borrowed pointers wrapped in
option/either? (Not that I have tested witheitherbut there is no reason the same issues does not come up).The field banner is not inconsistent. What's happening is that the type checker is inferring, based on the types given in
short_name(), that you want to construct an instance of Flag where the borrowed pointers within have the lifetime&. This is the lifetime parameter given by the caller: it lasts as long as the current call.In the first case, which reports an error, you have a region pointer in
short_name, which has the lifetime&, and a second region pointer in banner (self.banner). This has a different lifetime so an error is reported.In the second case, the field banner is assigned
none, which is a constant, and hence consistent with any lifetime, so no error is reported.I understand that. What seems odd to me is that the other fields are also borrowed pointers: I would expect the other fields, like
name: &strto be treated the same asbanner: option<&str>. This is what I find inconsistent. The compiler complains specifically about thebannerfield because it's an enum containing a borrowed pointer, while fields containing borrowed pointers without enums (likename) cause no issue, even though, as a programmer, I clearly intend both pointers to have the same lifetime.This is a more general problem with reporting of type error messages, I suppose. If you wrote something like this:
struct foo<T> { a: T; b: T; } fn main() { foo { a: 3, b: "hi" }; }You get an error reporting for the field
b, even though you clearly meant for the types ofaandbto be the same. So what is happening here is that the compiler has inferred based on thenamefield what the lifetime of the other fields should be, and then it found that the latter fields (namelybanner) were inconsistent with that. (In fact the situation is completely analogous to the type parameter; there is in fact an implicit lifetime parameter on the type whose value is being inferred)I agree it would be nice if, upon encountering a type error, the compiler displayed all mutually inconsistent items. In fact I think there is an issue open on this point.
Sorry if I'm being a bit dense here. It's not the explanation of the error which I have an issue with, it's the fact that I do not understand why the region pointer in
bannerhas a different lifetime from the rest. In spite of the fact that the borrowed pointer inbanneris wrapped insideoption, I expect it to have the same lifetime as, say,desc.Let me try another example. I'm going to henceforth banish all
optionfields fromFlag, and attempt to create a new flag by passing&sto it, and copying the rest of the borrowed pointers from the old object. If I understand it right, it should not work without using the&self/strtype fors: the lifetime ofs, whether it's assigned to anoptionfield or not, still needs to be at least equal to the lifetime of the object we are copying, correct?Let's find out:
mod argparse { use std; import std::map; import either::{either, left, right}; struct Flag { name: &str; desc: &str; max_count: uint; mut value: uint; } fn flag(name: &str, desc: &str) -> Flag { Flag { name: name, desc: desc, max_count: 1, value: 0 } } impl Flag { fn set_desc(self, s: &str) -> Flag { Flag { name: self.name, desc: s, max_count: self.max_count, value: self.value } } } } fn main () { let f : argparse::Flag = argparse::flag(~"flag", ~"My flag"); let updated_flag = f.set_desc(~"My new flag"); assert updated_flag.desc == "My new flag"; }
Shockingly, not only it compiles, but the assertion works!
Hmm, this seems like a bug. I do not think this should compile, because the lifetime of
self.nameis&selfand the lifetime ofsis the lifetime parameter&, and there is no relationship between them.Ok, this makes sense, thanks for taking time to discuss the issue.
No problem, thanks for uncovering a bug!
- added a commit that references this issue
on Nov 17, 2023 - added a commit that references this issue
on Jun 4, 2024
The following code fails with:
I would expect it to compile, given that the lifetime of the value inside the option will be the same as the lifetime of the other fields.