From 8d1513d3357717897fe316c5c8667eaf4d3e1e1c Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Fri, 29 Sep 2023 15:55:58 +0200 Subject: [PATCH] use ast spans for errors --- src/front/wgsl/lower/construction.rs | 16 +--- src/front/wgsl/lower/mod.rs | 119 ++++++++++++--------------- 2 files changed, 56 insertions(+), 79 deletions(-) diff --git a/src/front/wgsl/lower/construction.rs b/src/front/wgsl/lower/construction.rs index 970f63dca7..59d7b17435 100644 --- a/src/front/wgsl/lower/construction.rs +++ b/src/front/wgsl/lower/construction.rs @@ -582,20 +582,12 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { ast::ConstructorType::PartialArray => ConcreteConstructorHandle::PartialArray, ast::ConstructorType::Array { base, size } => { let base = self.resolve_ast_type(base, ctx.as_global())?; - let size = match size { - ast::ArraySize::Constant(expr) => { - let const_expr = self.expression(expr, ctx.as_const())?; - crate::ArraySize::Constant(ctx.as_const().array_length(const_expr)?) - } - ast::ArraySize::Dynamic => crate::ArraySize::Dynamic, - }; + let size = self.array_size(size, ctx.as_global())?; self.layouter.update(ctx.module.to_ctx()).unwrap(); - let ty = ctx.ensure_type_exists(crate::TypeInner::Array { - base, - size, - stride: self.layouter[base].to_stride(), - }); + let stride = self.layouter[base].to_stride(); + + let ty = ctx.ensure_type_exists(crate::TypeInner::Array { base, size, stride }); ConcreteConstructorHandle::Type(ty) } ast::ConstructorType::Type(ty) => ConcreteConstructorHandle::Type(ty), diff --git a/src/front/wgsl/lower/mod.rs b/src/front/wgsl/lower/mod.rs index 31136acd16..38fe317e78 100644 --- a/src/front/wgsl/lower/mod.rs +++ b/src/front/wgsl/lower/mod.rs @@ -396,57 +396,30 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> { } } - fn array_length( - &mut self, - const_expr: Handle, - ) -> Result> { - match self.expr_type { - ExpressionContextType::Runtime(_) => { - unreachable!() - } - ExpressionContextType::Constant => { - let span = self.module.const_expressions.get_span(const_expr); - let len = - self.module - .to_ctx() - .eval_expr_to_u32(const_expr) - .map_err(|err| match err { - crate::proc::U32EvalError::NonConst => { - Error::ExpectedConstExprConcreteIntegerScalar(span) - } - crate::proc::U32EvalError::Negative => { - Error::ExpectedPositiveArrayLength(span) - } - })?; - NonZeroU32::new(len).ok_or(Error::ExpectedPositiveArrayLength(span)) - } - } - } - fn gather_component( &mut self, expr: Handle, + component_span: Span, gather_span: Span, ) -> Result> { match self.expr_type { ExpressionContextType::Runtime(ref rctx) => { - let expr_span = rctx.naga_expressions.get_span(expr); let index = self .module .to_ctx() .eval_expr_to_u32_from(expr, rctx.naga_expressions) .map_err(|err| match err { crate::proc::U32EvalError::NonConst => { - Error::ExpectedConstExprConcreteIntegerScalar(expr_span) + Error::ExpectedConstExprConcreteIntegerScalar(component_span) } crate::proc::U32EvalError::Negative => { - Error::ExpectedNonNegative(expr_span) + Error::ExpectedNonNegative(component_span) } })?; crate::SwizzleComponent::XYZW .get(index as usize) .copied() - .ok_or(Error::InvalidGatherComponent(expr_span)) + .ok_or(Error::InvalidGatherComponent(component_span)) } // This means a `gather` operation appeared in a constant expression. // This error refers to the `gather` itself, not its "component" argument. @@ -1255,6 +1228,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { Ok(crate::SwitchCase { value: match case.value { ast::SwitchValue::Expr(expr) => { + let span = ctx.ast_expressions.get_span(expr); let expr = self.expression(expr, ctx.as_global().as_const())?; match ctx.module.to_ctx().eval_expr_to_literal(expr) { Some(crate::Literal::I32(value)) if !uint => { @@ -1264,10 +1238,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { crate::SwitchValue::U32(value) } _ => { - return Err(Error::InvalidSwitchValue { - uint, - span: ctx.module.const_expressions.get_span(expr), - }); + return Err(Error::InvalidSwitchValue { uint, span }); } } } @@ -2335,6 +2306,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { let (image, image_span, gather) = match fun { Texture::Gather => { let image_or_component = args.next()?; + let image_or_component_span = ctx.ast_expressions.get_span(image_or_component); // Gathers from depth textures don't take an initial `component` argument. let lowered_image_or_component = self.expression(image_or_component, ctx.reborrow())?; @@ -2344,20 +2316,21 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { crate::TypeInner::Image { class: crate::ImageClass::Depth { .. }, .. - } => { - let image_span = ctx.ast_expressions.get_span(image_or_component); - ( - lowered_image_or_component, - image_span, - Some(crate::SwizzleComponent::X), - ) - } + } => ( + lowered_image_or_component, + image_or_component_span, + Some(crate::SwizzleComponent::X), + ), _ => { let (image, image_span) = get_image_and_span(self, &mut args, &mut ctx)?; ( image, image_span, - Some(ctx.gather_component(lowered_image_or_component, span)?), + Some(ctx.gather_component( + lowered_image_or_component, + image_or_component_span, + span, + )?), ) } } @@ -2513,8 +2486,8 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { expr: Handle>, mut ctx: ExpressionContext<'source, '_, '_>, ) -> Result<(u32, Span), Error<'source>> { + let span = ctx.ast_expressions.get_span(expr); let expr = self.expression(expr, ctx.reborrow())?; - let span = ctx.module.const_expressions.get_span(expr); let value = ctx .module .to_ctx() @@ -2528,6 +2501,34 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { Ok((value, span)) } + fn array_size( + &mut self, + size: ast::ArraySize<'source>, + mut ctx: GlobalContext<'source, '_, '_>, + ) -> Result> { + Ok(match size { + ast::ArraySize::Constant(expr) => { + let span = ctx.ast_expressions.get_span(expr); + let const_expr = self.expression(expr, ctx.as_const())?; + let len = + ctx.module + .to_ctx() + .eval_expr_to_u32(const_expr) + .map_err(|err| match err { + crate::proc::U32EvalError::NonConst => { + Error::ExpectedConstExprConcreteIntegerScalar(span) + } + crate::proc::U32EvalError::Negative => { + Error::ExpectedPositiveArrayLength(span) + } + })?; + let size = NonZeroU32::new(len).ok_or(Error::ExpectedPositiveArrayLength(span))?; + crate::ArraySize::Constant(size) + } + ast::ArraySize::Dynamic => crate::ArraySize::Dynamic, + }) + } + /// Return a Naga `Handle` representing the front-end type `handle`. fn resolve_ast_type( &mut self, @@ -2555,19 +2556,12 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { } ast::Type::Array { base, size } => { let base = self.resolve_ast_type(base, ctx.reborrow())?; + let size = self.array_size(size, ctx.reborrow())?; + self.layouter.update(ctx.module.to_ctx()).unwrap(); + let stride = self.layouter[base].to_stride(); - crate::TypeInner::Array { - base, - size: match size { - ast::ArraySize::Constant(constant) => { - let const_expr = self.expression(constant, ctx.as_const())?; - crate::ArraySize::Constant(ctx.as_const().array_length(const_expr)?) - } - ast::ArraySize::Dynamic => crate::ArraySize::Dynamic, - }, - stride: self.layouter[base].to_stride(), - } + crate::TypeInner::Array { base, size, stride } } ast::Type::Image { dim, @@ -2583,17 +2577,8 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { ast::Type::RayQuery => crate::TypeInner::RayQuery, ast::Type::BindingArray { base, size } => { let base = self.resolve_ast_type(base, ctx.reborrow())?; - - crate::TypeInner::BindingArray { - base, - size: match size { - ast::ArraySize::Constant(constant) => { - let const_expr = self.expression(constant, ctx.as_const())?; - crate::ArraySize::Constant(ctx.as_const().array_length(const_expr)?) - } - ast::ArraySize::Dynamic => crate::ArraySize::Dynamic, - }, - } + let size = self.array_size(size, ctx.reborrow())?; + crate::TypeInner::BindingArray { base, size } } ast::Type::RayDesc => { return Ok(ctx.module.generate_ray_desc_type());