Skip to content

Commit

Permalink
fix: Foreach value placeholder can't be .Subscript when map
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Sep 19, 2024
1 parent b1bd476 commit 2a17e7e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
51 changes: 47 additions & 4 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1756,11 +1756,54 @@ fn resolvePlaceholderWithRelation(
},
.Subscript => {
if (resolved_type.def_type == .List) {
try self.resolvePlaceholder(child, resolved_type.resolved_type.?.List.item_type, false);
try self.resolvePlaceholder(
child,
resolved_type.resolved_type.?.List.item_type,
false,
);
} else if (resolved_type.def_type == .Map) {
try self.resolvePlaceholder(child, try resolved_type.resolved_type.?.Map.value_type.cloneOptional(&self.gc.type_registry), false);
try self.resolvePlaceholder(
child,
try resolved_type.resolved_type.?.Map.value_type.cloneOptional(&self.gc.type_registry),
false,
);
} else if (resolved_type.def_type == .String) {
try self.resolvePlaceholder(child, self.gc.type_registry.str_type, false);
try self.resolvePlaceholder(
child,
self.gc.type_registry.str_type,
false,
);
} else {
self.reporter.reportErrorFmt(
.map_key_type,
self.ast.tokens.get(child_placeholder.where),
"`{s}` can't be subscripted",
.{
(try resolved_type.toStringAlloc(self.gc.allocator)).items,
},
);
return;
}
},
.UnwrappedSubscript => {
if (resolved_type.def_type == .List) {
try self.resolvePlaceholder(
child,
resolved_type.resolved_type.?.List.item_type,
false,
);
} else if (resolved_type.def_type == .Map) {
try self.resolvePlaceholder(
child,
resolved_type.resolved_type.?.Map.value_type,
false,
);
} else if (resolved_type.def_type == .String) {
try self.resolvePlaceholder(
child,
self.gc.type_registry.str_type,
false,
);
} else {
self.reporter.reportErrorFmt(
.map_key_type,
Expand Down Expand Up @@ -8354,7 +8397,7 @@ fn forEachStatement(self: *Self) Error!Ast.Node.Index {
try obj.PlaceholderDef.link(
iterable_type_def,
placeholder,
.Subscript,
.UnwrappedSubscript,
);

break :placeholder placeholder;
Expand Down
1 change: 1 addition & 0 deletions src/obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4984,6 +4984,7 @@ pub const PlaceholderDef = struct {
Call,
Yield,
Subscript,
UnwrappedSubscript,
Key,
FieldAccess,
Assignment,
Expand Down

0 comments on commit 2a17e7e

Please sign in to comment.