From: Robert Bradshaw Date: Sun, 21 Feb 2010 10:11:21 +0000 (-0800) Subject: Reference type inference X-Git-Tag: 0.13.beta0~328 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=026e3de9584ec60457222fcd97131dad8dac46ac;p=cython.git Reference type inference --- diff --git a/Cython/Compiler/TypeInference.py b/Cython/Compiler/TypeInference.py index 8c10bcd3..6d33a7f8 100644 --- a/Cython/Compiler/TypeInference.py +++ b/Cython/Compiler/TypeInference.py @@ -276,10 +276,14 @@ def find_spanning_type(type1, type2): def aggressive_spanning_type(types, might_overflow): result_type = reduce(find_spanning_type, types) + if result_type.is_reference: + result_type = result_type.ref_base_type return result_type def safe_spanning_type(types, might_overflow): result_type = reduce(find_spanning_type, types) + if result_type.is_reference: + result_type = result_type.ref_base_type if result_type.is_pyobject: # any specific Python type is always safe to infer return result_type diff --git a/tests/wrappers/cpp_references.pyx b/tests/wrappers/cpp_references.pyx index bf53a2e2..a4c2edd2 100644 --- a/tests/wrappers/cpp_references.pyx +++ b/tests/wrappers/cpp_references.pyx @@ -1,9 +1,13 @@ +cimport cython + + cdef extern from "cpp_references_helper.h": cdef int& ref_func(int&) cdef int ref_var_value cdef int& ref_var + def test_ref_func(int x): """ >>> test_ref_func(2) @@ -42,3 +46,15 @@ def test_ref_assign(int x): """ cdef double d = ref_func(x) 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