mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
freshen annotations
This commit is contained in:
parent
d3ccdb51ea
commit
43adf0635e
10 changed files with 277 additions and 58 deletions
|
@ -88,6 +88,23 @@ pub struct Annotation {
|
|||
pub region: Region,
|
||||
}
|
||||
|
||||
impl Annotation {
|
||||
fn freshen(mut self, var_store: &mut VarStore) -> Self {
|
||||
let mut substitutions = MutMap::default();
|
||||
|
||||
for v in self.introduced_variables.lambda_sets.iter_mut() {
|
||||
let new = var_store.fresh();
|
||||
substitutions.insert(*v, new);
|
||||
|
||||
*v = new;
|
||||
}
|
||||
|
||||
self.signature.substitute_variables(&substitutions);
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct CanDefs {
|
||||
defs: Vec<Option<Def>>,
|
||||
|
@ -1638,6 +1655,12 @@ pub(crate) fn sort_can_defs_new(
|
|||
}
|
||||
};
|
||||
|
||||
let host_annotation = if exposed_symbols.contains(&symbol) {
|
||||
def.annotation.clone().map(|a| a.freshen(var_store))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if is_initial && !exposed_symbols.contains(&symbol) {
|
||||
env.problem(Problem::DefsOnlyUsedInRecursion(1, def.region()));
|
||||
}
|
||||
|
@ -1649,6 +1672,7 @@ pub(crate) fn sort_can_defs_new(
|
|||
Loc::at(def.loc_expr.region, closure_data),
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
host_annotation,
|
||||
specializes,
|
||||
);
|
||||
}
|
||||
|
@ -1658,55 +1682,76 @@ pub(crate) fn sort_can_defs_new(
|
|||
def.loc_expr,
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
host_annotation,
|
||||
specializes,
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match def.loc_pattern.value {
|
||||
Pattern::Identifier(symbol) => match def.loc_expr.value {
|
||||
Closure(closure_data) => {
|
||||
declarations.push_function_def(
|
||||
Loc::at(def.loc_pattern.region, symbol),
|
||||
Loc::at(def.loc_expr.region, closure_data),
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
None,
|
||||
);
|
||||
Pattern::Identifier(symbol) => {
|
||||
let host_annotation = if exposed_symbols.contains(&symbol) {
|
||||
def.annotation.clone().map(|a| a.freshen(var_store))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
match def.loc_expr.value {
|
||||
Closure(closure_data) => {
|
||||
declarations.push_function_def(
|
||||
Loc::at(def.loc_pattern.region, symbol),
|
||||
Loc::at(def.loc_expr.region, closure_data),
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
host_annotation,
|
||||
None,
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
declarations.push_value_def(
|
||||
Loc::at(def.loc_pattern.region, symbol),
|
||||
def.loc_expr,
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
host_annotation,
|
||||
None,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
declarations.push_value_def(
|
||||
Loc::at(def.loc_pattern.region, symbol),
|
||||
def.loc_expr,
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
None,
|
||||
);
|
||||
}
|
||||
},
|
||||
}
|
||||
Pattern::AbilityMemberSpecialization {
|
||||
ident: symbol,
|
||||
specializes,
|
||||
} => match def.loc_expr.value {
|
||||
Closure(closure_data) => {
|
||||
declarations.push_function_def(
|
||||
Loc::at(def.loc_pattern.region, symbol),
|
||||
Loc::at(def.loc_expr.region, closure_data),
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
Some(specializes),
|
||||
);
|
||||
} => {
|
||||
let host_annotation = if exposed_symbols.contains(&symbol) {
|
||||
def.annotation.clone().map(|a| a.freshen(var_store))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
match def.loc_expr.value {
|
||||
Closure(closure_data) => {
|
||||
declarations.push_function_def(
|
||||
Loc::at(def.loc_pattern.region, symbol),
|
||||
Loc::at(def.loc_expr.region, closure_data),
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
host_annotation,
|
||||
Some(specializes),
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
declarations.push_value_def(
|
||||
Loc::at(def.loc_pattern.region, symbol),
|
||||
def.loc_expr,
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
host_annotation,
|
||||
Some(specializes),
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
declarations.push_value_def(
|
||||
Loc::at(def.loc_pattern.region, symbol),
|
||||
def.loc_expr,
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
Some(specializes),
|
||||
);
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
declarations.push_destructure_def(
|
||||
def.loc_pattern,
|
||||
|
@ -1749,6 +1794,12 @@ pub(crate) fn sort_can_defs_new(
|
|||
Some(r) => Some(Region::span_across(&r, &def.region())),
|
||||
};
|
||||
|
||||
let host_annotation = if exposed_symbols.contains(&symbol) {
|
||||
def.annotation.clone().map(|a| a.freshen(var_store))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
match def.loc_expr.value {
|
||||
Closure(closure_data) => {
|
||||
declarations.push_recursive_def(
|
||||
|
@ -1756,6 +1807,7 @@ pub(crate) fn sort_can_defs_new(
|
|||
Loc::at(def.loc_expr.region, closure_data),
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
host_annotation,
|
||||
specializes,
|
||||
);
|
||||
}
|
||||
|
@ -1765,6 +1817,7 @@ pub(crate) fn sort_can_defs_new(
|
|||
def.loc_expr,
|
||||
def.expr_var,
|
||||
def.annotation,
|
||||
host_annotation,
|
||||
specializes,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -2535,6 +2535,8 @@ pub struct Declarations {
|
|||
// used for ability member specializatons.
|
||||
pub specializes: VecMap<usize, Symbol>,
|
||||
|
||||
pub host_exposed_annotations: VecMap<usize, crate::def::Annotation>,
|
||||
|
||||
pub function_bodies: Vec<Loc<FunctionDef>>,
|
||||
pub expressions: Vec<Loc<Expr>>,
|
||||
pub destructs: Vec<DestructureDef>,
|
||||
|
@ -2556,6 +2558,7 @@ impl Declarations {
|
|||
variables: Vec::with_capacity(capacity),
|
||||
symbols: Vec::with_capacity(capacity),
|
||||
annotations: Vec::with_capacity(capacity),
|
||||
host_exposed_annotations: VecMap::new(),
|
||||
function_bodies: Vec::with_capacity(capacity),
|
||||
expressions: Vec::with_capacity(capacity),
|
||||
specializes: VecMap::default(), // number of specializations is probably low
|
||||
|
@ -2586,6 +2589,7 @@ impl Declarations {
|
|||
loc_closure_data: Loc<ClosureData>,
|
||||
expr_var: Variable,
|
||||
annotation: Option<Annotation>,
|
||||
host_annotation: Option<Annotation>,
|
||||
specializes: Option<Symbol>,
|
||||
) -> usize {
|
||||
let index = self.declarations.len();
|
||||
|
@ -2608,6 +2612,11 @@ impl Declarations {
|
|||
Recursive::TailRecursive => DeclarationTag::TailRecursive(function_def_index),
|
||||
};
|
||||
|
||||
if let Some(annotation) = host_annotation {
|
||||
self.host_exposed_annotations
|
||||
.insert(self.declarations.len(), annotation);
|
||||
}
|
||||
|
||||
self.declarations.push(tag);
|
||||
self.variables.push(expr_var);
|
||||
self.symbols.push(symbol);
|
||||
|
@ -2628,6 +2637,7 @@ impl Declarations {
|
|||
loc_closure_data: Loc<ClosureData>,
|
||||
expr_var: Variable,
|
||||
annotation: Option<Annotation>,
|
||||
host_annotation: Option<Annotation>,
|
||||
specializes: Option<Symbol>,
|
||||
) -> usize {
|
||||
let index = self.declarations.len();
|
||||
|
@ -2643,6 +2653,11 @@ impl Declarations {
|
|||
|
||||
let function_def_index = Index::push_new(&mut self.function_bodies, loc_function_def);
|
||||
|
||||
if let Some(annotation) = host_annotation {
|
||||
self.host_exposed_annotations
|
||||
.insert(self.declarations.len(), annotation);
|
||||
}
|
||||
|
||||
self.declarations
|
||||
.push(DeclarationTag::Function(function_def_index));
|
||||
self.variables.push(expr_var);
|
||||
|
@ -2700,10 +2715,16 @@ impl Declarations {
|
|||
loc_expr: Loc<Expr>,
|
||||
expr_var: Variable,
|
||||
annotation: Option<Annotation>,
|
||||
host_annotation: Option<Annotation>,
|
||||
specializes: Option<Symbol>,
|
||||
) -> usize {
|
||||
let index = self.declarations.len();
|
||||
|
||||
if let Some(annotation) = host_annotation {
|
||||
self.host_exposed_annotations
|
||||
.insert(self.declarations.len(), annotation);
|
||||
}
|
||||
|
||||
self.declarations.push(DeclarationTag::Value);
|
||||
self.variables.push(expr_var);
|
||||
self.symbols.push(symbol);
|
||||
|
@ -2758,6 +2779,7 @@ impl Declarations {
|
|||
def.expr_var,
|
||||
def.annotation,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -2768,6 +2790,7 @@ impl Declarations {
|
|||
def.expr_var,
|
||||
def.annotation,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
},
|
||||
|
@ -2778,6 +2801,7 @@ impl Declarations {
|
|||
def.expr_var,
|
||||
def.annotation,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue