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

deparse doesn't handle classes #22777

Open
tonycoz opened this issue Nov 25, 2024 · 5 comments
Open

deparse doesn't handle classes #22777

tonycoz opened this issue Nov 25, 2024 · 5 comments
Assignees
Labels
class Issues related to 'class' keyword or __CLASS__

Comments

@tonycoz
Copy link
Contributor

tonycoz commented Nov 25, 2024

Module:

Trying to deparse a method of a class produces a message and incorrect code:

$ ./perl -Ilib -Mfeature=class -MO=Deparse -e 'class A { method b {} }'
class is experimental at -e line 1.
method is experimental at -e line 1.
unexpected OP_METHSTART at lib/B/Deparse.pm line 1677.
use feature 'class';
{
    ();
    package A;
    sub b : method {
        XXX;
        ();
    }
}
-e syntax OK

fields are also not deparsed:

$ ./perl -Ilib -Mfeature=class -MO=Deparse -e 'class A { field $x; method x { $x } }'
class is experimental at -e line 1.
field is experimental at -e line 1.
method is experimental at -e line 1.
unexpected OP_METHSTART at lib/B/Deparse.pm line 1677.
use feature 'class';
{
    package A;
    ;
    sub x : method {
        XXX;
        $x;
    }
}
-e syntax OK

Steps to Reproduce

See above.

Expected behavior

Deparse handles the OP_METHSTART and emits the field declarations.

Perl configuration

Summary of my perl5 (revision 5 version 41 subversion 7) configuration:
  Commit id: f254d774f8489de819d97ce662af2dbe3eb9f12b
  Platform:
    osname=linux
    osvers=6.1.0-25-amd64
    archname=x86_64-linux-thread-multi
    uname='linux venus 6.1.0-25-amd64 #1 smp preempt_dynamic debian 6.1.106-3 (2024-08-26) x86_64 gnulinux '
    config_args='-des -Dusedevel -Dusethreads'
    hint=recommended
    useposix=true
    d_sigaction=define
    useithreads=define
    usemultiplicity=define
    use64bitint=define
    use64bitall=define
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
  Compiler:
    cc='cc'
    ccflags ='-D_REENTRANT -D_GNU_SOURCE -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=2'
    optimize='-O2'
    cppflags='-D_REENTRANT -D_GNU_SOURCE -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include'
    ccversion=''
    gccversion='12.2.0'
    gccosandvers=''
    intsize=4
    longsize=8
    ptrsize=8
    doublesize=8
    byteorder=12345678
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=16
    longdblkind=3
    ivtype='long'
    ivsize=8
    nvtype='double'
    nvsize=8
    Off_t='off_t'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='cc'
    ldflags =' -fstack-protector-strong -L/usr/local/lib'
    libpth=/usr/local/lib /usr/lib/x86_64-linux-gnu /usr/lib /usr/lib64
    libs=-lpthread -lgdbm -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
    perllibs=-lpthread -ldl -lm -lcrypt -lutil -lc
    libc=/lib/x86_64-linux-gnu/libc.so.6
    so=so
    useshrplib=false
    libperl=libperl.a
    gnulibc_version='2.36'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs
    dlext=so
    d_dlsymun=undef
    ccdlflags='-Wl,-E'
    cccdlflags='-fPIC'
    lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector-strong'


Characteristics of this binary (from libperl): 
  Compile-time options:
    HAS_LONG_DOUBLE
    HAS_STRTOLD
    HAS_TIMES
    MULTIPLICITY
    PERLIO_LAYERS
    PERL_COPY_ON_WRITE
    PERL_DONT_CREATE_GVSV
    PERL_HASH_FUNC_SIPHASH13
    PERL_HASH_USE_SBOX32
    PERL_MALLOC_WRAP
    PERL_OP_PARENT
    PERL_PRESERVE_IVUV
    PERL_USE_DEVEL
    PERL_USE_SAFE_PUTENV
    USE_64_BIT_ALL
    USE_64_BIT_INT
    USE_ITHREADS
    USE_LARGE_FILES
    USE_LOCALE
    USE_LOCALE_COLLATE
    USE_LOCALE_CTYPE
    USE_LOCALE_NUMERIC
    USE_LOCALE_TIME
    USE_PERLIO
    USE_PERL_ATOF
    USE_REENTRANT_API
    USE_THREAD_SAFE_LOCALE
  Built under linux
  Compiled at Nov 25 2024 10:56:52
  %ENV:
    PERLBREW_BASHRC_VERSION="0.43"
    PERLBREW_HOME="/home/tony/.perlbrew"
    PERLBREW_MANPATH=""
    PERLBREW_PATH="/home/tony/perl5/perlbrew/bin"
    PERLBREW_ROOT="/home/tony/perl5/perlbrew"
    PERLBREW_VERSION="0.67"
  @INC:
    lib
    /usr/local/lib/perl5/site_perl/5.41.7/x86_64-linux-thread-multi
    /usr/local/lib/perl5/site_perl/5.41.7
    /usr/local/lib/perl5/5.41.7/x86_64-linux-thread-multi
    /usr/local/lib/perl5/5.41.7
