PY-77490 Evaluate language level only once per CFG traversal during name resolution

(cherry picked from commit 6c2a3d6eec7def72340feed8ae0ae2d6b54af6ff)

IJ-CR-149696

GitOrigin-RevId: 14edaf0fa2c49d69aa8d96883dc8b02dec7197e3
This commit is contained in:
Mikhail Golubev
2024-11-19 10:42:54 +02:00
committed by intellij-monorepo-bot
parent 9d2070052f
commit 19a768b4d7

View File

@@ -74,6 +74,7 @@ public final class PyDefUseUtil {
return Collections.emptyList();
}
LanguageLevel languageLevel = PythonLanguageLevelPusher.getLanguageLevelForFile(anchor.getContainingFile());
final Collection<Instruction> result = new LinkedHashSet<>();
final HashMap<PyCallSiteExpression, ConditionalInstruction> pendingTypeGuard = new HashMap<>();
ControlFlowUtil.iteratePrev(startNum, instructions,
@@ -113,7 +114,7 @@ public final class PyDefUseUtil {
if (access.isWriteAccess() || acceptTypeAssertions && access.isAssertTypeAccess()) {
final String name = elementName(element);
if (Comparing.strEqual(name, varName)) {
if (isReachableWithVersionChecks(rwInstruction)) {
if (isReachableWithVersionChecks(rwInstruction, languageLevel)) {
result.add(rwInstruction);
}
return ControlFlowUtil.Operation.CONTINUE;
@@ -122,7 +123,7 @@ public final class PyDefUseUtil {
}
else if (acceptImplicitImports && element instanceof PyImplicitImportNameDefiner implicit) {
if (!implicit.multiResolveName(varName).isEmpty()) {
if (isReachableWithVersionChecks(instruction)) {
if (isReachableWithVersionChecks(instruction, languageLevel)) {
result.add(instruction);
}
return ControlFlowUtil.Operation.CONTINUE;
@@ -152,10 +153,9 @@ public final class PyDefUseUtil {
return instr;
}
private static boolean isReachableWithVersionChecks(@NotNull Instruction instruction) {
private static boolean isReachableWithVersionChecks(@NotNull Instruction instruction, @NotNull LanguageLevel languageLevel) {
PsiElement element = instruction.getElement();
if (element == null) return true;
LanguageLevel languageLevel = PythonLanguageLevelPusher.getLanguageLevelForFile(element.getContainingFile());
Version version = new Version(languageLevel.getMajorVersion(), languageLevel.getMinorVersion(), 0);
return evaluateVersionsForElement(element).contains(version);
}