Skip to content

@bitSizeOf should error when no sensible result can be returned #4211

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

Open
shawnl opened this issue Jan 17, 2020 · 3 comments
Open

@bitSizeOf should error when no sensible result can be returned #4211

shawnl opened this issue Jan 17, 2020 · 3 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@shawnl
Copy link
Contributor

shawnl commented Jan 17, 2020

#2177 (comment)

This function measures the size at runtime. For types that are disallowed at runtime, such as
 {#syntax#}comptime_int{#endsyntax#} and {#syntax#}type{#endsyntax#}, the result is {#syntax#}0{#endsyntax#}.

I can't find a detailed reason why this is needed over erroring if we can't return a sensible result. Types are always comptime. Throwing an compile error when possible is part of the zen of zig, and prevents bugs.

@LemonBoy
Copy link
Contributor

LemonBoy commented Jan 17, 2020

Its behaviour is consistent with @sizeOf and sort-of makes sense if you consider the comptime types as "immaterial" as they don't get materialized past the analysis phase.

I do agree that throwing an error is a good idea, having the invariant that the size (either in bit or bytes) of such a type is never zero reduces the number of corner cases one has to be aware of.

@shawnl
Copy link
Contributor Author

shawnl commented Jan 17, 2020

I think some types validly have zero size. Look at the discussion of zero-width integers. However certain types have no size at all.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jan 17, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Jan 17, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
@rohlem
Copy link
Contributor

rohlem commented Sep 3, 2024

I failed to find a corresponding issue for @sizeOf and @alignOf, so I'll just add here that
imo they should equally be an error (or return null?) when given a comptime-only type which categorically lacks such properties
(as they are often properties the compiler decides for code generation for the runtime target system).

@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

4 participants