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

Improve #4708 #4852

Conversation

makenowjust
Copy link
Contributor

@makenowjust makenowjust commented Aug 19, 2017

Fix #4849

By this fix, ToSVisitor uses parent node information to decide to wrap receiver with parens, so we can detect chained comparison correctly!
to_s with 1 <= 2 <= 3 AST yields itself, and now to_s with (1 <= 2).as(Bool) AST yields itself too.

This fixes `ToSVisitor` uses parent node information to decide to wrap receiver with parens (so we can detect chained comparison correctly!)
`to_s` with `1 <= 2 <= 3` AST yields itself, and now `to_s` with `(1 <= 2).as(Bool)` AST yields itself too.
@makenowjust makenowjust changed the title Correct chained comparison to_s to fix #4708 Improve #4708 Aug 19, 2017
There are difference between `(1 <= 2) <= 3` and `1 <= 2 <= 3`, first is
comparison of comparison, but second is chained comparison.
This fix is need to work `pp (1 <= 2) <= 3` correctly (this code should
causes compilation error such like `Bool has no '<=' method` and I believe
this behavior is right.).
@makenowjust makenowjust force-pushed the fix/crystal/to-s-comparison-correctly branch from 5a0d095 to fd378d2 Compare August 19, 2017 13:42
@asterite
Copy link
Member

@makenowjust Maybe instead we can leave things as they are now, but change the normalize of Expressions and remove the case when 1 that returns exps[0]. I think that's a wrong optimization that's causing this bug. What do you think? I tried the new test and it still works, but I didn't run the whole compile spec suite.

I'd also add a test for the to_s that started to fail: (1 > 2).as(Bool)

@makenowjust
Copy link
Contributor Author

makenowjust commented Aug 19, 2017

@asterite It's interesting. But I have fundamental question: is chained comparison really needed? Crystal has Range, so we can rewrite 1 <= a <= 10 as (1..10).includes? a. I think it is simpler than chained comparison and more objective. In addition, (a < b) < c != a < b < c seems strange to me.

I'd also add a test for the to_s that started to fail: (1 > 2).as(Bool)

Yes. But where is this test placed? (We may need to add spec/compiler/normalize/expressions.cr.)

@asterite
Copy link
Member

We can probably removed chained comparison, yes. I added it in the past because it bothered me to write a <= b <= c as a <= b && b <= c but it's not a big deal. It will also simplify the language and remove some border cases.

So for me, yes, let's remove chained comparison. I'd like to know what others think, though.

@RX14
Copy link
Contributor

RX14 commented Aug 19, 2017

Please leave it in, 1 <= x <= 10 is much easier to read than using ranges and include, plus it's more flexible as it allows you to control strictness at both ends of the range. I think that problems with ASTNode#to_s are a pretty silly reason to remove a language feature.

@asterite
Copy link
Member

Yes, let's leave it in :-)

makenowjust added a commit to makenowjust/crystal that referenced this pull request Aug 20, 2017
Fix crystal-lang#4849
See crystal-lang#4852 (comment)

This contains some MainVisitor fix to follow this change, and adds specs
to `spec/compiler/normalize/expressions_spec.cr`, it's a new file.
makenowjust added a commit to makenowjust/crystal that referenced this pull request Aug 20, 2017
Fix crystal-lang#4849
See crystal-lang#4852 (comment)

This contains some MainVisitor fix to follow this change, and adds specs
to `spec/compiler/normalize/expressions_spec.cr`, it's a new file.
@bew
Copy link
Contributor

bew commented Sep 26, 2017

What is the state of this? Should it be closed or merged?

@RX14 RX14 added this to the Next milestone Sep 26, 2017
@asterite
Copy link
Member

This shouldn't be merge, #4861 is better. I think, I'm so confused with so many PRs targetting the same issue.

Copy link
Member

@asterite asterite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't merge, I think #4861 fixes this in a better way

@makenowjust
Copy link
Contributor Author

@asterite Thank you! and I think so too. I'll work for merging #4861. Sorry for late.

makenowjust added a commit to makenowjust/crystal that referenced this pull request Sep 27, 2017
Fix crystal-lang#4849
See crystal-lang#4852 (comment)

This contains some MainVisitor fix to follow this change, and adds specs
to `spec/compiler/normalize/expressions_spec.cr`, it's a new file.
@mverzilli mverzilli removed this from the Next milestone Sep 27, 2017
@mverzilli
Copy link

@makenowjust can we close then?

@makenowjust
Copy link
Contributor Author

@mverzilli of course welcome

@asterite asterite closed this Sep 27, 2017
@makenowjust makenowjust deleted the fix/crystal/to-s-comparison-correctly branch September 27, 2017 13:05
mverzilli pushed a commit that referenced this pull request Sep 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Significant paranthesis removed in macro interpolation (introduced by #4708)
5 participants