]> Cypherpunks repositories - gostls13.git/commitdiff
doc: document change in nil-ptr checking behavior
authorKeith Randall <khr@golang.org>
Tue, 18 Mar 2025 21:04:26 +0000 (14:04 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 18 Mar 2025 22:22:33 +0000 (15:22 -0700)
This could bite people during the 1.25 release, so make sure it
has good documentation in the release notes.

Update #72860

Change-Id: Ie9aaa219025a631e81ebc48461555c5fb898f43f
Reviewed-on: https://go-review.googlesource.com/c/go/+/658955
Reviewed-by: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

doc/next/5-toolchain.md

index 971fa39608e35249354be26431c1a27675ad78ef..c4d4744168d7804590dcbc3eb58f4fe3e609ca5b 100644 (file)
@@ -9,6 +9,35 @@ information in Go binaries.
 DWARF 5 generation is gated by the "dwarf5" GOEXPERIMENT; this
 functionality can be disabled (for now) using GOEXPERIMENT=nodwarf5.
 
+<!-- https://go.dev/issue/72860, CL 657715 -->
+
+The compiler [has been fixed](/cl/657715)
+to ensure that nil pointer checks are performed promptly. Programs like the following,
+which used to execute successfully, will now panic with a nil-pointer exception:
+
+```
+package main
+
+import "os"
+
+func main() {
+       f, err := os.Open("nonExistentFile")
+       name := f.Name()
+       if err != nil {
+               return
+       }
+       println(name)
+}
+```
+
+This program is incorrect in that it uses the result of `os.Open` before checking
+the error. The main result of `os.Open` can be a nil pointer if the error result is non-nil.
+But because of [a compiler bug](/issue/72860), this program ran successfully under
+Go versions 1.21 through 1.24 (in violation of the Go spec). It will no longer run
+successfully in Go 1.25. If this change is affecting your code, the solution is to put
+the non-nil error check earlier in your code, preferrably immediately after
+the error-generating statement.
+
 ## Assembler {#assembler}
 
 ## Linker {#linker}