lang: types: Add stringer information manually

This lets us get the more correct lowercase versions of type kinds in
error messages. (These match what the user would type.)
This commit is contained in:
James Shubin
2024-07-01 18:35:20 -04:00
parent 28253c4bd2
commit e10e92596f
22 changed files with 82 additions and 71 deletions

View File

@@ -33,11 +33,8 @@ SHELL = /usr/bin/env bash
all: build
build: parser/lexer.nn.go parser/y.go interpolate/parse.generated.go
@# recursively run make in child dir named types
@$(MAKE) --quiet -C types
clean:
$(MAKE) --quiet -C types clean
@rm -f parser/lexer.nn.go parser/y.go parser/y.output interpolate/parse.generated.go || true
parser/lexer.nn.go: parser/lexer.nex

View File

@@ -3830,7 +3830,7 @@ func (obj *StmtProg) SetScope(scope *interfaces.Scope) error {
}
// If we don't do this deterministically the type unification errors can
// flip from `type error: Int != Str` to `type error: Str != Int` etc...
// flip from `type error: int != str` to `type error: str != int` etc...
nodeOrder, err := orderingGraph.DeterministicTopologicalSort() // sorted!
if err != nil {

View File

@@ -12,4 +12,4 @@ class c1($a, $b []str) {
}
}
-- OUTPUT --
# err: errUnify: unify error with: str("hello"): type error: List != Str
# err: errUnify: unify error with: str("hello"): type error: list != str

View File

@@ -15,4 +15,4 @@ test ["x",] {
float32 => $b,
}
-- OUTPUT --
# err: errUnify: unify error with: topLevel(func() { <built-in:_operator> }): type error: Int != Float
# err: errUnify: unify error with: topLevel(func() { <built-in:_operator> }): type error: int != float

View File

@@ -10,4 +10,4 @@ print "msg" {
msg => fmt.printf("notfn: %d", $x),
}
-- OUTPUT --
# err: errUnify: unify error with: topLevel(singleton(int(42))): type error: Func != Int
# err: errUnify: unify error with: topLevel(singleton(int(42))): type error: func != int

View File

@@ -17,4 +17,4 @@ $out2 = $add($val) # hellohello
test [fmt.printf("%s + %s is %s", $val, $val, $out2),] {} # simple concat
-- OUTPUT --
# err: errUnify: unify error with: topLevel(singleton(func(x) { call:_operator(str("+"), var(x), var(x)) })): type error: Int != Str
# err: errUnify: unify error with: topLevel(singleton(func(x) { call:_operator(str("+"), var(x), var(x)) })): type error: int != str

View File

@@ -2,4 +2,4 @@
import "fmt"
test fmt.printf("%d%d", 42) {} # should not pass, missing second int
-- OUTPUT --
# err: errUnify: unify error with: call:fmt.printf(str("%d%d"), int(42)): type error: Str != List
# err: errUnify: unify error with: call:fmt.printf(str("%d%d"), int(42)): type error: str != list

View File

@@ -3,4 +3,4 @@ test "t1" {
stringptr => 42, # int, not str
}
-- OUTPUT --
# err: errUnify: unify error with: int(42): type error: Str != Int
# err: errUnify: unify error with: int(42): type error: str != int

View File

@@ -10,4 +10,4 @@ test "${name}" {}
# TODO: I would expect that if the "%s" and "%d" swapped, that speculatively we
# would be able to run this at compile time and know the result statically.
-- OUTPUT --
# err: errStream: func `printf@??????????` stopped before it was loaded: base kind does not match (Str != Int)
# err: errStream: func `printf@??????????` stopped before it was loaded: base kind does not match (str != int)

View File

@@ -10,4 +10,4 @@ test "test2" {
anotherstr => $id("hello"),
}
-- OUTPUT --
# err: errUnify: unify error with: topLevel(singleton(func(x) { var(x) })): type error: Int != Str
# err: errUnify: unify error with: topLevel(singleton(func(x) { var(x) })): type error: int != str

View File

@@ -12,4 +12,4 @@ class use_polymorphically($id) {
}
include use_polymorphically(func($x) {$x})
-- OUTPUT --
# err: errUnify: unify error with: topLevel(singleton(func(x) { var(x) })): type error: Int != Str
# err: errUnify: unify error with: topLevel(singleton(func(x) { var(x) })): type error: int != str

View File

@@ -10,4 +10,4 @@ test "test1" {
anotherstr => use_polymorphically(func($x) {$x}),
}
-- OUTPUT --
# err: errUnify: unify error with: param(id): type error: Int != Str
# err: errUnify: unify error with: param(id): type error: int != str

View File

@@ -10,4 +10,4 @@ test "test1" {
anotherstr => $use_polymorphically(func($x) {$x}),
}
-- OUTPUT --
# err: errUnify: unify error with: param(id): type error: Int != Str
# err: errUnify: unify error with: param(id): type error: int != str

View File

