Skip to content

Sema: disallow @intFromPtr to accept comptime-only types - #17540

Merged
Vexu merged 10 commits into
ziglang:masterfrom
wrongnull:disallow-comptime-only-types
Oct 17, 2023
Merged

Sema: disallow @intFromPtr to accept comptime-only types#17540
Vexu merged 10 commits into
ziglang:masterfrom
wrongnull:disallow-comptime-only-types

Conversation

@wrongnull

@wrongnull wrongnull commented Oct 15, 2023

Copy link
Copy Markdown
Contributor

It would make a lot more sense if we prevented it from accepting pointers to comptime-only types.

@wrongnull

Copy link
Copy Markdown
Contributor Author

It looks like anytype parameters are treated as comptime-only under any conditions. @Vexu , is it a bug? If so, I could open an issue and try to fix one.

@Vexu

Vexu commented Oct 16, 2023

Copy link
Copy Markdown
Member

No, this just has the same bug that you fixed for @extern in #17509. Also @SpexGuy was against this change when I previously tried to implement it in #11734 (comment).

@mlugg

mlugg commented Oct 16, 2023

Copy link
Copy Markdown
Member

Also @SpexGuy was against this change when I previously tried to implement it in #11734 (comment).

AFAICT that's about something else. @intFromPtr on pointers to comptime memory where the child type is not comptime-only could, in theory, be implemented in a semi-sane way using lazy values or something (although I still don't think that's a worthwhile idea). However, this is about @intFromPtr on an actual comptime-only type, where having a concrete address is just a nonsensical concept - it's not like you could ever perform meaningful arithmetic on that address, since the value doesn't have a size.

Currently, it seems backends (or at least LLVM) just lower these values as references to zero-bit types (so give them an arbitrary "fake address"), which is an entirely useless and unintuitive behavior.

@andrewrk andrewrk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! After a minor rewording of the error message, this is ready to land.

Comment thread src/Sema.zig Outdated
const pointee_ty = ptr_ty.childType(mod);
if (try sema.typeRequiresComptime(ptr_ty)) {
const msg = msg: {
const msg = try sema.errMsg(block, ptr_src, "cannot accept pointer to comptime-only type '{}'", .{pointee_ty.fmt(mod)});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
const msg = try sema.errMsg(block, ptr_src, "cannot accept pointer to comptime-only type '{}'", .{pointee_ty.fmt(mod)});
const msg = try sema.errMsg(block, ptr_src, "comptime-only type '{}' has no pointer address", .{pointee_ty.fmt(mod)});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@andrewrk Is it worth mentioning this in the langref?

@Vexu
Vexu enabled auto-merge (squash) October 17, 2023 17:34
@Vexu
Vexu merged commit ad168db into ziglang:master Oct 17, 2023
@wrongnull
wrongnull deleted the disallow-comptime-only-types branch October 18, 2023 06:52
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.

4 participants