@@ -535,50 +535,28 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
535535 . map_err ( |e| Box :: new ( Error :: ConstantEvaluatorError ( e. into ( ) , span) ) )
536536 }
537537
538- fn const_eval_expr_to_u32 (
538+ fn get_const_val < T : TryFrom < crate :: Literal , Error = proc :: ConstValueError > > (
539539 & self ,
540540 handle : Handle < ir:: Expression > ,
541- ) -> core:: result:: Result < u32 , proc:: U32EvalError > {
541+ ) -> core:: result:: Result < T , proc:: ConstValueError > {
542542 match self . expr_type {
543543 ExpressionContextType :: Runtime ( ref ctx) => {
544544 if !ctx. local_expression_kind_tracker . is_const ( handle) {
545- return Err ( proc:: U32EvalError :: NonConst ) ;
545+ return Err ( proc:: ConstValueError :: NonConst ) ;
546546 }
547547
548548 self . module
549549 . to_ctx ( )
550- . eval_expr_to_u32_from ( handle, & ctx. function . expressions )
550+ . get_const_val_from ( handle, & ctx. function . expressions )
551551 }
552552 ExpressionContextType :: Constant ( Some ( ref ctx) ) => {
553553 assert ! ( ctx. local_expression_kind_tracker. is_const( handle) ) ;
554554 self . module
555555 . to_ctx ( )
556- . eval_expr_to_u32_from ( handle, & ctx. function . expressions )
556+ . get_const_val_from ( handle, & ctx. function . expressions )
557557 }
558- ExpressionContextType :: Constant ( None ) => self . module . to_ctx ( ) . eval_expr_to_u32 ( handle) ,
559- ExpressionContextType :: Override => Err ( proc:: U32EvalError :: NonConst ) ,
560- }
561- }
562-
563- fn const_eval_expr_to_bool ( & self , handle : Handle < ir:: Expression > ) -> Option < bool > {
564- match self . expr_type {
565- ExpressionContextType :: Runtime ( ref ctx) => {
566- if !ctx. local_expression_kind_tracker . is_const ( handle) {
567- return None ;
568- }
569-
570- self . module
571- . to_ctx ( )
572- . eval_expr_to_bool_from ( handle, & ctx. function . expressions )
573- }
574- ExpressionContextType :: Constant ( Some ( ref ctx) ) => {
575- assert ! ( ctx. local_expression_kind_tracker. is_const( handle) ) ;
576- self . module
577- . to_ctx ( )
578- . eval_expr_to_bool_from ( handle, & ctx. function . expressions )
579- }
580- ExpressionContextType :: Constant ( None ) => self . module . to_ctx ( ) . eval_expr_to_bool ( handle) ,
581- ExpressionContextType :: Override => None ,
558+ ExpressionContextType :: Constant ( None ) => self . module . to_ctx ( ) . get_const_val ( handle) ,
559+ ExpressionContextType :: Override => Err ( proc:: ConstValueError :: NonConst ) ,
582560 }
583561 }
584562
@@ -716,12 +694,14 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
716694 let index = self
717695 . module
718696 . to_ctx ( )
719- . eval_expr_to_u32_from ( expr, & rctx. function . expressions )
697+ . get_const_val_from :: < u32 , _ > ( expr, & rctx. function . expressions )
720698 . map_err ( |err| match err {
721- proc:: U32EvalError :: NonConst => {
699+ proc:: ConstValueError :: NonConst | proc :: ConstValueError :: InvalidType => {
722700 Error :: ExpectedConstExprConcreteIntegerScalar ( component_span)
723701 }
724- proc:: U32EvalError :: Negative => Error :: ExpectedNonNegative ( component_span) ,
702+ proc:: ConstValueError :: Negative => {
703+ Error :: ExpectedNonNegative ( component_span)
704+ }
725705 } ) ?;
726706 ir:: SwizzleComponent :: XYZW
727707 . get ( index as usize )
@@ -1417,11 +1397,14 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
14171397 match ctx
14181398 . module
14191399 . to_ctx ( )
1420- . eval_expr_to_bool_from ( condition, & ctx. module . global_expressions )
1400+ . get_const_val_from ( condition, & ctx. module . global_expressions )
14211401 {
1422- Some ( true ) => Ok ( ( ) ) ,
1423- Some ( false ) => Err ( Error :: ConstAssertFailed ( span) ) ,
1424- _ => Err ( Error :: NotBool ( span) ) ,
1402+ Ok ( true ) => Ok ( ( ) ) ,
1403+ Ok ( false ) => Err ( Error :: ConstAssertFailed ( span) ) ,
1404+ Err ( proc:: ConstValueError :: NonConst | proc:: ConstValueError :: Negative ) => {
1405+ unreachable ! ( )
1406+ }
1407+ Err ( proc:: ConstValueError :: InvalidType ) => Err ( Error :: NotBool ( span) ) ,
14251408 } ?;
14261409 }
14271410 }
@@ -1956,14 +1939,10 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
19561939 match ctx
19571940 . module
19581941 . to_ctx ( )
1959- . eval_expr_to_literal_from ( expr, & ctx. function . expressions )
1942+ . get_const_val_from ( expr, & ctx. function . expressions )
19601943 {
1961- Some ( ir:: Literal :: I32 ( value) ) => {
1962- ir:: SwitchValue :: I32 ( value)
1963- }
1964- Some ( ir:: Literal :: U32 ( value) ) => {
1965- ir:: SwitchValue :: U32 ( value)
1966- }
1944+ Ok ( ir:: Literal :: I32 ( value) ) => ir:: SwitchValue :: I32 ( value) ,
1945+ Ok ( ir:: Literal :: U32 ( value) ) => ir:: SwitchValue :: U32 ( value) ,
19671946 _ => {
19681947 return Err ( Box :: new ( Error :: InvalidSwitchCase {
19691948 span,
@@ -2184,11 +2163,14 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
21842163 match ctx
21852164 . module
21862165 . to_ctx ( )
2187- . eval_expr_to_bool_from ( condition, & ctx. function . expressions )
2166+ . get_const_val_from ( condition, & ctx. function . expressions )
21882167 {
2189- Some ( true ) => Ok ( ( ) ) ,
2190- Some ( false ) => Err ( Error :: ConstAssertFailed ( span) ) ,
2191- _ => Err ( Error :: NotBool ( span) ) ,
2168+ Ok ( true ) => Ok ( ( ) ) ,
2169+ Ok ( false ) => Err ( Error :: ConstAssertFailed ( span) ) ,
2170+ Err ( proc:: ConstValueError :: NonConst | proc:: ConstValueError :: Negative ) => {
2171+ unreachable ! ( )
2172+ }
2173+ Err ( proc:: ConstValueError :: InvalidType ) => Err ( Error :: NotBool ( span) ) ,
21922174 } ?;
21932175
21942176 block. extend ( emitter. finish ( & ctx. function . expressions ) ) ;
@@ -2386,7 +2368,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
23862368 }
23872369 }
23882370
2389- lowered_base. try_map ( |base| match ctx. const_eval_expr_to_u32 ( index) . ok ( ) {
2371+ lowered_base. try_map ( |base| match ctx. get_const_val ( index) . ok ( ) {
23902372 Some ( index) => Ok :: < _ , Box < Error > > ( ir:: Expression :: AccessIndex { base, index } ) ,
23912373 None => {
23922374 // When an abstract array value e is indexed by an expression
@@ -2581,7 +2563,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
25812563 result_var,
25822564 ) ) )
25832565 } else {
2584- let left_val = ctx. const_eval_expr_to_bool ( left) ;
2566+ let left_val: Option < bool > = ctx. get_const_val ( left) . ok ( ) ;
25852567
25862568 if left_val. is_some_and ( |left_val| {
25872569 op == crate :: BinaryOperator :: LogicalAnd && !left_val
@@ -4239,10 +4221,12 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
42394221 let value = ctx
42404222 . module
42414223 . to_ctx ( )
4242- . eval_expr_to_u32 ( expr)
4224+ . get_const_val ( expr)
42434225 . map_err ( |err| match err {
4244- proc:: U32EvalError :: NonConst => Error :: ExpectedConstExprConcreteIntegerScalar ( span) ,
4245- proc:: U32EvalError :: Negative => Error :: ExpectedNonNegative ( span) ,
4226+ proc:: ConstValueError :: NonConst | proc:: ConstValueError :: InvalidType => {
4227+ Error :: ExpectedConstExprConcreteIntegerScalar ( span)
4228+ }
4229+ proc:: ConstValueError :: Negative => Error :: ExpectedNonNegative ( span) ,
42464230 } ) ?;
42474231 Ok ( ( value, span) )
42484232 }
@@ -4258,12 +4242,13 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
42584242 let const_expr = self . expression ( expr, & mut ctx. as_const ( ) ) ;
42594243 match const_expr {
42604244 Ok ( value) => {
4261- let len = ctx. const_eval_expr_to_u32 ( value) . map_err ( |err| {
4245+ let len = ctx. get_const_val ( value) . map_err ( |err| {
42624246 Box :: new ( match err {
4263- proc:: U32EvalError :: NonConst => {
4247+ proc:: ConstValueError :: NonConst
4248+ | proc:: ConstValueError :: InvalidType => {
42644249 Error :: ExpectedConstExprConcreteIntegerScalar ( span)
42654250 }
4266- proc:: U32EvalError :: Negative => {
4251+ proc:: ConstValueError :: Negative => {
42674252 Error :: ExpectedPositiveArrayLength ( span)
42684253 }
42694254 } )
0 commit comments