Skip to content

Commit b1eca3f

Browse files
committed
dogfooding: Fix main() issues
1 parent 07293f8 commit b1eca3f

5 files changed

Lines changed: 35 additions & 2 deletions

File tree

.claude/settings.local.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"Bash(for i in {1..5})",
2424
"Bash(do echo \"=== Run $i ===\")",
2525
"Bash(for:*)",
26-
"Bash(do dotnet test --no-build)"
26+
"Bash(do dotnet test --no-build)",
27+
"Bash(dotnet run:*)",
28+
"Bash(dotnet restore:*)"
2729
]
2830
}
2931
}

dogfood_output/issues/20260117_001232_compilation_failed_0001/source.spy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ print(result)
103103
print(5) # Done
104104

105105
# EXPECTED OUTPUT:
106-
# 1
107106
# 2
108107
# True
109108
# 4

src/Sharpy.Cli/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ static void EmitCSharp(FileInfo inputFile, FileInfo? output, ICompilerLogger log
599599
var context = new CodeGenContext(symbolTable, builtins)
600600
{
601601
SourceFilePath = inputFile.FullName,
602+
// Single-file emit is treated as an entry point for consistency with run/build
603+
IsEntryPoint = true,
602604
Logger = logger
603605
};
604606

src/Sharpy.Compiler/CodeGen/RoslynEmitter.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,6 +2753,34 @@ private StatementSyntax GenerateForEachCore(Expression target, ExpressionSyntax
27532753
if (target is Identifier varName)
27542754
{
27552755
var loopVar = NameMangler.ToCamelCase(varName.Name);
2756+
2757+
// Check if the variable is already declared in an enclosing scope
2758+
// If so, we need to use a temporary variable to avoid CS0136
2759+
if (_declaredVariables.Contains(loopVar) || _variableVersions.ContainsKey(loopVar))
2760+
{
2761+
// Variable already exists - use a temporary loop variable and assign
2762+
var tempLoopVar = GenerateTempVarName("loopVar");
2763+
2764+
// Prepend assignment to existing variable at the start of the body
2765+
var assignToExisting = ExpressionStatement(
2766+
AssignmentExpression(
2767+
SyntaxKind.SimpleAssignmentExpression,
2768+
IdentifierName(loopVar),
2769+
IdentifierName(tempLoopVar)));
2770+
2771+
var newBodyStatements = new List<StatementSyntax> { assignToExisting };
2772+
newBodyStatements.AddRange(body.Statements);
2773+
var newBody = Block(newBodyStatements);
2774+
2775+
return ForEachStatement(
2776+
IdentifierName("var"),
2777+
Identifier(tempLoopVar),
2778+
iterator,
2779+
newBody);
2780+
}
2781+
2782+
// Variable is new - declare it in the foreach
2783+
_declaredVariables.Add(loopVar);
27562784
return ForEachStatement(
27572785
IdentifierName("var"),
27582786
Identifier(loopVar),

src/Sharpy.Compiler/Compiler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ public CompilationResult Compile(string sourceCode, string filePath)
160160
ProjectNamespace = !string.IsNullOrEmpty(defaultNamespace)
161161
? $"Sharpy.{ToPascalCase(defaultNamespace)}"
162162
: null,
163+
// Single-file compilation is always an entry point - generate Main method
164+
IsEntryPoint = true,
163165
Logger = _logger
164166
};
165167
var emitter = new RoslynEmitter(codeGenContext);

0 commit comments

Comments
 (0)