Linked lists how to ?
With the latest changes and looking at the output of applying -analyze over cake sources we have this:
src/token.c
src/token.c:107:18: warning: passing a view argument to a owner parameter [-W]
107 | token_delete(p);
| ^
src/token.c:150:12: warning: cannot assign a non owner to owner [-W]
150 | return p;
| ^
src/token.c:150:12: warning: returning a non owner variable to a owner [-W]
150 | return p;
| ^
This is somehow one of main examples people use to critic rust borrow checker.
How this can be solved with cake ?
For the list_pop_back I found that this seems to work:
void token_list_pop_back(struct token_list* list) unchecked
{
if (list->head == NULL)
return;
if (list->head == list->tail)
{
token_delete(list->head);
list->head = NULL;
list->tail = NULL;
}
else
{
struct token* owner p = list->tail->prev->next;
list->tail = p->prev;
list->tail->next = NULL;
if (list->tail == list->head)
{
list->tail->prev = NULL;
}
token_delete(p);
}
}
And here is how I found to solve list_pop_front_get:
struct token* owner token_list_pop_front_get(struct token_list* list) unchecked
{
if (list->head == NULL)
return NULL;
struct token* owner p = list->head;
if (list->head == list->tail)
{
list->head = NULL;
list->tail = NULL;
}
else
{
list->head = p->next;
}
p->next = NULL;
p->prev = NULL;
return p;
}
But here cake should emmit an error and it is not doing it on the code shown bellow, when accessing list->head->next after struct token* owner p = list->head; should be an error.
void token_list_pop_front(struct token_list* list) unchecked
{
if (list->head == NULL)
return;
struct token* owner p = list->head;
if (list->head == list->tail)
{
list->head = NULL;
list->tail = NULL;
}
else
{
list->head = list->head->next; ///!!!!! here `list->head->next` should be an error
}
p->next = NULL;
p->prev = NULL;
token_delete(p);
}
Fixed code:
void token_list_pop_front(struct token_list* list) unchecked
{
if (list->head == NULL)
return;
struct token* owner p = list->head;
if (list->head == list->tail)
{
list->head = NULL;
list->tail = NULL;
}
else
{
list->head = p->next;
}
p->next = NULL;
p->prev = NULL;
token_delete(p);
}
In my last checkin all errors that are not described as "constrain" in the C standard are now user controlled.
Users will decide for each message if it is an error, warning or note or ignored.
There is a regression with (unchecked).
The idea of unchecked is to ignore ownership errors at the function body. We also have an alternative using pragma warning but it is not ready yet. Then unchecked can be removed.this is a design question..
On Thu, 22 Feb 2024 at 08:06, Domingo Alvarez Duarte < @.***> wrote:
But here cake should emmit an error and it is not doing it on the code shown bellow, when accessing list->head->next after struct token* owner p = list->head; should be an error.
void token_list_pop_front(struct token_list* list) unchecked { if (list->head == NULL) return;
struct token* owner p = list->head; if (list->head == list->tail) { list->head = NULL; list->tail = NULL; } else { list->head = list->head->next; ///!!!!! here `list->head->next` should be an error } p->next = NULL; p->prev = NULL; token_delete(p);}
Fixed code:
void token_list_pop_front(struct token_list* list) unchecked { if (list->head == NULL) return;
struct token* owner p = list->head; if (list->head == list->tail) { list->head = NULL; list->tail = NULL; } else { list->head = p->next; } p->next = NULL; p->prev = NULL; token_delete(p);}
— Reply to this email directly, view it on GitHub https://github.com/thradams/cake/issues/113#issuecomment-1959211961, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABSMZLRACRG4HRF22GFIPJDYU4RDZAVCNFSM6AAAAABDUV4OFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNJZGIYTCOJWGE . You are receiving this because you are subscribed to this thread.Message ID: @.***>
-- www.thradams.com
flow analysis requires we store the state of pointed objects. a->b->c->d It has a limit because otherwise it can go forever. (imagine next in a linked list)
I think the bug list->head->next can be caused because of this limit . Maybe it can be extended. The way states are stored also could be refactored to not have everything just used ones. the ones not used have a default value .
I want to review the linked list separately because it is easy to have bugs. there is a sample in cake about linked list I also want to update de sample. sample are at samples.js at web folder.
On Thu, 22 Feb 2024 at 07:36, Domingo Alvarez Duarte < @.***> wrote:
With the latest changes and looking at the output of applying -analyze over cake sources we have this:
src/token.c src/token.c:107:18: warning: passing a view argument to a owner parameter [-W] 107 | token_delete(p); | ^ src/token.c:150:12: warning: cannot assign a non owner to owner [-W] 150 | return p; | ^ src/token.c:150:12: warning: returning a non owner variable to a owner [-W] 150 | return p; | ^
This is somehow one of main examples people use to critic rust borrow checker. How this can be solved with cake ?
— Reply to this email directly, view it on GitHub https://github.com/thradams/cake/issues/113, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABSMZLUH3RGHBCVSIWQJHTDYU4NTJAVCNFSM6AAAAABDUV4OFGVHI2DSMVQWIX3LMV43ASLTON2WKOZSGE2DQNZUHAZTGMA . You are receiving this because you are subscribed to this thread.Message ID: @.***>
-- www.thradams.com