Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/canonicalize/Can.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8257,7 +8257,7 @@ const MainFunctionStatus = enum { valid, invalid, not_found };

/// Check if this module has a valid main! function (1 argument lambda).
/// Reports an error if main! exists but has the wrong arity.
fn checkMainFunction(self: *Self) std.mem.Allocator.Error!MainFunctionStatus {
pub fn checkMainFunction(self: *Self) std.mem.Allocator.Error!MainFunctionStatus {
const file = self.parse_ir.store.getFile();

for (self.parse_ir.store.statementSlice(file.statements)) |stmt_id| {
Expand Down
24 changes: 22 additions & 2 deletions src/playground_wasm/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,24 @@ fn compileSource(source: []const u8) !CompilerStageData {
}
};

// This is a hack for type modules to prevent "TYPE MODULE MISSING MATCHING TYPE" error
// Maybe there is a better way?
if (try czer.checkMainFunction() == .not_found) {
for (czer.parse_ir.store.statementSlice(file.statements)) |stmt_id| {
const stmt = czer.parse_ir.store.getStatement(stmt_id);
if (stmt == .type_decl) {
const type_decl = stmt.type_decl;
const header = czer.parse_ir.store.getTypeHeader(type_decl.header) catch continue;
const type_name_ident = czer.parse_ir.tokens.resolveIdentifier(header.name) orelse continue;
const type_name_text = czer.env.getIdent(type_name_ident);

// Just use the first type declaration as module name
module_env.module_name = type_name_text;
break;
}
}
}

czer.validateForChecking() catch |err| {
logDebug("compileSource: validateForChecking failed: {}\n", .{err});
if (err == error.OutOfMemory) {
Expand All @@ -1040,9 +1058,11 @@ fn compileSource(source: []const u8) !CompilerStageData {
// Collect canonicalization diagnostics
const diagnostics = try env.getDiagnostics();

const filename = try std.fmt.allocPrint(allocator, "{s}.roc", .{module_env.module_name});

// Process and store CAN diagnostics
for (diagnostics) |diagnostic| {
const report = env.diagnosticToReport(diagnostic, allocator, "main.roc") catch {
const report = env.diagnosticToReport(diagnostic, allocator, filename) catch {
// Log the error and continue processing other diagnostics
// This prevents crashes on malformed diagnostics or empty input
continue;
Expand Down Expand Up @@ -1078,7 +1098,7 @@ fn compileSource(source: []const u8) !CompilerStageData {
result.module_env,
type_can_ir,
&solver.snapshots,
"main.roc",
filename,
&.{}, // other_modules - empty for playground
);
defer report_builder.deinit();
Expand Down
Loading