[wgsl-in] Write a real type in type mismatch error

This commit is contained in:
Igor Shaposhnik authored and Dzmitry Malyshau committed 2022-01-11 22:42:15 -05:00
1 parent c0c6ba48ea
commit c09142eb3c
3 files changed
+61 -12

No files matched your search

+2
View File
@@ -13,3 +13,5 @@ Cargo.lock
/*.frag
/*.comp
/*.wgsl
/*.hlsl
/*.txt
+24 -5
View File
@@ -164,7 +164,7 @@ pub enum Error<'a> {
ZeroSizeOrAlign(Span),
InconsistentBinding(Span),
UnknownLocalFunction(Span),
InitializationTypeMismatch(Span, Handle<crate::Type>),
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,
));
}
}
+35 -7
View File
@@ -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`
"#,