shader: Check the existence of 'return'

This commit is contained in:
Hajime Hoshi 2020-09-06 22:33:19 +09:00
parent 0830e897fc
commit 4308bbbc31
4 changed files with 45 additions and 0 deletions

View File

@ -645,6 +645,28 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool
return function{}, false
}
if len(outParams) > 0 {
var hasReturn func(stmts []shaderir.Stmt) bool
hasReturn = func(stmts []shaderir.Stmt) bool {
for _, stmt := range stmts {
if stmt.Type == shaderir.Return {
return true
}
for _, b := range stmt.Blocks {
if hasReturn(b.Stmts) {
return true
}
}
}
return false
}
if !hasReturn(b.ir.Stmts) {
cs.addError(d.Pos(), fmt.Sprintf("function %s must have a return statement but not", d.Name))
return function{}, false
}
}
var inT, outT []shaderir.Type
for _, v := range inParams {
inT = append(inT, v.typ)

View File

@ -1,4 +1,6 @@
void F0(in vec2 l0, out vec4 l1);
void F0(in vec2 l0, out vec4 l1) {
l1 = vec4(0.0);
return;
}

View File

@ -1,4 +1,5 @@
package main
func Foo(foo vec2) vec4 {
return vec4(0)
}

View File

@ -259,6 +259,26 @@ func Foo() (float, float) {
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return vec4(0);
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
if _, err := NewShader([]byte(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
if _, err := NewShader([]byte(`package main
func Foo() float {
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return vec4(0)
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}