Giter Site home page Giter Site logo

Comments (8)

nikic avatar nikic commented on May 15, 2024 6

The problem here is that we compute a value to case table during compilation and all values must be known at that time. This happens pre-inheritance, so self::DEFAULT_VALUE is unknown at the time. This is unfortunate from a language design perspective, because what exactly can be evaluated during compilation and what can't is essentially an implementation detail (and may depend on whether opcache is used or not).

I think it should be possible to make this part of inheritance instead, which should allow us to evaluate all expressions. It would also delay some error reporting (about duplicate case values) to that point.

from php-src.

iluuu1994 avatar iluuu1994 commented on May 15, 2024 1

@SerafimArts Thanks! I think this should also be solved by #8190 but I'll make sure to add a test. You don't have to test it for older patches, not much has changed in the patches for enums. Edit: Although this qualifies as a bug. I'll see if I can fix that for 8.1.

from php-src.

wbars avatar wbars commented on May 15, 2024

It seems I didn't fully understood compile-time semantics, can be closed.

from php-src.

cmb69 avatar cmb69 commented on May 15, 2024

Not so fast; https://3v4l.org/5sZes works.

from php-src.

SerafimArts avatar SerafimArts commented on May 15, 2024

Found another interesting feature-bug:

class Example
{
    final public const TEST = 42;
}

enum ExampleEnum: int
{
    case CASE = Example::TEST;
}
  1. If you run the code for the first time, then it works
  2. If you run the code again (PHP 8.1.4), an error occurs: Enum case value must be compile-time evaluatable

There is a suspicion that this is somehow related to opcache:

$ php test.php -dopcache.jit_buffer_size=0 -dopcache.enable=1 -dopcache.enable_cli=0
// - First execution 42
// - Second execution 42
// - Third execution 42

$ php test.php -dopcache.jit_buffer_size=0 -dopcache.enable=1 -dopcache.enable_cli=1
// - First execution 42
// - Second execution: Enum case value must be compile-time evaluatable
// - Third execution: Enum case value must be compile-time evaluatable

P.S.

PHP 8.1.4 (cli) (built: Apr  4 2022 13:30:33) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.4, Copyright (c), by Zend Technologies

Does it make sense to try to reproduce the behavior in other versions of the language (8.1.3, 8.1.2, etc)?

from php-src.

SerafimArts avatar SerafimArts commented on May 15, 2024

Edit: Although this qualifies as a bug. I'll see if I can fix that for 8.1.

Yea. Let me move this information into a separate issue?

from php-src.

iluuu1994 avatar iluuu1994 commented on May 15, 2024

@SerafimArts Feel free to create a new issue.

from php-src.

SerafimArts avatar SerafimArts commented on May 15, 2024

@iluuu1994 Yes, I'm sorry, that's probably how it should have been done initially, so as not to confuse different situations.

from php-src.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.