Commit 3d3712c0 authored by Sergio Costas's avatar Sergio Costas

Doesn't return an error when returning NULL as borrowed

Until now, if the return value is marked as __crust_borrow__, crust
shown an error saying that a non-borrowed value is being returned.

This patch fixes this, because it is obvious that it must be possible
to return NULL in these cases.
parent af79c6b7
# History of versions # # History of versions #
* version 0.7 (2018-12-24)
* Now doesn't return an error when returning NULL as borrowed
* version 0.6 (2018-10-03) * version 0.6 (2018-10-03)
* Fixed case when a switch/case block lacks a 'default' statement * Fixed case when a switch/case block lacks a 'default' statement
......
...@@ -983,7 +983,7 @@ class crust(crust_helpers): ...@@ -983,7 +983,7 @@ class crust(crust_helpers):
return_is_borrowed = False return_is_borrowed = False
else: else:
return_is_borrowed = var_data["borrowed"] return_is_borrowed = var_data["borrowed"]
if tmp_status["return_is_borrowed"] and (not return_is_borrowed): if tmp_status["return_is_borrowed"] and (not return_is_borrowed) and (not node.child1[0].t_void):
self._add_error(tmp_status, self.MSG_ERROR, "Function '{:s}' expects a borrowed value to return, but a non-borrowed was used at line {:d}", tmp_status["current_function"], node.line) self._add_error(tmp_status, self.MSG_ERROR, "Function '{:s}' expects a borrowed value to return, but a non-borrowed was used at line {:d}", tmp_status["current_function"], node.line)
if (not tmp_status["return_is_borrowed"]) and return_is_borrowed: if (not tmp_status["return_is_borrowed"]) and return_is_borrowed:
self._add_error(tmp_status, self.MSG_ERROR, "Function '{:s}' expects a non-borrowed value to return, but a borrowed was used at line {:d}", tmp_status["current_function"], node.line) self._add_error(tmp_status, self.MSG_ERROR, "Function '{:s}' expects a non-borrowed value to return, but a borrowed was used at line {:d}", tmp_status["current_function"], node.line)
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
class crust_definitions(object): class crust_definitions(object):
version = "0.6" version = "0.7"
# when a variable hasn't been assigned yet # when a variable hasn't been assigned yet
VALUE_UNINITIALIZED = 0 VALUE_UNINITIALIZED = 0
......
...@@ -1030,6 +1030,9 @@ class Test(unittest.TestCase): ...@@ -1030,6 +1030,9 @@ class Test(unittest.TestCase):
def test252switchWithoutDefault(self): def test252switchWithoutDefault(self):
self._generic_test("unitest/test252.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", 'param', 5, 19) ]) self._generic_test("unitest/test252.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", 'param', 5, 19) ])
def test253ReturnNULLBorrowed(self):
self._all_fine_test("unitest/test253.c")
if __name__ == '__main__': if __name__ == '__main__':
try: try:
os.remove("error_list.txt") os.remove("error_list.txt")
......
typedef __crust__ unsigned char *crust_t;
#define NULL ((void *)0);
__crust_borrow__ crust_t function(void) {
return NULL; // is valid
}
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