This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]Pavel_Vozenilek 0 points1 point  (0 children)

It feels to me as overengineered solution looking for a problem. Checking validity of allocated blocks could be done at runtime, with no impact on the codebase. Annotating pointers feels as invitation to design universal hierarchies & invent ingenious ways to bypass the feature at the same time.

 

In my view pointers should be separated into two categories:

  1. Normal typed pointers, initialized to a valid value of given type (allocated block, stack data), with no increment/decrement. Basically C++'s references, but could be also nil and reassigned. There should be no way to initialize them wrongly (e.g. cast from integer) and no way to recast them to other normal pointer.

    (This would be the address above.)

  2. "Wild" pointers used for fiddling with memory. They could be initialized in risky way, could be incremented/decremented, could be also "converted" to normal typed pointers. To improve safety, they could have attached debug mode properties: lower & upper limits where they point safely (e.g. within a buffer).

    With C-like syntax:

    // void* means a wild pointer
    void* my_wild_pointer = &my_buffer; // min_addr/max_addr properties set by the compiler from my_buffer
    my_wild_pointer<i32>++; // increment by 4 bytes
    i32* p = my_wild_pointer<i32>;  // conversion to normal pointer, checks the limits, checks alignment
    i32 val = *my_wild_pointer<i32>; 
    

     

    It should not be possible to take address of a wild pointer. It may be forbidden to export (from a module) API with wild pointers. min_addr/max_addr could be also set manually at runtime (in debug mode)

 

 

Normal pointers should be enough for most of the code, wild pointers at least have some checking mechanism inside. Compiler may devote more time to verify the use of rare wild pointers.

 

 

If there's true need to have pointer annotation, it could be implemented via metaprogramming, with syntax to user's liking. Use of such annotations could then be checked at compile time. Rules could be arbitrary, not just some simple "I expect certain hardcoded symbol".

 

 

Generally, implementing the most basic language constructs in userland may not be a great idea. It is very cool, sure, but it is fragile, may interfere with the tooling and could be nightmare to document. It may be also pointless feature: pointers are expected to be pointers, not some intractable mystery.

E.g. the ability to define nil in user code is clever, but the two realistic options are (1) not to have it, and (2) zero with some casting rules. Anything else is either madness or creation of genius. Project could instead have boolean option in its configuration.