Skip to content

Expose the Error type for WGSL frontend #7855

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

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions naga/src/diagnostic_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ impl IntoIterator for DiagnosticFilterMap {
}
}

/// An error returned by [`DiagnosticFilterMap::add`] when it encounters conflicting rules.
/// An error returned when conflicting rules are present.
#[cfg(feature = "wgsl-in")]
#[derive(Clone, Debug)]
pub(crate) struct ConflictingDiagnosticRuleError {
pub struct ConflictingDiagnosticRuleError {
pub triggering_rule_spans: [Span; 2],
}

Expand Down
12 changes: 6 additions & 6 deletions naga/src/front/wgsl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub enum InvalidAssignmentType {
}

#[derive(Clone, Debug)]
pub(crate) enum Error<'a> {
pub enum Error<'a> {
Unexpected(Span, ExpectedToken<'a>),
UnexpectedComponents(Span),
UnexpectedOperationInConstContext(Span),
Expand Down Expand Up @@ -419,7 +419,7 @@ impl From<ConflictingDiagnosticRuleError> for Error<'_> {

/// Used for diagnostic refinement in [`Error::DiagnosticAttributeNotSupported`].
#[derive(Clone, Copy, Debug)]
pub(crate) enum DiagnosticAttributeNotSupportedPosition {
pub enum DiagnosticAttributeNotSupportedPosition {
SemicolonInModulePosition,
Other { display_plural: &'static str },
}
Expand All @@ -431,23 +431,23 @@ impl From<&'static str> for DiagnosticAttributeNotSupportedPosition {
}

#[derive(Clone, Debug)]
pub(crate) struct AutoConversionError {
pub struct AutoConversionError {
pub dest_span: Span,
pub dest_type: String,
pub source_span: Span,
pub source_type: String,
}

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

#[derive(Clone, Debug)]
pub(crate) struct ConcretizationFailedError {
pub struct ConcretizationFailedError {
pub expr_span: Span,
pub expr_type: String,
pub scalar: String,
Expand All @@ -457,7 +457,7 @@ pub(crate) struct ConcretizationFailedError {
impl<'a> Error<'a> {
#[cold]
#[inline(never)]
pub(crate) fn as_parse_error(&self, source: &'a str) -> ParseError {
pub fn as_parse_error(&self, source: &'a str) -> ParseError {
match *self {
Error::Unexpected(unexpected_span, expected) => {
let expected_str = match expected {
Expand Down
13 changes: 5 additions & 8 deletions naga/src/front/wgsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod parse;
#[cfg(test)]
mod tests;

pub use crate::front::wgsl::error::ParseError;
pub use crate::front::wgsl::error::{Error, ParseError};
pub use crate::front::wgsl::parse::directive::language_extension::{
ImplementedLanguageExtension, LanguageExtension, UnimplementedLanguageExtension,
};
Expand All @@ -20,7 +20,6 @@ pub use crate::front::wgsl::parse::Options;
use alloc::boxed::Box;
use thiserror::Error;

use crate::front::wgsl::error::Error;
use crate::front::wgsl::lower::Lowerer;
use crate::front::wgsl::parse::Parser;
use crate::Scalar;
Expand Down Expand Up @@ -49,11 +48,7 @@ impl Frontend {
}
}

pub fn parse(&mut self, source: &str) -> core::result::Result<crate::Module, ParseError> {
self.inner(source).map_err(|x| x.as_parse_error(source))
}

fn inner<'a>(&mut self, source: &'a str) -> Result<'a, crate::Module> {
pub fn parse<'a>(&mut self, source: &'a str) -> Result<'a, crate::Module> {
let tu = self.parser.parse(source, &self.options)?;
let index = index::Index::generate(&tu)?;
let module = Lowerer::new(&index).lower(tu)?;
Expand All @@ -74,7 +69,9 @@ impl Frontend {
///
/// </div>
pub fn parse_str(source: &str) -> core::result::Result<crate::Module, ParseError> {
Frontend::new().parse(source)
Frontend::new()
.parse(source)
.map_err(|e| e.as_parse_error(source))
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions naga/src/front/wgsl/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ fn parse_repeated_attributes() {
let span_end = span_start + name_length;
let expected_span = Span::new(span_start, span_end);

let result = Frontend::new().inner(&shader);
let result = Frontend::new().parse(&shader);
assert!(matches!(
*result.unwrap_err(),
Error::RepeatedAttribute(span) if span == expected_span
Expand All @@ -698,7 +698,7 @@ fn parse_missing_workgroup_size() {
};

let shader = "@compute fn vs() -> vec4<f32> { return vec4<f32>(0.0); }";
let result = Frontend::new().inner(shader);
let result = Frontend::new().parse(shader);
assert!(matches!(
*result.unwrap_err(),
Error::MissingWorkgroupSize(span) if span == Span::new(1, 8)
Expand Down
3 changes: 2 additions & 1 deletion naga/tests/naga/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,8 @@ fn convert_snapshots_wgsl() {
Ok(mut module) => check_targets(&input, &mut module, Some(&source)),
Err(e) => panic!(
"{}",
e.emit_to_string_with_path(&source, input.input_path())
e.as_parse_error(&source)
.emit_to_string_with_path(&source, input.input_path())
),
}
}
Expand Down