mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 07:14:46 +00:00
feat: add inner var to IntLiteral and FloatLiteral
This commit is contained in:
parent
d62471c73a
commit
127c4e1bcc
12 changed files with 92 additions and 74 deletions
|
@ -14,7 +14,7 @@ use roc_types::types::Type::{self, *};
|
|||
pub fn int_literal(num_var: Variable, expected: Expected<Type>, region: Region) -> Constraint {
|
||||
let num_type = Variable(num_var);
|
||||
let reason = Reason::IntLiteral;
|
||||
let expected_literal = ForReason(reason, num_int(), region);
|
||||
let expected_literal = ForReason(reason, num_int(Type::Variable(num_var)), region);
|
||||
|
||||
exists(
|
||||
vec![num_var],
|
||||
|
@ -29,7 +29,7 @@ pub fn int_literal(num_var: Variable, expected: Expected<Type>, region: Region)
|
|||
pub fn float_literal(num_var: Variable, expected: Expected<Type>, region: Region) -> Constraint {
|
||||
let num_type = Variable(num_var);
|
||||
let reason = Reason::FloatLiteral;
|
||||
let expected_literal = ForReason(reason, num_float(), region);
|
||||
let expected_literal = ForReason(reason, num_float(Type::Variable(num_var)), region);
|
||||
|
||||
exists(
|
||||
vec![num_var],
|
||||
|
@ -72,11 +72,11 @@ pub fn str_type() -> Type {
|
|||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn num_float() -> Type {
|
||||
pub fn num_float(range: Type) -> Type {
|
||||
Type::Alias(
|
||||
Symbol::NUM_F64,
|
||||
vec![],
|
||||
Box::new(num_num(num_floatingpoint(num_binary64()))),
|
||||
Symbol::NUM_FLOAT,
|
||||
vec![("range".into(), range.clone())],
|
||||
Box::new(num_num(num_floatingpoint(range))),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -108,11 +108,11 @@ pub fn num_binary64() -> Type {
|
|||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn num_int() -> Type {
|
||||
pub fn num_int(range: Type) -> Type {
|
||||
Type::Alias(
|
||||
Symbol::NUM_I64,
|
||||
vec![],
|
||||
Box::new(num_num(num_integer(num_signed64()))),
|
||||
Symbol::NUM_INT,
|
||||
vec![("range".into(), range.clone())],
|
||||
Box::new(num_num(num_integer(range))),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -57,8 +57,8 @@ fn headers_from_annotation_help(
|
|||
| MalformedPattern(_, _)
|
||||
| UnsupportedPattern(_)
|
||||
| NumLiteral(_, _)
|
||||
| IntLiteral(_)
|
||||
| FloatLiteral(_)
|
||||
| IntLiteral(_, _)
|
||||
| FloatLiteral(_, _)
|
||||
| StrLiteral(_) => true,
|
||||
|
||||
RecordDestructure { destructs, .. } => match annotation.value.shallow_dealias() {
|
||||
|
@ -154,20 +154,20 @@ pub fn constrain_pattern(
|
|||
));
|
||||
}
|
||||
|
||||
IntLiteral(_) => {
|
||||
IntLiteral(var, _) => {
|
||||
state.constraints.push(Constraint::Pattern(
|
||||
region,
|
||||
PatternCategory::Int,
|
||||
builtins::num_int(),
|
||||
builtins::num_int(Type::Variable(*var)),
|
||||
expected,
|
||||
));
|
||||
}
|
||||
|
||||
FloatLiteral(_) => {
|
||||
FloatLiteral(var, _) => {
|
||||
state.constraints.push(Constraint::Pattern(
|
||||
region,
|
||||
PatternCategory::Float,
|
||||
builtins::num_float(),
|
||||
builtins::num_float(Type::Variable(*var)),
|
||||
expected,
|
||||
));
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::builtins::{num_binary64, num_floatingpoint, num_integer, num_num, num_signed64};
|
||||
use crate::builtins::{num_floatingpoint, num_integer, num_num};
|
||||
use crate::expr::{exists, Info};
|
||||
use roc_can::annotation::IntroducedVariables;
|
||||
use roc_can::constraint::Constraint::{self, *};
|
||||
|
@ -173,18 +173,18 @@ fn constrain_pattern(
|
|||
));
|
||||
}
|
||||
|
||||
IntLiteral(_) => {
|
||||
let (a, b, c, num_type) = unique_int(var_store);
|
||||
IntLiteral(inner_var, _) => {
|
||||
let (a, b, c, num_type) = unique_int(*inner_var, var_store);
|
||||
state.constraints.push(exists(
|
||||
vec![a, b, c],
|
||||
vec![a, b, c, *inner_var],
|
||||
Constraint::Pattern(pattern.region, PatternCategory::Int, num_type, expected),
|
||||
));
|
||||
}
|
||||
FloatLiteral(_) => {
|
||||
let (a, b, c, num_type) = unique_float(var_store);
|
||||
FloatLiteral(inner_var, _) => {
|
||||
let (a, b, c, num_type) = unique_float(*inner_var, var_store);
|
||||
|
||||
state.constraints.push(exists(
|
||||
vec![a, b, c],
|
||||
vec![a, b, c, *inner_var],
|
||||
Constraint::Pattern(pattern.region, PatternCategory::Float, num_type, expected),
|
||||
));
|
||||
}
|
||||
|
@ -419,14 +419,17 @@ fn unique_unbound_num(inner_var: Variable, var_store: &mut VarStore) -> (Variabl
|
|||
(inner_uvar, num_uvar, num_type)
|
||||
}
|
||||
|
||||
fn unique_int(var_store: &mut VarStore) -> (Variable, Variable, Variable, Type) {
|
||||
fn unique_int(
|
||||
inner_var: Variable,
|
||||
var_store: &mut VarStore,
|
||||
) -> (Variable, Variable, Variable, Type) {
|
||||
let num_uvar1 = var_store.fresh();
|
||||
let num_uvar2 = var_store.fresh();
|
||||
let num_uvar3 = var_store.fresh();
|
||||
|
||||
let signed_64 = num_signed64();
|
||||
let attr_signed_64 = attr_type(Bool::variable(num_uvar1), signed_64);
|
||||
let integer = num_integer(attr_signed_64);
|
||||
let inner_type = Type::Variable(inner_var);
|
||||
let attr_inner_type = attr_type(Bool::variable(num_uvar1), inner_type);
|
||||
let integer = num_integer(attr_inner_type);
|
||||
let attr_int = attr_type(Bool::variable(num_uvar2), integer);
|
||||
let num = num_num(attr_int);
|
||||
let attr_num = attr_type(Bool::variable(num_uvar3), num);
|
||||
|
@ -434,14 +437,17 @@ fn unique_int(var_store: &mut VarStore) -> (Variable, Variable, Variable, Type)
|
|||
(num_uvar1, num_uvar2, num_uvar3, attr_num)
|
||||
}
|
||||
|
||||
fn unique_float(var_store: &mut VarStore) -> (Variable, Variable, Variable, Type) {
|
||||
fn unique_float(
|
||||
inner_var: Variable,
|
||||
var_store: &mut VarStore,
|
||||
) -> (Variable, Variable, Variable, Type) {
|
||||
let num_uvar1 = var_store.fresh();
|
||||
let num_uvar2 = var_store.fresh();
|
||||
let num_uvar3 = var_store.fresh();
|
||||
|
||||
let binary_64 = num_binary64();
|
||||
let attr_binary_64 = attr_type(Bool::variable(num_uvar1), binary_64);
|
||||
let fp = num_floatingpoint(attr_binary_64);
|
||||
let inner_type = Type::Variable(inner_var);
|
||||
let attr_inner_type = attr_type(Bool::variable(num_uvar1), inner_type);
|
||||
let fp = num_floatingpoint(attr_inner_type);
|
||||
let attr_fp = attr_type(Bool::variable(num_uvar2), fp);
|
||||
let num = num_num(attr_fp);
|
||||
let attr_num = attr_type(Bool::variable(num_uvar3), num);
|
||||
|
@ -479,7 +485,7 @@ pub fn constrain_expr(
|
|||
)
|
||||
}
|
||||
Int(var, _) => {
|
||||
let (a, b, c, num_type) = unique_int(var_store);
|
||||
let (a, b, c, num_type) = unique_int(*var, var_store);
|
||||
|
||||
exists(
|
||||
vec![*var, a, b, c],
|
||||
|
@ -495,7 +501,7 @@ pub fn constrain_expr(
|
|||
)
|
||||
}
|
||||
Float(var, _) => {
|
||||
let (a, b, c, num_type) = unique_float(var_store);
|
||||
let (a, b, c, num_type) = unique_float(*var, var_store);
|
||||
|
||||
exists(
|
||||
vec![*var, a, b, c],
|
||||
|
@ -532,7 +538,9 @@ pub fn constrain_expr(
|
|||
),
|
||||
)
|
||||
}
|
||||
Record { record_var, fields } => {
|
||||
Record {
|
||||
record_var, fields, ..
|
||||
} => {
|
||||
// NOTE: canonicalization guarantees at least one field
|
||||
// zero fields generates an EmptyRecord
|
||||
let mut field_types = SendMap::default();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue