Skip to content

Instantly share code, notes, and snippets.

@amatsuda
Last active September 5, 2020 07:02
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amatsuda/bf30593f5a98df43cbbe7cd5f5466f2e to your computer and use it in GitHub Desktop.
Save amatsuda/bf30593f5a98df43cbbe7cd5f5466f2e to your computer and use it in GitHub Desktop.

Proposal for the strict mode

I propose to introduce a new magic comment for the ”strict mode” which is generally equivalent to that of other languages such as Perl or recent versions of ECMAScript.

In short, it’s a way to enable the cleaner subset of Ruby.

It’s a file scoped mode switching, restricting some of the language core features that are discouraged to use.

Usage

# strict: true

#=> enables the strict mode
# strict: false

#=> disables the strict mode (default)

List of features to disallow in the strict mode

  • global variables
  • class variables
  • Perl-ish $vars
  • constant redefinition
  • method redefinition
  • too dynamic features, such as eval, instance_eval, module_eval, class_eval, method_missing
  • maybe some other features that are already being warned
  • maybe some other features hat Matz wants to remove in the future

Plus, this includes frozen_string_literal. I kind of regret frozen_string_literal magic comment that I proposed to introduce in 2.3, because it looks ugly. When strict: true was given, frozen_string_literal is also set to true. When both strict and frozen_string_literal were given, frozen_string_literal wins.

Implementation

All strict mode checks should be done in parse and compile phase.

Purpose

The biggest benefit that we can expect from the strict mode is optimization. By restricting those too broad scoped or too dynamic features, we will be able to simplify VM instructions. Also, this would be a help for other optimization approaches such as Vlad’s MJIT, shyouhei’s deopt, jruby, etc.

What is different from existing warning mechanizm?

Warnings are basically just a guideline. Users can still ignore the warning messages and it still works. So the implementation can never assume that these evil features are not in use. With strict more, we can 100% assume cleaner subset of Ruby.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment