Skip to content
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

Replace GetSymbolTable and derivatives with GetOrInit method #240

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

jakebailey
Copy link
Member

@jakebailey jakebailey commented Jan 20, 2025

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 jakebailey requested a review from ahejlsberg January 20, 2025 19:57
@jakebailey jakebailey requested a review from Copilot February 13, 2025 00:08
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (3)

internal/binder/binder.go:356

  • [nitpick] The name getMembers might be ambiguous. Consider renaming it to something more descriptive, such as getOrInitMembers.
func getMembers(symbol *ast.Symbol) ast.SymbolTable {

internal/binder/binder.go:360

  • [nitpick] The name getExports might be ambiguous. Consider renaming it to something more descriptive, such as getOrInitExports.
func getExports(symbol *ast.Symbol) ast.SymbolTable {

internal/binder/binder.go:364

  • [nitpick] The name getLocals might be ambiguous. Consider renaming it to something more descriptive, such as getOrInitLocals.
func getLocals(container *ast.Node) ast.SymbolTable {

@jakebailey jakebailey requested a review from Copilot February 13, 2025 00:14
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

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.

1 participant