internal/shaderir: bug fix: constants must be truncated correctly in an array

Closes #2840
This commit is contained in:
Hajime Hoshi 2023-11-15 00:36:35 +09:00
parent 2b46a77e39
commit 1e78c2e6b0
3 changed files with 52 additions and 1 deletions

View File

@ -854,6 +854,32 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
cs.addError(e.Pos(), "multiple-value context is not available at a composite literal")
return nil, nil, nil, false
}
expr := exprs[0]
if expr.Const != nil {
switch t.Sub[0].Main {
case shaderir.Bool:
if expr.Const.Kind() != gconstant.Bool {
cs.addError(e.Pos(), fmt.Sprintf("cannot %s to type bool", expr.Const.String()))
}
case shaderir.Int:
if !canTruncateToInteger(expr.Const) {
cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", expr.Const.String()))
return nil, nil, nil, false
}
expr.Const = gconstant.ToInt(expr.Const)
case shaderir.Float:
if !canTruncateToFloat(expr.Const) {
cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to float", expr.Const.String()))
return nil, nil, nil, false
}
expr.Const = gconstant.ToFloat(expr.Const)
default:
cs.addError(e.Pos(), fmt.Sprintf("constant %s cannot be used for the array type %s", expr.Const.String(), t.String()))
return nil, nil, nil, false
}
}
stmts = append(stmts, ss...)
stmts = append(stmts, shaderir.Stmt{
Type: shaderir.Assign,
@ -871,7 +897,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
},
},
},
exprs[0],
expr,
},
})
}

View File

@ -0,0 +1,16 @@
float[1] F0(void);
int[1] F1(void);
float[1] F0(void) {
float l0[1];
l0[0] = float(0);
(l0)[0] = 1.0;
return l0;
}
int[1] F1(void) {
int l0[1];
l0[0] = 0;
(l0)[0] = 1;
return l0;
}

9
internal/shader/testdata/issue2840.go vendored Normal file
View File

@ -0,0 +1,9 @@
package main
func Floats() [1]float {
return [1]float{1}
}
func Ints() [1]int {
return [1]int{1.0}
}