Skip to content

Commit

Permalink
Numeric type inference
Browse files Browse the repository at this point in the history
This does two things.

1. Integer types no longer automatically cast to each other.
Methods such as `as_u64` and `try_as_u8` have been introduced to require
the user to explicit the cast.
Warnings about casts losing precision have been removed as they are no
longer possible.

2. Numeric literals (such as `32` without further qualifications) no longer
immediately decay to a `u64` and we instead attempt to type them as
`TypeInfo::Numeric` as far as possible to allow their usage with any
integer type.
Numeric intrinsics unify arguments with the numeric type to enforce
their constraints.
Numeric values are forced to decay to `u64` when resolving methods or
traits.

This change is a prerequisite to move our representation of
sub-byte-sized values to a different memory representation, as numeric
types need to be distinct if we are ever to treat them differently.
  • Loading branch information
IGI-111 committed Jul 20, 2023
1 parent 5d27e25 commit 8143ce9
Show file tree
Hide file tree
Showing 54 changed files with 681 additions and 425 deletions.
23 changes: 1 addition & 22 deletions sway-core/src/asm_generation/from_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,28 +200,7 @@ pub enum StateAccessType {
}

pub(crate) fn ir_type_size_in_bytes(context: &Context, ty: &Type) -> u64 {
match ty.get_content(context) {
TypeContent::Unit | TypeContent::Bool | TypeContent::Uint(_) | TypeContent::Pointer(_) => 8,
TypeContent::Slice => 16,
TypeContent::B256 => 32,
TypeContent::String(n) => size_bytes_round_up_to_word_alignment!(*n),
TypeContent::Array(el_ty, cnt) => cnt * ir_type_size_in_bytes(context, el_ty),
TypeContent::Struct(field_tys) => {
// Sum up all the field sizes.
field_tys
.iter()
.map(|field_ty| ir_type_size_in_bytes(context, field_ty))
.sum()
}
TypeContent::Union(field_tys) => {
// Find the max size for field sizes.
field_tys
.iter()
.map(|field_ty| ir_type_size_in_bytes(context, field_ty))
.max()
.unwrap_or(0)
}
}
ty.size_in_bytes(context)
}

pub(crate) fn ir_type_str_size_in_bytes(context: &Context, ty: &Type) -> u64 {
Expand Down
13 changes: 5 additions & 8 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
},
metadata::MetadataManager,
semantic_analysis::*,
UnifyCheck,
};

use super::{
Expand Down Expand Up @@ -424,14 +425,10 @@ fn const_eval_typed_expr(
let te = lookup.engines.te();

assert!({
let elem_type_info = te.get(*elem_type);
element_typs.iter().all(|tid| {
lookup
.engines
.te()
.get(*tid)
.eq(&elem_type_info, lookup.engines)
})
let unify_check = UnifyCheck::coercion(lookup.engines);
element_typs
.iter()
.all(|tid| unify_check.check(*tid, *elem_type))
});

create_array_aggregate(
Expand Down
Loading

0 comments on commit 8143ce9

Please sign in to comment.