typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

Replace GetSymbolTable and derivatives with GetOrInit method

Open jakebailey opened this issue 11 months ago • 0 comments

A new method:

func (s *SymbolTable) GetOrInit() SymbolTable {
	if *s == nil {
		*s = make(SymbolTable)
	}
	return *s
}

Operates on the address of a symbol table; when calling it on a SymbolTable, usually on a struct field, Go will take the address of whatever SymbolTable it's accessing, allowing this method to mutate it.

This makes it clearer that this operation is a mutating one, unsafe for concurrent use; if you need to find all modifiers, you can just look for anyone assigning to SymbolTable or calling this method. (It also means you can find-references on them again.)

In terms of performance, it inlines; godbolt pegs it as two instructions (it probably was before).

In adding this, I've vetted everything that used to use the old methods; the binder is of course fine. The checker seemed suspect when I was fixing races previously, but it's not, as the modifications are only on merged symbols (which are either clones or transient symbols local to a checker), or were being called on SymbolTable fields on types, which are also local to a checker.

jakebailey avatar Jan 20 '25 19:01 jakebailey