From cd13c66ecd5e93601225a1b892526a02956052b7 Mon Sep 17 00:00:00 2001 From: Igor Shaposhnik Date: Mon, 10 Jan 2022 16:04:59 +0300 Subject: [PATCH] [wgsl-in] Write a real type in type mismatch error --- .gitignore | 2 ++ src/front/wgsl/mod.rs | 29 ++++++++++++++++++++++++----- tests/wgsl-errors.rs | 38 +++++++++++++++++++++++++++++++++----- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index bb84f2e164..df4cb1d395 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 93cdf29027..6103cf6192 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![], }, @@ -3453,7 +3453,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)); @@ -3518,7 +3521,8 @@ impl Parser { expr_inner ); return Err(Error::InitializationTypeMismatch( - name_span, ty, + name_span, + expr_inner.to_wgsl(context.types, context.constants), )); } ty @@ -4293,7 +4297,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 7607359de3..ac0d216880 100644 --- a/tests/wgsl-errors.rs +++ b/tests/wgsl-errors.rs @@ -461,28 +461,56 @@ 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; │ ^ definition of `x` +"#, + ); + + check( + r#" + fn foo() { + let x: f32 = true; + } + "#, + r#"error: the type of `x` is expected to be `bool` + ┌─ wgsl:3:21 + │ +3 │ let x: f32 = true; + │ ^ definition of `x` + "#, ); } #[test] -fn local_var_type_mismatch() { +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 = 1; + var x: f32 = 1u32; } "#, - r#"error: the type of `x` is expected to be [1] + r#"error: the type of `x` is expected to be `u32` ┌─ wgsl:3:21 │ -3 │ var x: f32 = 1; +3 │ var x: f32 = 1u32; │ ^ definition of `x` "#,