-
Notifications
You must be signed in to change notification settings - Fork 456
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
Add StatusOr
for error handling in modern C++ style
#768
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c851ea3
Add StatusOr for modern-C++-style error handling
PragmaTwice 797368b
add unit test
PragmaTwice 1d7fa54
fix lint
PragmaTwice 7781ddf
fix static str
PragmaTwice 276df70
remove max for darwin
PragmaTwice 4a821e7
more static assert
PragmaTwice 9475322
rename storage_ to value_or_error_
PragmaTwice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <memory> | ||
#include <status.h> | ||
|
||
TEST(StatusOr, Scalar) { | ||
auto f = [](int x) -> StatusOr<int> { | ||
if (x > 10) { | ||
return {Status::NotOK, "x large than 10"}; | ||
} | ||
|
||
return 2 * x + 5; | ||
}; | ||
|
||
ASSERT_EQ(*f(1), 7); | ||
ASSERT_EQ(*f(5), 15); | ||
ASSERT_EQ(f(7).GetValue(), 19); | ||
ASSERT_EQ(f(7).GetCode(), Status::cOK); | ||
ASSERT_EQ(f(7).Msg(), "ok"); | ||
ASSERT_TRUE(f(6)); | ||
ASSERT_EQ(f(11).GetCode(), Status::NotOK); | ||
ASSERT_EQ(f(11).Msg(), "x large than 10"); | ||
ASSERT_FALSE(f(12)); | ||
|
||
auto x = f(5); | ||
ASSERT_EQ(*x, 15); | ||
ASSERT_EQ(x.Msg(), "ok"); | ||
ASSERT_EQ(x.GetValue(), 15); | ||
ASSERT_EQ(x.GetCode(), Status::cOK); | ||
|
||
auto y = f(11); | ||
ASSERT_EQ(y.Msg(), "x large than 10"); | ||
ASSERT_EQ(y.GetCode(), Status::NotOK); | ||
|
||
auto g = [f](int y) -> StatusOr<int> { | ||
if (y > 5 && y < 15) { | ||
return {Status::NotOK, "y large than 5"}; | ||
} | ||
|
||
auto res = f(y); | ||
if (!res) return res; | ||
|
||
return *res * 10; | ||
}; | ||
|
||
ASSERT_EQ(*g(1), 70); | ||
ASSERT_EQ(*g(5), 150); | ||
ASSERT_EQ(g(1).GetValue(), 70); | ||
ASSERT_EQ(g(1).GetCode(), Status::cOK); | ||
ASSERT_EQ(g(1).Msg(), "ok"); | ||
ASSERT_EQ(g(6).GetCode(), Status::NotOK); | ||
ASSERT_EQ(g(6).Msg(), "y large than 5"); | ||
ASSERT_EQ(g(20).GetCode(), Status::NotOK); | ||
ASSERT_EQ(g(20).Msg(), "x large than 10"); | ||
ASSERT_EQ(g(11).GetCode(), Status::NotOK); | ||
ASSERT_EQ(g(11).Msg(), "y large than 5"); | ||
} | ||
|
||
TEST(StatusOr, String) { | ||
auto f = [](std::string x) -> StatusOr<std::string> { | ||
if (x.size() > 10) { | ||
return {Status::NotOK, "string too long"}; | ||
} | ||
|
||
return x + " hello"; | ||
}; | ||
|
||
auto g = [f](std::string x) -> StatusOr<std::string> { | ||
if (x.size() < 5) { | ||
return {Status::NotOK, "string too short"}; | ||
} | ||
|
||
auto res = f(x); | ||
if (!res) return res; | ||
|
||
return "hi " + *res; | ||
}; | ||
|
||
ASSERT_TRUE(f("1")); | ||
ASSERT_FALSE(f("12345678901")); | ||
ASSERT_FALSE(g("1")); | ||
|
||
ASSERT_EQ(*f("twice"), "twice hello"); | ||
ASSERT_EQ(*g("twice"), "hi twice hello"); | ||
ASSERT_EQ(g("shrt").GetCode(), Status::NotOK); | ||
ASSERT_EQ(g("shrt").Msg(), "string too short"); | ||
ASSERT_EQ(g("loooooooooooog").GetCode(), Status::NotOK); | ||
ASSERT_EQ(g("loooooooooooog").Msg(), "string too long"); | ||
|
||
ASSERT_EQ(g("twice").ToStatus().GetCode(), Status::cOK); | ||
ASSERT_EQ(g("").ToStatus().GetCode(), Status::NotOK); | ||
|
||
auto x = g("twice"); | ||
ASSERT_EQ(x.ToStatus().GetCode(), Status::cOK); | ||
auto y = g(""); | ||
ASSERT_EQ(y.ToStatus().GetCode(), Status::NotOK); | ||
} | ||
|
||
TEST(StatusOr, SharedPtr) { | ||
struct A { | ||
A(int *x) : x(x) { *x = 233; } | ||
~A() { *x = 0; } | ||
|
||
int *x; | ||
}; | ||
|
||
int val = 0; | ||
|
||
{ | ||
StatusOr<std::shared_ptr<A>> x(new A(&val)); | ||
|
||
ASSERT_EQ(val, 233); | ||
ASSERT_EQ(x->use_count(), 1); | ||
|
||
{ | ||
StatusOr<std::shared_ptr<A>> y(*x); | ||
ASSERT_EQ(val, 233); | ||
ASSERT_EQ(x->use_count(), 2); | ||
} | ||
|
||
ASSERT_EQ(x->use_count(), 1); | ||
} | ||
|
||
ASSERT_EQ(val, 0); | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what would happen if calling
f(11).GetValue()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ShooterIT, if you look at the definition of
StatusOr<T>::GetValue
:You will find there is three different overload of this method via changing the qualifier of
*this
.Hence, in
f(x).GetValue()
, obviouslyf(x)
is a prvalue since a temporary object is returned, and then the signaturevalue_type&& GetValue() &&
is decided to be called after the overload resolution, and thus you get an xvalue (which is a special kind of rvalue) referenced to the underlying value of the returnedStatusOr
.So we should take care of the lifetime in this case, the rvalue will become invalid after the computation of the current statement. We can extend it via a call to a move ctor (
T(T&&)
). This is not a defect of this type, but it is an idiom of C++, we should always care about lifetime of references to avoid dangling references in all standards of C++ (98/03/11/14/17/20 ...).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, the classic usage of
StatusOr<T>
we expected is:Use
StatusOr
as an rvalue is totally (well) supported, but everyone should be aware of what they are doing, otherwise just follow the classic usage is fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i want to describe
11
is greater than10
,GetValue()
will return error?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetValue()
will trigger a fatal error.It is just like you call
f
asStatus f(int *output)
, but do not check the returnedStatus
and directly dereference theoutput
. It is not allowed, you must check status before dereference.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that guarded by
CHECK(*this)
, it can cause a fatal error.I wonder if we can generate a compile time error instead of runtime fatal in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirm its failure as:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @tisonkun, thanks for your review.
Good idea. We consider an example for this:
In this example, only the
doSomething
part can actually dereference theres
.We cannot dereference it both before
if(!res)
and insideif(!res)
.So only in the execution path with the condition
res.IsOk() == true
being satisfied, we can dereferenceres
,It is hardly to express in compile time, since it is related to the control flow.
Like a raw/smart pointer, we should check it does point to a value (not null) before dereference it.
I have implemented a checker in Clang Static Analyzer (included in clang-tidy) for OneFlow that checks whether a type like
StatusOr
is dereferenced after checkingIsOk()
using symbolic execution, and I think it can be done in this way in the future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me.