Skip to content

Commit 14727d0

Browse files
jimblandyErichDonGubler
authored andcommitted
[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 868c449 commit 14727d0

File tree

7 files changed

+56
-284
lines changed

7 files changed

+56
-284
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use alloc::{
77
};
88
use core::num::NonZeroU32;
99

10+
use crate::common::wgsl::TypeContext;
11+
use crate::front::wgsl::{Error, Result};
1012
use crate::front::wgsl::lower::{ExpressionContext, Lowerer};
1113
use crate::front::wgsl::parse::ast;
12-
use crate::front::wgsl::{Error, Result};
1314
use crate::{Handle, Span};
1415

1516
/// A cooked form of `ast::ConstructorType` that uses Naga types whenever
@@ -71,7 +72,7 @@ impl Constructor<(Handle<crate::Type>, &crate::TypeInner)> {
7172
format!("mat{}x{}<?>", columns as u32, rows as u32,)
7273
}
7374
Self::PartialArray => "array<?, ?>".to_string(),
74-
Self::Type((handle, _inner)) => handle.to_wgsl(&ctx.module.to_ctx()),
75+
Self::Type((handle, _inner)) => ctx.type_to_string(handle),
7576
}
7677
}
7778
}
@@ -536,8 +537,8 @@ impl<'source> Lowerer<'source, '_> {
536537

537538
// Bad conversion (type cast)
538539
(Components::One { span, ty_inner, .. }, constructor) => {
539-
let from_type = ty_inner.to_wgsl(&ctx.module.to_ctx());
540-
return Err(Box::new(Error::BadTypeCast {
540+
let from_type = ctx.type_inner_to_string(ty_inner);
541+
return Err(Error::BadTypeCast {
541542
span,
542543
from_type,
543544
to_type: constructor.to_error_string(ctx),

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
};
@@ -53,9 +54,8 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
5354
match expr_inner.automatically_converts_to(goal_inner, types) {
5455
Some(scalars) => scalars,
5556
None => {
56-
let gctx = &self.module.to_ctx();
57-
let source_type = expr_resolution.to_wgsl(gctx);
58-
let dest_type = goal_ty.to_wgsl(gctx);
57+
let source_type = self.type_resolution_to_string(expr_resolution);
58+
let dest_type = self.type_resolution_to_string(goal_ty);
5959

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

9797
let make_error = || {
98-
let gctx = &self.module.to_ctx();
99-
let source_type = expr_resolution.to_wgsl(gctx);
98+
let source_type = self.type_resolution_to_string(expr_resolution);
10099
super::Error::AutoConversionLeafScalar(Box::new(AutoConversionLeafScalarError {
101100
dest_span: goal_span,
102-
dest_scalar: goal_scalar.to_wgsl(),
101+
dest_scalar: goal_scalar.to_wgsl_for_diagnostics(),
103102
source_span: expr_span,
104103
source_type,
105104
}))
@@ -275,8 +274,8 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
275274
let expr_type = &self.typifier()[expr];
276275
super::Error::ConcretizationFailed(Box::new(ConcretizationFailedError {
277276
expr_span,
278-
expr_type: expr_type.to_wgsl(&self.module.to_ctx()),
279-
scalar: concretized.to_wgsl(),
277+
expr_type: self.type_resolution_to_string(expr_type),
278+
scalar: concretized.to_wgsl_for_diagnostics(),
280279
inner: err,
281280
}))
282281
})?;
@@ -319,7 +318,7 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
319318
"wgsl automatic_conversion_consensus: {:?}",
320319
inners
321320
.clone()
322-
.map(|inner| inner.to_wgsl(&self.module.to_ctx()))
321+
.map(|inner| self.type_inner_to_string(inner))
323322
.collect::<Vec<String>>()
324323
);
325324
let mut best = inners.next().unwrap().scalar().ok_or(0_usize)?;
@@ -333,7 +332,7 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
333332
}
334333
}
335334

336-
log::debug!(" consensus: {:?}", best.to_wgsl());
335+
log::debug!(" consensus: {:?}", best.to_wgsl_for_diagnostics());
337336
Ok(best)
338337
}
339338
}

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

Lines changed: 35 additions & 12 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, ExpectedToken, InvalidAssignmentType};
1011
use crate::front::wgsl::index::Index;
1112
use crate::front::wgsl::parse::number::Number;
@@ -388,6 +389,30 @@ pub struct ExpressionContext<'source, 'temp, 'out> {
388389
expr_type: ExpressionContextType<'temp, 'out>,
389390
}
390391

392+
impl TypeContext for ExpressionContext<'_, '_, '_> {
393+
fn lookup_type(&self, handle: Handle<crate::Type>) -> &crate::Type {
394+
&self.module.types[handle]
395+
}
396+
397+
fn type_name(&self, handle: Handle<crate::Type>) -> &str {
398+
self.module.types[handle]
399+
.name
400+
.as_deref()
401+
.unwrap_or("{anonymous type}")
402+
}
403+
404+
fn write_override<W: core::fmt::Write>(
405+
&self,
406+
handle: Handle<crate::Override>,
407+
out: &mut W,
408+
) -> core::fmt::Result {
409+
match self.module.overrides[handle].name {
410+
Some(ref name) => out.write_str(name),
411+
None => write!(out, "{{anonymous override {handle:?}}}"),
412+
}
413+
}
414+
}
415+
391416
impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
392417
#[allow(dead_code)]
393418
fn as_const(&mut self) -> ExpressionContext<'source, '_, '_> {
@@ -1256,9 +1281,9 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
12561281
if !explicit_inner.equivalent(init_inner, &ectx.module.types) {
12571282
return Err(Box::new(Error::InitializationTypeMismatch {
12581283
name: name.span,
1259-
expected: explicit_inner.to_wgsl(&ectx.module.to_ctx()),
1260-
got: init_inner.to_wgsl(&ectx.module.to_ctx()),
1261-
}));
1284+
expected: ectx.type_inner_to_string(explicit_inner),
1285+
got: ectx.type_inner_to_string(init_inner),
1286+
});
12621287
}
12631288
ty = explicit_ty;
12641289
initializer = Some(init);
@@ -1487,12 +1512,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
14871512
.inner
14881513
.equivalent(&ctx.module.types[init_ty].inner, &ctx.module.types)
14891514
{
1490-
let gctx = &ctx.module.to_ctx();
14911515
return Err(Box::new(Error::InitializationTypeMismatch {
14921516
name: l.name.span,
1493-
expected: ty.to_wgsl(gctx),
1494-
got: init_ty.to_wgsl(gctx),
1495-
}));
1517+
expected: ctx.type_to_string(ty),
1518+
got: ctx.type_to_string(init_ty),
1519+
});
14961520
}
14971521
}
14981522

@@ -2192,12 +2216,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
21922216
crate::TypeInner::Vector { scalar, .. } => scalar,
21932217
_ => {
21942218
let ty = resolve!(ctx, expr);
2195-
let gctx = &ctx.module.to_ctx();
2196-
return Err(Box::new(Error::BadTypeCast {
2197-
from_type: ty.to_wgsl(gctx),
2219+
return Err(Error::BadTypeCast {
2220+
from_type: ctx.type_resolution_to_string(ty),
21982221
span: ty_span,
2199-
to_type: to_resolved.to_wgsl(gctx),
2200-
}));
2222+
to_type: ctx.type_to_string(to_resolved),
2223+
});
22012224
}
22022225
};
22032226

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)