Skip to content

Commit 7d514a1

Browse files
GearsDatapackslpil
authored andcommitted
Address review comments
1 parent cb21d77 commit 7d514a1

File tree

1 file changed

+13
-35
lines changed

1 file changed

+13
-35
lines changed

compiler-core/src/inline.rs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,12 @@ impl Inliner<'_> {
305305
// See the `inline_variables` documentation for an explanation.
306306
ValueConstructorVariant::LocalVariable { .. } => {
307307
// We remove the variable as inlined variables can only be
308-
// inlined once.
308+
// inlined once. `inline_variables` only contains variables
309+
// which we have already checked are possible to inline, as
310+
// we check for variables which are only used once when converting
311+
// to an `InlinableFunction`.
309312
match self.inline_variables.remove(name) {
310-
Some(expression) => expression,
313+
Some(inlined_expression) => inlined_expression,
311314
None => expression,
312315
}
313316
}
@@ -600,14 +603,7 @@ impl Inliner<'_> {
600603
{
601604
// First, we do the actual inlining, by converting it to
602605
// an anonymous function.
603-
let TypedExpr::Fn {
604-
args: parameters,
605-
body,
606-
..
607-
} = function.to_anonymous_function()
608-
else {
609-
unreachable!("to_anonymous_function always returns TypedExpr::fn");
610-
};
606+
let (parameters, body) = function.to_anonymous_function();
611607
// Then, we perform beta reduction, inlining the call to
612608
// the anonymous function.
613609
return self.inline_anonymous_function_call(
@@ -641,14 +637,7 @@ impl Inliner<'_> {
641637
.get(module_name)
642638
.and_then(|module| module.inline_functions.get(name))
643639
{
644-
let TypedExpr::Fn {
645-
args: parameters,
646-
body,
647-
..
648-
} = function.to_anonymous_function()
649-
else {
650-
unreachable!("to_anonymous_function always returns TypedExpr::fn");
651-
};
640+
let (parameters, body) = function.to_anonymous_function();
652641
return self.inline_anonymous_function_call(
653642
&parameters,
654643
arguments,
@@ -1059,10 +1048,7 @@ fn expand_block(expression: TypedExpr) -> TypedExpr {
10591048
location,
10601049
statements,
10611050
} if statements.len() == 1 => {
1062-
let first = statements
1063-
.into_iter()
1064-
.next()
1065-
.expect("Vec1 always has a first element");
1051+
let (first, _rest) = statements.split_off_first();
10661052

10671053
match first {
10681054
// If this is several blocks inside each other, we want to
@@ -1448,7 +1434,7 @@ const BLANK_LOCATION: SrcSpan = SrcSpan { start: 0, end: 0 };
14481434
impl InlinableFunction {
14491435
/// Converts an `InlinableFunction` to an anonymous function, which can then
14501436
/// be inlined within another function.
1451-
fn to_anonymous_function(&self) -> TypedExpr {
1437+
fn to_anonymous_function(&self) -> (Vec<TypedArg>, Vec1<TypedStatement>) {
14521438
let parameters = self
14531439
.parameters
14541440
.iter()
@@ -1461,19 +1447,11 @@ impl InlinableFunction {
14611447
.map(|ast| Statement::Expression(ast.to_expression()))
14621448
.collect_vec();
14631449

1464-
TypedExpr::Fn {
1465-
location: BLANK_LOCATION,
1466-
type_: unknown_type(),
1467-
kind: FunctionLiteralKind::Anonymous {
1468-
head: BLANK_LOCATION,
1469-
},
1470-
args: parameters,
1471-
body: body
1472-
.try_into()
1450+
(
1451+
parameters,
1452+
body.try_into()
14731453
.expect("Type-checking ensured that the body has at least 1 statement"),
1474-
return_annotation: None,
1475-
purity: Purity::Unknown,
1476-
}
1454+
)
14771455
}
14781456
}
14791457

0 commit comments

Comments
 (0)