Skip to content

Commit 41f2207

Browse files
DO NOT MERGE: yeet, fix it later
1 parent 1adbf95 commit 41f2207

3 files changed

Lines changed: 124 additions & 62 deletions

File tree

naga/src/front/wgsl/error.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
use crate::common::wgsl::TryToWgsl;
44
use crate::diagnostic_filter::ConflictingDiagnosticRuleError;
55
use crate::error::replace_control_chars;
6+
use crate::front::wgsl::parse::directive::enable_extension::NativeOnlyEnableExtensionError;
7+
use crate::front::wgsl::ImplementedEnableExtension;
68
use crate::proc::{Alignment, ConstantEvaluatorError, ResolveError};
79
use crate::{Scalar, SourceLocation, Span};
810

9-
use super::parse::directive::enable_extension::{EnableExtension, UnimplementedEnableExtension};
11+
use super::parse::directive::enable_extension::{
12+
EnableExtension, NativeOnlyEnableExtensionErrorReason, UnimplementedEnableExtension,
13+
};
1014
use super::parse::directive::language_extension::{
1115
LanguageExtension, UnimplementedLanguageExtension,
1216
};
@@ -381,6 +385,10 @@ pub(crate) enum Error<'a> {
381385
kind: EnableExtension,
382386
span: Span,
383387
},
388+
NativeOnlyEnableExtensionsNotAllowed {
389+
kind: ImplementedEnableExtension,
390+
span: Span,
391+
},
384392
LanguageExtensionNotYetImplemented {
385393
kind: UnimplementedLanguageExtension,
386394
span: Span,
@@ -440,6 +448,35 @@ impl From<&'static str> for DiagnosticAttributeNotSupportedPosition {
440448
}
441449
}
442450

451+
impl From<NativeOnlyEnableExtensionError> for Error<'_> {
452+
fn from(value: NativeOnlyEnableExtensionError) -> Self {
453+
let NativeOnlyEnableExtensionError {
454+
span,
455+
extension,
456+
reason,
457+
} = value;
458+
459+
match reason {
460+
NativeOnlyEnableExtensionErrorReason::NotEnabled => Error::EnableExtensionNotEnabled {
461+
span,
462+
kind: extension.into(),
463+
},
464+
NativeOnlyEnableExtensionErrorReason::NotAllowed {
465+
can_suggest_in_diagnostics,
466+
} => {
467+
if can_suggest_in_diagnostics {
468+
Error::NativeOnlyEnableExtensionsNotAllowed {
469+
span,
470+
kind: extension,
471+
}
472+
} else {
473+
Error::UnknownEnableExtension(span, todo!())
474+
}
475+
}
476+
}
477+
}
478+
}
479+
443480
#[derive(Clone, Debug)]
444481
pub(crate) struct AutoConversionError {
445482
pub dest_span: Span,
@@ -1221,6 +1258,44 @@ impl<'a> Error<'a> {
12211258
)
12221259
.into(),
12231260
)],
1261+
notes: if let EnableExtension::Unimplemented(kind) = kind {
1262+
vec![format!(
1263+
concat!(
1264+
"This \"Enable Extension\" is not yet implemented. ",
1265+
"Let Naga maintainers know that you ran into this at ",
1266+
"<https://github.com/gfx-rs/wgpu/issues/{}>, ",
1267+
"so they can prioritize it!"
1268+
),
1269+
kind.tracking_issue_num()
1270+
)]
1271+
} else {
1272+
vec![
1273+
format!(
1274+
concat!(
1275+
"You can enable this extension by adding `enable {};` ",
1276+
"at the top of the shader, before any other items."
1277+
),
1278+
kind.to_ident()
1279+
),
1280+
]
1281+
},
1282+
},
1283+
Error::NativeOnlyEnableExtensionsNotAllowed { kind, span } => ParseError {
1284+
message: format!(
1285+
"the `{}` enable extension is not allowed in the current environment",
1286+
kind.to_ident()
1287+
),
1288+
labels: vec![(
1289+
span,
1290+
format!(
1291+
concat!(
1292+
"the `{}` \"Enable Extension\" is needed for this functionality, ",
1293+
"but it is not currently enabled."
1294+
),
1295+
kind.to_ident()
1296+
)
1297+
.into(),
1298+
)],
12241299
notes: if let EnableExtension::Unimplemented(kind) = kind {
12251300
vec![format!(
12261301
concat!(

naga/src/front/wgsl/parse/directive/enable_extension.rs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ pub struct EnableExtensions {
1717
/// Whether `enable clip_distances;` was written earlier in the shader module.
1818
clip_distances: bool,
1919
/// Native-only extensions.
20-
wgpu: NativeOnly<WgpuEnableExtensions>,
20+
native_only: NativeOnly<NativeOnlyEnableExtensions>,
2121
}
2222

2323
impl EnableExtensions {
2424
pub(crate) const fn empty(native_only: NativeOnly<()>) -> Self {
2525
// TODO: It'd be nice to use a `const` `map` helper here, once
2626
// <https://github.com/rust-lang/rust/issues/73255> has landed.
27-
let wgpu = match native_only {
27+
let native_only = match native_only {
2828
NativeOnly::NotAllowed {
2929
can_suggest_in_diagnostics,
3030
} => NativeOnly::NotAllowed {
3131
can_suggest_in_diagnostics,
3232
},
33-
NativeOnly::Allowed(()) => NativeOnly::Allowed(WgpuEnableExtensions::empty()),
33+
NativeOnly::Allowed(()) => NativeOnly::Allowed(NativeOnlyEnableExtensions::empty()),
3434
};
3535
Self {
36-
wgpu,
36+
native_only,
3737
f16: false,
3838
dual_source_blending: false,
3939
clip_distances: false,
@@ -45,18 +45,18 @@ impl EnableExtensions {
4545
&mut self,
4646
span: Span,
4747
ext: ImplementedEnableExtension,
48-
) -> core::result::Result<(), EnableExtensionNotAvailableError> {
48+
) -> core::result::Result<(), NativeOnlyEnableExtensionError> {
4949
macro_rules! check_wgpu {
5050
($f:expr) => {{
51-
let f: fn(&mut WgpuEnableExtensions) -> &mut bool = $f;
52-
match &mut self.wgpu {
51+
let f: fn(&mut NativeOnlyEnableExtensions) -> &mut bool = $f;
52+
match &mut self.native_only {
5353
&mut NativeOnly::Allowed(ref mut wgpu) => Ok(f(wgpu)),
5454
&mut NativeOnly::NotAllowed {
5555
can_suggest_in_diagnostics,
56-
} => Err(EnableExtensionNotAvailableError {
56+
} => Err(NativeOnlyEnableExtensionError {
5757
span,
5858
extension: ext,
59-
reason: EnableExtensionNotAvailableErrorReason::NotAllowed {
59+
reason: NativeOnlyEnableExtensionErrorReason::NotAllowed {
6060
can_suggest_in_diagnostics,
6161
},
6262
}),
@@ -65,17 +65,17 @@ impl EnableExtensions {
6565
}
6666
let field = match ext {
6767
ImplementedEnableExtension::WgpuMeshShader => {
68-
check_wgpu!(|wgpu| &mut wgpu.mesh_shader)?
68+
check_wgpu!(|no| &mut no.wgpu_mesh_shader)?
6969
}
70-
ImplementedEnableExtension::WgpuRayQuery => check_wgpu!(|wgpu| &mut wgpu.ray_query)?,
70+
ImplementedEnableExtension::WgpuRayQuery => check_wgpu!(|no| &mut no.wgpu_ray_query)?,
7171
ImplementedEnableExtension::WgpuRayQueryVertexReturn => {
72-
check_wgpu!(|wgpu| &mut wgpu.ray_query_vertex_return)?
72+
check_wgpu!(|no| &mut no.wgpu_ray_query_vertex_return)?
7373
}
7474
ImplementedEnableExtension::DualSourceBlending => &mut self.dual_source_blending,
7575
ImplementedEnableExtension::F16 => &mut self.f16,
7676
ImplementedEnableExtension::ClipDistances => &mut self.clip_distances,
7777
ImplementedEnableExtension::WgpuCooperativeMatrix => {
78-
check_wgpu!(|wgpu| &mut wgpu.cooperative_matrix)?
78+
check_wgpu!(|no| &mut no.wgpu_cooperative_matrix)?
7979
}
8080
};
8181
*field = true;
@@ -88,41 +88,41 @@ impl EnableExtensions {
8888
&self,
8989
span: Span,
9090
extension: ImplementedEnableExtension,
91-
) -> core::result::Result<(), EnableExtensionNotAvailableError> {
92-
let check_wgpu = |f: fn(&WgpuEnableExtensions) -> bool| match &self.wgpu {
91+
) -> core::result::Result<(), NativeOnlyEnableExtensionError> {
92+
let check_wgpu = |f: fn(&NativeOnlyEnableExtensions) -> bool| match &self.native_only {
9393
&NativeOnly::Allowed(ref wgpu) => Ok(f(wgpu)),
9494
&NativeOnly::NotAllowed {
9595
can_suggest_in_diagnostics,
96-
} => Err(EnableExtensionNotAvailableError {
96+
} => Err(NativeOnlyEnableExtensionError {
9797
span,
9898
extension,
99-
reason: EnableExtensionNotAvailableErrorReason::NotAllowed {
99+
reason: NativeOnlyEnableExtensionErrorReason::NotAllowed {
100100
can_suggest_in_diagnostics,
101101
},
102102
}),
103103
};
104104

105105
let is_enabled = match extension {
106-
ImplementedEnableExtension::WgpuMeshShader => check_wgpu(|wgpu| wgpu.mesh_shader)?,
107-
ImplementedEnableExtension::WgpuRayQuery => check_wgpu(|wgpu| wgpu.ray_query)?,
106+
ImplementedEnableExtension::WgpuMeshShader => check_wgpu(|no| no.wgpu_mesh_shader)?,
107+
ImplementedEnableExtension::WgpuRayQuery => check_wgpu(|no| no.wgpu_ray_query)?,
108108
ImplementedEnableExtension::WgpuRayQueryVertexReturn => {
109-
check_wgpu(|wgpu| wgpu.ray_query_vertex_return)?
109+
check_wgpu(|no| no.wgpu_ray_query_vertex_return)?
110110
}
111111
ImplementedEnableExtension::DualSourceBlending => self.dual_source_blending,
112112
ImplementedEnableExtension::F16 => self.f16,
113113
ImplementedEnableExtension::ClipDistances => self.clip_distances,
114114
ImplementedEnableExtension::WgpuCooperativeMatrix => {
115-
check_wgpu(|wgpu| wgpu.cooperative_matrix)?
115+
check_wgpu(|no| no.wgpu_cooperative_matrix)?
116116
}
117117
};
118118

119119
if is_enabled {
120120
Ok(())
121121
} else {
122-
Err(EnableExtensionNotAvailableError {
122+
Err(NativeOnlyEnableExtensionError {
123123
span,
124124
extension,
125-
reason: EnableExtensionNotAvailableErrorReason::NotEnabled,
125+
reason: NativeOnlyEnableExtensionErrorReason::NotEnabled,
126126
})
127127
}
128128
}
@@ -174,46 +174,50 @@ impl<T> Default for NativeOnly<T> {
174174

175175
/// Native-only extensions under [`EnableExtensions::wgpu`].
176176
#[derive(Clone, Debug, Eq, PartialEq)]
177-
pub struct WgpuEnableExtensions {
177+
pub struct NativeOnlyEnableExtensions {
178178
/// Whether `enable wgpu_mesh_shader;` was written earlier in the shader module.
179-
mesh_shader: bool,
179+
wgpu_mesh_shader: bool,
180180
/// Whether `enable wgpu_ray_query;` was written earlier in the shader module.
181-
ray_query: bool,
181+
wgpu_ray_query: bool,
182182
/// Whether `enable wgpu_ray_query_vertex_return;` was written earlier in the shader module.
183-
ray_query_vertex_return: bool,
183+
wgpu_ray_query_vertex_return: bool,
184184
/// Whether `enable wgpu_cooperative_matrix;` was written earlier in the shader module.
185-
cooperative_matrix: bool,
185+
wgpu_cooperative_matrix: bool,
186186
}
187187

188-
impl WgpuEnableExtensions {
188+
impl NativeOnlyEnableExtensions {
189189
pub(crate) const fn empty() -> Self {
190190
Self {
191-
mesh_shader: false,
192-
ray_query: false,
193-
ray_query_vertex_return: false,
194-
cooperative_matrix: false,
191+
wgpu_mesh_shader: false,
192+
wgpu_ray_query: false,
193+
wgpu_ray_query_vertex_return: false,
194+
wgpu_cooperative_matrix: false,
195195
}
196196
}
197197
}
198198

199-
impl Default for WgpuEnableExtensions {
199+
impl Default for NativeOnlyEnableExtensions {
200200
fn default() -> Self {
201201
Self::empty()
202202
}
203203
}
204204

205205
/// An error returned by [`EnableExtensions::contains`].
206206
#[derive(Clone, Debug)]
207-
pub struct EnableExtensionNotAvailableError {
208-
span: Span,
209-
extension: ImplementedEnableExtension,
210-
reason: EnableExtensionNotAvailableErrorReason,
207+
pub struct NativeOnlyEnableExtensionError {
208+
pub span: Span,
209+
pub extension: ImplementedEnableExtension,
210+
pub reason: NativeOnlyEnableExtensionErrorReason,
211211
}
212212

213213
/// An [`EnableExtensionNotAvailableError::reason`].
214214
#[derive(Clone, Debug, Eq, PartialEq)]
215-
pub enum EnableExtensionNotAvailableErrorReason {
215+
pub enum NativeOnlyEnableExtensionErrorReason {
216+
/// [`EnableExtensions::check`] was called, and only failed because the user didn't write
217+
/// `enable <extension>;`.
216218
NotEnabled,
219+
/// [`EnableExtensions::check`] or `[EnableExtensions::enable]` was called for an extension
220+
/// not allowed in the current environment.
217221
NotAllowed { can_suggest_in_diagnostics: bool },
218222
}
219223

naga/src/front/wgsl/parse/mod.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,9 @@ impl<'a> BindingParser<'a> {
223223
self.invariant.set(true, name_span)?;
224224
}
225225
"blend_src" => {
226-
if !lexer
226+
lexer
227227
.enable_extensions
228-
.check(ImplementedEnableExtension::DualSourceBlending)
229-
{
230-
return Err(Box::new(Error::EnableExtensionNotEnabled {
231-
span: name_span,
232-
kind: ImplementedEnableExtension::DualSourceBlending.into(),
233-
}));
234-
}
228+
.check(name_span, ImplementedEnableExtension::DualSourceBlending)?;
235229

236230
lexer.expect(Token::Paren('('))?;
237231
self.blend_src
@@ -240,15 +234,9 @@ impl<'a> BindingParser<'a> {
240234
lexer.expect(Token::Paren(')'))?;
241235
}
242236
"per_primitive" => {
243-
if !lexer
237+
lexer
244238
.enable_extensions
245-
.check(ImplementedEnableExtension::WgpuMeshShader)
246-
{
247-
return Err(Box::new(Error::EnableExtensionNotEnabled {
248-
span: name_span,
249-
kind: ImplementedEnableExtension::WgpuMeshShader.into(),
250-
}));
251-
}
239+
.check(name_span, ImplementedEnableExtension::WgpuMeshShader)?;
252240
self.per_primitive.set((), name_span)?;
253241
}
254242
_ => return Err(Box::new(Error::UnknownAttribute(name_span))),
@@ -899,12 +887,7 @@ impl Parser {
899887
let num = res.map_err(|err| Error::BadNumber(span, err))?;
900888

901889
if let Some(enable_extension) = num.requires_enable_extension() {
902-
if !lexer.enable_extensions.check(enable_extension) {
903-
return Err(Box::new(Error::EnableExtensionNotEnabled {
904-
kind: enable_extension.into(),
905-
span,
906-
}));
907-
}
890+
lexer.enable_extensions.check(span, enable_extension)?
908891
}
909892

910893
ast::Expression::Literal(ast::Literal::Number(num))

0 commit comments

Comments
 (0)