Skip to content

Commit f04c36f

Browse files
committed
Add tests for issue #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 f04c36f

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

tests/test_enum.cpp

+10
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

+17
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,23 @@ 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 m.test_enum_overload_resolution(0.0, m.ScopedEnum.Two) == 'f(float, ScopedEnum)'
238+
assert m.test_enum_overload_resolution(0, m.ScopedEnum.Two) == 'f(float, ScopedEnum)'
239+
240+
241+
def test_enum_to_float():
242+
"""Passing an enum to a function taking a float should trigger a type error"""
243+
with pytest.raises(TypeError) as execinfo:
244+
m.test_enum_to_float(m.ScopedBoolEnum.TRUE)
245+
assert str(execinfo.value).startswith('TypeError: test_enum_to_float(): incompatible function arguments.')
246+
247+
248+
232249
def test_duplicate_enum_name():
233250
with pytest.raises(ValueError) as excinfo:
234251
m.register_bad_enum()

0 commit comments

Comments
 (0)