Skip to content

Commit 891b0a1

Browse files
committed
test(enum): add test for for #5020: silent conversion to float
Enums (including enum class) are silently converted to floats during method calls. This can cause overload resolution to fail. I believe this behavior is a bug. This commit adds tests for that behavior. These tests fail right now, but they should pass once the behavior is fixed. See: #5020
1 parent 8b48ff8 commit 891b0a1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

tests/test_enum.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ TEST_SUBMODULE(enums, m) {
5555
m.def("test_enum_to_uint", [](uint32_t) {});
5656
m.def("test_enum_to_long_long", [](long long) {});
5757

58+
// Trying to pass an enum to a function that accepts a float should
59+
// trigger a type error
60+
m.def("test_enum_to_float", [](double) {});
61+
62+
// When performing overload resolution, calling f(0, ScopedEnum.TWO)
63+
// should select f(float, ScopedEnum), NOT f(float, float)
64+
m.def("test_enum_overload_resolution", [](double, double) { return "f(float, float)"; });
65+
m.def("test_enum_overload_resolution",
66+
[](double, ScopedEnum) { return "f(float, ScopedEnum)"; });
67+
5868
// test_duplicate_enum_name
5969
enum SimpleEnum { ONE, TWO, THREE };
6070

tests/test_enum.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,28 @@ def test_enum_to_int():
229229
m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE)
230230

231231

232+
def test_enum_overload_resolution():
233+
"""When performing overload resolution, enums should not be silently
234+
converted to floats"""
235+
assert m.test_enum_overload_resolution(0.0, 0.0) == "f(float, float)"
236+
assert m.test_enum_overload_resolution(0, 0) == "f(float, float)"
237+
assert (
238+
m.test_enum_overload_resolution(0.0, m.ScopedEnum.Two) == "f(float, ScopedEnum)"
239+
)
240+
assert (
241+
m.test_enum_overload_resolution(0, m.ScopedEnum.Two) == "f(float, ScopedEnum)"
242+
)
243+
244+
245+
def test_enum_to_float():
246+
"""Passing an enum to a function taking a float should trigger a type error"""
247+
with pytest.raises(TypeError) as execinfo:
248+
m.test_enum_to_float(m.ScopedBoolEnum.TRUE)
249+
assert str(execinfo.value).startswith(
250+
"TypeError: test_enum_to_float(): incompatible function arguments."
251+
)
252+
253+
232254
def test_duplicate_enum_name():
233255
with pytest.raises(ValueError) as excinfo:
234256
m.register_bad_enum()

0 commit comments

Comments
 (0)