Fix failing compilation in Expr2

This commit is contained in:
ayazhafiz 2021-11-21 21:37:34 -05:00
parent 3b7596dd34
commit fe85af8d03
5 changed files with 48 additions and 13 deletions

View file

@ -6,7 +6,7 @@ use roc_module::{
ident::{Lowercase, TagName},
symbol::Symbol,
};
use roc_region::all::Region;
use roc_region::all::{Located, Region};
use roc_types::{
subs::Variable,
types::{self, AnnotationSource, PReason, PatternCategory},
@ -32,9 +32,9 @@ use crate::{
#[derive(Debug)]
pub enum Constraint<'a> {
Eq(Type2, Expected<Type2>, Category, Region),
Eq(Type2, Expected<Type2, Variable>, Category, Region),
// Store(Type, Variable, &'static str, u32),
Lookup(Symbol, Expected<Type2>, Region),
Lookup(Symbol, Expected<Type2, Variable>, Region),
Pattern(Region, PatternCategory, Type2, PExpected<Type2>),
And(BumpVec<'a, Constraint<'a>>),
Let(&'a LetConstraint<'a>),
@ -55,7 +55,7 @@ pub fn constrain_expr<'a>(
arena: &'a Bump,
env: &mut Env,
expr: &Expr2,
expected: Expected<Type2>,
expected: Expected<Type2, Variable>,
region: Region,
) -> Constraint<'a> {
use Constraint::*;
@ -506,7 +506,7 @@ pub fn constrain_expr<'a>(
flex_vars.push(*expr_var);
match expected {
Expected::FromAnnotation(name, arity, _, tipe) => {
Expected::FromAnnotation(name, arity, ann_source, tipe) => {
let num_branches = branches.len() + 1;
for (index, branch_id) in branches.iter_node_ids().enumerate() {
@ -528,6 +528,10 @@ pub fn constrain_expr<'a>(
AnnotationSource::TypedIfBranch {
index: Index::zero_based(index),
num_branches,
annotation: Located::at(
ann_source.region(),
ann_source.annotation().value,
),
},
tipe.shallow_clone(),
),
@ -548,6 +552,10 @@ pub fn constrain_expr<'a>(
AnnotationSource::TypedIfBranch {
index: Index::zero_based(branches.len()),
num_branches,
annotation: Located::at(
ann_source.region(),
ann_source.annotation().value,
),
},
tipe.shallow_clone(),
),
@ -654,7 +662,7 @@ pub fn constrain_expr<'a>(
flex_vars.push(*expr_var);
match &expected {
Expected::FromAnnotation(name, arity, _, _typ) => {
Expected::FromAnnotation(name, arity, ann_source, _typ) => {
// NOTE deviation from elm.
//
// in elm, `_typ` is used, but because we have this `expr_var` too
@ -687,6 +695,10 @@ pub fn constrain_expr<'a>(
*arity,
AnnotationSource::TypedWhenBranch {
index: Index::zero_based(index),
annotation: Located::at(
ann_source.region(),
ann_source.annotation().value,
),
},
typ.shallow_clone(),
),
@ -1173,7 +1185,7 @@ fn exists<'a>(
fn constrain_tag<'a>(
arena: &'a Bump,
env: &mut Env,
expected: Expected<Type2>,
expected: Expected<Type2, Variable>,
region: Region,
tag_name: TagName,
arguments: &PoolVec<(Variable, ExprId)>,
@ -1263,7 +1275,10 @@ fn constrain_field_update<'a>(
(field_type, con)
}
fn constrain_empty_record<'a>(expected: Expected<Type2>, region: Region) -> Constraint<'a> {
fn constrain_empty_record<'a>(
expected: Expected<Type2, Variable>,
region: Region,
) -> Constraint<'a> {
Constraint::Eq(Type2::EmptyRec, expected, Category::Record, region)
}
@ -1274,7 +1289,7 @@ fn constrain_when_branch<'a>(
region: Region,
when_branch: &WhenBranch,
pattern_expected: PExpected<Type2>,
expr_expected: Expected<Type2>,
expr_expected: Expected<Type2, Variable>,
) -> Constraint<'a> {
let when_expr = env.pool.get(when_branch.body);

View file

@ -1,12 +1,17 @@
use roc_can::expected::Expected;
use roc_can::expected::PExpected;
use roc_types::subs::Variable;
/// Clones the outer node, but does not clone any nodeids
pub trait ShallowClone {
fn shallow_clone(&self) -> Self;
}
impl<T: ShallowClone> ShallowClone for Expected<T> {
impl<T, Annot> ShallowClone for Expected<T, Annot>
where
T: ShallowClone,
Annot: Clone,
{
fn shallow_clone(&self) -> Self {
use Expected::*;
@ -14,7 +19,7 @@ impl<T: ShallowClone> ShallowClone for Expected<T> {
NoExpectation(t) => NoExpectation(t.shallow_clone()),
ForReason(reason, t, region) => ForReason(reason.clone(), t.shallow_clone(), *region),
FromAnnotation(loc_pat, n, source, t) => {
FromAnnotation(loc_pat.clone(), *n, *source, t.shallow_clone())
FromAnnotation(loc_pat.clone(), *n, source.clone(), t.shallow_clone())
}
}
}

View file

@ -75,7 +75,7 @@ use crate::mem_pool::shallow_clone::ShallowClone;
#[derive(PartialEq, Debug, Clone)]
pub enum TypeError {
BadExpr(Region, Category, ErrorType, Expected<ErrorType>),
BadExpr(Region, Category, ErrorType, Expected<ErrorType, Variable>),
BadPattern(Region, PatternCategory, ErrorType, PExpected<ErrorType>),
CircularType(Region, Symbol, ErrorType),
BadType(roc_types::types::Problem),

View file

@ -386,7 +386,18 @@ fn can_annotation_help(
}
},
Record { fields, ext, .. } => {
Record { fields, ext } => {
let field_types = can_assigned_fields(
env,
&fields.items,
region,
scope,
var_store,
introduced_variables,
local_aliases,
references,
);
let ext_type = match ext {
Some(loc_ann) => can_annotation_help(
env,

View file

@ -191,6 +191,7 @@ fn build_effect_always(
let def_annotation = roc_can::def::Annotation {
signature,
introduced_variables,
annotation_var: var_store.fresh(),
aliases: SendMap::default(),
region: Region::zero(),
};
@ -403,6 +404,7 @@ fn build_effect_map(
let def_annotation = roc_can::def::Annotation {
signature,
introduced_variables,
annotation_var: var_store.fresh(),
aliases: SendMap::default(),
region: Region::zero(),
};
@ -570,6 +572,7 @@ fn build_effect_after(
let def_annotation = roc_can::def::Annotation {
signature,
introduced_variables,
annotation_var: var_store.fresh(),
aliases: SendMap::default(),
region: Region::zero(),
};
@ -742,6 +745,7 @@ pub fn build_host_exposed_def(
let def_annotation = roc_can::def::Annotation {
signature: annotation.typ,
introduced_variables: annotation.introduced_variables,
annotation_var: var_store.fresh(),
aliases: annotation.aliases,
region: Region::zero(),
};