-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Cosmos] fix errors after using noFallthroughCasesInSwitch
default configuration
#20212
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
Thank you for your contribution bzhang0! We will review the pull request and get back to you soon. |
I think the change makes sense, thanks! Could you please revert the changes in package.json? |
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.
Would this not change the result? If we previously were falling through in all of these hash calculations, then the function was designed with that in mind. We can't just insert break
s and expect the function to work the same way.
Is it possible to disable this check on a file-by-file basis? Maybe we can just exclude murmurHash from the no-fallthrough test. |
Seems to be a requested feature in the compiler, but not implemented: microsoft/TypeScript#28306 |
For these changed switches, it would only fall through to the default case, which doesn't exist, right? |
@witemple-msft from the first glance, this change does not seem to change the result because the switched on variable does not get re-assigned in any of the case blocks and as @jeremymeng pointed out, there is no default block to fall into. Also for context, this module is copied from an old commit from https://github.com/pid/murmurHash3js where the code is originally written in JS. |
@deyaaeldeen, @jeremymeng Unless I'm very much misunderstanding something, cases fall through to the next case, not to the default case. Adding these The switch statement actually executes all the cases 9 through 1, not only case 9. Adding the breaks means that the steps from cases 8-1 just aren't executed anymore. Am I missing something? This TSPG demonstrates a simpler example: https://www.typescriptlang.org/play?#code/FAYw9gdgzgLgBADwFxwgVwLYCMCmAnOAXjgCYAGAbmFEijABscA6esAcwAoAiAZQHcAljBAALARDZIuAGkQBKKsCiDhIuBwRy4Ab2Bw4IAIZQccAIxkke-QdoNmrTlwtcF1oydKXr+8NHss7NzkrlS+xqYAzN42tv6MgU7RoT5wWHg4hgDWYQYRcAAsMTZ+dAmO3EUp+gAmOABmhmj0MFaxpQEVXAAiDU0tKQC+QA |
@witemple-msft you are right! TIL. I was probably thinking that they were like multiple if statements but they are not. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch |
I am questioning my understanding of imperative programming and I blame FP! @bzhang0 Perhaps you could rewrite switch (remainder) {
case 3:
k1 ^= bytes[j + 2] << 16;
case 2:
k1 ^= bytes[j + 1] << 8;
case 1:
k1 ^= bytes[j];
k1 = _x86Multiply(k1, c1);
k1 = _x86Rotl(k1, 15);
k1 = _x86Multiply(k1, c2);
h1 ^= k1;
} to if (remainder === 3) {
k1 ^= bytes[j + 2] << 16;
}
if (remainder === 2 || remainder === 3) {
k1 ^= bytes[j + 1] << 8;
}
if (remainder >= 1 || remainder <= 3) {
k1 ^= bytes[j];
k1 = _x86Multiply(k1, c1);
k1 = _x86Rotl(k1, 15);
k1 = _x86Multiply(k1, c2);
h1 ^= k1;
} I think it is common to use fallthrough switch statements in writing hash algorithms for performance reasons and I wonder if my (uglier) suggestion would degrade the performance much. Anyway, I am fine leaving the code as is since it was copied from somewhere else and wait until TS provides support for controlling this behavior per module. |
I've reverted |
@bzhang0 @deyaaeldeen I'd prefer keeping the original code if this is adopted from somewhere else. |
I agree with Jeremy. Since this is a module we vendor in from elsewhere I think the risk of messing with it outweighs the benefit of being able to say we have this flag on everywhere. Apologies to @bzhang0 for the confusion here. @ramya-rao-a can we make an exception to the rule for cosmos in this case? |
Fine by me. Can we summarize the reason in #11435 and close it? |
I left a note there and closed the issue, so I'll close this as well. Again, sorry to @bzhang0 for the confusion and thank you for helping us at least dig into this! |
Thanks for your work here @bzhang0, we definitely got to learn some learning here :) |
This fixes #11435. Let me know if there's anything I missed! I followed the instructions in the description and made sure that
rushx build
worked, but I'm not quite sure if this implementation is correct.A couple comments + what I did:
"noFallthroughCasesInSwitch: false"
rule fromtsconfig.json
rushx build
package.json
was updated (I ranrush update
andrush rebuild
while on this branch before pushing, maybe this is the case), so let me know if this is an issue.Hopefully this works! Thank you for your time.