Temporarily disable List.get in favor of getUnsafe

This commit is contained in:
Richard Feldman 2020-04-26 09:24:45 -04:00
parent 52a3e0afb5
commit 1bee16decd
6 changed files with 141 additions and 142 deletions

View file

@ -371,7 +371,20 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
// List module // List module
// get : List elem, Int -> Result elem [ IndexOutOfBounds ]* // get : List elem, Int -> Result elem [ OutOfBounds ]*
let index_out_of_bounds = SolvedType::TagUnion(
vec![(TagName::Global("OutOfBounds".into()), vec![])],
Box::new(SolvedType::Wildcard),
);
add_type(
Symbol::LIST_GET,
SolvedType::Func(
vec![list_type(flex(TVAR1)), int_type()],
Box::new(result_type(flex(TVAR1), index_out_of_bounds)),
),
);
add_type( add_type(
Symbol::LIST_GET_UNSAFE, Symbol::LIST_GET_UNSAFE,
SolvedType::Func( SolvedType::Func(

View file

@ -450,7 +450,20 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
unique_function(vec![list_type(UVAR1, TVAR1)], int_type(UVAR2)), unique_function(vec![list_type(UVAR1, TVAR1)], int_type(UVAR2)),
); );
// #getUnsafe : List elem, Int -> elem // get : List a, Int -> Result a [ OutOfBounds ]*
let index_out_of_bounds = SolvedType::TagUnion(
vec![(TagName::Global("OutOfBounds".into()), vec![])],
Box::new(SolvedType::Wildcard),
);
add_type(
Symbol::LIST_GET,
unique_function(
vec![list_type(UVAR1, TVAR1), int_type(UVAR2)],
result_type(UVAR3, flex(TVAR1), lift(UVAR4, index_out_of_bounds)),
),
);
add_type( add_type(
Symbol::LIST_GET_UNSAFE, Symbol::LIST_GET_UNSAFE,
unique_function(vec![list_type(UVAR1, TVAR1), int_type(UVAR2)], flex(TVAR1)), unique_function(vec![list_type(UVAR1, TVAR1), int_type(UVAR2)], flex(TVAR1)),
@ -934,6 +947,17 @@ fn num_type(u: VarId, a: VarId) -> SolvedType {
) )
} }
#[inline(always)]
fn result_type(u: VarId, a: SolvedType, e: SolvedType) -> SolvedType {
SolvedType::Apply(
Symbol::ATTR_ATTR,
vec![
flex(u),
SolvedType::Apply(Symbol::RESULT_RESULT, vec![a, e]),
],
)
}
#[inline(always)] #[inline(always)]
fn list_type(u: VarId, a: VarId) -> SolvedType { fn list_type(u: VarId, a: VarId) -> SolvedType {
SolvedType::Apply( SolvedType::Apply(

View file

@ -26,102 +26,102 @@ use roc_types::subs::{VarStore, Variable};
/// lookup (if the bounds check passed). That internal function is hardcoded in code gen, /// lookup (if the bounds check passed). That internal function is hardcoded in code gen,
/// which works fine because it doesn't involve any open tag unions. /// which works fine because it doesn't involve any open tag unions.
pub fn builtin_defs(var_store: &VarStore) -> Vec<Def> { pub fn builtin_defs(var_store: &VarStore) -> Vec<Def> {
vec![list_get(var_store), list_first(var_store)] vec![/*list_get(var_store),*/ list_first(var_store)]
} }
/// List.get : List elem, Int -> Result elem [ OutOfBounds ]* /// List.get : List elem, Int -> Result elem [ OutOfBounds ]*
fn list_get(var_store: &VarStore) -> Def { // fn list_get(var_store: &VarStore) -> Def {
use crate::expr::Expr::*; // use crate::expr::Expr::*;
use crate::pattern::Pattern::*; // use crate::pattern::Pattern::*;
let args = vec![ // let args = vec![
( // (
var_store.fresh(), // var_store.fresh(),
no_region(Identifier(Symbol::LIST_GET_ARG_LIST)), // no_region(Identifier(Symbol::LIST_GET_ARG_LIST)),
), // ),
( // (
var_store.fresh(), // var_store.fresh(),
no_region(Identifier(Symbol::LIST_GET_ARG_INDEX)), // no_region(Identifier(Symbol::LIST_GET_ARG_INDEX)),
), // ),
]; // ];
// Perform a bounds check. If it passes, delegate to List.#getUnsafe // // Perform a bounds check. If it passes, delegate to List.#getUnsafe
let body = If { // let body = If {
cond_var: var_store.fresh(), // cond_var: var_store.fresh(),
branch_var: var_store.fresh(), // branch_var: var_store.fresh(),
branches: vec![( // branches: vec![(
// if-condition // // if-condition
no_region( // no_region(
// index < List.len list // // index < List.len list
call( // call(
Symbol::NUM_LT, // Symbol::NUM_LT,
vec![ // vec![
Var(Symbol::LIST_GET_ARG_INDEX), // Var(Symbol::LIST_GET_ARG_INDEX),
call( // call(
Symbol::LIST_LEN, // Symbol::LIST_LEN,
vec![Var(Symbol::LIST_GET_ARG_LIST)], // vec![Var(Symbol::LIST_GET_ARG_LIST)],
var_store, // var_store,
), // ),
], // ],
var_store, // var_store,
), // ),
), // ),
// then-branch // // then-branch
no_region( // no_region(
// Ok // // Ok
tag( // tag(
"Ok", // "Ok",
vec![ // vec![
// List.getUnsafe list index // // List.getUnsafe list index
Call( // Call(
Box::new(( // Box::new((
var_store.fresh(), // var_store.fresh(),
no_region(Var(Symbol::LIST_GET_UNSAFE)), // no_region(Var(Symbol::LIST_GET_UNSAFE)),
var_store.fresh(), // var_store.fresh(),
)), // )),
vec![ // vec![
(var_store.fresh(), no_region(Var(Symbol::LIST_GET_ARG_LIST))), // (var_store.fresh(), no_region(Var(Symbol::LIST_GET_ARG_LIST))),
( // (
var_store.fresh(), // var_store.fresh(),
no_region(Var(Symbol::LIST_GET_ARG_INDEX)), // no_region(Var(Symbol::LIST_GET_ARG_INDEX)),
), // ),
], // ],
CalledVia::Space, // CalledVia::Space,
), // ),
], // ],
var_store, // var_store,
), // ),
), // ),
)], // )],
final_else: Box::new( // final_else: Box::new(
// else-branch // // else-branch
no_region( // no_region(
// Err // // Err
tag( // tag(
"Err", // "Err",
vec![tag("OutOfBounds", Vec::new(), var_store)], // vec![tag("OutOfBounds", Vec::new(), var_store)],
var_store, // var_store,
), // ),
), // ),
), // ),
}; // };
let expr = Closure( // let expr = Closure(
var_store.fresh(), // var_store.fresh(),
Symbol::LIST_GET, // Symbol::LIST_GET,
Recursive::NotRecursive, // Recursive::NotRecursive,
args, // args,
Box::new((no_region(body), var_store.fresh())), // Box::new((no_region(body), var_store.fresh())),
); // );
Def { // Def {
loc_pattern: no_region(Identifier(Symbol::LIST_GET)), // loc_pattern: no_region(Identifier(Symbol::LIST_GET)),
loc_expr: no_region(expr), // loc_expr: no_region(expr),
expr_var: var_store.fresh(), // expr_var: var_store.fresh(),
pattern_vars: SendMap::default(), // pattern_vars: SendMap::default(),
annotation: None, // annotation: None,
} // }
} // }
/// List.first : List elem -> Result elem [ ListWasEmpty ]* /// List.first : List elem -> Result elem [ ListWasEmpty ]*
fn list_first(var_store: &VarStore) -> Def { fn list_first(var_store: &VarStore) -> Def {

View file

@ -306,12 +306,12 @@ mod gen_builtins {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
when List.first [ 42, 7, 19, 21 ] is when List.first [ 12, 9, 6, 3 ] is
Ok val -> val Ok val -> val
Err _ -> -1 Err _ -> -1
"# "#
), ),
42, 12,
i64 i64
); );
} }
@ -334,32 +334,12 @@ mod gen_builtins {
#[test] #[test]
fn get_int_list() { fn get_int_list() {
assert_evals_to!( assert_evals_to!("List.getUnsafe [ 12, 9, 6 ] 1", 9, i64);
indoc!(
r#"
when List.get [ 42, 7, 19, 21 ] 1 is
Ok val -> val
Err _ -> -1
"#
),
7,
i64
);
} }
#[test] #[test]
fn get_set_unique_int_list() { fn get_set_unique_int_list() {
assert_evals_to!( assert_evals_to!("List.getUnsafe (List.set [ 12, 9, 7, 3 ] 1 42) 1", 42, i64);
indoc!(
r#"
when List.get (List.set [ 12, 9, 7, 3 ] 1 42) 1 is
Ok val -> val
Err _ -> -1
"#
),
42,
i64
);
} }
#[test] #[test]
@ -388,17 +368,9 @@ mod gen_builtins {
shared = [ 2.1, 4.3 ] shared = [ 2.1, 4.3 ]
# This should not mutate the original # This should not mutate the original
x = x = List.getUnsafe (List.set shared 1 7.7) 1
when List.get (List.set shared 1 7.7) 1 is
Ok val -> val
Err _ -> -1
y = { x, y: List.getUnsafe shared 1 }
when List.get shared 1 is
Ok val -> val
Err _ -> -1
{ x, y }
"# "#
), ),
(7.7, 4.3), (7.7, 4.3),
@ -414,17 +386,9 @@ mod gen_builtins {
shared = [ 2, 4 ] shared = [ 2, 4 ]
# This List.set is out of bounds, and should have no effect # This List.set is out of bounds, and should have no effect
x = x = List.getUnsafe (List.set shared 422 0) 1
when List.get (List.set shared 422 0) 1 is
Ok val -> val
Err _ -> -1
y = { x, y: List.getUnsafe shared 1 }
when List.get shared 1 is
Ok val -> val
Err _ -> -1
{ x, y }
"# "#
), ),
(4, 4), (4, 4),
@ -437,11 +401,9 @@ mod gen_builtins {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
unshared = [ 2, 4 ] shared = [ 2, 4 ]
when List.get unshared 1 is List.getUnsafe shared 1
Ok val -> val
Err _ -> -1
"# "#
), ),
4, 4,

View file

@ -642,7 +642,7 @@ define_builtins! {
10 LIST_LEN: "len" 10 LIST_LEN: "len"
11 LIST_FOLDL: "foldl" 11 LIST_FOLDL: "foldl"
12 LIST_FOLDR: "foldr" 12 LIST_FOLDR: "foldr"
13 LIST_GET_UNSAFE: "#getUnsafe" 13 LIST_GET_UNSAFE: "getUnsafe"
14 LIST_CONCAT: "concat" 14 LIST_CONCAT: "concat"
15 LIST_FIRST: "first" 15 LIST_FIRST: "first"
16 LIST_FIRST_ARG: "first#list" 16 LIST_FIRST_ARG: "first#list"

View file

@ -2097,8 +2097,8 @@ mod test_uniq_solve {
reverse reverse
"# "#
), ),
"Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr b c)))", // "Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr b c)))",
//"Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr a c)))", "Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr a c)))",
); );
} }