Skip to content
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

make decl literals work with single item pointers #21390

Merged
merged 2 commits into from
Sep 14, 2024

Conversation

xdBronch
Copy link
Contributor

closes #21387
also added test coverage for optionals

@mlugg
Copy link
Member

mlugg commented Sep 12, 2024

Looks good, would you mind expanding the test cases to also test with a function call decl literal?

src/Sema.zig Outdated
@@ -8992,6 +8992,7 @@ fn zirDeclLiteral(sema: *Sema, block: *Block, inst: Zir.Inst.Index, do_coerce: b
while (true) switch (ty.zigTypeTag(zcu)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't new to this PR, but since this is done in a loop it looks like you can wrap a type in an arbitrary level of error unions/optionals/pointers and still use them with decl literals.
In fact the master branch compiles the following just fine right now 🤔

const S = struct {
    const init: anyerror!anyerror!????S = .{};
};

test {
    const s: ????S = try try .init;
    _ = s;
}
All 1 tests passed.

@mlugg Is this intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

while it definitely allows some pretty silly stuff like your example i think its necessary, imagine something like fn init() !?*S {, i dont see any reason this shouldnt work and limiting it would seem arbitrary and potentially pretty complex to get right

Copy link
Contributor

Choose a reason for hiding this comment

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

It might be more fruitful to discuss it in the linked proposal, but as a suggestion I think restricting it to something like the following would make sense:

  • T
  • ?T
  • *qual T
  • ?*qual T
  • [*c]qual T
  • E!T
  • E!?T
  • E!*qual T
  • E!?*qual T
  • E![*c]qual T

All but the last 4 are supported as the first parameters in methods/member functions. If we could make the set of types that can participate in decl literals identical to the set that can participate in the method syntax it would make the rules nice and consistent and easy to reason about.

Copy link
Member

@mlugg mlugg Sep 12, 2024

Choose a reason for hiding this comment

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

This is intentional, and I don't think there's any reason to change it -- many RLS things (such as casting builtins) already strip error unions and optionals from types in a loop (i.e. const y: ???u32 = @intCast(@as(u64, 123)) is fine). Regardless, if you'd like to propose this, please discuss it either in the original proposal or in a new issue.

@mlugg
Copy link
Member

mlugg commented Sep 12, 2024

Looks like you're hitting CI failures from some self-hosted backends not liking these tests. They'll have to be skipped; see any other return error.SkipZigTest in behavior. You'll have to figure out which backends need to skip the tests; it looks like riscv does, and I wouldn't be particularly surprised if wasm does too.

@xdBronch
Copy link
Contributor Author

return func.fail("TODO: Implement OptionalPayloadPtrSet for optional with zero-sized type {}", .{payload_ty.fmtDebug()});

looks like wasm is only a TODO on zero sized types so we're good there

@xdBronch
Copy link
Contributor Author

guessing #21393 is gonna conflict, fine if i wait for that before pushing again?

@Rexicon226
Copy link
Contributor

I'll fix the RISC-V backend TODO in an hour.

@Rexicon226
Copy link
Contributor

@xdBronch you can cherry-pick ac69f40

@andrewrk
Copy link
Member

@mlugg please feel free to merge this when you feel it is ready

@mlugg mlugg merged commit 4d81e8e into ziglang:master Sep 14, 2024
10 checks passed
@xdBronch xdBronch deleted the push-tvovpsxztrqn branch September 14, 2024 05:40
DivergentClouds pushed a commit to DivergentClouds/zig that referenced this pull request Sep 24, 2024
make decl literals work with single item pointers
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.

decl literals work through optionals but not pointers
5 participants