Skip to content

Prepare AP and AR to be frozen string friendly#29655

Merged
matthewd merged 1 commit intorails:masterfrom
kirs:frozen-friendly-ap-ar
Jul 9, 2017
Merged

Prepare AP and AR to be frozen string friendly#29655
matthewd merged 1 commit intorails:masterfrom
kirs:frozen-friendly-ap-ar

Conversation

@kirs
Copy link
Copy Markdown
Member

@kirs kirs commented Jul 1, 2017

This PR makes ActionPack and ActiveRecord frozen string -friendly.
Shipping it should unblock us to move towards enforcing frozen-string-literal across all Rails components.

@matthewd

ref #29540
ref #29506

@rails-bot
Copy link
Copy Markdown

r? @eileencodes

(@rails-bot has picked a reviewer for you, use r? to override)

@pat
Copy link
Copy Markdown
Contributor

pat commented Jul 2, 2017

@kirs just to note that #29506 already covers off ActiveRecord, ActiveSupport, ActionMailer and ActionView (plus ActionJob didn't, when I last checked, require any changes). Glad to have ActionPack covered though :)

Guess that means just Railties is left!

@kirs
Copy link
Copy Markdown
Member Author

kirs commented Jul 2, 2017

@pat this PR fixes half of the failures that are happening on current master with #29506 merged and frozen_string_literal enabled in all files. I guess there were more of them?

@pat
Copy link
Copy Markdown
Contributor

pat commented Jul 2, 2017

Ah, must be things that aren't covered by tests? That's how I was checking things - running the tests with RUBYOPT="--enable-frozen-string-literal"

@kirs
Copy link
Copy Markdown
Member Author

kirs commented Jul 2, 2017

Fixed a failing test.

@pat those can be reproduced in master with:

cd actionpack
env RUBYOPT="--enable-frozen-string-literal" bundle exec rake test
cd ../activerecord
env RUBYOPT="--enable-frozen-string-literal" bundle exec rake test:postgresql

upd: RUBYOPT="--enable-frozen-string-literal" seems to have different behaviour than # frozen_string_literal: true on top of .rb file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mismerge with b86e313? This doesn't seem to be needed.

@kamipo
Copy link
Copy Markdown
Member

kamipo commented Jul 3, 2017

These dup in AR is necessary? I tried env RUBYOPT="--enable-frozen-string-literal" bundle exec rake test:postgresql but only failed the test_to_xml. It looks like an issue for builder gem.

% RUBYOPT="--enable-frozen-string-literal" ARCONN=postgresql be ruby -w -Itest test/cases/relations_test.rb -n test_to_xml               
Using postgresql
Run options: -n test_to_xml --seed 55082

# Running:

E

Finished in 0.255228s, 3.9181 runs/s, 0.0000 assertions/s.

  1) Error:
RelationTest#test_to_xml:
RuntimeError: can't modify frozen String
    /Users/kamipo/src/github.com/rails/rails/vendor/bundle/ruby/2.4.0/gems/builder-3.2.3/lib/builder/xmlmarkup.rb:287:in `_special'
    /Users/kamipo/src/github.com/rails/rails/vendor/bundle/ruby/2.4.0/gems/builder-3.2.3/lib/builder/xmlmarkup.rb:254:in `instruct!'
    /Users/kamipo/src/github.com/rails/rails/activesupport/lib/active_support/core_ext/array/conversions.rb:196:in `to_xml'
    /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/delegation.rb:39:in `to_xml'
    test/cases/relations_test.rb:83:in `block in test_to_xml'
    /Users/kamipo/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:30:in `assert_nothing_raised'
    test/cases/relations_test.rb:83:in `test_to_xml'

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
RUBYOPT="--enable-frozen-string-literal" ARCONN=postgresql bundle exec ruby -  2.30s user 0.98s system 34% cpu 9.580 total

@kirs
Copy link
Copy Markdown
Member Author

kirs commented Jul 3, 2017

@kamipo I think that happens because RUBYOPT="--enable-frozen-string-literal" and # frozen_string_literal: true in the top of every ActiveRecord file have different effect. The first one enforces frozen strings across all files and gems and the latter enforces it only in ActiveRecord.

Our goal is to enforce it in Rails codebase (#29540) so I think we should rely on # frozen_string_literal: true and not RUBYOPT to find all the spots that we want to fix. To reproduce the failures, I used the change set from #29540.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this dup necessary? Looks like that strip_heredoc returns new string and we have no strip_heredoc.dup.
https://github.com/rails/rails/blob/v5.1.2/activesupport/lib/active_support/core_ext/string/strip.rb#L21

% git grep 'strip_heredoc\.dup' | wc -l
0

@kirs
Copy link
Copy Markdown
Member Author

kirs commented Jul 3, 2017

@kamipo you were right, that one wasn't necessary. Thanks for 👀 it! I updated the code.

@rafaelfranca
Copy link
Copy Markdown
Member

Could you add the magic comments to the changed files?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not needed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm torn on this one.. does the change belong here, or is it the caller's responsibility to provide an unfrozen value?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about using sub instead of sub!, maybe that works better here?

@kirs
Copy link
Copy Markdown
Member Author

kirs commented Jul 4, 2017

Thanks for feedback! I addressed those.

If you think it's a good idea, I can also add these lines to .rubocop.yml:

Style/FrozenStringLiteralComment:
  Enabled: true
  EnforcedStyle: always
  Include:
    - 'actionpack/**/*'
    - 'activerecord/**/*'

However that would made the PR diff quite big to 👀 . My initial plan was to do it in a separate PR.

@matthewd
Copy link
Copy Markdown
Member

matthewd commented Jul 4, 2017

Yeah, separate PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we can remove all those freeze here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea! I didn't want to do that at first, but now when we have # frozen_string_literal: true in the updated files it makes sense to do it.

I updated the PR.

@kirs
Copy link
Copy Markdown
Member Author

kirs commented Jul 9, 2017

@matthewd please let me know if there's anything else I can improve in this PR.

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.

9 participants