Why throw away a dereferencing value in GO waitgroup.wait()?

Viewed 58

In the package 'waitgroup' the method Wait() seems to throw away a dereference to 'statep' with the comment "//trigger nil deref early"

Edit* Why do we trigger a nil deref early?

func (wg *WaitGroup) Wait() {
    statep, semap := wg.state()
    if race.Enabled {
        _ = *statep // trigger nil deref early
        race.Disable()
    }
    
1 Answers

The statement _ = *statep triggers a nil pointer panic when statep is nil.

The purpose of the statement is to simplify the later code in the function. The call to race.Disable() must be balanced with a call to race.Enable(). The code does not need to consider the possibility of a nil pointer panic when balancing the calls.

Related