Skip to content

Commit a6b1642

Browse files
fix: handle try-except blocks without except
1 parent 2c84ffc commit a6b1642

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/main/java/com/github/_1c_syntax/bsl/languageserver/cfg/CfgBuildingParseTreeVisitor.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,23 @@ public ParseTree visitTryStatement(BSLParser.TryStatementContext ctx) {
330330
// весь блок try
331331
blocks.enterBlock();
332332

333-
blocks.enterBlock();
334-
ctx.exceptCodeBlock().accept(this);
335-
var exception = blocks.leaveBlock();
333+
StatementsBlockWriter.StatementsBlockRecord exception;
334+
CfgVertex exceptionHandler;
335+
336+
// Check if except block exists before processing it
337+
if (ctx.exceptCodeBlock() != null) {
338+
blocks.enterBlock();
339+
ctx.exceptCodeBlock().accept(this);
340+
exception = blocks.leaveBlock();
341+
exceptionHandler = exception.begin();
342+
} else {
343+
// If no except block, use the parent's exception handler
344+
exceptionHandler = blocks.getCurrentBlock().getJumpContext().exceptionHandler;
345+
exception = null;
346+
}
336347

337348
var jumpInfo = new StatementsBlockWriter.JumpInformationRecord();
338-
jumpInfo.exceptionHandler = exception.begin();
349+
jumpInfo.exceptionHandler = exceptionHandler;
339350

340351
blocks.enterBlock(jumpInfo);
341352
ctx.tryCodeBlock().accept(this);
@@ -344,8 +355,13 @@ public ParseTree visitTryStatement(BSLParser.TryStatementContext ctx) {
344355
graph.addEdge(tryBranch, success.begin(), CfgEdgeType.TRUE_BRANCH);
345356
blocks.getCurrentBlock().getBuildParts().push(success.end());
346357

347-
graph.addEdge(tryBranch, exception.begin(), CfgEdgeType.FALSE_BRANCH);
348-
blocks.getCurrentBlock().getBuildParts().push(exception.end());
358+
if (exception != null) {
359+
graph.addEdge(tryBranch, exception.begin(), CfgEdgeType.FALSE_BRANCH);
360+
blocks.getCurrentBlock().getBuildParts().push(exception.end());
361+
} else {
362+
// If no except block, connect false branch to the parent's exception handler
363+
graph.addEdge(tryBranch, exceptionHandler, CfgEdgeType.FALSE_BRANCH);
364+
}
349365

350366
var builtBlock = blocks.leaveBlock();
351367

src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/AllFunctionPathMustHaveReturnDiagnosticTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,20 @@ void testExitByRaiseException() {
123123
assertThat(diagnostics).isEmpty();
124124

125125
}
126+
127+
@Test
128+
void testTryWithoutExceptBlock() {
129+
var sample =
130+
"""
131+
Функция Тест()
132+
Попытка
133+
Возврат ВыполнитьОперацию();
134+
КонецПопытки;
135+
КонецФункции""";
136+
137+
var documentContext = TestUtils.getDocumentContext(sample);
138+
var diagnostics = getDiagnostics(documentContext);
139+
140+
assertThat(diagnostics).isEmpty();
141+
}
126142
}

0 commit comments

Comments
 (0)