-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
KT-4569: Intention to simplify boolean expressions with constants #392
KT-4569: Intention to simplify boolean expressions with constants #392
Conversation
Hi! Sorry for not so fast review =).
|
Hey, I'm having some problems with getting canBeReducedToBooleanConstant to work correctly, so far the only thing that it evaluates is true or false, here is the test function i've been using for it:
private fun JetExpression.canBeReducedToTrue(): Boolean {
val bindingContext = AnalyzerFacadeWithCache.getContextForElement(this)
val trace = DelegatingBindingTrace(bindingContext, "trace for constant check")
val res = CompileTimeConstantUtils.canBeReducedToBooleanConstant(this, trace, true)
println(this.getText() + " can be reduced to true? " + res)
return res
}
and when i test it i get these results: any idea where i'm going wrong ? |
I've by accident left the check for JetConstantExpression in 'canBeReducedToBooleanConstant'.
should be just
Please add this change to your pull request. Thank you. |
Hey, I'm having trouble with the behavior of canBeReducedToBooleanConstant it will turn it into |
Hi, |
Side note: please write meaningful commit messages, e. g.:
And commits like "fixes" should be squashed into original one at the end of review. Thank you. |
umm okay, it looks like i've screwed up here, i realized this PR was opened a while ago and i wanted to update my stuff so i pulled and rebased into this branch, and it turns out that everything i pulled got into this PR. on a programming related note: |
Seems that you rebased master onto your branch, instead of the opposite. No need in opening new pull requests. You can use force push to fix it.
Implementation-related question will be answered by @NataliaUkhorskaya |
Yes, as long as it will become a one int field for all flags, you can move isPure to base class. As for new flag: it should be set to true for expressions that uses properties. For example if there this a property
So you should check that the expression isn't a property call and inner expressions don't contain calls to properties |
hey, I pushed the updated version so now my intention will simplify |
Hey, I pushed some changes to the compile time stuff (not a very meaningful commit message since ill soon squash it with the other and seperate them into the intention and the compile time stuff) I changed the flag to default to false and instantiate to true from SimpleExpressions I'm not completely sure how to handle AnnotationValue and ArrayValue, (both in visitCallExpression) do I evaluate a flag for them somehow or do I always instantiate them with false ? "a" == "a" now works fine, but when i try to evaluate MyEnum.A == MyEnum.A I actually get null (ie nothing to do with the new flag, the evaluator ecaluates just returns with null, i made sure MyEnum.A is evaluated fine (not null and correct flag) but then the comparsion returns null, is that bad ? should i change the logic in the implementation ? I tried adding tests for the new flag in AbstractEvaluateExpressionTest, but I couldn't use the doTest from isPure since it relies upon using vals which means that it will always evaluate to true. thanks |
It will be good if the flag for AnnotationValue and ArrayValue will be true when at least one argument has this flag to true. What about enums: now I see that What about a test: it checks compileTimeConstant which it written for property initializer, so if in initializer for this property there isn't an another property, the flag should be false. If you always get true it will mean that something works wrong. I think that the problem is that you didn't change a default flag to false in EvaluatorContext class, so the compileTimeConstant for variable has always this flag to true. |
Hi, pushed the latest stuff:
as for ArrayValue: I changed it to check if any of the values assigned to it have the flag set to true and if so set it to true (I didn't change it in the constructor because I was worried it will make in inflexible, i just made it so in all usages (3 instances)), I'm also unsure how to test it, in the test if i do something like as for AnnotationValue and JavaClassValue: i could not understand how to instantiate those flags, currently they are being set to false by default as for the intention itself, i changed the assert you pointed at and added the elvis you suggested, but the suggestion to remove the handling for binary expressions and parenthesized expressions doesn't seem to be applicable when you think about it, the intention does logic that the constant evaluation doesn't. thanks |
Good work! There are two small notes (see my line comments for the commit). |
fixed the inline comments, separated commits |
Unfortunatly, there are two tests failed. Please, fix them and run 'All tests' configuration before submitting a final version of pull-request. |
Oh sorry, I ran all tests a while ago and I thought the change in compile time wouldn't change it, it was a while ago though, right now i'm getting more than 2 failed tests (I think for reasons not related to this intention) so I'm working on figuring that out |
Hey, I'm looking at the tests that fail and I can't really figure them out, here is an example of one: and this is what it says https://gist.github.com/wutalman/02fe57affae75fb3e931, but when i look at the file this is what it contains https://gist.github.com/wutalman/86ea85c358a0a97a0614 and when i check this in the debug instance of IDEA i get http://i.imgur.com/WpuAdYV.png so basically all the actions that i get in the editor are those that are mentioned in the test, yet it tells me that it gets unexpected ones do you know what could be causing this ? |
We have some problems with QuickFixTestsGenerated on local machines, so you can ignore errors about "Some unexpected actions available.." for now. Have you fixed ControlFlowTestGenerated$ControlStructures and JetDiagnosticsTestGenerated$Tests$Annotations tests? |
fixed the tests you pointed out, rebased it into the 2nd commit (i hope that's okay) |
You should add fixes of tests to the first commit, because they failed after your changes in it. Also be careful with imports in generated files: now you have changes in JetDiagnosticTestGenerated and if you run 'Generate Tests' run configuration, this file will be modified. |
hey, yes i see what you mean about generate tests now modifying JetDiagnosticTestGenerated, but i'm not sure how to avoid it ? |
It seems that you have 'Optimize imports on the fly' option turned on. You can disable it in Settings -> Editor -> Auto Import |
after disabling it, what imports do i remove to make it not change the generated test though ? |
You can simple rerun 'Generate Tests' run configuration. with this option enabled IDE optimize imports in JetDiagnosticTestGenerated file when you open it and remove unused imports. |
It still creates the JetDiagnosticTestGenerated file when i run maybe i'm not understanding this correctly, i want it to stop generating a new JetDiagnosticTestGenerated when generate tests is run right? or do i want to commit this new one ? |
You should run Generate tests and commit the result. If after that you compare JetDiagnosticTestGenerated file with master, there should be changes about new test method and no changes in imports. |
seems the new test is adding two imports that weren't there before but how can i remove them ? (obviously just removing them won't change anything because they will be generated like that again) |
As I see in master there are those imports (https://github.com/JetBrains/kotlin/blob/master/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java), so I don't understand where the problem is. |
…stant being referenced by a variable
…em that can be simplified to constants
okay so it seems the new generated diagnostic test has the same imports as master, i added it to the first commit as well as the fixes for the failing tests, and the second commit is again just the intention |
KT-4569: Intention to simplify boolean expressions with constants #KT-4569 Fixed
Thank you! |
Intention to simplify boolean expressions with constants in them,
issue: KT-4569 (http://youtrack.jetbrains.com/issue/KT-4569)
I used some helper functions, I didn't know if the convention is to nest them inside the functions they're helping or not but when I looked at some other code it seemed that we just have them as private functions in the class so that's what I did
Parentheses handling:
if the binary expression has parentheses in it, and they don't contain any constants, they are kept as to not change what the user wanted to write
if the parentheses have constants in them and so the inner expression is changed, I will remove the parentheses only if there's only one element left: (that is again to keep the user's original intent)
if at the end of the evaluation the entire expression has parentheses around it, they will be removed: