internal/shader: add type checks for a bitwise operator + assignment

Closes #2754
This commit is contained in:
Hajime Hoshi 2023-09-13 04:17:43 +09:00
parent 1f67518319
commit 0e19cd10be
2 changed files with 121 additions and 2 deletions

View File

@ -110,6 +110,15 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator / not defined on %s", rts[0].String()))
return nil, false
}
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor {
if lts[0].Main != shaderir.Int && !lts[0].IsIntVector() {
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
}
if rts[0].Main != shaderir.Int && !rts[0].IsIntVector() {
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, rts[0].String()))
}
return nil, false
}
if lts[0].Main == shaderir.Int && rhs[0].Const != nil {
if !cs.forceToInt(stmt, &rhs[0]) {
return nil, false
@ -128,7 +137,9 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
}
}
case shaderir.Float:
if rhs[0].Const != nil &&
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor {
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
} else if rhs[0].Const != nil &&
(rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown {
rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
@ -137,7 +148,9 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
return nil, false
}
case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
if (op == shaderir.MatrixMul || op == shaderir.Div) &&
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor {
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
} else if (op == shaderir.MatrixMul || op == shaderir.Div) &&
(rts[0].Main == shaderir.Float ||
(rhs[0].Const != nil &&
(rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&

View File

@ -1421,6 +1421,112 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
}
}
// Issue #2754
func TestSyntaxBitwiseOperatorAssign(t *testing.T) {
cases := []struct {
stmt string
err bool
}{
{stmt: "a := 1; a &= 2", err: false},
{stmt: "a := 1; a &= 2.0", err: false},
{stmt: "const c = 2; a := 1; a &= c", err: false},
{stmt: "const c = 2.0; a := 1; a &= c", err: false},
{stmt: "const c int = 2; a := 1; a &= c", err: false},
{stmt: "const c int = 2.0; a := 1; a &= c", err: false},
{stmt: "const c float = 2; a := 1; a &= c", err: true},
{stmt: "const c float = 2.0; a := 1; a &= c", err: true},
{stmt: "a := 1; a &= int(2)", err: false},
{stmt: "a := 1; a &= vec2(2)", err: true},
{stmt: "a := 1; a &= vec3(2)", err: true},
{stmt: "a := 1; a &= vec4(2)", err: true},
{stmt: "a := 1; a &= ivec2(2)", err: true},
{stmt: "a := 1; a &= ivec3(2)", err: true},
{stmt: "a := 1; a &= ivec4(2)", err: true},
{stmt: "a := 1; a &= mat2(2)", err: true},
{stmt: "a := 1; a &= mat3(2)", err: true},
{stmt: "a := 1; a &= mat4(2)", err: true},
{stmt: "a := 1.0; a &= 2", err: true},
{stmt: "a := ivec2(1); a &= 2", err: false},
{stmt: "a := ivec2(1); a &= ivec2(1)", err: false},
{stmt: "a := ivec2(1); a &= ivec3(1)", err: true},
{stmt: "a := ivec2(1); a &= ivec4(1)", err: true},
{stmt: "a := vec2(1); a &= 2", err: true},
{stmt: "a := vec2(1); a &= vec2(2)", err: true},
{stmt: "a := mat2(1); a &= 2", err: true},
{stmt: "a := mat2(1); a &= mat2(2)", err: true},
{stmt: "a := 1; a |= 2", err: false},
{stmt: "a := 1; a |= 2.0", err: false},
{stmt: "const c = 2; a := 1; a |= c", err: false},
{stmt: "const c = 2.0; a := 1; a |= c", err: false},
{stmt: "const c int = 2; a := 1; a |= c", err: false},
{stmt: "const c int = 2.0; a := 1; a |= c", err: false},
{stmt: "const c float = 2; a := 1; a |= c", err: true},
{stmt: "const c float = 2.0; a := 1; a |= c", err: true},
{stmt: "a := 1; a |= int(2)", err: false},
{stmt: "a := 1; a |= vec2(2)", err: true},
{stmt: "a := 1; a |= vec3(2)", err: true},
{stmt: "a := 1; a |= vec4(2)", err: true},
{stmt: "a := 1; a |= ivec2(2)", err: true},
{stmt: "a := 1; a |= ivec3(2)", err: true},
{stmt: "a := 1; a |= ivec4(2)", err: true},
{stmt: "a := 1; a |= mat2(2)", err: true},
{stmt: "a := 1; a |= mat3(2)", err: true},
{stmt: "a := 1; a |= mat4(2)", err: true},
{stmt: "a := 1.0; a |= 2", err: true},
{stmt: "a := ivec2(1); a |= 2", err: false},
{stmt: "a := ivec2(1); a |= ivec2(1)", err: false},
{stmt: "a := ivec2(1); a |= ivec3(1)", err: true},
{stmt: "a := ivec2(1); a |= ivec4(1)", err: true},
{stmt: "a := vec2(1); a |= 2", err: true},
{stmt: "a := vec2(1); a |= vec2(2)", err: true},
{stmt: "a := mat2(1); a |= 2", err: true},
{stmt: "a := mat2(1); a |= mat2(2)", err: true},
{stmt: "a := 1; a ^= 2", err: false},
{stmt: "a := 1; a ^= 2.0", err: false},
{stmt: "const c = 2; a := 1; a ^= c", err: false},
{stmt: "const c = 2.0; a := 1; a ^= c", err: false},
{stmt: "const c int = 2; a := 1; a ^= c", err: false},
{stmt: "const c int = 2.0; a := 1; a ^= c", err: false},
{stmt: "const c float = 2; a := 1; a ^= c", err: true},
{stmt: "const c float = 2.0; a := 1; a ^= c", err: true},
{stmt: "a := 1; a ^= int(2)", err: false},
{stmt: "a := 1; a ^= vec2(2)", err: true},
{stmt: "a := 1; a ^= vec3(2)", err: true},
{stmt: "a := 1; a ^= vec4(2)", err: true},
{stmt: "a := 1; a ^= ivec2(2)", err: true},
{stmt: "a := 1; a ^= ivec3(2)", err: true},
{stmt: "a := 1; a ^= ivec4(2)", err: true},
{stmt: "a := 1; a ^= mat2(2)", err: true},
{stmt: "a := 1; a ^= mat3(2)", err: true},
{stmt: "a := 1; a ^= mat4(2)", err: true},
{stmt: "a := 1.0; a ^= 2", err: true},
{stmt: "a := ivec2(1); a ^= 2", err: false},
{stmt: "a := ivec2(1); a ^= ivec2(1)", err: false},
{stmt: "a := ivec2(1); a ^= ivec3(1)", err: true},
{stmt: "a := ivec2(1); a ^= ivec4(1)", err: true},
{stmt: "a := vec2(1); a ^= 2", err: true},
{stmt: "a := vec2(1); a ^= vec2(2)", err: true},
{stmt: "a := mat2(1); a ^= 2", err: true},
{stmt: "a := mat2(1); a ^= mat2(2)", err: true},
}
for _, c := range cases {
_, err := compileToIR([]byte(fmt.Sprintf(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
%s
return position
}`, c.stmt)))
if err == nil && c.err {
t.Errorf("%s must return an error but does not", c.stmt)
} else if err != nil && !c.err {
t.Errorf("%s must not return nil but returned %v", c.stmt, err)
}
}
}
func TestSyntaxAtan(t *testing.T) {
cases := []struct {
stmt string