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

Folded constants are not deparsed correctly #14304

Open
p5pRT opened this issue Dec 5, 2014 · 7 comments
Open

Folded constants are not deparsed correctly #14304

p5pRT opened this issue Dec 5, 2014 · 7 comments

Comments

@p5pRT
Copy link

p5pRT commented Dec 5, 2014

Migrated from rt.perl.org#123368 (status was 'open')

Searchable as RT123368$

@p5pRT
Copy link
Author

p5pRT commented Dec 5, 2014

From @cpansprout

Because constant folding is just an optimisation, we hide the fact that it has happened when you write things like \(3+4). That will give a reference to a new scalar each time, even though the + has been executed at compile time. (The flags on the constant are such that \ will copy it.)

B​::Deparse outputs \(3+4) as \7, which is a truly read-only value. That means the code behaves differently after deparsing (and core tests fail).

So, the question is​: How should something like this be deparsed? I could do 0+7 for numbers and ""."foo" for strings, but what about other values? Something like do { my $dummy = ... }?

Is this something we need to fix, or is this drawback that should just be documented as a known limitation?

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Dec 5, 2014

From @bulk88

Father Chrysostomos wrote​:

# New Ticket Created by Father Chrysostomos
# Please include the string​: [perl #123368]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=123368 >

Because constant folding is just an optimisation, we hide the fact that it has happened when you write things like \(3+4). That will give a reference to a new scalar each time, even though the + has been executed at compile time. (The flags on the constant are such that \ will copy it.)

B​::Deparse outputs \(3+4) as \7, which is a truly read-only value. That means the code behaves differently after deparsing (and core tests fail).

So, the question is​: How should something like this be deparsed? I could do 0+7 for numbers and ""."foo" for strings, but what about other values? Something like do { my $dummy = ... }?

Is this something we need to fix, or is this drawback that should just be documented as a known limitation?

What is the optree difference between folded \(3+4) and \7?

@p5pRT
Copy link
Author

p5pRT commented Dec 5, 2014

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Dec 5, 2014

From @cpansprout

On Fri Dec 05 08​:49​:24 2014, bulk88 wrote​:

Father Chrysostomos wrote​:

# New Ticket Created by Father Chrysostomos
# Please include the string​: [perl #123368]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=123368 >

Because constant folding is just an optimisation, we hide the fact
that it has happened when you write things like \(3+4). That will
give a reference to a new scalar each time, even though the + has
been executed at compile time. (The flags on the constant are such
that \ will copy it.)

B​::Deparse outputs \(3+4) as \7, which is a truly read-only value.
That means the code behaves differently after deparsing (and core
tests fail).

So, the question is​: How should something like this be deparsed? I
could do 0+7 for numbers and ""."foo" for strings, but what about
other values? Something like do { my $dummy = ... }?

Is this something we need to fix, or is this drawback that should
just be documented as a known limitation?

What is the optree difference between folded \(3+4) and \7?

The SV associated with 3+4 has the SVs_PADTMP flag on.

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Dec 6, 2014

From @ap

* Father Chrysostomos <perlbug-followup@​perl.org> [2014-12-05 02​:40]​:

Because constant folding is just an optimisation, we hide the fact
that it has happened when you write things like \(3+4). That will
give a reference to a new scalar each time, even though the + has been
executed at compile time. (The flags on the constant are such that
\ will copy it.)

  $ perl -e '++$$_ for \(3+4)'
  Modification of a read-only value attempted at -e line 1.

What am I missing?

@p5pRT
Copy link
Author

p5pRT commented Dec 6, 2014

From @mauke

Am 06.12.2014 um 18​:20 schrieb Aristotle Pagaltzis​:

* Father Chrysostomos <perlbug-followup@​perl.org> [2014-12-05 02​:40]​:

Because constant folding is just an optimisation, we hide the fact
that it has happened when you write things like \(3+4). That will
give a reference to a new scalar each time, even though the + has been
executed at compile time. (The flags on the constant are such that
\ will copy it.)

 $ perl \-e '\+\+$$\_ for \\\(3\+4\)'
 Modification of a read\-only value attempted at \-e line 1\.

What am I missing?

$ perl -wE 'my @​xs; for (1 .. 3) { push @​xs, \7; say $xs[-1]; }'
SCALAR(0x8b2ec08)
SCALAR(0x8b2ec08)
SCALAR(0x8b2ec08)

$ perl -wE 'my @​xs; for (1 .. 3) { push @​xs, \(3+4); say $xs[-1]; }'
SCALAR(0x83c2308)
SCALAR(0x83c2da8)
SCALAR(0x83c2dd8)

Apparently it's making a copy but the copy is still read-only (as I
think it should be).

--
Lukas Mai <plokinom@​gmail.com>

@p5pRT
Copy link
Author

p5pRT commented Dec 6, 2014

From @cpansprout

On Sat Dec 06 09​:21​:18 2014, aristotle wrote​:

* Father Chrysostomos <perlbug-followup@​perl.org> [2014-12-05 02​:40]​:

Because constant folding is just an optimisation, we hide the fact
that it has happened when you write things like \(3+4). That will
give a reference to a new scalar each time, even though the + has been
executed at compile time. (The flags on the constant are such that
\ will copy it.)

$ perl \-e '\+\+$$\_ for \\\(3\+4\)'
Modification of a read\-only value attempted at \-e line 1\.

What am I missing?

s/perl/perl5.20/

There was a big discussion leading up to the change. Basically the conclusion was that the documentation that says constant folding makes no observable changes should be considered correct, and the implementation changed to match it. Another consideration was that this opened up new venues for even more optimisations than it the past, without the risk of breaking code that expect to modify scalars returned by operators. Take join, for instance, which recently became subject to constant folding.

--

Father Chrysostomos

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

No branches or pull requests

2 participants