get ingesting working both for Str and List U8

This commit is contained in:
Brendan Hansknecht 2023-04-03 18:41:36 -07:00
parent 7079361841
commit 07eb3614ea
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
2 changed files with 78 additions and 21 deletions

View file

@ -30,8 +30,8 @@ use roc_region::all::{Loc, Region};
use roc_types::subs::{IllegalCycleMark, Variable};
use roc_types::types::Type::{self, *};
use roc_types::types::{
AliasKind, AnnotationSource, Category, IndexOrField, OptAbleType, PReason, Reason, RecordField,
TypeExtension, TypeTag, Types,
AliasCommon, AliasKind, AnnotationSource, Category, IndexOrField, OptAbleType, PReason, Reason,
RecordField, TypeExtension, TypeTag, Types,
};
/// This is for constraining Defs
@ -375,12 +375,37 @@ pub fn constrain_expr(
let expected_index = expected;
constraints.equal_types(str_index, expected_index, Category::Str, region)
}
IngestedFile(_, _) => {
// This is probably where real type checking happens?
let str_index = constraints.push_type(types, Types::STR);
let expected_index = expected;
constraints.equal_types(str_index, expected_index, Category::Str, region)
}
IngestedFile(_, anno) => match &anno.typ {
Type::Apply(Symbol::STR_STR, _, _) => {
let str_index = constraints.push_type(types, Types::STR);
let expected_index = expected;
constraints.equal_types(str_index, expected_index, Category::Str, region)
// TODO: I believe we also should check to make sure they bytes are valid utf8 and bubble up an error.
// I am just not sure how the error would get bubbled up.
}
Type::Apply(Symbol::LIST_LIST, elem_type, _)
if matches!(
elem_type[0].value,
Type::DelayedAlias(AliasCommon {
symbol: Symbol::NUM_U8,
..
})
) =>
{
let elem_var = Variable::U8;
let list_elem_type = Type::Variable(elem_var);
let elem_type_index = {
let typ = types.from_old_type(&list_type(list_elem_type));
constraints.push_type(types, typ)
};
constraints.equal_types(elem_type_index, expected, Category::List, region)
}
x => todo!(
"Unsupported requested type for ingested file, give proper error: {:?}",
x
),
},
SingleQuote(num_var, precision_var, _, bound) => single_quote_literal(
types,
constraints,

View file

@ -31,9 +31,13 @@ use roc_problem::can::{RuntimeError, ShadowKind};
use roc_region::all::{Loc, Region};
use roc_std::RocDec;
use roc_target::TargetInfo;
use roc_types::subs::{
instantiate_rigids, storage_copy_var_to, Content, ExhaustiveMark, FlatType, RedundantMark,
StorageSubs, Subs, Variable, VariableSubsSlice,
use roc_types::types::AliasCommon;
use roc_types::{
subs::{
instantiate_rigids, storage_copy_var_to, Content, ExhaustiveMark, FlatType, RedundantMark,
StorageSubs, Subs, Variable, VariableSubsSlice,
},
types::Type,
};
use std::collections::HashMap;
use ven_pretty::{BoxAllocator, DocAllocator, DocBuilder};
@ -4158,16 +4162,44 @@ pub fn with_hole<'a>(
hole,
),
// TODO: Actually generate for multiple types here.
// Should this be able to fail with utf8 or should that have been checked already?
IngestedFile(bytes, _) => Stmt::Let(
assigned,
Expr::Literal(Literal::Str(
arena.alloc(std::str::from_utf8(&bytes).unwrap().to_owned()),
)),
Layout::STR,
hole,
),
IngestedFile(bytes, anno) => match &anno.typ {
Type::Apply(Symbol::STR_STR, _, _) => Stmt::Let(
assigned,
Expr::Literal(Literal::Str(
// This is safe because we ensure the utf8 bytes are valid earlier in the compiler pipeline.
arena.alloc(unsafe { std::str::from_utf8_unchecked(&bytes) }.to_owned()),
)),
Layout::STR,
hole,
),
Type::Apply(Symbol::LIST_LIST, elem_type, _)
if matches!(
elem_type[0].value,
Type::DelayedAlias(AliasCommon {
symbol: Symbol::NUM_U8,
..
})
) =>
{
let elem_layout = Layout::U8;
let mut elements = Vec::with_capacity_in(bytes.len(), env.arena);
for byte in bytes {
elements.push(ListLiteralElement::Literal(Literal::Byte(byte)));
}
let expr = Expr::Array {
elem_layout,
elems: elements.into_bump_slice(),
};
let list_layout = layout_cache.put_in(Layout::Builtin(Builtin::List(elem_layout)));
Stmt::Let(assigned, expr, list_layout, hole)
}
x => todo!(
"Unsupported requested type for ingested file, give proper error: {:?}",
x
),
},
SingleQuote(_, _, character, _) => {
let layout = layout_cache