Skip to content

Commit

Permalink
Fix missing Position in reported error.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Mar 11, 2023
1 parent 22e6ecc commit 803c965
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
17 changes: 9 additions & 8 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import (
// inexhaustiveError is returned from check for each occurrence of inexhaustive
// case analysis in a Go type switch statement.
type inexhaustiveError struct {
Pos token.Position
Def sumTypeDef
Missing []types.Object
Position token.Position
Def sumTypeDef
Missing []types.Object
}

func (e inexhaustiveError) Pos() token.Position { return e.Position }
func (e inexhaustiveError) Error() string {
return fmt.Sprintf(
"%s: exhaustiveness check failed for sum type '%s': missing cases for %s",
e.Pos, e.Def.Decl.TypeName, strings.Join(e.Names(), ", "))
"%s: exhaustiveness check failed for sum type %q (from %s): missing cases for %s",
e.Pos(), e.Def.Decl.TypeName, e.Def.Decl.Pos, strings.Join(e.Names(), ", "))
}

// Names returns a sorted list of names corresponding to the missing variant
Expand Down Expand Up @@ -70,9 +71,9 @@ func checkSwitch(
def, missing := missingVariantsInSwitch(pkg, defs, swtch)
if len(missing) > 0 {
return inexhaustiveError{
Pos: pkg.Fset.Position(swtch.Pos()),
Def: *def,
Missing: missing,
Position: pkg.Fset.Position(swtch.Pos()),
Def: *def,
Missing: missing,
}
}
return nil
Expand Down
1 change: 1 addition & 0 deletions decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func findSumTypeDecls(pkgs []*packages.Package) ([]sumTypeDecl, error) {
retErr = notFoundError{Decl: sumTypeDecl{Package: pkg, Pos: pos}}
return false
}
pos = pkg.Fset.Position(tspec.Pos())
decl := sumTypeDecl{Package: pkg, TypeName: tspec.Name.Name, Pos: pos}
decls = append(decls, decl)
break
Expand Down

0 comments on commit 803c965

Please sign in to comment.