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

Modified CondExprNode generate_evaluation_code to resolve gh-5555 #5587

Closed

Conversation

oleksandr-pavlyk
Copy link
Contributor

Only wrap conditional statement in parenthesis if it does not start with ( or does not end with ) already.

This change resolves gh-5555 and its duplicate gh-5584 which I filed.

@da-woods Please let me know if you'd like tests to be added.

Only wrap conditional statement in parenthesis if it does not start
with ( or does not end with ) already.
@da-woods
Copy link
Contributor

da-woods commented Aug 2, 2023

I'm never hugely keen on inspecting strings of generated code, but I could believe this is the best solution for this problem.

Testing: We do now have some tests that match regexes in the generated code. I think we also have some tests where we make C warnings an error. Personally I'm inclined not to do either here. The main reason is that the warnings came from the cython.array utility code so they aren't a particularly clean or minimal thing to test.

One code-style comment, but other than that I'm happy. I'll leave it a day or two for other comments though.

Use template substitution, avoid string concatenation

Co-authored-by: da-woods <dw-git@d-woods.co.uk>
@scoder
Copy link
Contributor

scoder commented Aug 4, 2023

I'm never hugely keen on inspecting strings of generated code

Me neither.

One thing that seems to work for me is to wrap the condition in a no-op macro:

if (__Pyx_IsCTrue((...))) {

emits no warning even if I just #define __Pyx_IsCTrue(x) x.

Obfuscates the C code, though.

@scoder
Copy link
Contributor

scoder commented Aug 4, 2023

This change also works for me:

diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
index 94c8be17d..e64cec695 100644
--- a/Cython/Compiler/ExprNodes.py
+++ b/Cython/Compiler/ExprNodes.py
@@ -12800,7 +12800,7 @@ class CondExprNode(ExprNode):
         return self.true_val.is_ephemeral() or self.false_val.is_ephemeral()
 
     def analyse_types(self, env):
-        self.test = self.test.analyse_types(env).coerce_to_boolean(env)
+        self.test = self.test.analyse_temp_boolean_expression(env)
         self.true_val = self.true_val.analyse_types(env)
         self.false_val = self.false_val.analyse_types(env)
         return self.analyse_result_type(env)

It aligns conditional expressions with what we do for if-statements. It uses an additional temporary for the condition result, but that's a cheap cost, I'd say.

@scoder scoder closed this in f1d52e6 Aug 4, 2023
@scoder
Copy link
Contributor

scoder commented Aug 4, 2023

I've applied this change to the master branch.

@da-woods
Copy link
Contributor

da-woods commented Aug 4, 2023

Slightly irrelevant now, but I'm not sure how robust the string editing version would be. e.g. dereferencing and calling a function pointer:

(*fptr)(arg)

I'm not sure if Cython generates that exact code, but it'd have been worth investigating. (Missed this on my first look)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] clang warning from View.MemoryView's array_cwrapper
3 participants