@@ -7,4 +7,4 @@ test "test" {}
Test["${name}"] -> Test["test"] # must fail
-- OUTPUT --
# err: errUnify: unify error with: var(name): type error: Str != List
# err: errUnify: unify error with: var(name): type error: str != list

View File

@@ -7,4 +7,4 @@ test "test" {}
Test["test"] -> Test["${name}"] # must fail
-- OUTPUT --
# err: errUnify: unify error with: var(name): type error: Str != List
# err: errUnify: unify error with: var(name): type error: str != list

View File

@@ -5,4 +5,4 @@ $name = ["a", "bb", "ccc",]
test "${name}" {} # must fail
-- OUTPUT --
# err: errUnify: unify error with: var(name): type error: Str != List
# err: errUnify: unify error with: var(name): type error: str != list

View File

@@ -75,4 +75,4 @@ $foo = iter.map([$id1, $id2,], $generate)
$name = $foo[0] || "fail"
test "${name}" {}
-- OUTPUT --
# err: errUnify: unify error with: param(idn): type error: Str != Int
# err: errUnify: unify error with: param(idn): type error: str != int

View File

@@ -1,48 +0,0 @@
# Mgmt
# Copyright (C) 2013-2024+ James Shubin and the project contributors
# Written by James Shubin <james@shubin.ca> and the project contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Additional permission under GNU GPL version 3 section 7
#
# If you modify this program, or any covered work, by linking or combining it
# with embedded mcl code and modules (and that the embedded mcl code and
# modules which link with this program, contain a copy of their source code in
# the authoritative form) containing parts covered by the terms of any other
# license, the licensors of this program grant you additional permission to
# convey the resulting work. Furthermore, the licensors of this program grant
# the original author, James Shubin, additional permission to update this
# additional permission if he deems it necessary to achieve the goals of this
# additional permission.
SHELL = /usr/bin/env bash
.PHONY: all build clean
all: build
build: kind_stringer.go
clean:
@rm -f kind_stringer.go || true
kind_stringer.go: type.go
@echo "Generating: type kind strings..."
@# stringer fails if run from GOROOT.
@# see: https://github.com/golang/go/issues/31843
@if which stringer | grep `go env GOROOT`; then \
echo "stringer cannot run from GOROOT"; \
exit 1; \
fi
@go generate

64
lang/types/stringer.go Normal file
View File

@@ -0,0 +1,64 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package types
// String returns a representation for the kind. These are the strings seen in
// most type unification errors. We don't use stringer since it would capitalize
// them.
func (obj Kind) String() string {
switch obj {
case KindNil:
return "nil"
case KindBool:
return "bool"
case KindStr:
return "str"
case KindInt:
return "int"
case KindFloat:
return "float"
case KindList:
return "list"
case KindMap:
return "map"
case KindStruct:
return "struct"
case KindFunc:
return "func"
case KindVariant:
return "variant"
case KindUnification:
return "unification"
default:
panic("unknown kind")
}
}

View File

@@ -59,13 +59,12 @@ var (
TypeVariant = NewType("variant")
)
//go:generate stringer -type=Kind -trimprefix=Kind -output=kind_stringer.go
// The Kind represents the base type of each value.
type Kind int // this used to be called Type
// Each Kind represents a type in the language type system.
const (
// NOTE: Make sure you add entries to stringer.go if you add something.
KindNil Kind = iota
KindBool
KindStr

View File

@@ -27,7 +27,7 @@ if [ "$COMMITS" != "" ] && [ "$COMMITS" -gt "1" ]; then
fi
# find all go files, exluding temporary directories and generated files
LINT=$(find * -maxdepth 9 -iname '*.go' -not -path 'old/*' -not -path 'tmp/*' -not -path 'lang/parser/y.go' -not -path 'lang/parser/lexer.nn.go' -not -path 'lang/interpolate/parse.generated.go' -not -path 'lang/types/kind_stringer.go' -not -path 'vendor/*' -exec golint {} \;) # current golint output
LINT=$(find * -maxdepth 9 -iname '*.go' -not -path 'old/*' -not -path 'tmp/*' -not -path 'lang/parser/y.go' -not -path 'lang/parser/lexer.nn.go' -not -path 'lang/interpolate/parse.generated.go' -not -path 'vendor/*' -exec golint {} \;) # current golint output
COUNT=`echo -e "$LINT" | wc -l` # number of golint problems in current branch
[ "$LINT" = "" ] && echo PASS && exit # everything is "perfect"

View File

@@ -47,7 +47,6 @@ gml="$gml --enable=misspell"
# TODO: at least until https://github.com/alecthomas/gometalinter/issues/270
gml="$gml --exclude=lang/parser/lexer.nn.go"
gml="$gml --exclude=lang/parser/y.go"
gml="$gml --exclude=lang/types/kind_stringer.go"
gml="$gml --exclude=lang/interpolate/parse.generated.go"
gometalinter="$gml" # final