internal/shader: bug fix: a meaningless statement should raise an error (a non-call expression statement)

Updates #1898
This commit is contained in:
Hajime Hoshi 2021-12-27 23:14:05 +09:00
parent 481a2145ae
commit 374871c031
3 changed files with 20 additions and 1 deletions

View File

@ -373,6 +373,7 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
// TODO: Is this an error?
}
// These local-variable expressions are used for an outside function callers.
var exprs []shaderir.Expr
for _, p := range outParams {
exprs = append(exprs, shaderir.Expr{

View File

@ -463,6 +463,11 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
}
case *ast.ExprStmt:
if _, ok := stmt.X.(*ast.CallExpr); !ok {
cs.addError(stmt.Pos(), fmt.Sprintf("the statement is evaluated but not used"))
return nil, false
}
exprs, _, ss, ok := cs.parseExpr(block, stmt.X, true)
if !ok {
return nil, false
@ -470,8 +475,9 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
stmts = append(stmts, ss...)
for _, expr := range exprs {
// There can be a non-call expr like LocalVariable expressions.
// These are necessary to be used as arguments for an outside function callers.
if expr.Type != shaderir.Call {
// TODO: Should this return an error?
continue
}
if expr.Exprs[0].Type == shaderir.BuiltinFuncExpr {

View File

@ -1250,6 +1250,18 @@ func TestShaderMeaninglessSentence(t *testing.T) {
var Time float
var ScreenSize vec2
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
position
return position
}`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
if _, err := ebiten.NewShader([]byte(`package main
var Time float
var ScreenSize vec2
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
vec2(position)
return position