-
Notifications
You must be signed in to change notification settings - Fork 273
numeric_cast_v(expr) now requires constant_expr #4004
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
Conversation
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.
A few redundant invariants, and two apparent changes in behaviour that should be tested or reverted
@@ -155,7 +155,9 @@ void symbol_factoryt::gen_nondet_array_init( | |||
const recursion_sett &recursion_set) | |||
{ | |||
auto const &array_type = to_array_type(expr.type()); | |||
auto const array_size = numeric_cast_v<size_t>(array_type.size()); | |||
const auto &size = array_type.size(); | |||
PRECONDITION(size.id() == ID_constant); |
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.
Redundant with to_constant_expr
?
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.
⛏️ Just noting this never got addressed.
@@ -72,9 +72,13 @@ bool boolbvt::literal( | |||
std::size_t element_width=boolbv_width(index_expr.type()); | |||
CHECK_RETURN(element_width != 0); | |||
|
|||
mp_integer index = numeric_cast_v<mp_integer>(index_expr.index()); | |||
const auto &index = index_expr.index(); | |||
PRECONDITION(index.id() == ID_constant); |
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.
redundant
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.
⛏️ Or this
|
||
std::size_t element_size=boolbv_width(subtype); | ||
|
||
DATA_INVARIANT( | ||
size.id() == ID_constant, "update expects constant-sized array"); |
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.
Redundant
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.
@smowton 👎 - this provides more information that just relying on the cast. But according to guidelines, should be framed in the should form, for clarity:
Array should be constant size for boolbv_update
src/util/pointer_offset_size.cpp
Outdated
const mp_integer size = | ||
numeric_cast_v<mp_integer>(to_vector_type(type).size()); | ||
const mp_integer size_int = | ||
numeric_cast_v<mp_integer>(to_constant_expr(size)); |
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.
Change in behaviour: returning 0
vs. {}
?
src/util/simplify_expr.cpp
Outdated
|
||
const std::size_t n_el = numeric_cast_v<std::size_t>(array_type.size()); | ||
if(!size.is_constant()) |
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.
Change in behaviour: returning empty array vs. nil?
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.
That one seems like a bugfix!
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.
This is a change in behaviour - could you add a unit test checking this and ideally put it into a separate commit in case it breaks anything (this would be a big commit to comb through!)
Presumably this change came about, because in a regression test the size here is non-const, perhaps the test could be tightened so it would have failed before too?
src/goto-programs/xml_expr.cpp
Outdated
@@ -127,6 +127,8 @@ xmlt xml(const exprt &expr, const namespacet &ns) | |||
|
|||
if(expr.id() == ID_constant) | |||
{ | |||
const auto &constant_expr = to_constant_expr(expr); |
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.
Isn't this also in another PR? Maybe get that other one merged first?
@@ -2636,8 +2644,10 @@ void smt2_convt::convert_struct(const struct_exprt &expr) | |||
void smt2_convt::flatten_array(const exprt &expr) | |||
{ | |||
const array_typet &array_type = to_array_type(expr.type()); | |||
const exprt &size_expr = array_type.size(); | |||
PRECONDITION(size_expr.id() == ID_constant); |
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.
Redundant
src/util/pointer_offset_size.cpp
Outdated
const auto &size = vector_type.size(); | ||
|
||
auto sub = pointer_offset_bits(vector_type.subtype(), ns); | ||
if(!sub.has_value() || size.id() != ID_constant) |
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.
Should be an invariant
src/util/simplify_expr.cpp
Outdated
|
||
const std::size_t n_el = numeric_cast_v<std::size_t>(array_type.size()); | ||
if(!size.is_constant()) |
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.
That one seems like a bugfix!
On the preconditions: I do feel these are important as a way of documenting the contract of the function they are in. |
This PR will shrink a lot with #4007. |
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.
🚫
This PR failed Diffblue compatibility checks (cbmc commit: ebfb529).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/99263775
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.
Common spurious failures:
- the cbmc commit has disappeared in the mean time (e.g. in a force-push)
- the author is not in the list of contributors (e.g. first-time contributors).
The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.
5b1192e
to
00df0f4
Compare
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.
🚫
This PR failed Diffblue compatibility checks (cbmc commit: 94ec292).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/99494027
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.
Common spurious failures:
- the cbmc commit has disappeared in the mean time (e.g. in a force-push)
- the author is not in the list of contributors (e.g. first-time contributors).
The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.
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.
🚫
This PR failed Diffblue compatibility checks (cbmc commit: 00df0f4).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/99498173
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.
Common spurious failures:
- the cbmc commit has disappeared in the mean time (e.g. in a force-push)
- the author is not in the list of contributors (e.g. first-time contributors).
The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.
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.
Some small bits left, but also will need a TG bump.
Some tests are still failing on the TG bump for this, I'm investigating and I'll post updates here. |
The TG bump is now passing. 👍 |
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.
Please fix the variable name and the test for the bug fix
@@ -155,7 +155,9 @@ void symbol_factoryt::gen_nondet_array_init( | |||
const recursion_sett &recursion_set) | |||
{ | |||
auto const &array_type = to_array_type(expr.type()); | |||
auto const array_size = numeric_cast_v<size_t>(array_type.size()); | |||
const auto &size = array_type.size(); | |||
PRECONDITION(size.id() == ID_constant); |
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.
⛏️ Just noting this never got addressed.
@@ -72,9 +72,13 @@ bool boolbvt::literal( | |||
std::size_t element_width=boolbv_width(index_expr.type()); | |||
CHECK_RETURN(element_width != 0); | |||
|
|||
mp_integer index = numeric_cast_v<mp_integer>(index_expr.index()); | |||
const auto &index = index_expr.index(); | |||
PRECONDITION(index.id() == ID_constant); |
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.
⛏️ Or this
|
||
// constant? | ||
if(expr.index().is_constant()) | ||
if(index.is_constant()) |
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 more idiomatic in this code base to:
if(const auto &constant_index = try_expr_dynamic_cast<constant_exprt>(index))
{
...
numeric_cast_v<mp_integer>(constant_index);
Since seems plausible the is_constant
method will go away at some point
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.
or shorter using an optional:
if(const auto index_as_integer = numeric_cast<mp_integer>(index))
@@ -87,8 +87,10 @@ get_quantifier_var_max(const exprt &var_expr, const exprt &quantifier_expr) | |||
continue; | |||
if(expr_eq(var_expr, x.op0()) && x.op1().id()==ID_constant) | |||
{ | |||
exprt over_expr=x.op1(); | |||
const constant_exprt &over_expr = to_constant_expr(x.op1()); |
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.
⛏️ While in the area, you might like to tidy the if to not duplicate the checking the id by using try_dynamic_cast
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.
or the version of numeric_cast
which returns an optional
|
||
std::size_t element_size=boolbv_width(subtype); | ||
|
||
DATA_INVARIANT( | ||
size.id() == ID_constant, "update expects constant-sized array"); |
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.
@smowton 👎 - this provides more information that just relying on the cast. But according to guidelines, should be framed in the should form, for clarity:
Array should be constant size for boolbv_update
src/util/simplify_expr.cpp
Outdated
|
||
const std::size_t n_el = numeric_cast_v<std::size_t>(array_type.size()); | ||
if(!size.is_constant()) |
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.
This is a change in behaviour - could you add a unit test checking this and ideally put it into a separate commit in case it breaks anything (this would be a big commit to comb through!)
Presumably this change came about, because in a regression test the size here is non-const, perhaps the test could be tightened so it would have failed before too?
|
||
// constant? | ||
if(expr.index().is_constant()) | ||
if(index.is_constant()) |
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.
or shorter using an optional:
if(const auto index_as_integer = numeric_cast<mp_integer>(index))
@@ -87,8 +87,10 @@ get_quantifier_var_max(const exprt &var_expr, const exprt &quantifier_expr) | |||
continue; | |||
if(expr_eq(var_expr, x.op0()) && x.op1().id()==ID_constant) | |||
{ | |||
exprt over_expr=x.op1(); | |||
const constant_exprt &over_expr = to_constant_expr(x.op1()); |
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.
or the version of numeric_cast
which returns an optional
a4dded0
to
3a6425c
Compare
89345ef
to
bd13a73
Compare
src/util/simplify_expr.cpp
Outdated
|
||
for(std::size_t i=0; i<n_el; ++i) | ||
for(std::size_t i=0; i<number_of_elements; ++i) |
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.
This isn't clang-format correctly spaced out.
This is follow-up from a discussion on PR #3998, and a comment by @tautschnig. This function always fails, with an exception, when given anything but a constant_exprt. This change means that the caller must do the type conversion. The benefit is to make the caller more aware of the requirement that this must be a constant, and to make the caller handle the error appropriately (with an user-friendly error message) in case this is not possible. The disadvantage is additional code at the call site.
bd13a73
to
4a133b3
Compare
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.
✔️
Passed Diffblue compatibility checks (cbmc commit: 4a133b3).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/101202288
This is follow-up from a discussion on PR #3998, and a comment by
@tautschnig.
This function always fails, with an exception, when given anything but a
constant_exprt.
This change means that the caller must do the type conversion. The benefit
is to make the caller more aware of the requirement that this must be a
constant, and to make the caller handle the error appropriately (with an
user-friendly error message) in case this is not possible.
The disadvantage is additional code at the call site.