Skip to content

cmd/compile: compiler error for valid code #75069

Closed as not planned
Closed as not planned

Description

@Jorropo

reported by @ivanjaros in #72860 (comment)

I just updated my Go version to 1.25 and I immediately got compilation error for the following code - which is correct and should compile, but this new change makes it fail:

func RetrieveOauthCodeGrantHandler(ctx context.Context, req RetrieveOauthCodeGrantRequest) (res *RetrieveOauthCodeGrantResponse, err error) {
	errOut := func(errType, info string) {
		res = &RetrieveOauthCodeGrantResponse{
			Failure: &RetrieveOauthCodeGrantResponse_Failure{
				Error:            errType,
				ErrorDescription: info,
				ErrorURI:         "",
			},
		}
		err = nil
	}

	if err := req.Validate(); err != nil {
		errOut("invalid_request", errors.String(err))
		return
	}

	...
}

I had to rename the variable to e:

	if e := req.Validate(); e != nil {
		errOut("invalid_request", errors.String(e))
		return
	}

I think this change has not been implemented correctly if this is not compiling. I usually don't write my code like this but this is a special case where it had to be done like this and i caught it right away by accident(as I usually don't write code like this).

Activity

  1. Jorropo commented on Aug 18, 2025

    @Jorropo
    MemberAuthor

    cc @ivanjaros can you please post the compiler error and ideally a repro thanks ?

  2. added
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    BugReportIssues describing a possible bug in the Go implementation.
    on Aug 18, 2025
  3. ivanjaros commented on Aug 18, 2025

    @ivanjaros

    The error is:

    	if err := req.Validate(); err != nil { // 2dn error line: "inner declaration of var err error"
    		errOut("invalid_request", errors.String(err))
    		return // 1st error line: "result parameter err not in scope at return"
    	}
    

    The repo is private but there is nothing special. It's just those few lines of code I posted above, which you have shared.

    I doubt it needs to be explicitly stated but for the sake of clarity, note that this is because of/in the context of named return values.

  4. Jorropo commented on Aug 18, 2025

    @Jorropo
    MemberAuthor

    The error looks correct, if err := ... shadows err (the named return) so when you return the named return variable is not in scope (given it is shadowed).
    I don't know why it used to compile before, I've tried making a small repro but 1.23 and 1.24 also compile error:

    func f() (b, a int) {
    	af := func(b int) { a = b }
    
    	if a := 42; a == 42 {
    		af(a)
    		return
    	}
    	return 42, 0
    }
  5. TapirLiu commented on Aug 18, 2025

    @TapirLiu
    Contributor

    Looks like #54019. IMHO, compilers should not do vet jobs.

    @ivanjaros @Jorropo Can you confirm that the code compiles before Go 1.25?

  6. ianlancetaylor commented on Aug 18, 2025

    @ianlancetaylor
    Contributor

    Note that this is mentioned in the language spec. It's the "implementation restriction" at https://go.dev/ref/spec#Return_statements.

  7. Jorropo commented on Aug 18, 2025

    @Jorropo
    MemberAuthor

    @TapirLiu I'm not sure what you want me to do, I don't have access to the code

    The repo is private

    and I am not able to guess what magic incantation made 1.24 accept this as valid code #75069 (comment)

  8. removed
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    on Aug 18, 2025
  9. TapirLiu commented on Aug 18, 2025

    @TapirLiu
    Contributor

    Okay, then @ivanjaros solely again.

  10. ivanjaros commented on Aug 18, 2025

    @ivanjaros

    There is no magic here guys. It's very simple.

    The function has "err" as named return value, ie func foo() (err error).

    Inside, there is basic function within the scope of foo that simply mutates the return value, as it has access to it.

    func foo() (err error) {
      bar := func(someVarName string) {
        err = errors.New(someVarName) // <-- this mutates the return value of foo as it does not declare a new local err variable
      }
    
      bar("ooops")
      return
    }

    The code above will simply return error with value "ooops".

    Now we can expand this and use an if block, as in the original issue:

    func foo() (err error) {
      bar := func(someVarName string) {
        err = errors.New(someVarName) // <-- this mutates the return value of foo as it does not declare a new local err variable
      }
    
      // right here, before this block, the err is still referencing the foo's return value
      if err := baz(); err != nil {
        // but in here, the err is from the if block and the foo's error is not being referenced any more
        bar(err.Error()) // this will call bar() which will mutate the foo's err and not this block's err, as they are completely separate contexts
        return
      }
    
      // do something else...
      return
    }

    It is no different than something like this, except of an if block, it is a for loop block:

    func foo() (err error) {
      bar := func(someVarName string) {
        err = errors.New(someVarName)
      }
    
      for i := 0; i < 10; i++ {
        err := baz()
        if err != nil {
          bar(err.Error())
          return // or break, same result
        }
      }
    
      return
    }

    If we translate this into a version without named return values, the compilation error makes perfect sense:

    func foo() error {
      var err error
      bar := func(someVarName string) {
        err = errors.New(someVarName) // mutates the error variable declared above
      }
    
      if err := baz(); err != nil {
        // like before, we are declaring new variable with the same name as is in the scope of foo, outside of this block 
        bar(err.Error()) // mutates the "original" error
        return err // this returns the error from this block statement and not the original err as we have overwritten its declaration in this code block. this is why the compiler is having trouble, it needs to distinguish whether the function has named return values or not
      }
    
      // do something else...
      return err // returns the original error variable
    }

    As said in the last example, the problem here is that the compiler, with this new patch, is making no distinction between a function with named return value and one without.

  11. ianlancetaylor commented on Aug 18, 2025

    @ianlancetaylor
    Contributor

    @ivanjaros Please show us a complete, standalone, program that demonstrates the problem. We are looking for a program that compiles and run successfully with old Go versions, and fails to compile with Go 1.25.

    As far as I can tell, the partial programs that you are showing us will fail to compile with all Go versions. In order to find this bug, we need you to show us a complete program that compiles with old Go versions but not with Go 1.25.

    Thanks.

  12. ivanjaros commented on Aug 19, 2025

    @ivanjaros

    I tried to make a working example via go.dev, which has go 1.24 and 1.23, and both failed to compile. So it looks like I was mistaken and I had tried to compile code, after updating to go 1.25, which i have not compiled with previous version beforehand and that gave me the impression that this patch was not working in all cases. The code and behavior seemed perfectly logical, but obviously it was a user error.

  13. TapirLiu commented on Aug 19, 2025

    @TapirLiu
    Contributor

    The code and behavior seemed perfectly logical

    Not much. Even @ianlancetaylor was surprised initially. :D

    It is consistent though, since Go 1.0.

  14. locked and limited conversation to collaborators on Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.FrozenDueToAgecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions