Commit 41e1d225 authored by da-woods's avatar da-woods Committed by Stefan Behnel

Fix C++ bool coercion where no "operator bool" exists (GH-4349)

This was causing c++ classes in if-statements to crash.
Fixes #4348
parent 54b0bf52
......@@ -1012,11 +1012,11 @@ class ExprNode(Node):
return self
elif type.is_pyobject or type.is_int or type.is_ptr or type.is_float:
return CoerceToBooleanNode(self, env)
elif type.is_cpp_class:
elif type.is_cpp_class and type.scope and type.scope.lookup("operator bool"):
return SimpleCallNode(
self.pos,
function=AttributeNode(
self.pos, obj=self, attribute='operator bool'),
self.pos, obj=self, attribute=StringEncoding.EncodedString('operator bool')),
args=[]).analyse_types(env)
elif type.is_ctuple:
bool_value = len(type.components) == 0
......
# tag: cpp
# mode: error
from libcpp.string cimport string
cdef foo():
cdef string field
if field: # field cannot be coerced to book
pass
_ERRORS = u"""
8:7: Type 'string' not acceptable as a boolean
"""
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment