-
Notifications
You must be signed in to change notification settings - Fork 336
Allow e.g. if (cond) break/continue/return/kill;
without brackets.
#705
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
Conversation
This can improve readability and intent-communication of e.g. `if (cond) break;`. This comes from my own c++ style, where brackets are generally required, except for (keyword) branch/jump statements. We can choose to have a language with opinionated style and embed this reasonable requirement into the grammar.
if (cond) break/continue/return/kill;
.if (cond) break/continue/return/kill;
without brackets.
This works for me. |
The return and kill parts of this are probably overreach. |
I don't think we have the evidence to suggest that a grammar complication is needed. |
As for This proposal splits the difference between being opinionated/strict and flexible/expressive. |
Although I am not in favor of removing braces after an if as a general rule, I would be willing to accept a PR that allows it for the case where there is just a break/continue/return/kill statement in the branch. But this is not at all what this pull request does. I see several issues with its changes to the grammar:
For each of those I'd like to know whether it is intended or just an accident. |
This was discussed at the 2020-05-12 meeting. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with Robin that this should also remove the break if();
syntax so we have only one way to do it.
I'm ok with allowing this as a limited exception, but I don't want to see this used as a reason to allow things like the following for consistency:
if (a)
i = i + 1;
|
||
body_stmt: | ||
: BRACKET_LEFT statements BRACKET_RIGHT | ||
| branch_stmt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be too general as this makes it grammatically correct to say
fn foo() -> f32
return 2.3;
Seems like we should have an if_body_stmt
and then we need to update the if section so you can't do something like:
if (a)
break;
else if (b)
continue;
else
kill;
as I don't think that should be allowed?
There is a loophole in this approach:
Unless we have special rules for the return expressions in the uniform control flow analysis, the expression has to adhere to the same restrictions as anything else in a I could see 3 solutions to this:
|
I expressed concern regarding this change for the dangling-else problem. I wanted to look into it further to convince myself it was or was not an issue. As I read the grammar, I see that this change introduces no way to follow an I think @dj2 makes a good point that this would introduce a rather strange function definition. As such, I'd support the introduction of Apologies if I came on too strong earlier. Nevertheless, I feel this additional consideration has been beneficial. |
Discussed at the 2020-06-02 meeting. |
Discussed at the 2020-09-29 meeting. |
With required braces, the dangling else problem is naturally solved. We should not make a change which creates the dangling else problem. Also, consider something like
This code is unclear. |
I believe this PR does not have a dangling-else problem, as you cannot write
Allowed:
|
Discussed at the 2020-12-08 meeting. Resolution: Land this as is unless people speak up by next week. |
We discussed this for a long time, and I think the discussion has evolved a bit. Edit: the de-factor standard C++ code formatter - Clang-tidy - has a rule "readability-braces-around-statements" specifically to handle that, and it's enabled by default. Mozilla's |
It seems like WGSL syntax is heavily inspired by Rust, which has managed to get by just fine without such syntax. Adding this has the downside of precluding other possible syntax extensions, and it also introduces unnecessary special-cases into the grammar (in order to avoid the dangling-else problem). You need only look at Javascript to see the problems with adding too much syntax too quickly. It is much better to start with a simpler grammar, and extend it later with sugar and "nice to haves" once you have a body of real code against which you can evaluate the benefit of any potential additions. |
Thinking about this some more, I think it falls into the bucket of "we can't undo this later, but we can add it later". The change itself is simple enough, but it isn't strictly adding anything that can't be done without it and being in the can't undo bucket, I think we should punt this to post MVP. |
FWIW, unsurprisingly I have fought to get this warning disabled for certain directories in Gecko, where the prevailing style embraces succinct braceless early-outs. I feel relatively strongly about this requirement being overly restrictive and generally supported merely via personal opinions and strawman arguments like goto-fail. I know we all have opinions here, but this isn't a choice between how your code will be written, but rather whether one camp's code style is allowed to be written at all. The most equitable thing is to support both camps, absent anything particularly onerous. I don't think we will have more data here unless we allow this for MVP and check if people use it. This is a minor enough papercut that people (me) will grumble when writing it, but it won't end up on any powerpoint slides about pain-points. However, it seems easy enough to spec and support, and @dneto0 and I are already extant users requesting it for two different reasons. I propose we take this now, and if we run into any onerous aspects here, we can remove it before v1. |
It's not really a style question if it doesn't exist in the language to begin with. Perhaps it should be possible to omit brackets entirely and use indentation to delimit blocks: that's required in python after all. The problem with this argument is that WGSL is not python, or C++, or any other language.
Adding every possible feature and then removing them later is a bad way to design a language and invites code breakage for no benefit. |
We are at the stage of language development where we are deciding what styles are and aren't allowed, and this is exactly that sort of style question. I agree that we should not add literally everything, but I've made my case that this makes the cut. |
Specifically, at this point there has been consensus that this is tolerable, spec-able, and implementable, so I'm looking for any arguments that it's onerous or unduly risky such that we should not proceed with the consensus that we have already reached, even if it was a weak consensus. |
From what I've seen, most of the spec is fairly minimalist and driven from an actual need. For example, clearly you need some way in the language to manage control flow, therefore you need
All you said is that you feel strongly about it, and that it's possible to implement. I don't doubt either of those things, but the same argument applies to almost everything. Significant whitespace is clearly possible to implement since python has it, and I guarantee there exist people who feel strongly about that (having encountered several wanting to add this feature to Rust and C++). The barrier to entry for new features, especially early on like this should be far higher than a personal preference. |
Given that we don't need this now, there's rough consensus that we probably want to hold off on it for now, but that we can add it later. Maybe people will express positive interest in it, or maybe we'll just feel like adding it, but we're inundated with work right now and this is probably low priority. |
Fwiw @Diggsey I don't think this has the same bar as other sophisticated "new features", as it's ultimately a minor quality-of-life improvement, while also being fairly trivial to implement. |
@kdashg I think this PR could be closed since WGSL now supports paren-less control flow, which means conflicts with bracket-less control flow. (though we could allow control flow to be either of bracket-less or paren-less, but ew?) |
* api,validation,state,device_mismatched plan - Add validation test plan for one device using objects from another device in all APIs where an object is passed. - Split tests to each API file. - The APIs and Objects include: - GPUDevice: createPipelineLayout with BGLs createBindGroup with {BGL, BindResource(buffer,sampler,texture,etc.)} createComputePipeline(Async) with {PipelineLayout, ShaderModule} createRenderPipeline(Async) with {PipelineLayout, ShaderModule} - GPUCommandEncoder: beginRenderPass with {Texture views in attachments, OcclusionQuerySet} copyBufferToBuffer with Buffers copyBufferToTexture/copyTextureToBuffer with {Buffer, Texture} (test in image_copy/) copyTextureToTexture with Textures writeTimestamp/resolveQuerySet with QuerySet - GPUProgrammablePassEncoder setBindGroup {with, without} Uint32Array with BindGroup x = {compute pass, render pass, render bundle} - GPUComputePassEncoder setPipeline with ComputePipeline beginPipelineStatisticsQuery/writeTimestamp with QuerySet - GPURenderEncoderBase setPipeline with RenderPipeline setIndexBuffer/setVertexBuffer with Buffer drawIndirect/drawIndexedIndirect with Buffer beginPipelineStatisticsQuery/writeTimestamp with QuerySet x = {render pass, render bundle} - GPURenderPassEncoder executeBundles with RenderBundles - GPUQueue submit with CommandBuffers writeBuffer with Buffer writeTexture with Texture (test in image_copy/) copyExternalImageToTexture with Texture * Address comments * Update src/webgpu/api/validation/encoding/cmds/setBindGroup.spec.ts Co-authored-by: Kai Ninomiya <[email protected]> * Update src/webgpu/api/validation/encoding/queries/general.spec.ts Co-authored-by: Kai Ninomiya <[email protected]> * Update src/webgpu/api/validation/queue/submit.spec.ts Co-authored-by: Kai Ninomiya <[email protected]>
This can improve readability and intent-communication of e.g.
if (cond) break;
.This comes from my own c++ style, where brackets are generally required,
except for (keyword) branch/jump statements.
We can choose to have a language with opinionated style and embed this
reasonable requirement into the grammar.