Skip to content

Conversation

@mablr
Copy link
Contributor

@mablr mablr commented Dec 13, 2025

Closes #611

  • Introduced integer explicit conversions
  • Added tests

Follows Solidity 0.8.0+ rules regarding type conversions (see https://docs.soliditylang.org/en/latest/080-breaking-changes.html#new-restrictions)

- Added tests
- Follows Solidity 0.8.0+ rules regarding type conversions.
@mablr mablr marked this pull request as ready for review December 13, 2025 17:27
@mablr mablr marked this pull request as draft December 13, 2025 17:49
@mablr mablr marked this pull request as ready for review December 13, 2025 20:38
function validPositiveLiteralVariousSizes() public pure {
uint128 a = uint128(0);
uint32 b = uint32(0xFFFFFFFF);
int64 c = int64(9223372036854775807); // max int64
Copy link
Member

Choose a reason for hiding this comment

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

can you also check overflow fails? same line but with 9223372036854775808

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right, mb. The check on int literal is not sufficient btw. I'll fix it to comply w/:

  1. Explicit conversions between literals and an integer type T are only allowed if the literal lies between type(T).min and type(T).max. [...]

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 think it's actually blocked for the moment (maybe solved by #566)

In fact we need to check the value and the sign of the literal to make this assertion : type(T).min <= literal_value < type(T).max.

So, mapping from/to Ty objects is not enough, as we need the value from LitKind::Number.

Copy link
Member

Choose a reason for hiding this comment

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

would it be possible with TypeSize storing bits? that way min..=max for intX is X-1 bits and uintX is X bits

Copy link
Contributor Author

@mablr mablr Dec 14, 2025

Choose a reason for hiding this comment

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

That's what i tried initially, but it seems to be to restrictive given the granularity of TypeSize (byte).

IntLiteral(positive) -> Uint: OK

(IntLiteral(false, size_from), Elementary(UInt(size_to))) => { if size_from.bits() <= size_to.bits() { Ok(()) } else { Result::Err(()) }},

IntLiteral(negative) -> Int: I guess OK (but can't test it without #566)

(IntLiteral(true, size_from), Elementary(Int(size_to))) => { if size_from.bits() <= size_to.bits() { Ok(()) } else { Result::Err(()) } },

IntLiteral(positive) -> Int: too restrictive

(IntLiteral(false, size_from), Elementary(Int(size_to))) => { if size_from.bits() < size_to.bits() { Ok(()) } else { Result::Err(()) } },

i.e int8 d = int8(0xF0); is rejected because size_from.bits() == 8 and size_to.bits() == 8 which doesn't reflect the value bounds (−2^(n−1) to 2^(n−1) − 1)

Copy link
Member

@DaniPopes DaniPopes Dec 15, 2025

Choose a reason for hiding this comment

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

right, what if typesize had bit granularity for literals? so that intX::MIN is typesize(X-1 : u16) instead of typesize(X/8 : u8)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what if typesize had bit granularity for literals?

That would be great!

Correct me if I’m wrong, i think to produce Ty IntLiteral objects, it will be necessary anyway to hold the sign in the ast/hir number (the value as u256 and boolean for the sign).

Copy link
Contributor

@dipanshuhappy dipanshuhappy Dec 17, 2025

Choose a reason for hiding this comment

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

what if typesize had bit granularity for literals?

That would be great!

Correct me if I’m wrong, i think to produce Ty IntLiteral objects, it will be necessary anyway to hold the sign in the ast/hir number (the value as u256 and boolean for the sign).

Yep it seems so, looking at the expression enum here
Lit only stores the numerical value, while sign is held by Unary

All these are translated in the typechecker to Ty::IntLiteral here

@onbjerg onbjerg added C-enhancement Category: an issue proposing an enhancement or a PR with one A-sema Area: semantic analysis labels Dec 15, 2025
@mablr mablr marked this pull request as draft December 15, 2025 23:16
@onbjerg
Copy link
Contributor

onbjerg commented Dec 17, 2025

I'm going to mark this blocked by #650 (see above discussion).

@onbjerg onbjerg added the S-blocked Status: this cannot more forward until something else changes label Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-sema Area: semantic analysis C-enhancement Category: an issue proposing an enhancement or a PR with one S-blocked Status: this cannot more forward until something else changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integer explicit conversions

4 participants