@@ -10,6 +10,9 @@ use std::ops::Range;
10
10
use termcolor:: { ColorChoice , NoColor , StandardStream } ;
11
11
use thiserror:: Error ;
12
12
13
+ #[ cfg( test) ]
14
+ use std:: mem:: size_of;
15
+
13
16
#[ derive( Clone , Debug ) ]
14
17
pub struct ParseError {
15
18
message : String ,
@@ -144,7 +147,7 @@ pub enum InvalidAssignmentType {
144
147
}
145
148
146
149
#[ derive( Clone , Debug ) ]
147
- pub enum Error < ' a > {
150
+ pub ( crate ) enum Error < ' a > {
148
151
Unexpected ( Span , ExpectedToken < ' a > ) ,
149
152
UnexpectedComponents ( Span ) ,
150
153
UnexpectedOperationInConstContext ( Span ) ,
@@ -154,8 +157,8 @@ pub enum Error<'a> {
154
157
BadTexture ( Span ) ,
155
158
BadTypeCast {
156
159
span : Span ,
157
- from_type : String ,
158
- to_type : String ,
160
+ from_type : Box < str > ,
161
+ to_type : Box < str > ,
159
162
} ,
160
163
BadTextureSampleType {
161
164
span : Span ,
@@ -188,8 +191,8 @@ pub enum Error<'a> {
188
191
TypeNotInferable ( Span ) ,
189
192
InitializationTypeMismatch {
190
193
name : Span ,
191
- expected : String ,
192
- got : String ,
194
+ expected : Box < str > ,
195
+ got : Box < str > ,
193
196
} ,
194
197
DeclMissingTypeAndInit ( Span ) ,
195
198
MissingAttribute ( & ' static str , Span ) ,
@@ -232,7 +235,7 @@ pub enum Error<'a> {
232
235
/// name is `decl` has an identifier at `reference` whose definition is
233
236
/// the next declaration in the cycle. The last pair's `reference` is
234
237
/// the same identifier as `ident`, above.
235
- path : Vec < ( Span , Span ) > ,
238
+ path : Box < [ ( Span , Span ) ] > ,
236
239
} ,
237
240
InvalidSwitchValue {
238
241
uint : bool ,
@@ -251,32 +254,41 @@ pub enum Error<'a> {
251
254
ExpectedNonNegative ( Span ) ,
252
255
ExpectedPositiveArrayLength ( Span ) ,
253
256
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 > ) ,
273
261
ExceededLimitForNestedBraces {
274
262
span : Span ,
275
263
limit : u8 ,
276
264
} ,
277
265
PipelineConstantIDValue ( Span ) ,
278
266
}
279
267
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
+
280
292
impl < ' a > Error < ' a > {
281
293
#[ cold]
282
294
#[ inline( never) ]
@@ -738,45 +750,55 @@ impl<'a> Error<'a> {
738
750
) ] ,
739
751
notes : vec ! [ ] ,
740
752
} ,
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
+ }
754
770
} ,
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
+ }
768
787
} ,
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
+ }
780
802
} ,
781
803
Error :: ExceededLimitForNestedBraces { span, limit } => ParseError {
782
804
message : "brace nesting limit reached" . into ( ) ,
@@ -796,3 +818,8 @@ impl<'a> Error<'a> {
796
818
}
797
819
}
798
820
}
821
+
822
+ #[ test]
823
+ fn test_error_size ( ) {
824
+ assert ! ( size_of:: <Error <' _>>( ) <= 48 ) ;
825
+ }
0 commit comments