Skip to content

Commit 31a4e28

Browse files
committed
[naga wgsl-in] Use common type-formatting code.
Remove the `naga::front::wgsl::to_wgsl` module. Instead, use the implementations of `naga::common::wgsl`'s `TryToWgsl` and `TypeContext` traits.
1 parent 2ba5b54 commit 31a4e28

File tree

7 files changed

+50
-278
lines changed

7 files changed

+50
-278
lines changed

naga/src/front/wgsl/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Formatting WGSL front end error messages.
22
3+
use crate::common::wgsl::TryToWgsl;
34
use crate::diagnostic_filter::ConflictingDiagnosticRuleError;
45
use crate::proc::{Alignment, ConstantEvaluatorError, ResolveError};
56
use crate::{Scalar, SourceLocation, Span};
@@ -446,7 +447,7 @@ impl<'a> Error<'a> {
446447
Error::BadMatrixScalarKind(span, scalar) => ParseError {
447448
message: format!(
448449
"matrix scalar type must be floating-point, but found `{}`",
449-
scalar.to_wgsl()
450+
scalar.to_wgsl_for_diagnostics()
450451
),
451452
labels: vec![(span, "must be floating-point (e.g. `f32`)".into())],
452453
notes: vec![],
@@ -469,7 +470,7 @@ impl<'a> Error<'a> {
469470
Error::BadTextureSampleType { span, scalar } => ParseError {
470471
message: format!(
471472
"texture sample type must be one of f32, i32 or u32, but found {}",
472-
scalar.to_wgsl()
473+
scalar.to_wgsl_for_diagnostics()
473474
),
474475
labels: vec![(span, "must be one of f32, i32 or u32".into())],
475476
notes: vec![],

naga/src/front/wgsl/lower/construction.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use alloc::{
66
};
77
use core::num::NonZeroU32;
88

9+
use crate::common::wgsl::TypeContext;
910
use crate::front::wgsl::error::Error;
1011
use crate::front::wgsl::lower::{ExpressionContext, Lowerer};
1112
use crate::front::wgsl::parse::ast;
@@ -70,7 +71,7 @@ impl Constructor<(Handle<crate::Type>, &crate::TypeInner)> {
7071
format!("mat{}x{}<?>", columns as u32, rows as u32,)
7172
}
7273
Self::PartialArray => "array<?, ?>".to_string(),
73-
Self::Type((handle, _inner)) => handle.to_wgsl(&ctx.module.to_ctx()),
74+
Self::Type((handle, _inner)) => ctx.type_to_string(handle),
7475
}
7576
}
7677
}
@@ -535,7 +536,7 @@ impl<'source> Lowerer<'source, '_> {
535536

536537
// Bad conversion (type cast)
537538
(Components::One { span, ty_inner, .. }, constructor) => {
538-
let from_type = ty_inner.to_wgsl(&ctx.module.to_ctx()).into();
539+
let from_type = ctx.type_inner_to_string(ty_inner).into();
539540
return Err(Error::BadTypeCast {
540541
span,
541542
from_type,

naga/src/front/wgsl/lower/conversion.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use alloc::{boxed::Box, string::String, vec::Vec};
44

5+
use crate::common::wgsl::{TryToWgsl, TypeContext};
56
use crate::front::wgsl::error::{
67
AutoConversionError, AutoConversionLeafScalarError, ConcretizationFailedError,
78
};
@@ -52,9 +53,8 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
5253
match expr_inner.automatically_converts_to(goal_inner, types) {
5354
Some(scalars) => scalars,
5455
None => {
55-
let gctx = &self.module.to_ctx();
56-
let source_type = expr_resolution.to_wgsl(gctx).into();
57-
let dest_type = goal_ty.to_wgsl(gctx).into();
56+
let source_type = self.type_resolution_to_string(expr_resolution).into();
57+
let dest_type = self.type_resolution_to_string(goal_ty).into();
5858

5959
return Err(super::Error::AutoConversion(Box::new(
6060
AutoConversionError {
@@ -94,11 +94,10 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
9494
let expr_inner = expr_resolution.inner_with(types);
9595

9696
let make_error = || {
97-
let gctx = &self.module.to_ctx();
98-
let source_type = expr_resolution.to_wgsl(gctx).into();
97+
let source_type = self.type_resolution_to_string(expr_resolution).into();
9998
super::Error::AutoConversionLeafScalar(Box::new(AutoConversionLeafScalarError {
10099
dest_span: goal_span,
101-
dest_scalar: goal_scalar.to_wgsl().into(),
100+
dest_scalar: goal_scalar.to_wgsl_for_diagnostics().into(),
102101
source_span: expr_span,
103102
source_type,
104103
}))
@@ -272,8 +271,8 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
272271
let expr_type = &self.typifier()[expr];
273272
super::Error::ConcretizationFailed(Box::new(ConcretizationFailedError {
274273
expr_span,
275-
expr_type: expr_type.to_wgsl(&self.module.to_ctx()).into(),
276-
scalar: concretized.to_wgsl().into(),
274+
expr_type: self.type_resolution_to_string(expr_type).into(),
275+
scalar: concretized.to_wgsl_for_diagnostics().into(),
277276
inner: err,
278277
}))
279278
})?;
@@ -316,7 +315,7 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
316315
"wgsl automatic_conversion_consensus: {:?}",
317316
inners
318317
.clone()
319-
.map(|inner| inner.to_wgsl(&self.module.to_ctx()))
318+
.map(|inner| self.type_inner_to_string(inner))
320319
.collect::<Vec<String>>()
321320
);
322321
let mut best = inners.next().unwrap().scalar().ok_or(0_usize)?;
@@ -330,7 +329,7 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
330329
}
331330
}
332331

333-
log::debug!(" consensus: {:?}", best.to_wgsl());
332+
log::debug!(" consensus: {:?}", best.to_wgsl_for_diagnostics());
334333
Ok(best)
335334
}
336335
}

naga/src/front/wgsl/lower/mod.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use alloc::{
55
};
66
use core::num::NonZeroU32;
77

8+
use crate::common::wgsl::TypeContext;
89
use crate::front::wgsl::error::{Error, ExpectedToken, InvalidAssignmentType};
910
use crate::front::wgsl::index::Index;
1011
use crate::front::wgsl::parse::number::Number;
@@ -386,6 +387,30 @@ pub struct ExpressionContext<'source, 'temp, 'out> {
386387
expr_type: ExpressionContextType<'temp, 'out>,
387388
}
388389

390+
impl TypeContext for ExpressionContext<'_, '_, '_> {
391+
fn lookup_type(&self, handle: Handle<crate::Type>) -> &crate::Type {
392+
&self.module.types[handle]
393+
}
394+
395+
fn type_name(&self, handle: Handle<crate::Type>) -> &str {
396+
self.module.types[handle]
397+
.name
398+
.as_deref()
399+
.unwrap_or("{anonymous type}")
400+
}
401+
402+
fn write_override<W: core::fmt::Write>(
403+
&self,
404+
handle: Handle<crate::Override>,
405+
out: &mut W,
406+
) -> core::fmt::Result {
407+
match self.module.overrides[handle].name {
408+
Some(ref name) => out.write_str(name),
409+
None => write!(out, "{{anonymous override {handle:?}}}"),
410+
}
411+
}
412+
}
413+
389414
impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
390415
#[allow(dead_code)]
391416
fn as_const(&mut self) -> ExpressionContext<'source, '_, '_> {
@@ -1257,8 +1282,8 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
12571282
if !explicit_inner.equivalent(init_inner, &ectx.module.types) {
12581283
return Err(Error::InitializationTypeMismatch {
12591284
name: name.span,
1260-
expected: explicit_inner.to_wgsl(&ectx.module.to_ctx()).into(),
1261-
got: init_inner.to_wgsl(&ectx.module.to_ctx()).into(),
1285+
expected: ectx.type_inner_to_string(explicit_inner).into(),
1286+
got: ectx.type_inner_to_string(init_inner).into(),
12621287
});
12631288
}
12641289
ty = explicit_ty;
@@ -1486,11 +1511,10 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
14861511
.inner
14871512
.equivalent(&ctx.module.types[init_ty].inner, &ctx.module.types)
14881513
{
1489-
let gctx = &ctx.module.to_ctx();
14901514
return Err(Error::InitializationTypeMismatch {
14911515
name: l.name.span,
1492-
expected: ty.to_wgsl(gctx).into(),
1493-
got: init_ty.to_wgsl(gctx).into(),
1516+
expected: ctx.type_to_string(ty).into(),
1517+
got: ctx.type_to_string(init_ty).into(),
14941518
});
14951519
}
14961520
}
@@ -2182,11 +2206,10 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
21822206
crate::TypeInner::Vector { scalar, .. } => scalar,
21832207
_ => {
21842208
let ty = resolve!(ctx, expr);
2185-
let gctx = &ctx.module.to_ctx();
21862209
return Err(Error::BadTypeCast {
2187-
from_type: ty.to_wgsl(gctx).into(),
2210+
from_type: ctx.type_resolution_to_string(ty).into(),
21882211
span: ty_span,
2189-
to_type: to_resolved.to_wgsl(gctx).into(),
2212+
to_type: ctx.type_to_string(to_resolved).into(),
21902213
});
21912214
}
21922215
};

naga/src/front/wgsl/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod lower;
1010
mod parse;
1111
#[cfg(test)]
1212
mod tests;
13-
mod to_wgsl;
1413

1514
pub use crate::front::wgsl::error::ParseError;
1615
pub use crate::front::wgsl::parse::directive::language_extension::{

0 commit comments

Comments
 (0)