fix clippy complaints

This commit is contained in:
Folkert 2020-08-06 21:39:55 +02:00
parent 9dbce40a55
commit 0fbcfc7431
4 changed files with 45 additions and 146 deletions

View file

@ -4,7 +4,6 @@ use inkwell::module::Linkage;
use inkwell::passes::PassManager;
use inkwell::types::BasicType;
use inkwell::OptimizationLevel;
use roc_collections::all::ImMap;
use roc_gen::layout_id::LayoutIds;
use roc_gen::llvm::build::{
build_proc, build_proc_header, get_call_conventions, module_from_builtins, OptLevel,

View file

@ -20,7 +20,7 @@ use roc_collections::all::ImMap;
use roc_module::low_level::LowLevel;
use roc_module::symbol::{Interns, Symbol};
use roc_mono::ir::JoinPointId;
use roc_mono::layout::{Builtin, Layout, Ownership};
use roc_mono::layout::{Builtin, Layout};
use target_lexicon::CallingConvention;
/// This is for Inkwell's FunctionValue::verify - we want to know the verification
@ -193,9 +193,6 @@ pub fn add_passes(fpm: &PassManager<FunctionValue<'_>>, opt_level: OptLevel) {
pub fn build_exp_literal<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
scope: &Scope<'a, 'ctx>,
parent: FunctionValue<'ctx>,
literal: &roc_mono::ir::Literal<'a>,
) -> BasicValueEnum<'ctx> {
use roc_mono::ir::Literal::*;
@ -258,20 +255,9 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
use roc_mono::ir::Expr::*;
match expr {
Literal(literal) => build_exp_literal(env, layout_ids, scope, parent, literal),
Literal(literal) => build_exp_literal(env, literal),
Alias(symbol) => load_symbol(env, scope, symbol),
RunLowLevel(op, symbols) => {
let mut args = Vec::with_capacity_in(symbols.len(), env.arena);
for symbol in symbols.iter() {
match scope.get(symbol) {
Some((layout, _)) => args.push(*symbol),
None => panic!("There was no entry for {:?} in scope {:?}", symbol, scope),
}
}
run_low_level(env, layout_ids, scope, parent, *op, args.into_bump_slice())
}
RunLowLevel(op, symbols) => run_low_level(env, scope, parent, *op, symbols),
FunctionCall {
call_type: ByName(name),
@ -284,7 +270,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
arg_tuples.push(load_symbol(env, scope, symbol));
}
call_with_args_ir(
call_with_args(
env,
layout_ids,
layout,
@ -296,7 +282,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
FunctionCall {
call_type: ByPointer(name),
layout,
layout: _,
args,
} => {
let sub_expr = load_symbol(env, scope, name);
@ -392,7 +378,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
let mut field_types = Vec::with_capacity_in(num_fields, env.arena);
let mut field_vals = Vec::with_capacity_in(num_fields, env.arena);
for (field_symbol) in it {
for field_symbol in it {
let (val, field_layout) = load_symbol_and_layout(env, scope, field_symbol);
// Zero-sized fields have no runtime representation.
// The layout of the struct expects them to be dropped!
@ -590,9 +576,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
.expect("desired field did not decode")
}
EmptyArray => empty_polymorphic_list(env),
Array { elem_layout, elems } => {
list_literal(env, layout_ids, scope, parent, elem_layout, elems)
}
Array { elem_layout, elems } => list_literal(env, scope, elem_layout, elems),
FunctionPointer(symbol, layout) => {
let fn_name = layout_ids
.get(*symbol, layout)
@ -830,6 +814,7 @@ fn refcount_is_one_comparison<'ctx>(
)
}
#[allow(dead_code)]
fn list_get_refcount_ptr<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
list_wrapper: StructValue<'ctx>,
@ -861,6 +846,7 @@ fn list_get_refcount_ptr<'a, 'ctx, 'env>(
)
}
#[allow(dead_code)]
fn increment_refcount_list<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
original_wrapper: StructValue<'ctx>,
@ -889,6 +875,7 @@ fn increment_refcount_list<'a, 'ctx, 'env>(
body
}
#[allow(dead_code)]
fn decrement_refcount_list<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
parent: FunctionValue<'ctx>,
@ -942,7 +929,7 @@ fn load_symbol<'a, 'ctx, 'env>(
symbol: &Symbol,
) -> BasicValueEnum<'ctx> {
match scope.get(symbol) {
Some((layout, ptr)) => env
Some((_, ptr)) => env
.builder
.build_load(*ptr, symbol.ident_string(&env.interns)),
None => panic!("There was no entry for {:?} in scope {:?}", symbol, scope),
@ -1534,49 +1521,9 @@ fn list_repeat<'a, 'ctx, 'env>(
)
}
// #[allow(clippy::cognitive_complexity)]
#[inline(always)]
#[allow(clippy::cognitive_complexity)]
fn call_with_args<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
layout: &Layout<'a>,
symbol: Symbol,
_parent: FunctionValue<'ctx>,
args: &[(BasicValueEnum<'ctx>, &'a Layout<'a>)],
) -> BasicValueEnum<'ctx> {
let fn_name = layout_ids
.get(symbol, layout)
.to_symbol_string(symbol, &env.interns);
let fn_val = env
.module
.get_function(fn_name.as_str())
.unwrap_or_else(|| {
if symbol.is_builtin() {
panic!("Unrecognized builtin function: {:?}", symbol)
} else {
panic!("Unrecognized non-builtin function: {:?}", symbol)
}
});
let mut arg_vals: Vec<BasicValueEnum> = Vec::with_capacity_in(args.len(), env.arena);
for (arg, _layout) in args.iter() {
arg_vals.push(*arg);
}
let call = env
.builder
.build_call(fn_val, arg_vals.into_bump_slice(), "call");
call.set_call_convention(fn_val.get_call_conventions());
call.try_as_basic_value()
.left()
.unwrap_or_else(|| panic!("LLVM error: Invalid call by name for name {:?}", symbol))
}
#[inline(always)]
#[allow(clippy::cognitive_complexity)]
fn call_with_args_ir<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
layout: &Layout<'a>,
@ -1597,15 +1544,8 @@ fn call_with_args_ir<'a, 'ctx, 'env>(
panic!("Unrecognized non-builtin function: {:?}", symbol)
}
});
let mut arg_vals: Vec<BasicValueEnum> = Vec::with_capacity_in(args.len(), env.arena);
for (arg) in args.iter() {
arg_vals.push(*arg);
}
let call = env
.builder
.build_call(fn_val, arg_vals.into_bump_slice(), "call");
let call = env.builder.build_call(fn_val, args, "call");
call.set_call_convention(fn_val.get_call_conventions());
@ -1764,9 +1704,7 @@ fn empty_polymorphic_list<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> BasicVal
fn list_literal<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
scope: &Scope<'a, 'ctx>,
parent: FunctionValue<'ctx>,
elem_layout: &Layout<'a>,
elems: &&[Symbol],
) -> BasicValueEnum<'ctx> {
@ -2008,7 +1946,6 @@ pub static COLD_CALL_CONV: u32 = 9;
fn run_low_level<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
scope: &Scope<'a, 'ctx>,
parent: FunctionValue<'ctx>,
op: LowLevel,
@ -2202,7 +2139,7 @@ fn run_low_level<'a, 'ctx, 'env>(
}
}
}
ListAppend => list_append(env, layout_ids, scope, parent, args),
ListAppend => list_append(env, scope, parent, args),
ListPush => {
// List.push List elem, elem -> List elem
debug_assert_eq!(args.len(), 2);
@ -2422,7 +2359,6 @@ fn build_int_binop<'a, 'ctx, 'env>(
fn list_append<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
scope: &Scope<'a, 'ctx>,
parent: FunctionValue<'ctx>,
args: &[Symbol],

View file

@ -1,7 +1,6 @@
use crate::exhaustive::{Ctor, RenderAs, TagId, Union};
use crate::ir::{DestructType, Env, Expr, JoinPointId, Literal, Pattern, Stmt};
use crate::layout::{Builtin, Layout};
use bumpalo::Bump;
use roc_collections::all::{MutMap, MutSet};
use roc_module::ident::TagName;
use roc_module::low_level::LowLevel;
@ -879,7 +878,6 @@ enum Choice<'a> {
Jump(Label),
}
type Stores<'a> = &'a [(Symbol, Layout<'a>, Expr<'a>)];
type StoresVec<'a> = bumpalo::collections::Vec<'a, (Symbol, Layout<'a>, Expr<'a>)>;
pub fn optimize_when<'a>(
@ -1176,11 +1174,7 @@ fn decide_to_branching<'a>(
let fail = &*env.arena.alloc(fail_expr);
let pass = &*env.arena.alloc(pass_expr);
// TODO totally wrong
let condition = Expr::Literal(Literal::Int(42));
let branching_symbol = env.unique_symbol();
let branching_layout = Layout::Builtin(Builtin::Int1);
let mut cond = Stmt::Cond {
@ -1273,7 +1267,7 @@ fn decide_to_branching<'a>(
current_symbol = accum;
}
for ((new_stores, lhs, rhs, layout), accum) in tests.into_iter().rev().zip(accum_it) {
for ((new_stores, lhs, rhs, _layout), accum) in tests.into_iter().rev().zip(accum_it) {
let test_symbol = env.unique_symbol();
let test = Expr::RunLowLevel(
LowLevel::Eq,

View file

@ -1,8 +1,6 @@
use self::InProgressProc::*;
use crate::exhaustive::{Ctor, Guard, RenderAs, TagId};
use crate::layout::{
list_layout_from_elem, Builtin, Layout, LayoutCache, LayoutProblem, Ownership,
};
use crate::layout::{Builtin, Layout, LayoutCache, LayoutProblem, Ownership};
use bumpalo::collections::Vec;
use bumpalo::Bump;
use roc_collections::all::{default_hasher, MutMap, MutSet};
@ -60,7 +58,7 @@ impl<'a> Proc<'a> {
.append(alloc.intersperse(args_doc, ", "))
.append("):")
.append(alloc.hardline())
.append(self.body.to_doc(alloc, false).indent(4))
.append(self.body.to_doc(alloc).indent(4))
}
pub fn to_pretty(&self, width: usize) -> String {
@ -104,7 +102,7 @@ impl<'a> Procs<'a> {
loc_body: Located<roc_can::expr::Expr>,
ret_var: Variable,
) {
match patterns_to_when(env, self, layout_cache, loc_args, ret_var, loc_body) {
match patterns_to_when(env, layout_cache, loc_args, ret_var, loc_body) {
Ok((_, pattern_symbols, body)) => {
// a named closure. Since these aren't specialized by the surrounding
// context, we can't add pending specializations for them yet.
@ -148,7 +146,7 @@ impl<'a> Procs<'a> {
ret_var: Variable,
layout_cache: &mut LayoutCache<'a>,
) -> Result<Layout<'a>, RuntimeError> {
match patterns_to_when(env, self, layout_cache, loc_args, ret_var, loc_body) {
match patterns_to_when(env, layout_cache, loc_args, ret_var, loc_body) {
Ok((pattern_vars, pattern_symbols, body)) => {
// an anonymous closure. These will always be specialized already
// by the surrounding context, so we can add pending specializations
@ -422,7 +420,7 @@ pub enum Expr<'a> {
}
impl<'a> Literal<'a> {
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D, parens: bool) -> DocBuilder<'b, D, A>
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D) -> DocBuilder<'b, D, A>
where
D: DocAllocator<'b, A>,
D::Doc: Clone,
@ -459,7 +457,7 @@ where
}
impl<'a> Expr<'a> {
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D, parens: bool) -> DocBuilder<'b, D, A>
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D) -> DocBuilder<'b, D, A>
where
D: DocAllocator<'b, A>,
D::Doc: Clone,
@ -468,7 +466,7 @@ impl<'a> Expr<'a> {
use Expr::*;
match self {
Literal(lit) => lit.to_doc(alloc, false),
Literal(lit) => lit.to_doc(alloc),
Alias(symbol) => alloc.text("alias ").append(symbol_to_doc(alloc, *symbol)),
FunctionPointer(symbol, _) => symbol_to_doc(alloc, *symbol),
@ -555,7 +553,7 @@ impl<'a> Stmt<'a> {
from_can(env, can_expr, procs, &mut layout_cache)
}
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D, parens: bool) -> DocBuilder<'b, D, A>
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D) -> DocBuilder<'b, D, A>
where
D: DocAllocator<'b, A>,
D::Doc: Clone,
@ -568,10 +566,10 @@ impl<'a> Stmt<'a> {
.text("let ")
.append(symbol_to_doc(alloc, *symbol))
.append(" = ")
.append(expr.to_doc(alloc, false))
.append(expr.to_doc(alloc))
.append(";")
.append(alloc.hardline())
.append(cont.to_doc(alloc, false)),
.append(cont.to_doc(alloc)),
Ret(symbol) => alloc
.text("ret ")
@ -587,7 +585,7 @@ impl<'a> Stmt<'a> {
let default_doc = alloc
.text("default:")
.append(alloc.hardline())
.append(default_branch.to_doc(alloc, false).indent(4))
.append(default_branch.to_doc(alloc).indent(4))
.indent(4);
let branches_docs = branches
@ -596,7 +594,7 @@ impl<'a> Stmt<'a> {
alloc
.text(format!("case {}:", tag))
.append(alloc.hardline())
.append(expr.to_doc(alloc, false).indent(4))
.append(expr.to_doc(alloc).indent(4))
.indent(4)
})
.chain(std::iter::once(default_doc));
@ -618,11 +616,11 @@ impl<'a> Stmt<'a> {
} => alloc
.text(format!("if {} then", branching_symbol))
.append(alloc.hardline())
.append(pass.to_doc(alloc, false).indent(4))
.append(pass.to_doc(alloc).indent(4))
.append(alloc.hardline())
.append(alloc.text("else"))
.append(alloc.hardline())
.append(fail.to_doc(alloc, false).indent(4)),
.append(fail.to_doc(alloc).indent(4)),
RuntimeError(s) => alloc.text(format!("Error {}", s)),
Join {
@ -635,14 +633,14 @@ impl<'a> Stmt<'a> {
alloc.intersperse(
vec![
remainder.to_doc(alloc, false),
remainder.to_doc(alloc),
alloc
.text("joinpoint ")
.append(join_point_to_doc(alloc, *id))
.append(" ".repeat(arguments.len().min(1)))
.append(alloc.intersperse(it, alloc.space()))
.append(":"),
continuation.to_doc(alloc, false).indent(4),
continuation.to_doc(alloc).indent(4),
],
alloc.hardline(),
)
@ -669,7 +667,7 @@ impl<'a> Stmt<'a> {
pub fn to_pretty(&self, width: usize) -> String {
let allocator = BoxAllocator;
let mut w = std::vec::Vec::new();
self.to_doc::<_, ()>(&allocator, false)
self.to_doc::<_, ()>(&allocator)
.1
.render(width, &mut w)
.unwrap();
@ -690,7 +688,6 @@ impl<'a> Stmt<'a> {
#[allow(clippy::type_complexity)]
fn patterns_to_when<'a>(
env: &mut Env<'a, '_>,
procs: &mut Procs<'a>,
layout_cache: &mut LayoutCache<'a>,
patterns: std::vec::Vec<(Variable, Located<roc_can::pattern::Pattern>)>,
body_var: Variable,
@ -1049,10 +1046,6 @@ pub fn with_hole<'a>(
// A bit ugly, but it does the job
match hole {
Stmt::Jump(id, _) => Stmt::Jump(*id, env.arena.alloc([symbol])),
Stmt::Ret(s) => {
//
Stmt::Ret(symbol)
}
_ => {
// if you see this, there is variable aliasing going on
Stmt::Ret(symbol)
@ -1147,7 +1140,7 @@ pub fn with_hole<'a>(
}
Wrapped(sorted_tag_layouts) => {
let union_size = sorted_tag_layouts.len() as u8;
let (tag_id, (_, argument_layouts)) = sorted_tag_layouts
let (tag_id, (_, _)) = sorted_tag_layouts
.iter()
.enumerate()
.find(|(_, (key, _))| key == &tag_name)
@ -1165,8 +1158,6 @@ pub fn with_hole<'a>(
}
}
let layout_it = argument_layouts.iter();
let mut layouts: Vec<&'a [Layout<'a>]> =
Vec::with_capacity_in(sorted_tag_layouts.len(), env.arena);
@ -1221,8 +1212,6 @@ pub fn with_hole<'a>(
mut fields,
..
} => {
let arena = env.arena;
let sorted_fields = crate::layout::sort_record_fields(
env.arena,
record_var,
@ -1238,13 +1227,13 @@ pub fn with_hole<'a>(
field_layouts.push(layout);
let field = fields.remove(&label).unwrap();
let field_symbol = if let roc_can::expr::Expr::Var(symbol) = field.loc_expr.value {
if let roc_can::expr::Expr::Var(symbol) = field.loc_expr.value {
field_symbols.push(symbol);
can_fields.push(None);
} else {
field_symbols.push(env.unique_symbol());
can_fields.push(Some(field));
};
}
}
// creating a record from the var will unpack it if it's just a single field.
@ -1280,8 +1269,6 @@ pub fn with_hole<'a>(
branches,
final_else,
} => {
let arena = env.arena;
let ret_layout = layout_cache
.from_var(env.arena, branch_var, env.subs, env.pointer_size)
.expect("invalid ret_layout");
@ -1440,7 +1427,7 @@ pub fn with_hole<'a>(
stmt
}
LetRec(_, _, _, _) | LetNonRec(_, _, _, _) => todo!("lets"),
LetRec(_, _, _, _) => todo!("lets"),
Access {
record_var,
@ -1449,8 +1436,6 @@ pub fn with_hole<'a>(
loc_expr,
..
} => {
let arena = env.arena;
let sorted_fields = crate::layout::sort_record_fields(
env.arena,
record_var,
@ -1488,7 +1473,7 @@ pub fn with_hole<'a>(
let mut stmt = Stmt::Let(assigned, expr, layout, hole);
if let roc_can::expr::Expr::Var(symbol) = loc_expr.value {
if let roc_can::expr::Expr::Var(_) = loc_expr.value {
// do nothing
} else {
stmt = with_hole(
@ -1728,7 +1713,7 @@ pub fn from_can<'a>(
from_can(env, cont.value, procs, layout_cache)
}
LetNonRec(def, cont, xvar, _) => {
LetNonRec(def, cont, _, _) => {
if let roc_can::pattern::Pattern::Identifier(symbol) = &def.loc_pattern.value {
if let Closure(_, _, _, _, _) = &def.loc_expr.value {
// Now that we know for sure it's a closure, get an owned
@ -1799,7 +1784,7 @@ pub fn from_can<'a>(
let mut stores = Vec::new_in(env.arena);
let outer_symbol = env.unique_symbol();
store_pattern(env, &mono_pattern, outer_symbol, layout, &mut stores);
store_pattern(env, &mono_pattern, outer_symbol, layout, &mut stores).unwrap();
// convert the continuation
let mut stmt = from_can(env, cont.value, procs, layout_cache);
@ -1831,11 +1816,8 @@ pub fn from_can<'a>(
fn to_opt_branches<'a>(
env: &mut Env<'a, '_>,
cond_var: Variable,
expr_var: Variable,
region: Region,
cond_symbol: Symbol,
mut branches: std::vec::Vec<roc_can::expr::WhenBranch>,
branches: std::vec::Vec<roc_can::expr::WhenBranch>,
layout_cache: &mut LayoutCache<'a>,
) -> std::vec::Vec<(
Pattern<'a>,
@ -1844,10 +1826,6 @@ fn to_opt_branches<'a>(
)> {
debug_assert!(!branches.is_empty());
let cond_layout = layout_cache
.from_var(env.arena, cond_var, env.subs, env.pointer_size)
.unwrap_or_else(|err| panic!("TODO turn this into a RuntimeError {:?}", err));
let mut loc_branches = std::vec::Vec::new();
let mut opt_branches = std::vec::Vec::new();
@ -1935,15 +1913,7 @@ fn from_can_when<'a>(
// We can't know what to return!
return Stmt::RuntimeError("Hit a 0-branch when expression");
}
let opt_branches = to_opt_branches(
env,
cond_var,
expr_var,
region,
cond_symbol,
branches,
layout_cache,
);
let opt_branches = to_opt_branches(env, region, branches, layout_cache);
let cond_layout = layout_cache
.from_var(env.arena, cond_var, env.subs, env.pointer_size)
@ -2188,10 +2158,10 @@ fn call_by_name<'a>(
}
let field_symbols = field_symbols.into_bump_slice();
for (var, loc_arg) in loc_args.clone() {
match layout_cache.from_var(&env.arena, var, &env.subs, env.pointer_size) {
Ok(layout) => {
pattern_vars.push(var);
for (var, _) in &loc_args {
match layout_cache.from_var(&env.arena, *var, &env.subs, env.pointer_size) {
Ok(_) => {
pattern_vars.push(*var);
}
Err(_) => {
// One of this function's arguments code gens to a runtime error,