aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2024-10-12 10:33:24 +0200
committerAyke <[email protected]>2024-10-18 17:43:17 +0200
commitdf724f5827e2b96d53a708d642ee52798506915c (patch)
tree186743b9f6e7b33b1840b2b848ba4e30268baff1
parent539cc5c47bb1bc46bcaf663d8f963670a7fbb111 (diff)
downloadtinygo-df724f5827e2b96d53a708d642ee52798506915c.tar.gz
tinygo-df724f5827e2b96d53a708d642ee52798506915c.zip
loader: don't panic when main package is not named 'main'
This can in fact happen in practice, so return an actual error message instead.
-rw-r--r--errors_test.go1
-rw-r--r--loader/loader.go8
-rw-r--r--testdata/errors/invalidmain.go6
3 files changed, 13 insertions, 2 deletions
diff --git a/errors_test.go b/errors_test.go
index 62d5af2cb..5045840ff 100644
--- a/errors_test.go
+++ b/errors_test.go
@@ -24,6 +24,7 @@ func TestErrors(t *testing.T) {
{name: "cgo"},
{name: "compiler"},
{name: "interp"},
+ {name: "invalidmain"},
{name: "linker-flashoverflow", target: "cortex-m-qemu"},
{name: "linker-ramoverflow", target: "cortex-m-qemu"},
{name: "linker-undefined", target: "darwin/arm64"},
diff --git a/loader/loader.go b/loader/loader.go
index d10485707..6786ee533 100644
--- a/loader/loader.go
+++ b/loader/loader.go
@@ -418,8 +418,12 @@ func (p *Package) Check() error {
packageName := p.ImportPath
if p == p.program.MainPkg() {
if p.Name != "main" {
- // Sanity check. Should not ever trigger.
- panic("expected main package to have name 'main'")
+ return Errors{p, []error{
+ scanner.Error{
+ Pos: p.program.fset.Position(p.Files[0].Name.Pos()),
+ Msg: fmt.Sprintf("expected main package to have name \"main\", not %#v", p.Name),
+ },
+ }}
}
packageName = "main"
}
diff --git a/testdata/errors/invalidmain.go b/testdata/errors/invalidmain.go
new file mode 100644
index 000000000..a86e32c8d
--- /dev/null
+++ b/testdata/errors/invalidmain.go
@@ -0,0 +1,6 @@
+// some comment to move the first line
+
+package foobar
+
+// ERROR: # command-line-arguments
+// ERROR: invalidmain.go:3:9: expected main package to have name "main", not "foobar"