Skip to content

Commit

Permalink
Update ReturnsOnAllPathsTests.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Oct 22, 2024
1 parent eb2751c commit 9f8e7cd
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/Draco.Compiler.Tests/Semantics/ReturnsOnAllPathsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,105 @@ public void NonUnitMethodWithNoExitPointDoesNotReturn()
// Assert
Assert.Empty(diagnostics);
}

[Fact]
public void NonUnitMethodReturnsUnconditionally()
{
// Arrange
// func foo(): int32 {
// return 42;
//
var tree = SyntaxTree.Create(CompilationUnit(FunctionDeclaration(
"foo",
ParameterList(),
NameType("int32"),
BlockFunctionBody(ExpressionStatement(ReturnExpression(LiteralExpression(42)))))));

// Act
var compilation = CreateCompilation(tree);
var semanticModel = compilation.GetSemanticModel(tree);
var diagnostics = semanticModel.Diagnostics;

// Assert
Assert.Empty(diagnostics);
}

[Fact]
public void NonUnitMethodReturnsConditionally()
{
// Arrange
// func foo(b: bool): int32 {
// if (b) return 42;
// }
var tree = SyntaxTree.Create(CompilationUnit(FunctionDeclaration(
"foo",
ParameterList(Parameter("b", NameType("bool"))),
NameType("int32"),
BlockFunctionBody(
ExpressionStatement(IfExpression(
NameExpression("b"),
ReturnExpression(LiteralExpression(42))))))));

// Act
var compilation = CreateCompilation(tree);
var semanticModel = compilation.GetSemanticModel(tree);
var diagnostics = semanticModel.Diagnostics;

// Assert
Assert.Single(diagnostics);
AssertDiagnostics(diagnostics, FlowAnalysisErrors.DoesNotReturn);
}

[Fact]
public void NonUnitMethodReturnsConditionallyButThenUnconditionally()
{
// Arrange
// func foo(b: bool): int32 {
// if (b) return 42;
// return 0;
// }
var tree = SyntaxTree.Create(CompilationUnit(FunctionDeclaration(
"foo",
ParameterList(Parameter("b", NameType("bool"))),
NameType("int32"),
BlockFunctionBody(
ExpressionStatement(IfExpression(
NameExpression("b"),
ReturnExpression(LiteralExpression(42)))),
ExpressionStatement(ReturnExpression(LiteralExpression(0)))))));

// Act
var compilation = CreateCompilation(tree);
var semanticModel = compilation.GetSemanticModel(tree);
var diagnostics = semanticModel.Diagnostics;

// Assert
Assert.Empty(diagnostics);
}

[Fact]
public void NonUnitMethodReturnsInConditionalLoop()
{
// Arrange
// func foo(b: bool): int32 {
// while (b) return 42;
// }
var tree = SyntaxTree.Create(CompilationUnit(FunctionDeclaration(
"foo",
ParameterList(Parameter("b", NameType("bool"))),
NameType("int32"),
BlockFunctionBody(
ExpressionStatement(WhileExpression(
NameExpression("b"),
ReturnExpression(LiteralExpression(42))))))));

// Act
var compilation = CreateCompilation(tree);
var semanticModel = compilation.GetSemanticModel(tree);
var diagnostics = semanticModel.Diagnostics;

// Assert
Assert.Single(diagnostics);
AssertDiagnostics(diagnostics, FlowAnalysisErrors.DoesNotReturn);
}
}

0 comments on commit 9f8e7cd

Please sign in to comment.