Skip to content

Commit 90cb8a1

Browse files
committed
Improve numeric cast invariants
1 parent dcb925a commit 90cb8a1

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/util/arith_tools.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,30 +125,31 @@ optionalt<Target> numeric_cast(const exprt &arg)
125125
}
126126

127127
/// Convert an mp_integer to integral type Target
128-
/// An invariant with fail with message "Bad conversion" if conversion
129-
/// is not possible.
128+
/// An invariant with fail if the conversion is not possible.
130129
/// \tparam Target: type to convert to
131130
/// \param arg: mp_integer
132131
/// \return value of type Target
133132
template <typename Target>
134133
Target numeric_cast_v(const mp_integer &arg)
135134
{
136135
const auto maybe = numeric_castt<Target>{}(arg);
137-
INVARIANT(maybe, "Bad conversion");
136+
INVARIANT(maybe, "mp_integer should be convertible to target integral type");
138137
return *maybe;
139138
}
140139

141140
/// Convert an expression to integral type Target
142-
/// An invariant with fail with message "Bad conversion" if conversion
143-
/// is not possible.
141+
/// An invariant with fail if the conversion is not possible.
144142
/// \tparam Target: type to convert to
145143
/// \param arg: constant expression
146144
/// \return value of type Target
147145
template <typename Target>
148146
Target numeric_cast_v(const exprt &arg)
149147
{
150148
const auto maybe = numeric_castt<Target>{}(arg);
151-
INVARIANT(maybe, "Bad conversion");
149+
INVARIANT_WITH_DIAGNOSTICS(
150+
maybe,
151+
"expression should be convertible to target integral type",
152+
irep_pretty_diagnosticst(arg));
152153
return *maybe;
153154
}
154155

0 commit comments

Comments
 (0)