Skip to content

cmd/compile: function compiles OK with no return statement #49003

Closed

Description

@rogpeppe

commit 1cbec68

The following program compiles and runs, but it should complain that there's no return statement in f:

package main

func main() {
	_ = f("")
}

func f(s string) string {
	for range s {
	}
}

Activity

  1. added this to the Go1.18 milestone on Oct 15, 2021
  2. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Oct 15, 2021
  3. randall77 commented on Oct 15, 2021

    @randall77
    Contributor

    This is kind of a corner case in the definition of terminating statements.

    A "for" statement in which:
    there are no "break" statements referring to the "for" statement, and
    the loop condition is absent.

    Technically that for statement qualifies, so no return after it is needed. There should maybe be an exception for a for range statement?

    go1.17 gets this correct (it gives an error).

  4. gopherbot commented on Oct 15, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/356189 mentions this issue: cmd/compile: make for loops with range statements terminating

  5. griesemer commented on Oct 15, 2021

    @griesemer
    Contributor

    Nice find!

  6. gopherbot commented on Oct 16, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/356411 mentions this issue: go/types, types2: add test case for missing return

  7. locked and limited conversation to collaborators on Oct 16, 2022
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

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.release-blocker

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions