-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Borrowed pointers inside enums #3166
Comments
I believe the problem is that the field
all the changes are in the declaration of
The first change is that The return type also has to change to |
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 |
The field banner is not inconsistent. What's happening is that the type checker is inferring, based on the types given in In the first case, which reports an error, you have a region pointer in In the second case, the field banner is assigned |
I understand that. What seems odd to me is that the other fields are also borrowed pointers: I would expect the other fields, like |
This is a more general problem with reporting of type error messages, I suppose. If you wrote something like this:
You get an error reporting for the field 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 Let me try another example. I'm going to henceforth banish all 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 |
Ok, this makes sense, thanks for taking time to discuss the issue. |
No problem, thanks for uncovering a bug! |
reallocarray shim linux/freebsd support proposal.
Bumps [tests/perf/s2n-quic](https://github.com/aws/s2n-quic) from `9730578` to `1436af7`. <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/aws/s2n-quic/commit/1436af712b6e73edc11640dc7c3cae23e456c0a8"><code>1436af7</code></a> ci: Remove neqo from required resumption test clients (<a href="https://redirect.github.com/aws/s2n-quic/issues/2191">#2191</a>)</li> <li><a href="https://github.com/aws/s2n-quic/commit/c0bcef639de0e2a6a89202e84c9933d99d431047"><code>c0bcef6</code></a> build: remove --cfg s2n_quic_unstable (<a href="https://redirect.github.com/aws/s2n-quic/issues/2190">#2190</a>)</li> <li><a href="https://github.com/aws/s2n-quic/commit/fed54a59dcfdbc70e7c3c2d4b1cf3c4991ad4403"><code>fed54a5</code></a> feat(s2n-quic-platform): make message methods public (<a href="https://redirect.github.com/aws/s2n-quic/issues/2189">#2189</a>)</li> <li>See full diff in <a href="https://github.com/aws/s2n-quic/compare/9730578c0d562d80bbbe663161b3a5408ed3116c...1436af712b6e73edc11640dc7c3cae23e456c0a8">compare view</a></li> </ul> </details> <br /> Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details> Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.
The text was updated successfully, but these errors were encountered: