From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-0.5 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id B974E1F463 for ; Wed, 11 Dec 2019 14:54:21 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 2CA481209E6; Wed, 11 Dec 2019 23:54:09 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id AFE90120939 for ; Wed, 11 Dec 2019 23:54:06 +0900 (JST) Received: by filter0082p3mdw1.sendgrid.net with SMTP id filter0082p3mdw1-11630-5DF10313-11 2019-12-11 14:54:11.327553338 +0000 UTC m=+1868558.367057157 Received: from herokuapp.com (unknown [52.207.12.188]) by ismtpd0005p1iad1.sendgrid.net (SG) with ESMTP id JKCUYW0wQVOIKa6-_ync-g for ; Wed, 11 Dec 2019 14:54:11.165 +0000 (UTC) Date: Wed, 11 Dec 2019 14:54:11 +0000 (UTC) From: daniel@dan42.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 71856 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16377 X-Redmine-Issue-Author: byroot X-Redmine-Sender: Dan0042 X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?8sy4RigFvRTdBfCVJrT9zb2J88PC92TMQwdNgaWYaq6VUHWXeoaVdcUZosQz6q?= =?us-ascii?Q?Tb3bZU05HFMrQRYZ1PF8cgqe25=2Ft=2FGXZ7FYH3h9?= =?us-ascii?Q?PWg0Q0quSdTV5QHq0UnIt2oP26zPH0=2FwA3ai+S3?= =?us-ascii?Q?Pwzd=2FDc5C65iy9a5OHc9gelaJd9g66RGSFFXo2I?= =?us-ascii?Q?voq8O61la6i8OalgGGeLTUWSXqPtNeXjT=2Fw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 96204 Subject: [ruby-core:96204] [Ruby master Feature#16377] Regexp literals should be frozen X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16377 has been updated by Dan0042 (Daniel DeLorme). I really hope this does not go through. Regexp literals have been "unduplicated" like this since at least 1.8 and we've never had problems. And now we should freeze them and introduce an incompatibility just for the sake of Communicating the Holy Gospel of Immutability? I don't find that a valid reason. Additionaly, Regexp literals are not deduplicated in the same sense as frozen string literals; one `/abc/` is independant from another `/abc/` so we're not actually leaking "global" state. In the example above the state is local to the method but shared between invocations. Not sure how that should be called, but certainly not "global". And what if the `mutate` behavior shown above is actually wanted? Sure it's a hack, but it's a bit like function-static variables in php. Or, more realistically, what about something like this? ```ruby class Regexp def analyze @analyze ||= RegexpAnalyzer.analyze_performance_issues(self) end end ``` Freezing objects closes off possibilities when it's done by default, and should only be done when absolutely necessary. ---------------------------------------- Feature #16377: Regexp literals should be frozen https://bugs.ruby-lang.org/issues/16377#change-83093 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- The following script: ```ruby def mutate re = /foo/ state = re.instance_variable_get(:@state) re.instance_variable_set(:@state, state.to_i + 1) state end 3.times do p mutate end ``` Output this: ``` nil 1 2 ``` IMHO, you shouldn't be able to mutate an unduplicated literal. GitHub pull request: https://github.com/ruby/ruby/pull/2705 -- https://bugs.ruby-lang.org/