Skip to content

Make naga span methods take path as generic AsRef Path #7643

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 9 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Bottom level categories:
#### Naga

- When emitting GLSL, Uniform and Storage Buffer memory layouts are now emitted even if no explicit binding is given. By @cloone8 in [#7579](https://github.com/gfx-rs/wgpu/pull/7579).
- Diagnostic rendering methods (i.e., `naga::{front::wgsl::ParseError,WithSpan}::emit_error_to_string_with_path`) now accept more types for their `path` argument via a new sealed `AsDiagnosticFilePath` trait. By @atlv24 and @ErichDonGubler in [#7643](https://github.com/gfx-rs/wgpu/pull/7643).
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think its probably worth putting bushRAT in here too


### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions naga/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ default = []
dot-out = []
glsl-in = ["dep:pp-rs"]
glsl-out = []
std = []

## Enables outputting to the Metal Shading Language (MSL).
##
Expand Down
76 changes: 76 additions & 0 deletions naga/src/as_diagnostic_file_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! [`AsDiagnosticFilePath`] and its supporting items.

use alloc::borrow::Cow;

#[cfg(feature = "std")]
use std::path::Path;

#[cfg(not(feature = "std"))]
use alloc::string::String;

mod sealed {
pub trait Sealed {}
}

/// A trait that abstracts over types accepted for conversion to the most
/// featureful path representation possible; that is:
///
/// - When `no_std` is active, this represents types can be converted to `Cow<'_, str>`.
/// - Otherwise, types that implement `AsRef<Path>` (to extract a `&Path`).
///
/// This type is used as the type bounds for various diagnostic rendering methods, i.e.,
/// [`WithSpan::emit_to_string_with_path`](crate::span::WithSpan::emit_to_string_with_path).
pub trait AsDiagnosticFilePath: sealed::Sealed {
fn to_string_lossy(&self) -> Cow<'_, str>;
}

#[cfg(feature = "std")]
impl<T: AsRef<Path> + ?Sized> AsDiagnosticFilePath for T {
fn to_string_lossy(&self) -> Cow<'_, str> {
self.as_ref().to_string_lossy()
}
}

#[cfg(feature = "std")]
impl<T: AsRef<Path> + ?Sized> sealed::Sealed for T {}

#[cfg(not(feature = "std"))]
impl AsDiagnosticFilePath for String {
fn to_string_lossy(&self) -> Cow<'_, str> {
Cow::Borrowed(self.as_str())
}
}

#[cfg(not(feature = "std"))]
impl sealed::Sealed for String {}

#[cfg(not(feature = "std"))]
impl AsDiagnosticFilePath for str {
fn to_string_lossy(&self) -> Cow<'_, str> {
Cow::Borrowed(self)
}
}

#[cfg(not(feature = "std"))]
impl sealed::Sealed for str {}

#[cfg(not(feature = "std"))]
impl AsDiagnosticFilePath for Cow<'_, str> {
fn to_string_lossy(&self) -> Cow<'_, str> {
use core::borrow::Borrow;
Cow::Borrowed(self.borrow())
}
}

#[cfg(not(feature = "std"))]
impl sealed::Sealed for Cow<'_, str> {}

#[cfg(not(feature = "std"))]
impl<T: AsDiagnosticFilePath + ?Sized> AsDiagnosticFilePath for &T {
fn to_string_lossy(&self) -> Cow<'_, str> {
(*self).to_string_lossy()
}
}

#[cfg(not(feature = "std"))]
impl<T: AsDiagnosticFilePath + ?Sized> sealed::Sealed for &T {}
1 change: 1 addition & 0 deletions naga/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ extern crate std;
extern crate alloc;

mod arena;
mod as_diagnostic_file_path;
pub mod back;
pub mod common;
#[cfg(feature = "compact")]
Expand Down
10 changes: 7 additions & 3 deletions naga/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloc::{
};
use core::{error::Error, fmt, ops::Range};

use crate::{Arena, Handle, UniqueArena};
use crate::{as_diagnostic_file_path::AsDiagnosticFilePath, Arena, Handle, UniqueArena};

/// A source code span, used for error reporting.
#[derive(Clone, Copy, Debug, PartialEq, Default)]
Expand Down Expand Up @@ -283,12 +283,14 @@ impl<E> WithSpan<E> {

/// Emits a summary of the error to standard error stream.
#[cfg(feature = "stderr")]
pub fn emit_to_stderr_with_path(&self, source: &str, path: &str)
pub fn emit_to_stderr_with_path<P>(&self, source: &str, path: P)
where
E: Error,
P: AsDiagnosticFilePath,
{
use codespan_reporting::{files, term};

let path = path.to_string_lossy();
let files = files::SimpleFile::new(path, source);
let config = term::Config::default();

Expand All @@ -313,12 +315,14 @@ impl<E> WithSpan<E> {
}

/// Emits a summary of the error to a string.
pub fn emit_to_string_with_path(&self, source: &str, path: &str) -> String
pub fn emit_to_string_with_path<P>(&self, source: &str, path: P) -> String
where
E: Error,
P: AsDiagnosticFilePath,
{
use codespan_reporting::{files, term};

let path = path.to_string_lossy();
let files = files::SimpleFile::new(path, source);
let config = term::Config::default();

Expand Down
Loading