-
Notifications
You must be signed in to change notification settings - Fork 740
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
remove null check during access #674
Conversation
based on @xaxxon's work fixes microsoft#649 fixes microsoft#604
thanks @ericLemanissier , I would love to remove the unnecessary null check, but it actually catches a case when a moved-from not null unique_ptr is accessed (see discussion in #674), so will close this for now, unless some other solution is found later. |
This obviously can't provide guarantees that something externally modified isn't null. I question the usefulness of notifying the user much later after the thing has already become null that it is null. Instead you're paying a runtime cost that only "helps" when you violate a basic premise of the type. The "help" doesn't give you any actual idea where you messed up, though, so I really don't understand forcing everyone to pay for it all the time. |
If someone were to dereference the result, isn't it even possible the compiler will elide the check as well since it's not possible to be null in a valid program? edit: no, that's not possible because it might terminate before it gets to the dereference. |
@annagrin: Does not_null now support unique_ptr? That wasn't the case last time I checked (admittedly some time ago). |
@xaxxon: As not_null supports move, a pointer like type that becomes null when moved from (like almost all smart pointers) could create a legal program state where a moved from not_null object contains a nullptr. |
It's "legal" but nonsensical - it violates the entire point of a not_null - that you know at contract time that a variable isn't null. The error produced by accessing it is not helpful as it doesn't tell you WHY you are in this nonsensical state or WHERE in your program caused it. Your program is almost certainly going to crash and it's entirely possible the compiler elides the check in many circumstances if you were to somehow rely on the check. it gives a false sense of safety, provides very little (if any), and will have a runtime cost to provide that. Its literally worse than useless both logically and for performance. |
@xaxxon: I was just answering your question if the compiler could optimize it away and the answer is no. That doesn't mean I like the design, but without language support for destructive move I'm not sure if there are any good solutions here (personally I'd just not support move and live with the consequences). @annagrin : Are you aware that this check will not only fire for a moved from Is defaulting the move constructor of // rant on |
I propose to treat the moved-from Pro.lifetime: Lifetime safety profile or static analyzer checks, such as clang-tidy - bugprone-use-after-move , are one of the solutions. |
@KVic: A There may be different views on what contract checks are (supposed to be). The reason for post condition checks to exist in the first place is that a) we are not sure if the implementation of a function is bugfree and b) that we might not have stated and checked all explicit and implicit preconditions. In this case, an implicit precondition is that the object is in a valid state. Now sure, we could delegate the responsibility to ensure that |
I also think that having Maybe you could have two different types? First a |
@MikeGitb I would love to hear fustrations with GSL in a separate thread - would you be able to summarize them and send me an email (alternatively, you can start an issue so others can join. "Rant allowed" in the title would help:). I also greatly appreciate suggestions with PRs, since this is a community project. I only have very limited time to work on this, and mostly at my free time, so response and fixing issues can be delayed) @xaxxon I would love to be able to resolve all the problems at once, but not remove the safety. I would prefer not to rely on static analysis checkers since they are not always used and, to my knowledge, not formally proved to detect all issues related to moved-from objects. If null check is removed, @galik @MikeGitb you have a good point about the untrustworthy |
based on @xaxxon's work
fixes #649
fixes #604