@jkeenan
Copy link
Contributor

jkeenan commented Nov 26, 2024

I get this in perl-5.38.2 and perl-5.40.0, so is it safe to say that this has been a flaw in class "from the beginning"? @tonycoz @leonerd

@jkeenan jkeenan added class Issues related to 'class' keyword or __CLASS__ and removed Needs Triage labels Nov 26, 2024
@leonerd
Copy link
Contributor

leonerd commented Nov 26, 2024

Indeed so

@leonerd leonerd self-assigned this Nov 26, 2024
@leonerd
Copy link
Contributor

leonerd commented Nov 27, 2024

I don't really have any good thoughts on how to properly tackle this one.

In the small scope, I can easily just get Deparse to handle an OP_METHSTART, skipping over and mostly ignoring it, and having it emit the word method rather than sub. That's not difficult.

But in that situation it will just emit a named method definition that refers to field variables that are outside. Admittedly this is no worse than the usual situation with regards lexical closures capturing outer variables, it's just it'll be more likely to be of interest in methods because they almost always tend to capture field variables.

Ultimately I don't think Deparse is really set up to handle methods in classes.

@leonerd
Copy link
Contributor

leonerd commented Nov 27, 2024

I have started to address this in #22790, but it only opens more questions that start a long and awkward slope.

With that fix in place, the following output would be produced:

$ ./perl -Ilib -MO=Deparse -Mfeature=class -E 'class C { field $x; method m () { $x++ } }'
class is experimental at -e line 1.
field is experimental at -e line 1.
method is experimental at -e line 1.
use feature 'class';
use feature 'current_sub', 'bitwise', 'evalbytes', 'fc', 'isa', 'module_true', 'postderef_qq', 'say', 'signatures', 'state', 'try', 'unicode_strings', 'unicode_eval';
use builtin (':5.41');
{
    package C;
    ;
    method m () {
        $x++;
    }
}
-e syntax OK

As you can see, no more complaints about unrecognised OP_METHSTART, and the body is emitted under a method keyword rather than sub. But the emitted code body isn't usable for eval any more, because it puts a method inside a plain package, rather than a class.

OK, so maybe the solution is to emit a class? But if you do that, the body still isn't eval'able because classes can't be reöpened.

Furthermore, this body isn't really usable because we've forgotten the definition of field $x. Reconstructing that is somewhat harder, because we've also forgotten things like any :reader or :writer attributes that might have been applied to them.

Any of these questions probably can be answered, but only by introducing a lot more awkward memories of what the original declaration syntax was, purely for the purpose of deparse. I don't know to what extent such a thing is justifiable.

@tonycoz
Copy link
Contributor Author

tonycoz commented Nov 28, 2024

I see two main uses for Deparse:
a) saving sub definitions to longer term storage so they can be restored, as Data::Dumper can do
b) seeing what the code supplied to the compiler compiles to

So while deparsing a class isn't so useful for the first, it's more useful for the second.

As to :reader/:writer, I could see those simply deparsing the synthesized methods.

Of course, to deparse the classes themselves, I expect B itself will need to be extended and those extensions might be useful elsewhere, like for meta.pm.

I don't think any of this is urgent.

It does bring up a possible flaw in the PPC process - do we try to resolve how some feature interacts with existing perl mechanisms, including magic, overloading, Data::Dumper, Storable, B::Deparse, Safe, eval (should failing to load a class allow a second attempt? #22374), quoting/subparsing and I'm sure I missed some.

Right now that's left to the implementer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
class Issues related to 'class' keyword or __CLASS__
Projects
None yet
Development

No branches or pull requests

3 participants