-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Numeric type literal syntax #2015
Numeric type literal syntax #2015
Conversation
68f713a
to
ce1d828
Compare
proposals/p2015.md
Outdated
#### Syntax example use in arbitrary fixed-size numeric types | ||
|
||
In the more general case, this syntax can express N-width types. | ||
|
||
- `iN` for `Carbon.Int(N)`, An N-width two’s complement signed integer type | ||
- `uN` for `Carbon.UInt(N)`, An N-width unsigned integer type | ||
- `fN` for `Carbon.Float(N)`, An N-width IEEE 754 floating-point number type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally feel like we should limit the short syntax to the common sizes. The only use case I am aware of for things like i3
are for bit fields, and I feel like adding them to the language before we have a design for bit fields is premature. Furthermore, I think there are big benefits to limiting the syntax:
- The compiler can reject typos like
i23
,i15
, etc. i1
,i2
, ... currently appear in C++ code in practice, examples:- https://github.com/google/googletest/blob/main/googlemock/include/gmock/gmock-matchers.h#L878
- https://chromium.googlesource.com/external/github.com/abseil/abseil-cpp/+/HEAD/absl/container/btree_test.cc#2772
- https://sourcegraph.com/search?q=context:global+lang:c%2B%2B+%5Ei1%24+type:symbol&patternType=regexp&case=yes
- Rules for other sizes are different, for example, we can't support pointers to arbitrary bits.
- Potential for confusion:
i1
andil
,i18
andi18n
I think this is a case where we should carefully consider the alternatives and have a clear rationale for the choice we make.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this as well. As you stated, limiting to common sizes removes a bunch of visual confusion in the type names and only affects relatively fringe type cases. Do you think this should be reflected here in this proposal at all? Perhaps a note around this section? Or would it be more appropriate to remove it completely?
I feel like as this is a proposal centered around the syntax, it's not unreasonable to have this material covered here for the future, as long as it's clear to the reader.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest starting with multiples of 8 for integer types, but allowing all multiples of 8 (other than 0). LLVM has really good support for this in most operations, it only struggles with things like division and a few other oddities. We should (IMO) consider adding limits when we need to rather than front-loading them here, and I think that restriction should be on the aliased thing (Carbon.Int(102400)
or however its spelled), not on the literal syntax.
I'd actually suggest following the same pattern for floating point literals as well. While we'll immediately need to restrict the set of Carbon.Float(N)
types to the values of N
we can implement, the restriction should be there in the library and not in the literal syntax IMO.
9163006
to
455f800
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried to identify the text that should be updated to reflect supporting multiples-of-8 bit widths.
Co-authored-by: josh11b <[email protected]> Co-authored-by: Richard Smith <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is in good shape! Once you address these suggestions, please ping the Carbon leads for final approval.
proposals/p2015.md
Outdated
### Syntax examples and considerations | ||
|
||
These examples are not meant to be formal mappings. They are only used to | ||
illustrate to the reader how the numeric type syntax may connect to the | ||
underlying language semantics in future numeric type additions. | ||
|
||
#### Syntax example use in common fixed-size numeric types | ||
|
||
[As proposed by @chandlerc](https://github.com/carbon-language/carbon-lang/issues/543#issuecomment-845620894), | ||
our heuristic for type qualification here will be "Any truly pervasive | ||
non-aggregate/composite/compound type that is also fundamentally | ||
size-parameterized." | ||
|
||
These fixed-width sizes can naturally cover the common register widths of 8-, | ||
16-, 32-, 64-, and 128-bits, as seen in other languages. | ||
|
||
Which expands to: | ||
|
||
- `i8`, `i16`, `i32`, `i64`, and `i128` keyword aliases for | ||
`Carbon.Int(N={8,16,32,64,128})` | ||
- `u8`, `u16`, `u32`, `u64`, and `u128` keyword aliases for | ||
`Carbon.UInt(N={8,16,32,64,128})` | ||
- `f16`, `f32`, and `f64` keyword aliases for `Carbon.Float(N={16,32,64})` | ||
|
||
Additionally, other type sizes may be available, depending on the platform (for | ||
example, [x86](https://en.wikipedia.org/wiki/X86)), such as `f80`, `f128`, or | ||
`f256`. | ||
|
||
#### Syntax example use in arbitrary fixed-size numeric types | ||
|
||
In the more general case, this syntax can express N-width types. | ||
|
||
- `iN` for `Carbon.Int(N)`, An N-width two's complement signed integer type | ||
- `uN` for `Carbon.UInt(N)`, An N-width unsigned integer type | ||
- `fN` for `Carbon.Float(N)`, An N-width IEEE 754 binary floating-point number | ||
type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what these sections are adding, particularly now that there are not differences between "common" and "arbitrary" sizes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's fair, and I agree. I'm going to remove both of these sections.
Co-authored-by: josh11b <[email protected]>
@carbon-language/carbon-leads I think we're all set here but of course, always welcome any additional feedback. Please give this a final review/approval when you have the chance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving on behalf of leads.
A few changes: - Merge content from the `primitive_types.md` design doc into the overall design `README.md` since there was so much overlap and no need for two copies. - Consistently spell integer types `Carbon.Int(N)` and `Carbon.UInt(N)`, including the `Carbon.` prefix and avoiding `Unsigned(N)`. - Consistently use a comprehensive set of floating-point types. - Incorporates #2015 into the design docs.
…teral-syntax' of https://github.com/avicorp/carbon-lang into Design-updates-for-carbon-language#2015-numeric-type-literal-syntax
Edit numeric literal design for the literal type proposal and add reference where #2015 was mentioned. Closes #2159 Co-authored-by: Richard Smith <[email protected]> Co-authored-by: josh11b <[email protected]>
This proposal aims to add a literal syntax for fixed-sized numeric types: integers, unsigned integers, and floating-point numbers.
Related issue: #1998