Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
Remove static locals
Browse files Browse the repository at this point in the history
Needed for nightlies, not needed for 4.1
  • Loading branch information
fredemmott committed Mar 26, 2019
1 parent fcbc65a commit f1f10d8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
1 change: 1 addition & 0 deletions .hhconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ safe_array = true
safe_vector_array = true
disallow_destruct = true
user_attributes=
disable_static_local_variables = true
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 13 additions & 9 deletions src/Linters/ASTLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,38 @@ use namespace Facebook\HHAST\Linters\SuppressASTLinter;
abstract class ASTLinter<Tnode as HHAST\EditableNode> extends BaseLinter {
private ?HHAST\EditableNode $ast;

private static ?shape('hash' => string, 'ast' => HHAST\EditableNode)
$lastFileCache = null;

private static async function getASTFromFileAsync(
File $file,
): Awaitable<HHAST\EditableNode> {
static $cache = null;

$cache = self::$lastFileCache;
$hash = \sha1($file->getContents(), /* raw = */ true);
if ($cache !== null && $cache['hash'] === $hash) {
if ($cache is nonnull && $cache['hash'] === $hash) {
return $cache['ast'];
}

$ast = await HHAST\from_code_async($file->getContents());

$cache = shape(
self::$lastFileCache = shape(
'hash' => $hash,
'ast' => $ast,
);
return $ast;
}

private function getASTWithParents(): vec<(EditableNode, vec<EditableNode>)> {
static $cache = null;
private static ?vec<(EditableNode, vec<EditableNode>)> $parentsCache = null;

private function getASTWithParents(): vec<(EditableNode, vec<EditableNode>)> {
$cache = self::$parentsCache;
$ast = $this->getAST();
if ($cache !== null && $cache[0][0] === $ast) {
if ($cache is nonnull && ($cache[0][0] ?? null) === $ast) {
return $cache;
}
$cache = $ast->traverseWithParents();
return $cache;
$result = $ast->traverseWithParents();
self::$parentsCache = $result;
return $result;
}

abstract protected static function getTargetType(): classname<Tnode>;
Expand Down
5 changes: 3 additions & 2 deletions src/__Private/LSPImpl/ExecuteCommandCommand.hack
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ final class ExecuteCommandCommand extends LSPLib\ExecuteCommandCommand {
return self::error(0, 'Unsupported command: '.$command, null);
}

private static int $idCounter = 0;

private static function generateID(): string {
static $counter = 0;
return __CLASS__.'!'.$counter++;
return __CLASS__.'!'.self::$idCounter++;
}
}
11 changes: 6 additions & 5 deletions src/__Private/LintRunCLIEventHandler.hack
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ final class LintRunCLIEventHandler implements LintRunEventHandler {
\file_put_contents($file->getPath(), $file->getContents());
}

private dict<string, bool> $userResponseCache = dict[];

private async function shouldFixLintAsync<Terror as Linters\LintError>(
Linters\AutoFixingLinter<Terror> $linter,
Terror $error,
Expand All @@ -157,10 +159,9 @@ final class LintRunCLIEventHandler implements LintRunEventHandler {
return false;
}

static $cache = dict[];
$cache_key = \get_class($error->getLinter());
if (C\contains_key($cache, $cache_key)) {
$should_fix = $cache[$cache_key];
if (C\contains_key($this->userResponseCache, $cache_key)) {
$should_fix = $this->userResponseCache[$cache_key];
await $this->terminal
->getStdout()
->writeAsync(Str\format(
Expand All @@ -183,12 +184,12 @@ final class LintRunCLIEventHandler implements LintRunEventHandler {
$response = Str\trim($response);
switch ($response) {
case 'a':
$cache[$cache_key] = true;
$this->userResponseCache[$cache_key] = true;
// FALLTHROUGH
case 'y':
return true;
case 'o':
$cache[$cache_key] = false;
$this->userResponseCache[$cache_key] = false;
// FALLTHROUGH
case 'n':
case '':
Expand Down
13 changes: 7 additions & 6 deletions src/__Private/MigrationCLI.hack
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,11 @@ class MigrationCLI extends CLIWithRequiredArguments {
return 0;
}

private static ?(string, bool) $lastFileIsHack = null;

private static function isHackFile(string $file): bool {
static $cache = null;
if ($cache !== null) {
list($cache_file, $cache_result) = $cache;
if (self::$lastFileIsHack is nonnull){
list($cache_file, $cache_result) = self::$lastFileIsHack;
if ($cache_file === $file) {
return $cache_result;
}
Expand All @@ -353,12 +354,12 @@ class MigrationCLI extends CLIWithRequiredArguments {
$f = \fopen($file, 'r');
$prefix = \fread($f, 4);
if ($prefix === '<?hh') {
$cache = tuple($file, true);
self::$lastFileIsHack = tuple($file, true);
return true;
}

if (!Str\starts_with($prefix, '#!')) {
$cache = tuple($file, false);
self::$lastFileIsHack = tuple($file, false);
return false;
}
\rewind($f);
Expand All @@ -367,7 +368,7 @@ class MigrationCLI extends CLIWithRequiredArguments {

$is_hh = $prefix === '<?hh';

$cache = tuple($file, $is_hh);
self::$lastFileIsHack = tuple($file, $is_hh);
return $is_hh;
}
}

0 comments on commit f1f10d8

Please sign in to comment.