mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 21:39:07 +00:00
Add module_path to can env instead of threading it through
This commit is contained in:
parent
e5789158e5
commit
5112e064e5
7 changed files with 76 additions and 194 deletions
|
@ -619,7 +619,6 @@ pub fn canonicalize_expr<'a>(
|
|||
scope: &mut Scope,
|
||||
region: Region,
|
||||
expr: &'a ast::Expr<'a>,
|
||||
module_path: &str,
|
||||
) -> (Loc<Expr>, Output) {
|
||||
use Expr::*;
|
||||
|
||||
|
@ -641,8 +640,7 @@ pub fn canonicalize_expr<'a>(
|
|||
if fields.is_empty() {
|
||||
(EmptyRecord, Output::default())
|
||||
} else {
|
||||
match canonicalize_fields(env, var_store, scope, region, fields.items, module_path)
|
||||
{
|
||||
match canonicalize_fields(env, var_store, scope, region, fields.items) {
|
||||
Ok((can_fields, output)) => (
|
||||
Record {
|
||||
record_var: var_store.fresh(),
|
||||
|
@ -670,17 +668,10 @@ pub fn canonicalize_expr<'a>(
|
|||
fields,
|
||||
update: loc_update,
|
||||
} => {
|
||||
let (can_update, update_out) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
loc_update.region,
|
||||
&loc_update.value,
|
||||
module_path,
|
||||
);
|
||||
let (can_update, update_out) =
|
||||
canonicalize_expr(env, var_store, scope, loc_update.region, &loc_update.value);
|
||||
if let Var(symbol, _) = &can_update.value {
|
||||
match canonicalize_fields(env, var_store, scope, region, fields.items, module_path)
|
||||
{
|
||||
match canonicalize_fields(env, var_store, scope, region, fields.items) {
|
||||
Ok((can_fields, mut output)) => {
|
||||
output.references.union_mut(&update_out.references);
|
||||
|
||||
|
@ -726,14 +717,8 @@ pub fn canonicalize_expr<'a>(
|
|||
let mut references = References::new();
|
||||
|
||||
for loc_elem in fields.iter() {
|
||||
let (can_expr, elem_out) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
loc_elem.region,
|
||||
&loc_elem.value,
|
||||
module_path,
|
||||
);
|
||||
let (can_expr, elem_out) =
|
||||
canonicalize_expr(env, var_store, scope, loc_elem.region, &loc_elem.value);
|
||||
|
||||
references.union_mut(&elem_out.references);
|
||||
|
||||
|
@ -755,7 +740,7 @@ pub fn canonicalize_expr<'a>(
|
|||
)
|
||||
}
|
||||
|
||||
ast::Expr::Str(literal) => flatten_str_literal(env, var_store, scope, literal, module_path),
|
||||
ast::Expr::Str(literal) => flatten_str_literal(env, var_store, scope, literal),
|
||||
|
||||
ast::Expr::IngestedFile(file_path, _) => match File::open(file_path) {
|
||||
Ok(mut file) => {
|
||||
|
@ -842,14 +827,8 @@ pub fn canonicalize_expr<'a>(
|
|||
let mut references = References::new();
|
||||
|
||||
for loc_elem in loc_elems.iter() {
|
||||
let (can_expr, elem_out) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
loc_elem.region,
|
||||
&loc_elem.value,
|
||||
module_path,
|
||||
);
|
||||
let (can_expr, elem_out) =
|
||||
canonicalize_expr(env, var_store, scope, loc_elem.region, &loc_elem.value);
|
||||
|
||||
references.union_mut(&elem_out.references);
|
||||
|
||||
|
@ -881,14 +860,8 @@ pub fn canonicalize_expr<'a>(
|
|||
let mut output = Output::default();
|
||||
|
||||
for loc_arg in loc_args.iter() {
|
||||
let (arg_expr, arg_out) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
loc_arg.region,
|
||||
&loc_arg.value,
|
||||
module_path,
|
||||
);
|
||||
let (arg_expr, arg_out) =
|
||||
canonicalize_expr(env, var_store, scope, loc_arg.region, &loc_arg.value);
|
||||
|
||||
args.push((var_store.fresh(), arg_expr));
|
||||
output.references.union_mut(&arg_out.references);
|
||||
|
@ -941,14 +914,8 @@ pub fn canonicalize_expr<'a>(
|
|||
let mut output = Output::default();
|
||||
|
||||
for loc_arg in loc_args.iter() {
|
||||
let (arg_expr, arg_out) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
loc_arg.region,
|
||||
&loc_arg.value,
|
||||
module_path,
|
||||
);
|
||||
let (arg_expr, arg_out) =
|
||||
canonicalize_expr(env, var_store, scope, loc_arg.region, &loc_arg.value);
|
||||
|
||||
args.push(arg_expr);
|
||||
output.references.union_mut(&arg_out.references);
|
||||
|
@ -982,7 +949,7 @@ pub fn canonicalize_expr<'a>(
|
|||
} else {
|
||||
// Canonicalize the function expression and its arguments
|
||||
let (fn_expr, fn_expr_output) =
|
||||
canonicalize_expr(env, var_store, scope, fn_region, &loc_fn.value, module_path);
|
||||
canonicalize_expr(env, var_store, scope, fn_region, &loc_fn.value);
|
||||
|
||||
output.union(fn_expr_output);
|
||||
|
||||
|
@ -1100,14 +1067,7 @@ pub fn canonicalize_expr<'a>(
|
|||
// The body expression gets a new scope for canonicalization,
|
||||
scope.inner_scope(|inner_scope| {
|
||||
let defs: Defs = (*loc_defs).clone();
|
||||
can_defs_with_return(
|
||||
env,
|
||||
var_store,
|
||||
inner_scope,
|
||||
env.arena.alloc(defs),
|
||||
loc_ret,
|
||||
module_path,
|
||||
)
|
||||
can_defs_with_return(env, var_store, inner_scope, env.arena.alloc(defs), loc_ret)
|
||||
})
|
||||
}
|
||||
ast::Expr::RecordBuilder(_) => {
|
||||
|
@ -1117,29 +1077,16 @@ pub fn canonicalize_expr<'a>(
|
|||
internal_error!("Backpassing should have been desugared by now")
|
||||
}
|
||||
ast::Expr::Closure(loc_arg_patterns, loc_body_expr) => {
|
||||
let (closure_data, output) = canonicalize_closure(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
loc_arg_patterns,
|
||||
loc_body_expr,
|
||||
None,
|
||||
module_path,
|
||||
);
|
||||
let (closure_data, output) =
|
||||
canonicalize_closure(env, var_store, scope, loc_arg_patterns, loc_body_expr, None);
|
||||
|
||||
(Closure(closure_data), output)
|
||||
}
|
||||
ast::Expr::When(loc_cond, branches) => {
|
||||
// Infer the condition expression's type.
|
||||
let cond_var = var_store.fresh();
|
||||
let (can_cond, mut output) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
loc_cond.region,
|
||||
&loc_cond.value,
|
||||
module_path,
|
||||
);
|
||||
let (can_cond, mut output) =
|
||||
canonicalize_expr(env, var_store, scope, loc_cond.region, &loc_cond.value);
|
||||
|
||||
// the condition can never be a tail-call
|
||||
output.tail_call = None;
|
||||
|
@ -1155,7 +1102,6 @@ pub fn canonicalize_expr<'a>(
|
|||
region,
|
||||
branch,
|
||||
&mut output,
|
||||
module_path,
|
||||
)
|
||||
});
|
||||
|
||||
|
@ -1185,8 +1131,7 @@ pub fn canonicalize_expr<'a>(
|
|||
(expr, output)
|
||||
}
|
||||
ast::Expr::RecordAccess(record_expr, field) => {
|
||||
let (loc_expr, output) =
|
||||
canonicalize_expr(env, var_store, scope, region, record_expr, module_path);
|
||||
let (loc_expr, output) = canonicalize_expr(env, var_store, scope, region, record_expr);
|
||||
|
||||
(
|
||||
RecordAccess {
|
||||
|
@ -1215,8 +1160,7 @@ pub fn canonicalize_expr<'a>(
|
|||
Output::default(),
|
||||
),
|
||||
ast::Expr::TupleAccess(tuple_expr, field) => {
|
||||
let (loc_expr, output) =
|
||||
canonicalize_expr(env, var_store, scope, region, tuple_expr, module_path);
|
||||
let (loc_expr, output) = canonicalize_expr(env, var_store, scope, region, tuple_expr);
|
||||
|
||||
(
|
||||
TupleAccess {
|
||||
|
@ -1286,14 +1230,8 @@ pub fn canonicalize_expr<'a>(
|
|||
ast::Expr::Expect(condition, continuation) => {
|
||||
let mut output = Output::default();
|
||||
|
||||
let (loc_condition, output1) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
condition.region,
|
||||
&condition.value,
|
||||
module_path,
|
||||
);
|
||||
let (loc_condition, output1) =
|
||||
canonicalize_expr(env, var_store, scope, condition.region, &condition.value);
|
||||
|
||||
// Get all the lookups that were referenced in the condition,
|
||||
// so we can print their values later.
|
||||
|
@ -1305,7 +1243,6 @@ pub fn canonicalize_expr<'a>(
|
|||
scope,
|
||||
continuation.region,
|
||||
&continuation.value,
|
||||
module_path,
|
||||
);
|
||||
|
||||
output.union(output1);
|
||||
|
@ -1326,14 +1263,8 @@ pub fn canonicalize_expr<'a>(
|
|||
ast::Expr::LowLevelDbg((source_location, source), message, continuation) => {
|
||||
let mut output = Output::default();
|
||||
|
||||
let (loc_message, output1) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
message.region,
|
||||
&message.value,
|
||||
module_path,
|
||||
);
|
||||
let (loc_message, output1) =
|
||||
canonicalize_expr(env, var_store, scope, message.region, &message.value);
|
||||
|
||||
let (loc_continuation, output2) = canonicalize_expr(
|
||||
env,
|
||||
|
@ -1341,7 +1272,6 @@ pub fn canonicalize_expr<'a>(
|
|||
scope,
|
||||
continuation.region,
|
||||
&continuation.value,
|
||||
module_path,
|
||||
);
|
||||
|
||||
output.union(output1);
|
||||
|
@ -1372,14 +1302,8 @@ pub fn canonicalize_expr<'a>(
|
|||
let mut output = Output::default();
|
||||
|
||||
for (condition, then_branch) in if_thens.iter() {
|
||||
let (loc_cond, cond_output) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
condition.region,
|
||||
&condition.value,
|
||||
module_path,
|
||||
);
|
||||
let (loc_cond, cond_output) =
|
||||
canonicalize_expr(env, var_store, scope, condition.region, &condition.value);
|
||||
|
||||
let (loc_then, then_output) = canonicalize_expr(
|
||||
env,
|
||||
|
@ -1387,7 +1311,6 @@ pub fn canonicalize_expr<'a>(
|
|||
scope,
|
||||
then_branch.region,
|
||||
&then_branch.value,
|
||||
module_path,
|
||||
);
|
||||
|
||||
branches.push((loc_cond, loc_then));
|
||||
|
@ -1402,7 +1325,6 @@ pub fn canonicalize_expr<'a>(
|
|||
scope,
|
||||
final_else_branch.region,
|
||||
&final_else_branch.value,
|
||||
module_path,
|
||||
);
|
||||
|
||||
output.references.union_mut(&else_output.references);
|
||||
|
@ -1502,8 +1424,7 @@ pub fn canonicalize_expr<'a>(
|
|||
(answer, Output::default())
|
||||
}
|
||||
&ast::Expr::ParensAround(sub_expr) => {
|
||||
let (loc_expr, output) =
|
||||
canonicalize_expr(env, var_store, scope, region, sub_expr, module_path);
|
||||
let (loc_expr, output) = canonicalize_expr(env, var_store, scope, region, sub_expr);
|
||||
|
||||
(loc_expr.value, output)
|
||||
}
|
||||
|
@ -1558,7 +1479,6 @@ pub fn canonicalize_closure<'a>(
|
|||
loc_arg_patterns: &'a [Loc<ast::Pattern<'a>>],
|
||||
loc_body_expr: &'a Loc<ast::Expr<'a>>,
|
||||
opt_def_name: Option<Symbol>,
|
||||
module_path: &str,
|
||||
) -> (ClosureData, Output) {
|
||||
scope.inner_scope(|inner_scope| {
|
||||
canonicalize_closure_body(
|
||||
|
@ -1568,7 +1488,6 @@ pub fn canonicalize_closure<'a>(
|
|||
loc_arg_patterns,
|
||||
loc_body_expr,
|
||||
opt_def_name,
|
||||
module_path,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
@ -1580,7 +1499,6 @@ fn canonicalize_closure_body<'a>(
|
|||
loc_arg_patterns: &'a [Loc<ast::Pattern<'a>>],
|
||||
loc_body_expr: &'a Loc<ast::Expr<'a>>,
|
||||
opt_def_name: Option<Symbol>,
|
||||
module_path: &str,
|
||||
) -> (ClosureData, Output) {
|
||||
// The globally unique symbol that will refer to this closure once it gets converted
|
||||
// into a top-level procedure for code gen.
|
||||
|
@ -1602,7 +1520,6 @@ fn canonicalize_closure_body<'a>(
|
|||
&loc_pattern.value,
|
||||
loc_pattern.region,
|
||||
PermitShadows(false),
|
||||
module_path,
|
||||
);
|
||||
|
||||
can_args.push((
|
||||
|
@ -1621,7 +1538,6 @@ fn canonicalize_closure_body<'a>(
|
|||
scope,
|
||||
loc_body_expr.region,
|
||||
&loc_body_expr.value,
|
||||
module_path,
|
||||
);
|
||||
|
||||
let mut captured_symbols: Vec<_> = new_output
|
||||
|
@ -1759,7 +1675,6 @@ fn canonicalize_when_branch<'a>(
|
|||
_region: Region,
|
||||
branch: &'a ast::WhenBranch<'a>,
|
||||
output: &mut Output,
|
||||
module_path: &str,
|
||||
) -> (WhenBranch, References) {
|
||||
let mut patterns = Vec::with_capacity(branch.patterns.len());
|
||||
let mut multi_pattern_variables = MultiPatternVariables::new(branch.patterns.len());
|
||||
|
@ -1776,7 +1691,6 @@ fn canonicalize_when_branch<'a>(
|
|||
&loc_pattern.value,
|
||||
loc_pattern.region,
|
||||
permit_shadows,
|
||||
module_path,
|
||||
);
|
||||
|
||||
multi_pattern_variables.add_pattern(&can_pattern);
|
||||
|
@ -1802,20 +1716,13 @@ fn canonicalize_when_branch<'a>(
|
|||
scope,
|
||||
branch.value.region,
|
||||
&branch.value.value,
|
||||
module_path,
|
||||
);
|
||||
|
||||
let guard = match &branch.guard {
|
||||
None => None,
|
||||
Some(loc_expr) => {
|
||||
let (can_guard, guard_branch_output) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
loc_expr.region,
|
||||
&loc_expr.value,
|
||||
module_path,
|
||||
);
|
||||
let (can_guard, guard_branch_output) =
|
||||
canonicalize_expr(env, var_store, scope, loc_expr.region, &loc_expr.value);
|
||||
|
||||
branch_output.union(guard_branch_output);
|
||||
Some(can_guard)
|
||||
|
@ -1878,13 +1785,12 @@ fn canonicalize_fields<'a>(
|
|||
scope: &mut Scope,
|
||||
region: Region,
|
||||
fields: &'a [Loc<ast::AssignedField<'a, ast::Expr<'a>>>],
|
||||
module_path: &str,
|
||||
) -> Result<(SendMap<Lowercase, Field>, Output), CanonicalizeRecordProblem> {
|
||||
let mut can_fields = SendMap::default();
|
||||
let mut output = Output::default();
|
||||
|
||||
for loc_field in fields.iter() {
|
||||
match canonicalize_field(env, var_store, scope, &loc_field.value, module_path) {
|
||||
match canonicalize_field(env, var_store, scope, &loc_field.value) {
|
||||
Ok((label, field_expr, field_out, field_var)) => {
|
||||
let field = Field {
|
||||
var: field_var,
|
||||
|
@ -1937,7 +1843,6 @@ fn canonicalize_field<'a>(
|
|||
var_store: &mut VarStore,
|
||||
scope: &mut Scope,
|
||||
field: &'a ast::AssignedField<'a, ast::Expr<'a>>,
|
||||
module_path: &str,
|
||||
) -> Result<(Lowercase, Loc<Expr>, Output, Variable), CanonicalizeFieldProblem> {
|
||||
use roc_parse::ast::AssignedField::*;
|
||||
|
||||
|
@ -1945,14 +1850,8 @@ fn canonicalize_field<'a>(
|
|||
// Both a label and a value, e.g. `{ name: "blah" }`
|
||||
RequiredValue(label, _, loc_expr) => {
|
||||
let field_var = var_store.fresh();
|
||||
let (loc_can_expr, output) = canonicalize_expr(
|
||||
env,
|
||||
var_store,
|
||||
scope,
|
||||
loc_expr.region,
|
||||
&loc_expr.value,
|
||||
module_path,
|
||||
);
|
||||
let (loc_can_expr, output) =
|
||||
canonicalize_expr(env, var_store, scope, loc_expr.region, &loc_expr.value);
|
||||
|
||||
Ok((
|
||||
Lowercase::from(label.value),
|
||||
|
@ -1973,7 +1872,7 @@ fn canonicalize_field<'a>(
|
|||
}
|
||||
|
||||
SpaceBefore(sub_field, _) | SpaceAfter(sub_field, _) => {
|
||||
canonicalize_field(env, var_store, scope, sub_field, module_path)
|
||||
canonicalize_field(env, var_store, scope, sub_field)
|
||||
}
|
||||
|
||||
Malformed(_string) => {
|
||||
|
@ -2502,14 +2401,13 @@ fn flatten_str_literal<'a>(
|
|||
var_store: &mut VarStore,
|
||||
scope: &mut Scope,
|
||||
literal: &StrLiteral<'a>,
|
||||
module_path: &str,
|
||||
) -> (Expr, Output) {
|
||||
use ast::StrLiteral::*;
|
||||
|
||||
match literal {
|
||||
PlainLine(str_slice) => (Expr::Str((*str_slice).into()), Output::default()),
|
||||
Line(segments) => flatten_str_lines(env, var_store, scope, &[segments], module_path),
|
||||
Block(lines) => flatten_str_lines(env, var_store, scope, lines, module_path),
|
||||
Line(segments) => flatten_str_lines(env, var_store, scope, &[segments]),
|
||||
Block(lines) => flatten_str_lines(env, var_store, scope, lines),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2640,7 +2538,6 @@ fn flatten_str_lines<'a>(
|
|||
var_store: &mut VarStore,
|
||||
scope: &mut Scope,
|
||||
lines: &[&[ast::StrSegment<'a>]],
|
||||
module_path: &str,
|
||||
) -> (Expr, Output) {
|
||||
use ast::StrSegment::*;
|
||||
|
||||
|
@ -2698,7 +2595,6 @@ fn flatten_str_lines<'a>(
|
|||
scope,
|
||||
loc_expr.region,
|
||||
loc_expr.value,
|
||||
module_path,
|
||||
);
|
||||
|
||||
output.union(new_output);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue