|
|
|
|
|
by 1718627440
262 days ago
|
|
> That's one of the things named lifetimes let you do, but named lifetimes are flexible enough for more than just that use case, from slightly more complex things like dealing with multiple independent lifetimes at the same time (for example, returning two references instead of just one, or structs/functions using multiple lifetimes), to more obscure stuff like dealing with higher-ranked trait bounds [1]. In SPlint you would describe something as `special` and then describe the individual elements. > Kind of a tangent, but the manual is a bit unclear to me as to what `returned` is capable of. Aliasing is mutual and storage refers to the region an object lives. It doesn't refer to exact pointer values. > Would that be accepted as well? $ cat test.c
int *
max (/*@returned@*/ int * a, /*@returned@*/ int * b) {
if (*a > *b) return a; else return b;
}
int
main (void)
{
int * a = malloc (sizeof *a);
int * b = malloc (sizeof *b);
int * c;
if (!a || !b) abort ();
*a = 4;
*b = 6;
c = max (a, b);
free (a);
free (b);
free (c);
return 0;
}
$ splint test.c
Splint 3.1.1 --- 05 Jan 2023
Finished checking --- 2 code warnings
test.c: (in function main)
test.c:22:8: Dead storage c passed as out parameter to free: c
Memory is used after it has been released (either by passing as an only param
or assigning to an only global). (Use -usereleased to inhibit warning)
test.c:20:8: Storage c released
test.c:2:1: Function exported but not used outside test: max
A declaration is exported, but not used outside this module. Declaration can
use static qualifier. (Use -exportlocal to inhibit warning)
test.c:4:1: Definition of max
|
|
Interesting. That does seem to provide more flexibility, though based on what the manual says I still feel like it's not quite to the same level as named lifetimes since `special` looks like it revolves around allocation/initialization?
At least based on a quick skim of the manual structs with non-owning pointers still seems like a potential difference since from what I can tell struct field checks are either for ownership (/@only@/ for fields by default), initialization/allocation (`partial`, state clauses), or requires overhead (/@refs@/). Nothing quite like "the data here will live for some arbitrary lifetime(s) dictated by the use context".
> Aliasing is mutual and storage refers to the region an object lives. It doesn't refer to exact pointer values.
I... think that answers my question? Some quick tests seem to bear that out as well.
> $ cat test.c
So I think I'm a bit dim and for some reason I thought Splint was not free. Sorry for the bother! I could have tried things out myself this entire time!
Good to see that /@returned@/ works like I hoped at least.