diff --git a/.gitignore b/.gitignore index bb84f2e16..df4cb1d39 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ Cargo.lock /*.frag /*.comp /*.wgsl +/*.hlsl +/*.txt diff --git a/src/front/wgsl/mod.rs b/src/front/wgsl/mod.rs index a58f63d42..87fce1237 100644 --- a/src/front/wgsl/mod.rs +++ b/src/front/wgsl/mod.rs @@ -164,7 +164,7 @@ pub enum Error<'a> { ZeroSizeOrAlign(Span), InconsistentBinding(Span), UnknownLocalFunction(Span), - InitializationTypeMismatch(Span, Handle), + InitializationTypeMismatch(Span, String), MissingType(Span), InvalidAtomicPointer(Span), InvalidAtomicOperandType(Span), @@ -414,7 +414,7 @@ impl<'a> Error<'a> { notes: vec![], }, Error::InitializationTypeMismatch(ref name_span, ref expected_ty) => ParseError { - message: format!("the type of `{}` is expected to be {:?}", &source[name_span.clone()], expected_ty), + message: format!("the type of `{}` is expected to be `{}`", &source[name_span.clone()], expected_ty), labels: vec![(name_span.clone(), format!("definition of `{}`", &source[name_span.clone()]).into())], notes: vec![], }, @@ -3448,7 +3448,10 @@ impl Parser { given_inner, expr_inner ); - return Err(Error::InitializationTypeMismatch(name_span, ty)); + return Err(Error::InitializationTypeMismatch( + name_span, + expr_inner.to_wgsl(context.types, context.constants), + )); } } block.extend(emitter.finish(context.expressions)); @@ -3513,7 +3516,8 @@ impl Parser { expr_inner ); return Err(Error::InitializationTypeMismatch( - name_span, ty, + name_span, + expr_inner.to_wgsl(context.types, context.constants), )); } ty @@ -4288,7 +4292,22 @@ impl Parser { crate::ConstantInner::Composite { ty, components: _ } => ty == explicit_ty, }; if !type_match { - return Err(Error::InitializationTypeMismatch(name_span, explicit_ty)); + let exptected_inner_str = match con.inner { + crate::ConstantInner::Scalar { width, value } => { + crate::TypeInner::Scalar { + kind: value.scalar_kind(), + width, + } + .to_wgsl(&module.types, &module.constants) + } + crate::ConstantInner::Composite { .. } => module.types[explicit_ty] + .inner + .to_wgsl(&module.types, &module.constants), + }; + return Err(Error::InitializationTypeMismatch( + name_span, + exptected_inner_str, + )); } } diff --git a/tests/wgsl-errors.rs b/tests/wgsl-errors.rs index 7607359de..ac0d21688 100644 --- a/tests/wgsl-errors.rs +++ b/tests/wgsl-errors.rs @@ -461,7 +461,7 @@ fn let_type_mismatch() { r#" let x: i32 = 1.0; "#, - r#"error: the type of `x` is expected to be [1] + r#"error: the type of `x` is expected to be `f32` ┌─ wgsl:2:17 │ 2 │ let x: i32 = 1.0; @@ -469,20 +469,48 @@ fn let_type_mismatch() { "#, ); -} -#[test] -fn local_var_type_mismatch() { check( r#" fn foo() { - var x: f32 = 1; + let x: f32 = true; } "#, - r#"error: the type of `x` is expected to be [1] + r#"error: the type of `x` is expected to be `bool` ┌─ wgsl:3:21 │ -3 │ var x: f32 = 1; +3 │ let x: f32 = true; + │ ^ definition of `x` + +"#, + ); +} + +#[test] +fn var_type_mismatch() { + check( + r#" + let x: f32 = 1; + "#, + r#"error: the type of `x` is expected to be `i32` + ┌─ wgsl:2:17 + │ +2 │ let x: f32 = 1; + │ ^ definition of `x` + +"#, + ); + + check( + r#" + fn foo() { + var x: f32 = 1u32; + } + "#, + r#"error: the type of `x` is expected to be `u32` + ┌─ wgsl:3:21 + │ +3 │ var x: f32 = 1u32; │ ^ definition of `x` "#,