Skip to content

Box error values in Results #7346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions naga/src/front/wgsl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ pub(crate) enum Error<'a> {
BadTexture(Span),
BadTypeCast {
span: Span,
from_type: Box<str>,
to_type: Box<str>,
from_type: String,
to_type: String,
},
BadTextureSampleType {
span: Span,
Expand Down Expand Up @@ -211,8 +211,8 @@ pub(crate) enum Error<'a> {
TypeNotInferable(Span),
InitializationTypeMismatch {
name: Span,
expected: Box<str>,
got: Box<str>,
expected: String,
got: String,
},
DeclMissingTypeAndInit(Span),
MissingAttribute(&'static str, Span),
Expand Down Expand Up @@ -342,24 +342,24 @@ impl From<&'static str> for DiagnosticAttributeNotSupportedPosition {
#[derive(Clone, Debug)]
pub(crate) struct AutoConversionError {
pub dest_span: Span,
pub dest_type: Box<str>,
pub dest_type: String,
pub source_span: Span,
pub source_type: Box<str>,
pub source_type: String,
}

#[derive(Clone, Debug)]
pub(crate) struct AutoConversionLeafScalarError {
pub dest_span: Span,
pub dest_scalar: Box<str>,
pub dest_scalar: String,
pub source_span: Span,
pub source_type: Box<str>,
pub source_type: String,
}

#[derive(Clone, Debug)]
pub(crate) struct ConcretizationFailedError {
pub expr_span: Span,
pub expr_type: Box<str>,
pub scalar: Box<str>,
pub expr_type: String,
pub scalar: String,
pub inner: ConstantEvaluatorError,
}

Expand Down Expand Up @@ -1179,8 +1179,3 @@ impl<'a> Error<'a> {
}
}
}

