diff --git a/naga-cli/src/bin/naga.rs b/naga-cli/src/bin/naga.rs index bbdc63fad..b620ecc70 100644 --- a/naga-cli/src/bin/naga.rs +++ b/naga-cli/src/bin/naga.rs @@ -452,7 +452,7 @@ fn run() -> anyhow::Result<()> { params.spv_in = naga::front::spv::Options { adjust_coordinate_space: !args.keep_coordinate_space, strict_capabilities: false, - block_ctx_dump_prefix: args.block_ctx_dir.clone().map(Into::into), + block_ctx_dump_prefix: args.block_ctx_dir.clone(), }; params.entry_point.clone_from(&args.entry_point); @@ -482,7 +482,7 @@ fn run() -> anyhow::Result<()> { params.compact = args.compact; if args.bulk_validate { - return bulk_validate(args, ¶ms); + return bulk_validate(&args, ¶ms); } let mut files = args.files.iter(); @@ -498,6 +498,8 @@ fn run() -> anyhow::Result<()> { return Err(CliError("Input file path is not specified").into()); }; + let file_name = input_path.to_string_lossy(); + params.input_kind = args.input_kind; params.shader_stage = args.shader_stage; @@ -516,7 +518,7 @@ fn run() -> anyhow::Result<()> { .set(naga::back::spv::WriterFlags::DEBUG, true); params.spv_out.debug_info = Some(naga::back::spv::DebugInfo { source_code: input_text, - file_name: input_path.into(), + file_name: &file_name, language, }) } else { @@ -913,9 +915,9 @@ fn write_output( Ok(()) } -fn bulk_validate(args: Args, params: &Parameters) -> anyhow::Result<()> { +fn bulk_validate(args: &Args, params: &Parameters) -> anyhow::Result<()> { let mut invalid = vec![]; - for input_path in args.files { + for input_path in &args.files { let path = Path::new(&input_path); let input = fs::read(path)?; diff --git a/naga/src/back/spv/mod.rs b/naga/src/back/spv/mod.rs index 371b3f7db..4690dc719 100644 --- a/naga/src/back/spv/mod.rs +++ b/naga/src/back/spv/mod.rs @@ -26,7 +26,6 @@ use spirv::Word; use thiserror::Error; use crate::arena::{Handle, HandleVec}; -use crate::path_like::PathLikeRef; use crate::proc::{BoundsCheckPolicies, TypeResolution}; #[derive(Clone)] @@ -96,7 +95,7 @@ impl IdGenerator { #[derive(Debug, Clone)] pub struct DebugInfo<'a> { pub source_code: &'a str, - pub file_name: PathLikeRef<'a>, + pub file_name: &'a str, pub language: SourceLanguage, } diff --git a/naga/src/back/spv/writer.rs b/naga/src/back/spv/writer.rs index 636766d1e..c86a53c6e 100644 --- a/naga/src/back/spv/writer.rs +++ b/naga/src/back/spv/writer.rs @@ -14,7 +14,6 @@ use super::{ use crate::{ arena::{Handle, HandleVec, UniqueArena}, back::spv::{BindingInfo, WrappedFunction}, - path_like::PathLike, proc::{Alignment, TypeResolution}, valid::{FunctionInfo, ModuleInfo}, }; @@ -2519,10 +2518,8 @@ impl Writer { if self.flags.contains(WriterFlags::DEBUG) { if let Some(debug_info) = debug_info.as_ref() { let source_file_id = self.id_gen.next(); - self.debugs.push(Instruction::string( - &debug_info.file_name.to_string_lossy(), - source_file_id, - )); + self.debugs + .push(Instruction::string(debug_info.file_name, source_file_id)); debug_info_inner = Some(DebugInfoInner { source_code: debug_info.source_code, diff --git a/naga/src/front/spv/mod.rs b/naga/src/front/spv/mod.rs index 835cbe1aa..f54c8269f 100644 --- a/naga/src/front/spv/mod.rs +++ b/naga/src/front/spv/mod.rs @@ -44,7 +44,6 @@ use petgraph::graphmap::GraphMap; use super::atomic_upgrade::Upgrades; use crate::{ arena::{Arena, Handle, UniqueArena}, - path_like::PathLikeOwned, proc::{Alignment, Layouter}, FastHashMap, FastHashSet, FastIndexMap, }; @@ -384,7 +383,7 @@ pub struct Options { pub adjust_coordinate_space: bool, /// Only allow shaders with the known set of capabilities. pub strict_capabilities: bool, - pub block_ctx_dump_prefix: Option, + pub block_ctx_dump_prefix: Option, } impl Default for Options { diff --git a/naga/src/lib.rs b/naga/src/lib.rs index 19b406588..af990fb1d 100644 --- a/naga/src/lib.rs +++ b/naga/src/lib.rs @@ -116,7 +116,6 @@ pub mod front; pub mod ir; pub mod keywords; mod non_max_u32; -mod path_like; pub mod proc; mod racy_lock; mod span; diff --git a/naga/src/path_like.rs b/naga/src/path_like.rs deleted file mode 100644 index 36ca39c49..000000000 --- a/naga/src/path_like.rs +++ /dev/null @@ -1,238 +0,0 @@ -//! [`PathLike`] and its supporting items, such as [`PathLikeRef`] and [`PathLikeOwned`]. -//! This trait and these types provide a common denominator API for `Path`-like -//! types and operations in `std` and `no_std` contexts. -//! -//! # Usage -//! -//! - Store a [`PathLikeRef<'a>`] instead of a `&'a Path` in structs and enums. -//! - Store a [`PathLikeOwned`] instead of a `PathBuf` in structs and enums. -//! - Accept `impl PathLike` instead of `impl AsRef` for methods which directly -//! work with `Path`-like values. -//! - Accept `Into>` and/or `Into` in methods which -//! will store a `Path`-like value. - -use alloc::{borrow::Cow, string::String}; -use core::fmt; - -mod sealed { - /// Seal for [`PathLike`](super::PathLike). - 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 is implemented for: -/// - [`str`], -/// - [`String`], -/// - [`Cow<'_, str>`], and -/// - [`PathLikeRef`] -/// - Otherwise, types that implement `AsRef` (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 PathLike: sealed::Sealed { - fn to_string_lossy(&self) -> Cow<'_, str>; -} - -/// Abstraction over `Path` which falls back to [`str`] for `no_std` compatibility. -/// -/// This type should be used for _storing_ a reference to a [`PathLike`]. -/// Functions which accept a `Path` should prefer to use `impl PathLike` -/// or `impl Into>`. -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct PathLikeRef<'a>(&'a path_like_impls::PathInner); - -impl fmt::Debug for PathLikeRef<'_> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(self.0, f) - } -} - -impl<'a> From<&'a str> for PathLikeRef<'a> { - fn from(value: &'a str) -> Self { - cfg_if::cfg_if! { - if #[cfg(std)] { - Self(std::path::Path::new(value)) - } else { - Self(value) - } - } - } -} - -/// Abstraction over `PathBuf` which falls back to [`String`] for `no_std` compatibility. -/// -/// This type should be used for _storing_ an owned [`PathLike`]. -/// Functions which accept a `PathBuf` should prefer to use `impl PathLike` -/// or `impl Into`. -#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct PathLikeOwned(::Owned); - -impl fmt::Debug for PathLikeOwned { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&self.0, f) - } -} - -impl From for PathLikeOwned { - fn from(value: String) -> Self { - cfg_if::cfg_if! { - if #[cfg(std)] { - Self(value.into()) - } else { - Self(value) - } - } - } -} - -#[cfg(std)] -mod path_like_impls { - //! Implementations of [`PathLike`] within an `std` context. - //! - //! Since `std` is available, we blanket implement [`PathLike`] for all types - //! implementing [`AsRef`]. - - use alloc::borrow::Cow; - use std::path::Path; - - use super::{sealed, PathLike}; - - pub(super) type PathInner = Path; - - impl + ?Sized> PathLike for T { - fn to_string_lossy(&self) -> Cow<'_, str> { - self.as_ref().to_string_lossy() - } - } - - impl + ?Sized> sealed::Sealed for T {} -} - -#[cfg(no_std)] -mod path_like_impls { - //! Implementations of [`PathLike`] within a `no_std` context. - //! - //! Without `std`, we cannot blanket implement on [`AsRef`]. - //! Instead, we manually implement for a subset of types which are known - //! to implement [`AsRef`] when `std` is available. - //! - //! Implementing [`PathLike`] for a type which does _not_ implement [`AsRef`] - //! with `std` enabled breaks the additive requirement of Cargo features. - - use alloc::{borrow::Cow, string::String}; - use core::borrow::Borrow; - - use super::{sealed, PathLike, PathLikeOwned, PathLikeRef}; - - pub(super) type PathInner = str; - - impl PathLike for String { - fn to_string_lossy(&self) -> Cow<'_, str> { - Cow::Borrowed(self.as_str()) - } - } - - impl sealed::Sealed for String {} - - impl PathLike for str { - fn to_string_lossy(&self) -> Cow<'_, str> { - Cow::Borrowed(self) - } - } - - impl sealed::Sealed for str {} - - impl PathLike for Cow<'_, str> { - fn to_string_lossy(&self) -> Cow<'_, str> { - Cow::Borrowed(self.borrow()) - } - } - - impl sealed::Sealed for Cow<'_, str> {} - - impl PathLike for &T { - fn to_string_lossy(&self) -> Cow<'_, str> { - (*self).to_string_lossy() - } - } - - impl sealed::Sealed for &T {} - - impl PathLike for PathLikeRef<'_> { - fn to_string_lossy(&self) -> Cow<'_, str> { - Cow::Borrowed(self.0) - } - } - - impl sealed::Sealed for PathLikeRef<'_> {} - - impl PathLike for PathLikeOwned { - fn to_string_lossy(&self) -> Cow<'_, str> { - Cow::Borrowed(self.0.borrow()) - } - } - - impl sealed::Sealed for PathLikeOwned {} -} - -#[cfg(std)] -mod path_like_owned_std_impls { - //! Traits which can only be implemented for [`PathLikeOwned`] with `std`. - - use std::path::{Path, PathBuf}; - - use super::PathLikeOwned; - - impl AsRef for PathLikeOwned { - fn as_ref(&self) -> &Path { - self.0.as_ref() - } - } - - impl From for PathLikeOwned { - fn from(value: PathBuf) -> Self { - Self(value) - } - } - - impl From for PathBuf { - fn from(value: PathLikeOwned) -> Self { - value.0 - } - } - - impl AsRef for PathLikeOwned { - fn as_ref(&self) -> &PathBuf { - &self.0 - } - } -} - -#[cfg(std)] -mod path_like_ref_std_impls { - //! Traits which can only be implemented for [`PathLikeRef`] with `std`. - - use std::path::Path; - - use super::PathLikeRef; - - impl AsRef for PathLikeRef<'_> { - fn as_ref(&self) -> &Path { - self.0 - } - } - - impl<'a> From<&'a Path> for PathLikeRef<'a> { - fn from(value: &'a Path) -> Self { - Self(value) - } - } - - impl<'a> From> for &'a Path { - fn from(value: PathLikeRef<'a>) -> Self { - value.0 - } - } -} diff --git a/naga/src/span.rs b/naga/src/span.rs index 357c2379f..adfe7ad60 100644 --- a/naga/src/span.rs +++ b/naga/src/span.rs @@ -6,7 +6,7 @@ use alloc::{ }; use core::{error::Error, fmt, ops::Range}; -use crate::{error::replace_control_chars, path_like::PathLike, Arena, Handle, UniqueArena}; +use crate::{error::replace_control_chars, Arena, Handle, UniqueArena}; /// A source code span, used for error reporting. #[derive(Clone, Copy, Debug, PartialEq, Default)] @@ -283,14 +283,12 @@ impl WithSpan { /// Emits a summary of the error to standard error stream. #[cfg(feature = "stderr")] - pub fn emit_to_stderr_with_path

(&self, source: &str, path: P) + pub fn emit_to_stderr_with_path(&self, source: &str, path: &str) where E: Error, - P: PathLike, { use codespan_reporting::{files, term}; - let path = path.to_string_lossy(); let files = files::SimpleFile::new(path, replace_control_chars(source)); let config = term::Config::default(); @@ -315,14 +313,12 @@ impl WithSpan { } /// Emits a summary of the error to a string. - pub fn emit_to_string_with_path

(&self, source: &str, path: P) -> String + pub fn emit_to_string_with_path(&self, source: &str, path: &str) -> String where E: Error, - P: PathLike, { use codespan_reporting::{files, term}; - let path = path.to_string_lossy(); let files = files::SimpleFile::new(path, replace_control_chars(source)); let config = term::Config::default(); diff --git a/naga/tests/naga/snapshots.rs b/naga/tests/naga/snapshots.rs index e2d940281..e01610f29 100644 --- a/naga/tests/naga/snapshots.rs +++ b/naga/tests/naga/snapshots.rs @@ -7,7 +7,7 @@ const DIR_OUT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/out"); #[allow(unused_variables)] fn check_targets(input: &Input, module: &mut naga::Module, source_code: Option<&str>) { let params = input.read_parameters(DIR_IN); - let name = &input.file_name; + let name = input.file_name.display().to_string(); let targets = params.targets.unwrap(); @@ -44,11 +44,7 @@ fn check_targets(input: &Input, module: &mut naga::Module, source_code: Option<& .subgroup_operations(subgroup_operations) .validate(module) .unwrap_or_else(|err| { - panic!( - "Naga module validation failed on test `{}`:\n{:?}", - name.display(), - err - ); + panic!("Naga module validation failed on test `{name}`:\n{err:?}"); }); let info = { @@ -73,11 +69,7 @@ fn check_targets(input: &Input, module: &mut naga::Module, source_code: Option<& .subgroup_operations(subgroup_operations) .validate(module) .unwrap_or_else(|err| { - panic!( - "Post-compaction module validation failed on test '{}':\n<{:?}", - name.display(), - err, - ) + panic!("Post-compaction module validation failed on test '{name}':\n<{err:?}") }) }; @@ -94,7 +86,7 @@ fn check_targets(input: &Input, module: &mut naga::Module, source_code: Option<& if let Some(source_code) = source_code { debug_info = Some(naga::back::spv::DebugInfo { source_code, - file_name: name.as_path().into(), + file_name: &name, // wgpu#6266: we technically know all the information here to // produce the valid language but it's not too important for // validation purposes diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index 17d075c38..1a710cb2d 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -896,7 +896,7 @@ impl super::Device { if let Some(ref debug) = naga_shader.debug_source { temp_options.debug_info = Some(naga::back::spv::DebugInfo { source_code: &debug.source_code, - file_name: debug.file_name.as_ref().into(), + file_name: debug.file_name.as_ref(), language: naga::back::spv::SourceLanguage::WGSL, }) } @@ -1932,7 +1932,7 @@ impl crate::Device for super::Device { .as_ref() .map(|d| naga::back::spv::DebugInfo { source_code: d.source_code.as_ref(), - file_name: d.file_name.as_ref().into(), + file_name: d.file_name.as_ref(), language: naga::back::spv::SourceLanguage::WGSL, }); if !desc.runtime_checks.bounds_checks {