Skip to content

Commit c7e5d07

Browse files
kornelskiteoxoy
authored andcommitted
Reduce size of parse error
1 parent a93dcb6 commit c7e5d07

File tree

4 files changed

+130
-113
lines changed

4 files changed

+130
-113
lines changed

naga/src/front/wgsl/error.rs

Lines changed: 89 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use std::ops::Range;
1010
use termcolor::{ColorChoice, NoColor, StandardStream};
1111
use thiserror::Error;
1212

13+
#[cfg(test)]
14+
use std::mem::size_of;
15+
1316
#[derive(Clone, Debug)]
1417
pub struct ParseError {
1518
message: String,
@@ -144,7 +147,7 @@ pub enum InvalidAssignmentType {
144147
}
145148

146149
#[derive(Clone, Debug)]
147-
pub enum Error<'a> {
150+
pub(crate) enum Error<'a> {
148151
Unexpected(Span, ExpectedToken<'a>),
149152
UnexpectedComponents(Span),
150153
UnexpectedOperationInConstContext(Span),
@@ -154,8 +157,8 @@ pub enum Error<'a> {
154157
BadTexture(Span),
155158
BadTypeCast {
156159
span: Span,
157-
from_type: String,
158-
to_type: String,
160+
from_type: Box<str>,
161+
to_type: Box<str>,
159162
},
160163
BadTextureSampleType {
161164
span: Span,
@@ -188,8 +191,8 @@ pub enum Error<'a> {
188191
TypeNotInferable(Span),
189192
InitializationTypeMismatch {
190193
name: Span,
191-
expected: String,
192-
got: String,
194+
expected: Box<str>,
195+
got: Box<str>,
193196
},
194197
DeclMissingTypeAndInit(Span),
195198
MissingAttribute(&'static str, Span),
@@ -232,7 +235,7 @@ pub enum Error<'a> {
232235
/// name is `decl` has an identifier at `reference` whose definition is
233236
/// the next declaration in the cycle. The last pair's `reference` is
234237
/// the same identifier as `ident`, above.
235-
path: Vec<(Span, Span)>,
238+
path: Box<[(Span, Span)]>,
236239
},
237240
InvalidSwitchValue {
238241
uint: bool,
@@ -251,32 +254,41 @@ pub enum Error<'a> {
251254
ExpectedNonNegative(Span),
252255
ExpectedPositiveArrayLength(Span),
253256
MissingWorkgroupSize(Span),
254-
ConstantEvaluatorError(ConstantEvaluatorError, Span),
255-
AutoConversion {
256-
dest_span: Span,
257-
dest_type: String,
258-
source_span: Span,
259-
source_type: String,
260-
},
261-
AutoConversionLeafScalar {
262-
dest_span: Span,
263-
dest_scalar: String,
264-
source_span: Span,
265-
source_type: String,
266-
},
267-
ConcretizationFailed {
268-
expr_span: Span,
269-
expr_type: String,
270-
scalar: String,
271-
inner: ConstantEvaluatorError,
272-
},
257+
ConstantEvaluatorError(Box<ConstantEvaluatorError>, Span),
258+
AutoConversion(Box<AutoConversionError>),
259+
AutoConversionLeafScalar(Box<AutoConversionLeafScalarError>),
260+
ConcretizationFailed(Box<ConcretizationFailedError>),
273261
ExceededLimitForNestedBraces {
274262
span: Span,
275263
limit: u8,
276264
},
277265
PipelineConstantIDValue(Span),
278266
}
279267

268+
#[derive(Clone, Debug)]
269+
pub(crate) struct AutoConversionError {
270+
pub dest_span: Span,
271+
pub dest_type: Box<str>,
272+
pub source_span: Span,
273+
pub source_type: Box<str>,
274+
}
275+
276+
#[derive(Clone, Debug)]
277+
pub(crate) struct AutoConversionLeafScalarError {
278+
pub dest_span: Span,
279+
pub dest_scalar: Box<str>,
280+
pub source_span: Span,
281+
pub source_type: Box<str>,
282+
}
283+
284+
#[derive(Clone, Debug)]
285+
pub(crate) struct ConcretizationFailedError {
286+
pub expr_span: Span,
287+
pub expr_type: Box<str>,
288+
pub scalar: Box<str>,
289+
pub inner: ConstantEvaluatorError,
290+
}
291+
280292
impl<'a> Error<'a> {
281293
#[cold]
282294
#[inline(never)]
@@ -738,45 +750,55 @@ impl<'a> Error<'a> {
738750
)],
739751
notes: vec![],
740752
},
741-
Error::AutoConversion { dest_span, ref dest_type, source_span, ref source_type } => ParseError {
742-
message: format!("automatic conversions cannot convert `{source_type}` to `{dest_type}`"),
743-
labels: vec![
744-
(
745-
dest_span,
746-
format!("a value of type {dest_type} is required here").into(),
747-
),
748-
(
749-
source_span,
750-
format!("this expression has type {source_type}").into(),
751-
)
752-
],
753-
notes: vec![],
753+
Error::AutoConversion(ref error) => {
754+
// destructuring ensures all fields are handled
755+
let AutoConversionError { dest_span, ref dest_type, source_span, ref source_type } = **error;
756+
ParseError {
757+
message: format!("automatic conversions cannot convert `{source_type}` to `{dest_type}`"),
758+
labels: vec![
759+
(
760+
dest_span,
761+
format!("a value of type {dest_type} is required here").into(),
762+
),
763+
(
764+
source_span,
765+
format!("this expression has type {source_type}").into(),
766+
)
767+
],
768+
notes: vec![],
769+
}
754770
},
755-
Error::AutoConversionLeafScalar { dest_span, ref dest_scalar, source_span, ref source_type } => ParseError {
756-
message: format!("automatic conversions cannot convert elements of `{source_type}` to `{dest_scalar}`"),
757-
labels: vec![
758-
(
759-
dest_span,
760-
format!("a value with elements of type {dest_scalar} is required here").into(),
761-
),
762-
(
763-
source_span,
764-
format!("this expression has type {source_type}").into(),
765-
)
766-
],
767-
notes: vec![],
771+
Error::AutoConversionLeafScalar(ref error) => {
772+
let AutoConversionLeafScalarError { dest_span, ref dest_scalar, source_span, ref source_type } = **error;
773+
ParseError {
774+
message: format!("automatic conversions cannot convert elements of `{source_type}` to `{dest_scalar}`"),
775+
labels: vec![
776+
(
777+
dest_span,
778+
format!("a value with elements of type {dest_scalar} is required here").into(),
779+
),
780+
(
781+
source_span,
782+
format!("this expression has type {source_type}").into(),
783+
)
784+
],
785+
notes: vec![],
786+
}
768787
},
769-
Error::ConcretizationFailed { expr_span, ref expr_type, ref scalar, ref inner } => ParseError {
770-
message: format!("failed to convert expression to a concrete type: {}", inner),
771-
labels: vec![
772-
(
773-
expr_span,
774-
format!("this expression has type {}", expr_type).into(),
775-
)
776-
],
777-
notes: vec![
778-
format!("the expression should have been converted to have {} scalar type", scalar),
779-
]
788+
Error::ConcretizationFailed(ref error) => {
789+
let ConcretizationFailedError { expr_span, ref expr_type, ref scalar, ref inner } = **error;
790+
ParseError {
791+
message: format!("failed to convert expression to a concrete type: {}", inner),
792+
labels: vec![
793+
(
794+
expr_span,
795+
format!("this expression has type {}", expr_type).into(),
796+
)
797+
],
798+
notes: vec![
799+
format!("the expression should have been converted to have {} scalar type", scalar),
800+
]
801+
}
780802
},
781803
Error::ExceededLimitForNestedBraces { span, limit } => ParseError {
782804
message: "brace nesting limit reached".into(),
@@ -796,3 +818,8 @@ impl<'a> Error<'a> {
796818
}
797819
}
798820
}
821+
822+
#[test]
823+
fn test_error_size() {
824+
assert!(size_of::<Error<'_>>() <= 48);
825+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
530530

531531
// Bad conversion (type cast)
532532
(Components::One { span, ty_inner, .. }, constructor) => {
533-
let from_type = ty_inner.to_wgsl(&ctx.module.to_ctx());
533+
let from_type = ty_inner.to_wgsl(&ctx.module.to_ctx()).into();
534534
return Err(Error::BadTypeCast {
535535
span,
536536
from_type,
537-
to_type: constructor.to_error_string(ctx),
537+
to_type: constructor.to_error_string(ctx).into(),
538538
});
539539
}
540540

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! WGSL's automatic conversions for abstract types.
22
3+
use crate::front::wgsl::error::{
4+
AutoConversionError, AutoConversionLeafScalarError, ConcretizationFailedError,
5+
};
36
use crate::{Handle, Span};
47

58
impl<'source, 'temp, 'out> super::ExpressionContext<'source, 'temp, 'out> {
@@ -39,15 +42,17 @@ impl<'source, 'temp, 'out> super::ExpressionContext<'source, 'temp, 'out> {
3942
Some(scalars) => scalars,
4043
None => {
4144
let gctx = &self.module.to_ctx();
42-
let source_type = expr_resolution.to_wgsl(gctx);
43-
let dest_type = goal_ty.to_wgsl(gctx);
44-
45-
return Err(super::Error::AutoConversion {
46-
dest_span: goal_span,
47-
dest_type,
48-
source_span: expr_span,
49-
source_type,
50-
});
45+
let source_type = expr_resolution.to_wgsl(gctx).into();
46+
let dest_type = goal_ty.to_wgsl(gctx).into();
47+
48+
return Err(super::Error::AutoConversion(Box::new(
49+
AutoConversionError {
50+
dest_span: goal_span,
51+
dest_type,
52+
source_span: expr_span,
53+
source_type,
54+
},
55+
)));
5156
}
5257
};
5358

@@ -79,13 +84,13 @@ impl<'source, 'temp, 'out> super::ExpressionContext<'source, 'temp, 'out> {
7984

8085
let make_error = || {
8186
let gctx = &self.module.to_ctx();
82-
let source_type = expr_resolution.to_wgsl(gctx);
83-
super::Error::AutoConversionLeafScalar {
87+
let source_type = expr_resolution.to_wgsl(gctx).into();
88+
super::Error::AutoConversionLeafScalar(Box::new(AutoConversionLeafScalarError {
8489
dest_span: goal_span,
85-
dest_scalar: goal_scalar.to_wgsl(),
90+
dest_scalar: goal_scalar.to_wgsl().into(),
8691
source_span: expr_span,
8792
source_type,
88-
}
93+
}))
8994
};
9095

9196
let expr_scalar = match expr_inner.scalar() {
@@ -116,7 +121,7 @@ impl<'source, 'temp, 'out> super::ExpressionContext<'source, 'temp, 'out> {
116121
if let crate::TypeInner::Array { .. } = *expr_inner {
117122
self.as_const_evaluator()
118123
.cast_array(expr, goal_scalar, expr_span)
119-
.map_err(|err| super::Error::ConstantEvaluatorError(err, expr_span))
124+
.map_err(|err| super::Error::ConstantEvaluatorError(err.into(), expr_span))
120125
} else {
121126
let cast = crate::Expression::As {
122127
expr,
@@ -254,12 +259,12 @@ impl<'source, 'temp, 'out> super::ExpressionContext<'source, 'temp, 'out> {
254259
// it has one. Also, avoid holding the borrow of `inner`
255260
// across the call to `cast_array`.
256261
let expr_type = &self.typifier()[expr];
257-
super::Error::ConcretizationFailed {
262+
super::Error::ConcretizationFailed(Box::new(ConcretizationFailedError {
258263
expr_span,
259-
expr_type: expr_type.to_wgsl(&self.module.to_ctx()),
260-
scalar: concretized.to_wgsl(),
264+
expr_type: expr_type.to_wgsl(&self.module.to_ctx()).into(),
265+
scalar: concretized.to_wgsl().into(),
261266
inner: err,
262-
}
267+
}))
263268
})?;
264269
}
265270
}

0 commit comments

Comments
 (0)