Commit 026e3de9 authored by Robert Bradshaw's avatar Robert Bradshaw

Reference type inference

parent 9444212e
...@@ -276,10 +276,14 @@ def find_spanning_type(type1, type2): ...@@ -276,10 +276,14 @@ def find_spanning_type(type1, type2):
def aggressive_spanning_type(types, might_overflow): def aggressive_spanning_type(types, might_overflow):
result_type = reduce(find_spanning_type, types) result_type = reduce(find_spanning_type, types)
if result_type.is_reference:
result_type = result_type.ref_base_type
return result_type return result_type
def safe_spanning_type(types, might_overflow): def safe_spanning_type(types, might_overflow):
result_type = reduce(find_spanning_type, types) result_type = reduce(find_spanning_type, types)
if result_type.is_reference:
result_type = result_type.ref_base_type
if result_type.is_pyobject: if result_type.is_pyobject:
# any specific Python type is always safe to infer # any specific Python type is always safe to infer
return result_type return result_type
......
cimport cython
cdef extern from "cpp_references_helper.h": cdef extern from "cpp_references_helper.h":
cdef int& ref_func(int&) cdef int& ref_func(int&)
cdef int ref_var_value cdef int ref_var_value
cdef int& ref_var cdef int& ref_var
def test_ref_func(int x): def test_ref_func(int x):
""" """
>>> test_ref_func(2) >>> test_ref_func(2)
...@@ -42,3 +46,15 @@ def test_ref_assign(int x): ...@@ -42,3 +46,15 @@ def test_ref_assign(int x):
""" """
cdef double d = ref_func(x) cdef double d = ref_func(x)
return d return d
@cython.infer_types(True)
def test_ref_inference(int x):
"""
>>> test_ref_inference(23)
23
>>> test_ref_inference(29)
29
"""
z = ref_func(x)
assert cython.typeof(z) == "int", cython.typeof(z)
return z
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