From 026e3de9584ec60457222fcd97131dad8dac46ac Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Sun, 21 Feb 2010 02:11:21 -0800 Subject: [PATCH] Reference type inference --- Cython/Compiler/TypeInference.py | 4 ++++ tests/wrappers/cpp_references.pyx | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) 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 -- 2.26.2