Skip to content

Stage2: Implement comptime closures and the This builtin #9823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 23, 2021

Conversation

SpexGuy
Copy link
Contributor

@SpexGuy SpexGuy commented Sep 23, 2021

No description provided.

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work.

src/Module.zig Outdated
/// child decls. Values stored in the value_arena of the linked decl.
/// During sema, this map is backed by the gpa. Once sema completes,
/// it is reallocated using the value_arena.
captures: std.AutoArrayHashMapUnmanaged(Zir.Inst.Ref, TypedValue) = .{},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, whenever a key is added here it is always an Index, not a Ref, and then indexToRef is used on the way in, and refToIndex on the way out. Simpler would be to make the key an Index right? And then those functions don't need to be moved from AstGen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that originally, but it doesn't quite work. zirClosureGet uses the un_node field, which uses Ref as its operand. I could reinterpret it as an index, but that would be really nasty. The indexToRef in zirClosureCapture could be moved to be a refToIndex in zirClosureGet, but it doesn't really improve the situation much. A second option would be to introduce a un_node_index field, but that also feels like overkill to me.

Copy link
Member

@andrewrk andrewrk Sep 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's overkill - plenty of ZIR instructions have their own form. closure_get doesn't want a un_node, it wants a node and a zir index, right? Let's give it the type-correct thing that it wants.

Another argument is that it helps readability/maintainability of the codebase. Requiring a Ref or an Index mean different things and it could be confusing to someone reading the code that it requires the wrong one.

Comment on lines +307 to +309
// use a temp to avoid unintentional aliasing due to RLS
const tmp = try self.scope.captures.clone(self.perm_arena);
self.scope.captures = tmp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sound like we need to have a big language design discussion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm always down for that! This is #3696 and friends.

@andrewrk andrewrk merged commit a0a847f into ziglang:master Sep 23, 2021
@andrewrk andrewrk deleted the comptime-closures branch September 23, 2021 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants