Enforce stricter type coercion rules in SkSL.

Literals are still flexible; we still allow `1` to coerce to float.
However, we no longer accept code like `int x = sqrt(2);` or
`int x = 0; float y = x;` without an explicit cast.

Change-Id: Ieb294a4877447e2336252f876e8bc489d1e4a59a
Bug: skia:11164
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/353417
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
This commit is contained in:
John Stiles 2021-01-14 11:59:23 -05:00 committed by Skia Commit-Bot
parent 491911003e
commit 4a2d651243
2 changed files with 26 additions and 4 deletions

View File

@ -14,20 +14,25 @@ CoercionCost Type::coercionCost(const Type& other) const {
if (*this == other) {
return CoercionCost::Free();
}
if (this->typeKind() == TypeKind::kVector && other.typeKind() == TypeKind::kVector) {
if (this->isVector() && other.isVector()) {
if (this->columns() == other.columns()) {
return this->componentType().coercionCost(other.componentType());
}
return CoercionCost::Impossible();
}
if (this->typeKind() == TypeKind::kMatrix) {
if (this->isMatrix()) {
if (this->columns() == other.columns() && this->rows() == other.rows()) {
return this->componentType().coercionCost(other.componentType());
}
return CoercionCost::Impossible();
}
if (this->isInteger() && this->isLiteral() && other.isFloat()) {
return CoercionCost::Free();
}
if (this->isNumber() && other.isNumber()) {
if (other.priority() >= this->priority()) {
if (this->isInteger() != other.isInteger()) {
return CoercionCost::Impossible();
} else if (other.priority() >= this->priority()) {
return CoercionCost::Normal(other.priority() - this->priority());
} else {
return CoercionCost::Narrowing(this->priority() - other.priority());

View File

@ -2,4 +2,21 @@
error: 16: type mismatch: '=' cannot operate on 'int', 'float'
error: 17: type mismatch: '=' cannot operate on 'int', 'float'
2 errors
error: 18: type mismatch: '=' cannot operate on 'float', 'int'
error: 19: type mismatch: '+' cannot operate on 'int', 'float'
error: 20: type mismatch: '-' cannot operate on 'int', 'float'
error: 21: type mismatch: '*' cannot operate on 'int', 'float'
error: 22: type mismatch: '/' cannot operate on 'int', 'float'
error: 23: type mismatch: '+' cannot operate on 'float', 'int'
error: 24: type mismatch: '-' cannot operate on 'float', 'int'
error: 25: type mismatch: '*' cannot operate on 'float', 'int'
error: 26: type mismatch: '/' cannot operate on 'float', 'int'
error: 27: type mismatch: '+' cannot operate on 'int', 'float'
error: 28: type mismatch: '-' cannot operate on 'int', 'float'
error: 29: type mismatch: '*' cannot operate on 'int', 'float'
error: 30: type mismatch: '/' cannot operate on 'int', 'float'
error: 31: type mismatch: '+' cannot operate on 'float', 'int'
error: 32: type mismatch: '-' cannot operate on 'float', 'int'
error: 33: type mismatch: '*' cannot operate on 'float', 'int'
error: 34: type mismatch: '/' cannot operate on 'float', 'int'
19 errors