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

Numeric type literal syntax #2015

Merged

Conversation

paulfryzel
Copy link
Contributor

@paulfryzel paulfryzel commented Aug 12, 2022

This proposal aims to add a literal syntax for fixed-sized numeric types: integers, unsigned integers, and floating-point numbers.

Related issue: #1998

@paulfryzel paulfryzel requested a review from a team August 12, 2022 19:21
@paulfryzel paulfryzel force-pushed the proposal-numeric-type-literal branch from 68f713a to ce1d828 Compare August 12, 2022 19:22
@paulfryzel paulfryzel added proposal A proposal proposal draft Proposal in draft, not ready for review labels Aug 12, 2022
@paulfryzel paulfryzel marked this pull request as ready for review August 16, 2022 05:03
@github-actions github-actions bot added proposal rfc Proposal with request-for-comment sent out and removed proposal draft Proposal in draft, not ready for review labels Aug 16, 2022
@josh11b josh11b mentioned this pull request Aug 16, 2022
proposals/p2015.md Outdated Show resolved Hide resolved
Comment on lines 154 to 156
#### 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
Copy link
Contributor

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:

I think this is a case where we should carefully consider the alternatives and have a clear rationale for the choice we make.

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 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.

Copy link
Contributor

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.

proposals/p2015.md Outdated Show resolved Hide resolved
proposals/p2015.md Outdated Show resolved Hide resolved
@paulfryzel paulfryzel force-pushed the proposal-numeric-type-literal branch from 9163006 to 455f800 Compare August 17, 2022 15:46
Copy link
Contributor

@josh11b josh11b left a 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.

proposals/p2015.md Outdated Show resolved Hide resolved
proposals/p2015.md Show resolved Hide resolved
proposals/p2015.md Outdated Show resolved Hide resolved
proposals/p2015.md Show resolved Hide resolved
proposals/p2015.md Outdated Show resolved Hide resolved
josh11b added a commit to josh11b/carbon-lang that referenced this pull request Aug 25, 2022
proposals/p2015.md Show resolved Hide resolved
proposals/p2015.md Outdated Show resolved Hide resolved
Co-authored-by: josh11b <[email protected]>
Co-authored-by: Richard Smith <[email protected]>
josh11b added a commit to josh11b/carbon-lang that referenced this pull request Aug 30, 2022
Copy link
Contributor

@josh11b josh11b left a 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.

Comment on lines 129 to 164
### 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
Copy link
Contributor

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.

Copy link
Contributor Author

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.

proposals/p2015.md Show resolved Hide resolved
proposals/p2015.md Outdated Show resolved Hide resolved
proposals/p2015.md Outdated Show resolved Hide resolved
@paulfryzel
Copy link
Contributor Author

@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.

Copy link
Contributor

@zygoloid zygoloid left a 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!

Copy link
Contributor

@zygoloid zygoloid left a 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.

@zygoloid zygoloid merged commit 49c9732 into carbon-language:trunk Sep 8, 2022
josh11b added a commit that referenced this pull request Sep 9, 2022
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.
avicorp added a commit to avicorp/carbon-lang that referenced this pull request Nov 13, 2022
avicorp added a commit to avicorp/carbon-lang that referenced this pull request Nov 21, 2022
josh11b added a commit that referenced this pull request Dec 9, 2022
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal rfc Proposal with request-for-comment sent out proposal A proposal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants