From 92f970a69d7e0c8a223daf9beba45d348d4ee70e Mon Sep 17 00:00:00 2001 From: sotrh Date: Fri, 15 Jan 2021 07:33:48 -0700 Subject: [PATCH] Change ErrorKind::SemanticError to use Cow<'static, str> instead of &'static str (#356) * Changed front::glsl::error::ErrorKind::Semantic to use Cow * cargo fmt * appeasing clippy * Update src/front/glsl/functions.rs Co-authored-by: monocodus[bot] <49363530+monocodus[bot]@users.noreply.github.com> * Fixed more clippy errors Co-authored-by: Dzmitry Malyshau Co-authored-by: monocodus[bot] <49363530+monocodus[bot]@users.noreply.github.com> --- src/front/glsl/ast.rs | 2 +- src/front/glsl/error.rs | 4 ++-- src/front/glsl/functions.rs | 17 +++++++++-------- src/front/glsl/parser.rs | 12 ++++++------ src/front/glsl/variables.rs | 14 +++++++++++--- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/front/glsl/ast.rs b/src/front/glsl/ast.rs index 58161ad7be..6f3669a75a 100644 --- a/src/front/glsl/ast.rs +++ b/src/front/glsl/ast.rs @@ -72,7 +72,7 @@ impl Program { &resolve_ctx, ) { //TODO: better error report - Err(_) => Err(ErrorKind::SemanticError("Can't resolve type")), + Err(_) => Err(ErrorKind::SemanticError("Can't resolve type".into())), Ok(()) => Ok(self.context.typifier.get(handle, &self.module.types)), } } diff --git a/src/front/glsl/error.rs b/src/front/glsl/error.rs index 66f3e9f5d7..5c2d6f44c2 100644 --- a/src/front/glsl/error.rs +++ b/src/front/glsl/error.rs @@ -1,6 +1,6 @@ use super::parser::Token; use super::token::TokenMetadata; -use std::{fmt, io}; +use std::{borrow::Cow, fmt, io}; #[derive(Debug)] pub enum ErrorKind { @@ -20,7 +20,7 @@ pub enum ErrorKind { #[cfg(feature = "glsl-validate")] VariableNotAvailable(String), ExpectedConstant, - SemanticError(&'static str), + SemanticError(Cow<'static, str>), PreprocessorError(String), WrongNumberArgs(String, usize, usize), } diff --git a/src/front/glsl/functions.rs b/src/front/glsl/functions.rs index 91ac166f2c..fa07d891ec 100644 --- a/src/front/glsl/functions.rs +++ b/src/front/glsl/functions.rs @@ -10,9 +10,9 @@ impl Program { match fc.kind { FunctionCallKind::TypeConstructor(ty) => { let h = if fc.args.len() == 1 { - let kind = self.module.types[ty].inner.scalar_kind().ok_or( - ErrorKind::SemanticError("Can only cast to scalar or vector"), - )?; + let kind = self.module.types[ty].inner.scalar_kind().ok_or_else(|| { + ErrorKind::SemanticError("Can only cast to scalar or vector".into()) + })?; self.context.expressions.append(Expression::As { kind, expr: fc.args[0].expression, @@ -78,7 +78,7 @@ impl Program { .collect(), }) } else { - Err(ErrorKind::SemanticError("Bad call to texture")) + Err(ErrorKind::SemanticError("Bad call to texture".into())) } } "ceil" | "round" | "floor" | "fract" | "trunc" => { @@ -104,10 +104,11 @@ impl Program { }) } func_name => { - let function = *self - .lookup_function - .get(func_name) - .ok_or(ErrorKind::SemanticError("Unknown function"))?; + let function = *self.lookup_function.get(func_name).ok_or_else(|| { + ErrorKind::SemanticError( + format!("Unknown function: {}", func_name).into(), + ) + })?; Ok(ExpressionRule { expression: self.context.expressions.append(Expression::Call { function, diff --git a/src/front/glsl/parser.rs b/src/front/glsl/parser.rs index efc09fc8f1..bd0cbb7e46 100644 --- a/src/front/glsl/parser.rs +++ b/src/front/glsl/parser.rs @@ -517,7 +517,7 @@ pomelo! { } single_declaration ::= fully_specified_type(t) { - let ty = t.1.ok_or(ErrorKind::SemanticError("Empty type for declaration"))?; + let ty = t.1.ok_or_else(||ErrorKind::SemanticError("Empty type for declaration".into()))?; VarDeclaration { type_qualifiers: t.0, @@ -526,7 +526,7 @@ pomelo! { } } single_declaration ::= fully_specified_type(t) Identifier(i) { - let ty = t.1.ok_or(ErrorKind::SemanticError("Empty type for declaration"))?; + let ty = t.1.ok_or_else(|| ErrorKind::SemanticError("Empty type for declaration".into()))?; VarDeclaration { type_qualifiers: t.0, @@ -537,7 +537,7 @@ pomelo! { // single_declaration ::= fully_specified_type Identifier array_specifier; // single_declaration ::= fully_specified_type Identifier array_specifier Equal initializer; single_declaration ::= fully_specified_type(t) Identifier(i) Equal initializer(init) { - let ty = t.1.ok_or(ErrorKind::SemanticError("Empty type for declaration"))?; + let ty = t.1.ok_or_else(|| ErrorKind::SemanticError("Empty type for declaration".into()))?; VarDeclaration { type_qualifiers: t.0, @@ -676,7 +676,7 @@ pomelo! { ty, }).collect() } else { - return Err(ErrorKind::SemanticError("Struct member can't be void")) + return Err(ErrorKind::SemanticError("Struct member can't be void".into())) } } //struct_declaration ::= type_qualifier type_specifier struct_declarator_list Semicolon; @@ -707,7 +707,7 @@ pomelo! { // local variables if let Some(d) = d { for (id, initializer) in d.ids_initializers { - let id = id.ok_or(ErrorKind::SemanticError("local var must be named"))?; + let id = id.ok_or_else(|| ErrorKind::SemanticError("Local var must be named".into()))?; // check if already declared in current scope #[cfg(feature = "glsl-validate")] { @@ -1002,7 +1002,7 @@ pomelo! { if let Some(d) = d { let class = d.type_qualifiers.iter().find_map(|tq| { if let TypeQualifier::StorageClass(sc) = tq { Some(*sc) } else { None } - }).ok_or(ErrorKind::SemanticError("Missing storage class for global var"))?; + }).ok_or_else(|| ErrorKind::SemanticError(format!("Missing storage class for global var \"{:?}\"", d).into()))?; let binding = d.type_qualifiers.iter().find_map(|tq| { if let TypeQualifier::Binding(b) = tq { Some(b.clone()) } else { None } diff --git a/src/front/glsl/variables.rs b/src/front/glsl/variables.rs index 37335987d6..70022a2ac5 100644 --- a/src/front/glsl/variables.rs +++ b/src/front/glsl/variables.rs @@ -204,7 +204,11 @@ impl Program { 4 => VectorSize::Quad, _ => { return Err(ErrorKind::SemanticError( - "Bad swizzle size", + format!( + "Bad swizzle size for \"{:?}\": {:?}", + name, v + ) + .into(), )); } }, @@ -214,10 +218,14 @@ impl Program { })) } } else { - Err(ErrorKind::SemanticError("Invalid swizzle for vector")) + Err(ErrorKind::SemanticError( + format!("Invalid swizzle for vector \"{}\"", name).into(), + )) } } - _ => Err(ErrorKind::SemanticError("Can't lookup field on this type")), + _ => Err(ErrorKind::SemanticError( + format!("Can't lookup field on this type \"{}\"", name).into(), + )), } } }