#[test]
fn test_error_size() {
assert!(size_of::<Error<'_>>() <= 48);
}
22 changes: 11 additions & 11 deletions naga/src/front/wgsl/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloc::{vec, vec::Vec};
use alloc::{boxed::Box, vec, vec::Vec};

use super::Error;
use super::{Error, Result};
use crate::front::wgsl::parse::ast;
use crate::{FastHashMap, Handle, Span};

Expand All @@ -17,20 +17,20 @@ impl<'a> Index<'a> {
///
/// Return an error if the graph of references between declarations contains
/// any cycles.
pub fn generate(tu: &ast::TranslationUnit<'a>) -> Result<Self, Error<'a>> {
pub fn generate(tu: &ast::TranslationUnit<'a>) -> Result<'a, Self> {
// Produce a map from global definitions' names to their `Handle<GlobalDecl>`s.
// While doing so, reject conflicting definitions.
let mut globals = FastHashMap::with_capacity_and_hasher(tu.decls.len(), Default::default());
for (handle, decl) in tu.decls.iter() {
if let Some(ident) = decl_ident(decl) {
let name = ident.name;
if let Some(old) = globals.insert(name, handle) {
return Err(Error::Redefinition {
return Err(Box::new(Error::Redefinition {
previous: decl_ident(&tu.decls[old])
.expect("decl should have ident for redefinition")
.span,
current: ident.span,
});
}));
}
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ struct DependencySolver<'source, 'temp> {

impl<'a> DependencySolver<'a, '_> {
/// Produce the sorted list of declaration handles, and check for cycles.
fn solve(mut self) -> Result<Vec<Handle<ast::GlobalDecl<'a>>>, Error<'a>> {
fn solve(mut self) -> Result<'a, Vec<Handle<ast::GlobalDecl<'a>>>> {
for (id, _) in self.module.decls.iter() {
if self.visited[id.index()] {
continue;
Expand All @@ -117,7 +117,7 @@ impl<'a> DependencySolver<'a, '_> {

/// Ensure that all declarations used by `id` have been added to the
/// ordering, and then append `id` itself.
fn dfs(&mut self, id: Handle<ast::GlobalDecl<'a>>) -> Result<(), Error<'a>> {
fn dfs(&mut self, id: Handle<ast::GlobalDecl<'a>>) -> Result<'a, ()> {
let decl = &self.module.decls[id];
let id_usize = id.index();

Expand All @@ -134,10 +134,10 @@ impl<'a> DependencySolver<'a, '_> {
// Found a cycle.
return if dep_id == id {
// A declaration refers to itself directly.
Err(Error::RecursiveDeclaration {
Err(Box::new(Error::RecursiveDeclaration {
ident: decl_ident(decl).expect("decl should have ident").span,
usage: dep.usage,
})
}))
} else {
// A declaration refers to itself indirectly, through
// one or more other definitions. Report the entire path
Expand All @@ -150,7 +150,7 @@ impl<'a> DependencySolver<'a, '_> {
.find_map(|(i, dep)| (dep.decl == dep_id).then_some(i))
.unwrap_or(0);

Err(Error::CyclicDeclaration {
Err(Box::new(Error::CyclicDeclaration {
ident: decl_ident(&self.module.decls[dep_id])
.expect("decl should have ident")
.span,
Expand All @@ -166,7 +166,7 @@ impl<'a> DependencySolver<'a, '_> {
)
})
.collect(),
})
}))
};
} else if !self.visited[dep_id_usize] {
self.dfs(dep_id)?;
Expand Down
33 changes: 17 additions & 16 deletions naga/src/front/wgsl/lower/construction.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use alloc::{
boxed::Box,
format,
string::{String, ToString},
vec,
vec::Vec,
};
use core::num::NonZeroU32;

use crate::front::wgsl::error::Error;
use crate::front::wgsl::lower::{ExpressionContext, Lowerer};
use crate::front::wgsl::parse::ast;
use crate::front::wgsl::{Error, Result};
use crate::{Handle, Span};

/// A cooked form of `ast::ConstructorType` that uses Naga types whenever
Expand Down Expand Up @@ -119,7 +120,7 @@ impl<'source> Lowerer<'source, '_> {
ty_span: Span,
components: &[Handle<ast::Expression<'source>>],
ctx: &mut ExpressionContext<'source, '_, '_>,
) -> Result<Handle<crate::Expression>, Error<'source>> {
) -> Result<'source, Handle<crate::Expression>> {
use crate::proc::TypeResolution as Tr;

let constructor_h = self.constructor(constructor, ctx)?;
Expand All @@ -141,7 +142,7 @@ impl<'source> Lowerer<'source, '_> {
let components = ast_components
.iter()
.map(|&expr| self.expression_for_abstract(expr, ctx))
.collect::<Result<_, _>>()?;
.collect::<Result<_>>()?;
let spans = ast_components
.iter()
.map(|&expr| ctx.ast_expressions.get_span(expr))
Expand Down Expand Up @@ -172,7 +173,7 @@ impl<'source> Lowerer<'source, '_> {
| Constructor::PartialArray => {
// We have no arguments from which to infer the result type, so
// partial constructors aren't acceptable here.
return Err(Error::TypeNotInferable(ty_span));
return Err(Box::new(Error::TypeNotInferable(ty_span)));
}
},

Expand Down Expand Up @@ -373,7 +374,7 @@ impl<'source> Lowerer<'source, '_> {
Default::default(),
)
})
.collect::<Result<Vec<_>, _>>()?;
.collect::<Result<Vec<_>>>()?;

let ty = ctx.ensure_type_exists(crate::TypeInner::Matrix {
columns,
Expand Down Expand Up @@ -410,7 +411,7 @@ impl<'source> Lowerer<'source, '_> {
Default::default(),
)
})
.collect::<Result<Vec<_>, _>>()?;
.collect::<Result<Vec<_>>>()?;

let ty = ctx.ensure_type_exists(crate::TypeInner::Matrix {
columns,
Expand Down Expand Up @@ -535,12 +536,12 @@ impl<'source> Lowerer<'source, '_> {

// Bad conversion (type cast)
(Components::One { span, ty_inner, .. }, constructor) => {
let from_type = ty_inner.to_wgsl(&ctx.module.to_ctx()).into();
return Err(Error::BadTypeCast {
let from_type = ty_inner.to_wgsl(&ctx.module.to_ctx());
return Err(Box::new(Error::BadTypeCast {
span,
from_type,
to_type: constructor.to_error_string(ctx).into(),
});
to_type: constructor.to_error_string(ctx),
}));
}

// Too many parameters for scalar constructor
Expand All @@ -549,11 +550,11 @@ impl<'source> Lowerer<'source, '_> {
Constructor::Type((_, &crate::TypeInner::Scalar { .. })),
) => {
let span = spans[1].until(spans.last().unwrap());
return Err(Error::UnexpectedComponents(span));
return Err(Box::new(Error::UnexpectedComponents(span)));
}

// Other types can't be constructed
_ => return Err(Error::TypeNotConstructible(ty_span)),
_ => return Err(Box::new(Error::TypeNotConstructible(ty_span))),
}

let expr = ctx.append_expression(expr, span)?;
Expand All @@ -576,7 +577,7 @@ impl<'source> Lowerer<'source, '_> {
&mut self,
constructor: &ast::ConstructorType<'source>,
ctx: &mut ExpressionContext<'source, '_, 'out>,
) -> Result<Constructor<Handle<crate::Type>>, Error<'source>> {
) -> Result<'source, Constructor<Handle<crate::Type>>> {
let handle = match *constructor {
ast::ConstructorType::Scalar(scalar) => {
let ty = ctx.ensure_type_exists(scalar.to_inner_scalar());
Expand All @@ -587,7 +588,7 @@ impl<'source> Lowerer<'source, '_> {
let ty = self.resolve_ast_type(ty, &mut ctx.as_const())?;
let scalar = match ctx.module.types[ty].inner {
crate::TypeInner::Scalar(sc) => sc,
_ => return Err(Error::UnknownScalarType(ty_span)),
_ => return Err(Box::new(Error::UnknownScalarType(ty_span))),
};
let ty = ctx.ensure_type_exists(crate::TypeInner::Vector { size, scalar });
Constructor::Type(ty)
Expand All @@ -604,15 +605,15 @@ impl<'source> Lowerer<'source, '_> {
let ty = self.resolve_ast_type(ty, &mut ctx.as_const())?;
let scalar = match ctx.module.types[ty].inner {
crate::TypeInner::Scalar(sc) => sc,
_ => return Err(Error::UnknownScalarType(ty_span)),
_ => return Err(Box::new(Error::UnknownScalarType(ty_span))),
};
let ty = match scalar.kind {
crate::ScalarKind::Float => ctx.ensure_type_exists(crate::TypeInner::Matrix {
columns,
rows,
scalar,
}),
_ => return Err(Error::BadMatrixScalarKind(ty_span, scalar)),
_ => return Err(Box::new(Error::BadMatrixScalarKind(ty_span, scalar))),
};
Constructor::Type(ty)
}
Expand Down
Loading
Loading