Skip to content

Add -vet-explicit-allocators #5081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Lperlind
Copy link
Contributor

This vet flag will make it so that allocators must be explicitly used in places where context.allocator and context.temp_allocator are a procedure parameter.

The goal of this flag is to prevent using the context.allocator in cases where a different allocator was meant to be used. Some code bases default context.allocator to nil/panic allocator to catch this at runtime. This effectively makes it a compile time error instead.

This change intentionally just targets context.temp_allocator and context.allocator rather than all runtime.Allocator default values as I feel that if an explicit value is set that is not context then some much more intentional default value was intended.

Example code:

#+vet explicit-allocators
package alloc

import "core:fmt"

foo :: proc(allocator := context.allocator) {
}

foo_temp :: proc(allocator := context.temp_allocator) {
}

main :: proc() {
    foo(context.allocator)
    foo_temp(context.allocator)
    result := fmt.aprintf("hello %v", "world", allocator = context.allocator)

    foo() // Error: Parameter 'allocator' of type 'Allocator' must be explicitly provided in procedure call
    foo_temp() // Error: Parameter 'allocator' of type 'Allocator' must be explicitly provided in procedure call
    result := fmt.aprintf("hello %v", "world") // Error: Parameter 'allocator' of type 'Allocator' must be explicitly provided in procedure call
}

This vet flag will make it so that allocators must be explicitly used
in places where context.allocator and context.temp_allocator are a
procedure parameter.

The goal of this flag is to prevent using the context.allocator in
cases where a different allocator was meant to be used.
Some code bases default context.allocator to nil/panic allocator
to catch this at runtime. This effectively makes it a compile
time error instead.
@gingerBill
Copy link
Member

The idea and implementation is good.

My issue is that -vet-explicit-allocators is pretty much never going to be a good thing in practice since loads of core library will break. So #+vet explicit-allocators is probably one of the main use cases of it.

@Lperlind Lperlind force-pushed the vet-explicit-allocators branch from c97c69f to 9163fa1 Compare April 28, 2025 09:32
@Lperlind Lperlind force-pushed the vet-explicit-allocators branch from 9163fa1 to 51d427f Compare April 28, 2025 09:33
@dozn
Copy link
Contributor

dozn commented Apr 28, 2025

The idea and implementation is good.

My issue is that -vet-explicit-allocators is pretty much never going to be a good thing in practice since loads of core library will break. So #+vet explicit-allocators is probably one of the main use cases of it.

Could it use -vet-packages:?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants