-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Adding support for i32, i8 and i64 to-char conversion
#6425
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
base: trunk
Are you sure you want to change the base?
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
I don't see Could you clarify where in the design or a proposal or leads issue this is from? |
Oh I see it:
Could you add a link to the PR description? |
danakj
left a comment
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.
Please add tests including both tests that now pass and tests that fail for bad values or types, and tests that pass/fail for making a "convert_char" builtin signature that is correct or malformed.
| fn Convert[self: Self]() -> Char = "char.convert_checked"; | ||
| } | ||
|
|
||
| impl Int(32) as As(Char) { |
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.
Can we make this generic for all integer sizes, like it is for u8?
| final impl forall [From:! IntLiteral(), To:! IntLiteral()] UInt(From) as As(UInt(To)) { |
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.
Good idea!
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.
Hi @danakj
I had issues with this kind of implementation, but I managed to get support working for i32, i8, and i64! However, I’ll work on adapting it to what you suggested in the future.
| for (const auto& constraint : info.extend_named_constraints) { | ||
| ResolveSpecificDeclForSpecificId(eval_context, | ||
| constraint.specific_id); | ||
| } | ||
| for (const auto& constraint : info.self_impls_named_constraints) { | ||
| ResolveSpecificDeclForSpecificId(eval_context, | ||
| constraint.specific_id); | ||
| } |
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.
This looks like a bad rebase?
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.
This might be a branch update problem!
I'll fix that as well!
This code should be included.
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 added this snippet again!
toolchain/check/eval.cpp
Outdated
| //// Performs a conversion from integer to character type. | ||
| // Performs a conversion between character types, diagnosing if the value | ||
| // doesn't fit in the destination 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.
This looks like a copy paste that needs to be cleaned up?
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.
Yes!! I will remove this
toolchain/check/eval.cpp
Outdated
| // Performs a conversion between character types, diagnosing if the value | ||
| // doesn't fit in the destination type. | ||
| static auto PerformCharConvert(Context& context, SemIR::LocId loc_id, | ||
| SemIR::InstId arg_id, |
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 suggest setting up pre-commit locally so things like formatting are fixed before uploading. https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/contribution_tools.md
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.
Thank you! I used pre-commit in my new submition :)
toolchain/check/eval.cpp
Outdated
| // Values over 0x80 require multiple code units in UTF-8. | ||
| if (value >= 0x80) { |
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.
This surprised me, could you point me to where the design says this should happen, rather than converting anything up to 255?
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.
Actually, it’s a restriction I initially included, but it may indeed not make sense to keep it.
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.
Yes! I removed this code snippet! I don’t think it makes sense to add this check.
| constexpr BuiltinInfo ReadChar = {"read.char", | ||
| ValidateSignature<auto()->AnySizedInt>}; | ||
|
|
||
|
|
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.
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.
Thank you!
core/prelude/types/char.carbon
Outdated
| fn Convert[self: Self]() -> Char = "int.convert_char"; | ||
| } |
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.
[diff] reported by reviewdog 🐶
| fn Convert[self: Self]() -> Char = "int.convert_char"; | |
| } | |
| fn Convert[self: Self]() -> Char = "int.convert_char"; | |
| } | |
toolchain/check/eval.cpp
Outdated
| SemIR::InstId arg_id, | ||
| SemIR::TypeId dest_type_id) | ||
| -> SemIR::ConstantId { | ||
|
|
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.
[diff] reported by reviewdog 🐶
toolchain/check/eval.cpp
Outdated
| SemIR::InstId arg_id, | ||
| SemIR::TypeId dest_type_id) |
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.
[diff] reported by reviewdog 🐶
| SemIR::InstId arg_id, | |
| SemIR::TypeId dest_type_id) | |
| SemIR::InstId arg_id, SemIR::TypeId dest_type_id) |
toolchain/check/eval.cpp
Outdated
| SemIR::InstId arg_id, | ||
| SemIR::TypeId dest_type_id) | ||
| -> SemIR::ConstantId { | ||
|
|
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.
[diff] reported by reviewdog 🐶
toolchain/check/eval.cpp
Outdated
|
|
||
| auto [is_signed, bit_width_id] = | ||
| context.sem_ir().types().GetIntTypeInfo(dest_type_id); | ||
|
|
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.
[diff] reported by reviewdog 🐶
toolchain/check/eval.cpp
Outdated
| {.type = arg.type_id, .value = arg_val}, | ||
| dest_type_id); | ||
| } | ||
|
|
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.
[diff] reported by reviewdog 🐶
| CARBON_DIAGNOSTIC( | ||
| NegativeIntInUnsignedType, Error, | ||
| "character value {0} too large for type {1}", TypedInt, | ||
| SemIR::TypeId); |
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.
[diff] reported by reviewdog 🐶
| CARBON_DIAGNOSTIC( | |
| NegativeIntInUnsignedType, Error, | |
| "character value {0} too large for type {1}", TypedInt, | |
| SemIR::TypeId); | |
| CARBON_DIAGNOSTIC(NegativeIntInUnsignedType, Error, | |
| "character value {0} too large for type {1}", TypedInt, | |
| SemIR::TypeId); |
toolchain/check/eval.cpp
Outdated
|
|
||
| // Integer conversions. | ||
| case SemIR::BuiltinFunctionKind::IntConvertChar: { | ||
| if (phase != Phase::Concrete) { |
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.
[diff] reported by reviewdog 🐶
| if (phase != Phase::Concrete) { | |
| if (phase != Phase::Concrete) { |
| constexpr BuiltinInfo ReadChar = {"read.char", | ||
| ValidateSignature<auto()->AnySizedInt>}; | ||
|
|
||
|
|
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.
[diff] reported by reviewdog 🐶
| constexpr BuiltinInfo IntConvertChar = {"int.convert_char", | ||
| ValidateSignature<auto(AnyInt)->CharCompatible>}; |
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.
[diff] reported by reviewdog 🐶
| constexpr BuiltinInfo IntConvertChar = {"int.convert_char", | |
| ValidateSignature<auto(AnyInt)->CharCompatible>}; | |
| constexpr BuiltinInfo IntConvertChar = { | |
| "int.convert_char", ValidateSignature<auto(AnyInt)->CharCompatible>}; |
Co-authored-by: Dana Jansens <danakj@orodu.net>
Co-authored-by: Dana Jansens <danakj@orodu.net>
i32, i8 and i64 to-char conversion
i32, i8 and i64 to-char conversioni32, i8 and i64 to-char conversion
This pull request adds support for integer-to-char conversion, allowing the compiler to correctly handle character casting, implementing part of the issue #5922.