mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 14:01:08 +01:00
Validate unary ops in WGSL lowerer (#10036)
Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
This commit is contained in:
1 parent
f32f76dcb1
commit
0e47071fbd
4 files changed
+163
-8
No files matched your search
@@ -9,7 +9,7 @@ use crate::common::wgsl::TryToWgsl;
|
||||
use crate::diagnostic_filter::ConflictingDiagnosticRuleError;
|
||||
use crate::error::replace_control_chars;
|
||||
use crate::proc::{Alignment, ConstantEvaluatorError, ResolveError};
|
||||
use crate::{Scalar, SourceLocation, Span};
|
||||
use crate::{Scalar, SourceLocation, Span, UnaryOperator};
|
||||
|
||||
use super::parse::directive::enable_extension::{EnableExtension, UnimplementedEnableExtension};
|
||||
use super::parse::directive::language_extension::{
|
||||
@@ -385,6 +385,12 @@ pub(crate) enum Error<'a> {
|
||||
},
|
||||
DeclMissingTypeAndInit(Span),
|
||||
MissingAttribute(&'static str, Span),
|
||||
InvalidUnaryOperandType {
|
||||
span: Span,
|
||||
op: UnaryOperator,
|
||||
operand_type: String,
|
||||
},
|
||||
|
||||
InvalidAddrOfOperand(Span),
|
||||
InvalidAtomicPointer(Span),
|
||||
InvalidAtomicOperandType(Span),
|
||||
@@ -964,6 +970,7 @@ impl<'a> Error<'a> {
|
||||
| Error::WrongArgumentCount { span, .. }
|
||||
| Error::ConstantEvaluatorError(_, span)
|
||||
| Error::EnableExtensionNotSupported { span, .. }
|
||||
| Error::InvalidUnaryOperandType { span, .. }
|
||||
| Error::MissingTemplateArg { span, .. } => {
|
||||
let (message, label) = match self {
|
||||
Error::BadMatrixScalarKind(_, scalar) => (
|
||||
@@ -1023,6 +1030,20 @@ impl<'a> Error<'a> {
|
||||
),
|
||||
"is missing a template argument"
|
||||
),
|
||||
Error::InvalidUnaryOperandType { op, operand_type, .. } => {
|
||||
let operator = match op {
|
||||
UnaryOperator::Negate => "-",
|
||||
UnaryOperator::LogicalNot => "!",
|
||||
UnaryOperator::BitwiseNot => "~",
|
||||
};
|
||||
(
|
||||
format!(
|
||||
"unary operator `{operator}` is not defined for operand type `{}`",
|
||||
operand_type
|
||||
),
|
||||
"invalid operand type for this operator"
|
||||
)
|
||||
},
|
||||
_ => unreachable!()
|
||||
};
|
||||
|
||||
@@ -1660,7 +1681,7 @@ impl<'a> Error<'a> {
|
||||
)],
|
||||
message: "unterminated block comment".into(),
|
||||
notes: vec![],
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ use alloc::{
|
||||
string::{String, ToString},
|
||||
vec::Vec,
|
||||
};
|
||||
use core::num::NonZeroU32;
|
||||
use core::{matches, num::NonZeroU32};
|
||||
|
||||
use crate::front::wgsl::error::{Error, ExpectedToken, InvalidAssignmentType};
|
||||
use crate::front::wgsl::index::Index;
|
||||
@@ -2414,10 +2414,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
|
||||
return expr.try_map(|handle| ctx.interrupt_emitter(handle, span));
|
||||
}
|
||||
ast::Expression::Unary { op, expr } => {
|
||||
let expr = self.expression_for_abstract(expr, ctx)?;
|
||||
Typed::Plain(ir::Expression::Unary { op, expr })
|
||||
}
|
||||
ast::Expression::Unary { op, expr } => self.unary(op, expr, span, ctx)?,
|
||||
ast::Expression::AddrOf(expr) => {
|
||||
// The `&` operator simply converts a reference to a pointer. And since a
|
||||
// reference is required, the Load Rule is not applied.
|
||||
@@ -2909,6 +2906,63 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
Ok(ty)
|
||||
}
|
||||
|
||||
fn unary(
|
||||
&mut self,
|
||||
op: ir::UnaryOperator,
|
||||
expr: Handle<ast::Expression<'source>>,
|
||||
span: Span,
|
||||
ctx: &mut ExpressionContext<'source, '_, '_>,
|
||||
) -> Result<'source, Typed<ir::Expression>> {
|
||||
let make_error = |operand_type: String| Error::InvalidUnaryOperandType {
|
||||
span,
|
||||
op,
|
||||
operand_type,
|
||||
};
|
||||
|
||||
let expr = self.expression_for_abstract(expr, ctx)?;
|
||||
ctx.grow_types(expr)?;
|
||||
let expr_ty_resolution = resolve!(ctx, expr);
|
||||
|
||||
// All unary operators are only defined for scalars and vectors of scalars.
|
||||
let Some(kind) = expr_ty_resolution
|
||||
.inner_with(&ctx.module.types)
|
||||
.vector_size_and_scalar()
|
||||
.map(|(_, scalar)| scalar.kind)
|
||||
else {
|
||||
let operand_type = ctx.type_resolution_to_string(expr_ty_resolution);
|
||||
return Err(Box::new(make_error(operand_type)));
|
||||
};
|
||||
// validate preconditions
|
||||
match (op, kind) {
|
||||
// `T` is `bool` or `vecN<bool>`. These types have no automatic conversions.
|
||||
(ir::UnaryOperator::LogicalNot, ir::ScalarKind::Bool) => {}
|
||||
|
||||
// `T` is `AbstractInt`, `AbstractFloat`, `i32`, `f32`, `f16`,
|
||||
// `vecN<AbstractInt>`, `vecN<AbstractFloat>`, `vecN<i32>`, `vecN<f32>`, or `vecN<f16>`.
|
||||
(
|
||||
ir::UnaryOperator::Negate,
|
||||
ir::ScalarKind::AbstractInt
|
||||
| ir::ScalarKind::AbstractFloat
|
||||
| ir::ScalarKind::Sint
|
||||
| ir::ScalarKind::Float,
|
||||
) => {}
|
||||
|
||||
// `S` is `AbstractInt`, `i32`, or `u32`.
|
||||
// `T` is `S` or `vecN<S>`.
|
||||
(
|
||||
ir::UnaryOperator::BitwiseNot,
|
||||
ir::ScalarKind::Sint | ir::ScalarKind::Uint | ir::ScalarKind::AbstractInt,
|
||||
) => {}
|
||||
|
||||
_ => {
|
||||
let operand_type = ctx.type_resolution_to_string(expr_ty_resolution);
|
||||
return Err(Box::new(make_error(operand_type)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Typed::Plain(ir::Expression::Unary { op, expr }))
|
||||
}
|
||||
|
||||
fn binary(
|
||||
&mut self,
|
||||
op: ir::BinaryOperator,
|
||||
|
||||
@@ -782,6 +782,86 @@ fn parse_missing_workgroup_size() {
|
||||
mod diagnostic_filter {
|
||||
use crate::front::wgsl::assert_parse_err;
|
||||
|
||||
#[test]
|
||||
fn invalid_unary_operand_negative_unsigned_integer() {
|
||||
let shader = "
|
||||
fn myfunc() {
|
||||
_ = -(2u);
|
||||
}
|
||||
";
|
||||
assert_parse_err(
|
||||
shader,
|
||||
"\
|
||||
error: unary operator `-` is not defined for operand type `u32`
|
||||
┌─ wgsl:3:9
|
||||
│
|
||||
3 │ _ = -(2u);
|
||||
│ ^^^^^ invalid operand type for this operator
|
||||
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_unary_operand_negative_neither_scalar_nor_vector() {
|
||||
let shader = "
|
||||
fn myfunc() {
|
||||
_ = -mat2x2f();
|
||||
}
|
||||
";
|
||||
assert_parse_err(
|
||||
shader,
|
||||
"\
|
||||
error: unary operator `-` is not defined for operand type `mat2x2<f32>`
|
||||
┌─ wgsl:3:9
|
||||
│
|
||||
3 │ _ = -mat2x2f();
|
||||
│ ^^^^^^^^^^ invalid operand type for this operator
|
||||
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_unary_operand_logical_not() {
|
||||
let shader = "
|
||||
fn myfunc() {
|
||||
_ = !1;
|
||||
}
|
||||
";
|
||||
assert_parse_err(
|
||||
shader,
|
||||
"\
|
||||
error: unary operator `!` is not defined for operand type `{AbstractInt}`
|
||||
┌─ wgsl:3:9
|
||||
│
|
||||
3 │ _ = !1;
|
||||
│ ^^ invalid operand type for this operator
|
||||
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_unary_operand_bitwise_not() {
|
||||
let shader = "
|
||||
fn myfunc() {
|
||||
_ = ~(1.0f);
|
||||
}
|
||||
";
|
||||
assert_parse_err(
|
||||
shader,
|
||||
"\
|
||||
error: unary operator `~` is not defined for operand type `f32`
|
||||
┌─ wgsl:3:9
|
||||
│
|
||||
3 │ _ = ~(1.0f);
|
||||
│ ^^^^^^^ invalid operand type for this operator
|
||||
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intended_global_directive() {
|
||||
let shader = "@diagnostic(off, my.lint);";
|
||||
|
||||
@@ -2725,7 +2725,7 @@ impl<'a> ConstantEvaluator<'a> {
|
||||
components: ref src_components,
|
||||
} => {
|
||||
match self.types[ty].inner {
|
||||
TypeInner::Vector { .. } | TypeInner::Matrix { .. } => (),
|
||||
TypeInner::Vector { .. } => (),
|
||||
_ => return Err(ConstantEvaluatorError::InvalidUnaryOpArg),
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user