Fix Ast2 constraining of opaques

This commit is contained in:
Ayaz Hafiz 2022-04-25 12:07:31 -04:00
parent 55706ae5c4
commit 0d24e279f1
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
5 changed files with 30 additions and 8 deletions

View file

@ -1857,7 +1857,7 @@ fn num_floatingpoint(pool: &mut Pool, range: TypeId) -> Type2 {
let alias_content = range_type.shallow_clone(); let alias_content = range_type.shallow_clone();
Type2::Alias( Type2::Opaque(
Symbol::NUM_FLOATINGPOINT, Symbol::NUM_FLOATINGPOINT,
PoolVec::new(vec![(PoolStr::new("range", pool), range)].into_iter(), pool), PoolVec::new(vec![(PoolStr::new("range", pool), range)].into_iter(), pool),
pool.add(alias_content), pool.add(alias_content),
@ -1905,7 +1905,7 @@ fn _num_integer(pool: &mut Pool, range: TypeId) -> Type2 {
let alias_content = range_type.shallow_clone(); let alias_content = range_type.shallow_clone();
Type2::Alias( Type2::Opaque(
Symbol::NUM_INTEGER, Symbol::NUM_INTEGER,
PoolVec::new(vec![(PoolStr::new("range", pool), range)].into_iter(), pool), PoolVec::new(vec![(PoolStr::new("range", pool), range)].into_iter(), pool),
pool.add(alias_content), pool.add(alias_content),
@ -1918,7 +1918,7 @@ fn num_num(pool: &mut Pool, type_id: TypeId) -> Type2 {
let alias_content = range_type.shallow_clone(); let alias_content = range_type.shallow_clone();
Type2::Alias( Type2::Opaque(
Symbol::NUM_NUM, Symbol::NUM_NUM,
PoolVec::new( PoolVec::new(
vec![(PoolStr::new("range", pool), type_id)].into_iter(), vec![(PoolStr::new("range", pool), type_id)].into_iter(),

View file

@ -24,6 +24,7 @@ pub enum Type2 {
Variable(Variable), // 4B Variable(Variable), // 4B
Alias(Symbol, PoolVec<(PoolStr, TypeId)>, TypeId), // 24B = 8B + 8B + 4B + pad Alias(Symbol, PoolVec<(PoolStr, TypeId)>, TypeId), // 24B = 8B + 8B + 4B + pad
Opaque(Symbol, PoolVec<(PoolStr, TypeId)>, TypeId), // 24B = 8B + 8B + 4B + pad
AsAlias(Symbol, PoolVec<(PoolStr, TypeId)>, TypeId), // 24B = 8B + 8B + 4B + pad AsAlias(Symbol, PoolVec<(PoolStr, TypeId)>, TypeId), // 24B = 8B + 8B + 4B + pad
// 24B // 24B
@ -74,6 +75,9 @@ impl ShallowClone for Type2 {
Self::Alias(symbol, args, alias_type_id) => { Self::Alias(symbol, args, alias_type_id) => {
Self::Alias(*symbol, args.shallow_clone(), alias_type_id.clone()) Self::Alias(*symbol, args.shallow_clone(), alias_type_id.clone())
} }
Self::Opaque(symbol, args, alias_type_id) => {
Self::Opaque(*symbol, args.shallow_clone(), alias_type_id.clone())
}
Self::Record(fields, ext_id) => Self::Record(fields.shallow_clone(), ext_id.clone()), Self::Record(fields, ext_id) => Self::Record(fields.shallow_clone(), ext_id.clone()),
Self::Function(args, closure_type_id, ret_type_id) => Self::Function( Self::Function(args, closure_type_id, ret_type_id) => Self::Function(
args.shallow_clone(), args.shallow_clone(),
@ -101,7 +105,7 @@ impl Type2 {
Variable(v) => { Variable(v) => {
result.insert(*v); result.insert(*v);
} }
Alias(_, _, actual) | AsAlias(_, _, actual) => { Alias(_, _, actual) | AsAlias(_, _, actual) | Opaque(_, _, actual) => {
stack.push(pool.get(*actual)); stack.push(pool.get(*actual));
} }
HostExposedAlias { HostExposedAlias {

View file

@ -3,6 +3,7 @@
use bumpalo::Bump; use bumpalo::Bump;
use roc_can::expected::{Expected, PExpected}; use roc_can::expected::{Expected, PExpected};
use roc_collections::all::{BumpMap, BumpMapDefault, MutMap}; use roc_collections::all::{BumpMap, BumpMapDefault, MutMap};
use roc_error_macros::internal_error;
use roc_module::ident::TagName; use roc_module::ident::TagName;
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use roc_region::all::{Loc, Region}; use roc_region::all::{Loc, Region};
@ -868,7 +869,7 @@ fn type_to_variable<'a>(
register(subs, rank, pools, content) register(subs, rank, pools, content)
} }
Alias(symbol, args, alias_type_id) => { Alias(symbol, args, alias_type_id) | Opaque(symbol, args, alias_type_id) => {
// TODO cache in uniqueness inference gives problems! all Int's get the same uniqueness var! // TODO cache in uniqueness inference gives problems! all Int's get the same uniqueness var!
// Cache aliases without type arguments. Commonly used aliases like `Int` would otherwise get O(n) // Cache aliases without type arguments. Commonly used aliases like `Int` would otherwise get O(n)
// different variables (once for each occurrence). The recursion restriction is required // different variables (once for each occurrence). The recursion restriction is required
@ -910,8 +911,12 @@ fn type_to_variable<'a>(
let alias_var = type_to_variable(arena, mempool, subs, rank, pools, cached, alias_type); let alias_var = type_to_variable(arena, mempool, subs, rank, pools, cached, alias_type);
// TODO(opaques): take opaques into account let kind = match typ {
let content = Content::Alias(*symbol, arg_vars, alias_var, AliasKind::Structural); Alias(..) => AliasKind::Structural,
Opaque(..) => AliasKind::Opaque,
_ => internal_error!(),
};
let content = Content::Alias(*symbol, arg_vars, alias_var, kind);
let result = register(subs, rank, pools, content); let result = register(subs, rank, pools, content);

View file

@ -6168,4 +6168,17 @@ mod solve_expr {
"a -> Task a *", "a -> Task a *",
); );
} }
#[test]
fn list_with_num_and_str() {
infer_eq_without_problem(
indoc!(
r#"
val = [ 1, "abc" ]
val
"#
),
"",
)
}
} }

View file

@ -3169,7 +3169,7 @@ pub mod test_ed_update {
assert_type_tooltips_clean( assert_type_tooltips_clean(
ovec!["val = [ [ 0, 1, \"2\" ], [ 3, 4, 5 ┃] ]"], ovec!["val = [ [ 0, 1, \"2\" ], [ 3, 4, 5 ┃] ]"],
ovec!["List (Num *)", "List (List <type mismatch>)"], ovec!["List (Num *)", "List <type mismatch>"],
)?; )?;
Ok(()) Ok(())