Skip to content
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

Detect a common syntax error case for diagnostic_directive #6718

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148]

- Show types of LHS and RHS in binary operation type mismatch errors. By @ErichDonGubler in [#6450](https://github.com/gfx-rs/wgpu/pull/6450).
- The GLSL parser now uses less expressions for function calls. By @magcius in [#6604](https://github.com/gfx-rs/wgpu/pull/6604).
- Add a note to help with a common syntax error case for global diagnostic filter directives. By @e-hat in [#6718](https://github.com/gfx-rs/wgpu/pull/6718)

#### General

Expand Down
12 changes: 11 additions & 1 deletion naga/src/front/wgsl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ pub(crate) enum Error<'a> {
},
DiagnosticAttributeNotSupported {
on_what_plural: &'static str,
intended_diagnostic_directive: bool,
spans: Vec<Span>,
},
}
Expand Down Expand Up @@ -1082,6 +1083,7 @@ impl<'a> Error<'a> {
},
Error::DiagnosticAttributeNotSupported {
on_what_plural,
intended_diagnostic_directive,
ref spans,
} => ParseError {
message: format!(
Expand All @@ -1098,7 +1100,15 @@ impl<'a> Error<'a> {
"some statements, and `switch`/`loop` bodies."
)
.into(),
"These attributes are well-formed, you likely just need to move them.".into(),
{
if intended_diagnostic_directive {
concat!("To declare a diagnostic filter that applies to the entire module, ",
"move this line to the top of the file and remove the `@` symbol.").into()
} else {
"These attributes are well-formed, you likely just need to move them."
.into()
}
},
],
},
}
Expand Down
3 changes: 3 additions & 0 deletions naga/src/front/wgsl/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2472,6 +2472,9 @@ impl Parser {
} else {
Err(Error::DiagnosticAttributeNotSupported {
on_what_plural,
// In this case the user may have intended to create a global diagnostic filter directive,
// so display a note to them suggesting the correct syntax.
intended_diagnostic_directive: on_what_plural == "semicolons",
ErichDonGubler marked this conversation as resolved.
Show resolved Hide resolved
spans: filters.spans().collect(),
})
}
Expand Down
22 changes: 22 additions & 0 deletions naga/src/front/wgsl/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,4 +797,26 @@ error: found conflicting `diagnostic(…)` rule(s)
");
}
}

use crate::front::wgsl::assert_parse_err;
#[test]
fn intended_global_directive() {
let shader = "
@diagnostic(off, my.lint);
";
assert_parse_err(
shader,
"\
error: `@diagnostic(…)` attribute(s) on semicolons are not supported
┌─ wgsl:2:1
2 │ @diagnostic(off, my.lint);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `@diagnostic(…)` attributes are only permitted on `fn`s, some statements, and `switch`/`loop` bodies.
= note: To declare a diagnostic filter that applies to the entire module, move this line to the top of the file and remove the `@` symbol.

"
);
}
}
Loading