Inferring during transpilation
I maintainer di solito rispondono entro 3 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 25/100
Direzione di ricerca
Start with the provided main.php reproducer and run the shown kphp compilation command. Investigate constant-condition handling and type inference for the ternary involving KPHP_VERSION, then verify that the single-line assignment compiles while preserving the KPHP branch and PHP branch behavior.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
If I do someting like:
EmulatedEnumInt.php:
<?php
class EmulatedEnumInt {
protected int $value;
protected function __construct(int $value) {
$this->value = $value;
}
public function __toString() : string {
return (string) $this->value;
}
}
CfgType.php:
<?php
#ifndef KPHP
enum CfgType : int {
case STRING = 1;
case INT = 2;
case BOOL = 3;
case ARRAY = 4;
}
if (false) {
#endif
final class CfgType extends EmulatedEnumInt
{
const STRING = 1;
const INT = 2;
const BOOL = 3;
const ARRAY = 4;
// GENERATED
public static function STRING() : static {
return new static(1);
}
public static function INT() : static {
return new static(2);
}
public static function BOOL() : static {
return new static(3);
}
public static function ARRAY() : static {
return new static(4);
}
private static $fromMap = [
1 => 'STRING',
2 => 'INT',
3 => 'BOOL',
4 => 'ARRAY',
];
public static function tryFrom(string|int $value) : static {
if (isset(self::$fromMap[$value])) return new static(self::$fromMap[$value]);
throw new \Exception("No such enum value:".$value);
}
}
#ifndef KPHP
}
#endif
code.php:
<?php
include_once('CfgType.php');
#ifndef KPHP
/**
* Get a configuration value (PHP VERSION)
*
* (Assumes Config object is already stored in Memcache (in KEY: '___PROJECT_SHORT____CONFIG') )
*
* @param string $settingName
* @param CfgType $type
* @param int $ttl
*
* @return ?int|?string|boolean|array
*/
function cfg(string $settingName, CfgType $type = CfgType::STRING, int $ttl = 86400) {
switch($type) {
case CfgType::STRING:
$value = (time() % 2 === 0) ? NULL : (string)$settingName;
return $value;
case CfgType::INT:
$value = (time() % 2 === 0) ? NULL : (int)80;
return $value;
case CfgType::BOOL:
$value = (time() % 2 === 0) ? NULL : (bool) $settingName;
return $value;
case CfgType::ARRAY:
$value = (time() % 2 === 0) ? NULL : ['foo', 'bar'];
return $value;
}
}
if (false) {
#endif
/**
* Get a configuration value (KPHP VERSION)
*
* (Assumes Config object is already stored in Memcache (in KEY: '___PROJECT_SHORT____CONFIG') )
*
* @param string $settingName
* @param CfgType $type
* @param int $ttl
*
* @return ?int|?string|boolean|array
*/
function cfg(string $settingName, CfgType $type, int $ttl = 86400) {
$value = (string) $type;
switch($value) {
case (string)CfgType::STRING():
$value = (time() % 2 === 0) ? NULL : (string)$settingName;
return $value;
case (string)CfgType::INT():
$value = (time() % 2 === 0) ? NULL : (int)80;
return $value;
case (string)CfgType::BOOL():
$value = (time() % 2 === 0) ? NULL : (bool)$settingName;
return (bool) $settingName;
case (string)CfgType::ARRAY():
$value = (time() % 2 === 0) ? NULL : (array) ['foo', 'bar'];
return $value;
default:
return (string) $settingName.'_default';
}
}
#ifndef KPHP
}
#endif
main.php:
<?php
#ifndef KPHP // PHP
define('KPHP_VERSION', 0);
if (false)
#endif // KPHP
define('KPHP_VERSION', 1);
include('EmulatedEnumInt.php');
include('CfgType.php');
include('code.php');
$user = (KPHP_VERSION) ? cfg('USER', CfgType::STRING()) : cfg('USER', CfgType::STRING);
echo "User=".$user.PHP_EOL;
to cater for the fact that enums are not supported in KPHP (the code base I am trying to convert uses many enums), I would expect the transpiler to be able to deduce that the above statement can be re-written as:
$user = cfg('USER', CfgType::STRING());
since KPHP_VERSION is defined to be 1 when compiling with KPHP (and the else part of the ternary will only be taken when running in a PHP context)
However, if I compile like this:
# kphp main.php --mode server --include-dir $(pwd) -o ./server
I get this error:
Compilation error at stage: Calc actual calls, gen by type-inferer.cpp:53
main.php:14 in global scope
$user = (KPHP_VERSION) ? cfg('USER', CfgType::STRING()) : cfg('USER', CfgType::STRING);
pass int to argument $type of cfg
but it's declared as @param CfgType
main.php:14 in global scope
$user = (KPHP_VERSION) ? cfg('USER', CfgType::STRING()) : cfg('USER', CfgType::STRING);
1 is int
apparently because it takes the else part into account ?
I would like to be able to do assignments like ($user = ) on a single line.
However, I am aware that the $user = ... line can be replaced by:
#ifndef KPHP // PHP
$user = cfg('USER', CfgType::STRING);
if (FALSE) { // KPHP
#endif
$user = cfg('USER', CfgType::STRING());
#ifndef KPHP // PHP
}
#endif
but that syntax is rather cumbersome ...
So can't the transpiler be improved in this regard ?
- Lingua principale
- C++
- Stelle
- 1.5k
- Fork
- 116
- Merge medio
- 3g 23h
- PR unite (30g)
- 8
Preparare l'ambiente
- Include un Dockerfile o un file Docker Compose
- Nessun modello di pull request
- Nessuna guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di VKCOM/kphp
-
SendAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 10/100
I maintainer di solito rispondono entro 3 giorni
-
TontontuAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 10/100
VKCOM/kphp#1698 · 2 commenti ·
I maintainer di solito rispondono entro 3 giorni
-
PK85MSHQ0000089200087465Aperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 10/100
VKCOM/kphp#1656 · 3 commenti · 4 reazioni ·
I maintainer di solito rispondono entro 3 giorni
-
KhanAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 10/100
VKCOM/kphp#1655 · 2 reazioni ·
I maintainer di solito rispondono entro 3 giorni
-
.github/CODEOWNERSAperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 25/100
VKCOM/kphp#1645 · 1 commento · 2 reazioni ·
I maintainer di solito rispondono entro 3 giorni
Issue simili
-
Run CICD on any branch pushAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
ChicoState/autovalidate#195 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
petercorke/robotics-toolbox-python#709 ·
I maintainer di solito rispondono entro 2 giorni
-
doc: "How to Upgrade" release notes section is outdated since the `bitcoin` wrapper executableAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 2 giorni