Skip to content

Commit

Permalink
fix: solve clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Oct 21, 2019
1 parent d31f02c commit 870e7bd
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 62 deletions.
6 changes: 2 additions & 4 deletions crates/mun_abi/src/autogen.rs
@@ -1,6 +1,4 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]

use crate::prelude::*;

Expand Down Expand Up @@ -177,8 +175,8 @@ impl AssemblyInfo {
mod tests {
use super::*;
use std::ffi::CString;
use std::ptr;
use std::os::raw::c_char;
use std::ptr;

fn fake_type_info(name: &CStr) -> TypeInfo {
TypeInfo {
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_codegen/src/code_gen.rs
Expand Up @@ -114,7 +114,7 @@ pub fn write_module_shared_object(

if !result.status.success() {
let error = String::from_utf8(result.stderr)
.unwrap_or("<linker error contains invalid utf8>".to_owned());
.unwrap_or_else(|_| "<linker error contains invalid utf8>".to_owned());
Err(CodeGenerationError::LinkerError(error).into())
} else {
Ok(())
Expand Down
24 changes: 15 additions & 9 deletions crates/mun_codegen/src/code_gen/symbols.rs
Expand Up @@ -16,7 +16,7 @@ use std::hash::{Hash, Hasher};

pub type Guid = [u8; 16];

#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, Debug)]
#[derive(Clone, Eq, Ord, PartialOrd, Debug)]
pub struct TypeInfo {
pub guid: Guid,
pub name: String,
Expand All @@ -28,6 +28,12 @@ impl Hash for TypeInfo {
}
}

impl PartialEq for TypeInfo {
fn eq(&self, other: &Self) -> bool {
self.guid == other.guid
}
}

impl TypeInfo {
fn from_name<S: AsRef<str>>(name: S) -> TypeInfo {
TypeInfo {
Expand All @@ -53,7 +59,7 @@ pub fn type_info_query(_db: &impl IrDatabase, ty: Ty) -> TypeInfo {
fn type_info_ir(ty: &TypeInfo, module: &Module) -> StructValue {
let context = module.get_context();
let guid_values: [IntValue; 16] =
array_init::array_init(|i| context.i8_type().const_int(ty.guid[i] as u64, false));
array_init::array_init(|i| context.i8_type().const_int(u64::from(ty.guid[i]), false));
context.const_struct(
&[
context.i8_type().const_array(&guid_values).into(),
Expand All @@ -77,7 +83,7 @@ fn gen_signature_from_function<D: IrDatabase>(
db: &D,
module: &Module,
types: &AbiTypes,
function: &hir::Function,
function: hir::Function,
) -> StructValue {
let name_str = intern_string(&module, &function.name(db).to_string());
let ret_type_ir = gen_signature_return_type(db, module, types, function);
Expand All @@ -103,7 +109,7 @@ fn gen_signature_argument_types<D: IrDatabase>(
db: &D,
module: &Module,
types: &AbiTypes,
function: &hir::Function,
function: hir::Function,
) -> PointerValue {
let body = function.body(db);
let infer = function.infer(db);
Expand All @@ -130,7 +136,7 @@ fn gen_signature_return_type<D: IrDatabase>(
db: &D,
module: &Module,
types: &AbiTypes,
function: &hir::Function,
function: hir::Function,
) -> PointerValue {
let body = function.body(db);
let infer = function.infer(db);
Expand Down Expand Up @@ -163,7 +169,7 @@ fn gen_function_info_array<'a, D: IrDatabase>(
value.set_linkage(Linkage::Private);

// Generate the signature from the function
let signature = gen_signature_from_function(db, module, types, f);
let signature = gen_signature_from_function(db, module, types, *f);

// Generate the function info value
types.function_info_type.const_named_struct(&[
Expand Down Expand Up @@ -200,7 +206,7 @@ fn gen_dispatch_table<D: IrDatabase>(
let signatures: Vec<StructValue> = dispatch_table
.entries()
.iter()
.map(|f| gen_signature_from_function(db, module, types, f))
.map(|f| gen_signature_from_function(db, module, types, *f))
.collect();

// Construct an IR array from the signatures
Expand Down Expand Up @@ -269,13 +275,13 @@ pub(super) fn gen_reflection_ir(
let dispatch_table = gen_dispatch_table(db, &abi_types, module, dispatch_table);

// Construct the actual `get_info` function
gen_get_info_fn(db, &module, &abi_types, module_info, dispatch_table);
gen_get_info_fn(db, module, &abi_types, module_info, dispatch_table);
}

/// Construct the actual `get_info` function.
fn gen_get_info_fn(
db: &impl IrDatabase,
module: &&Module,
module: &Module,
abi_types: &AbiTypes,
module_info: StructValue,
dispatch_table: StructValue,
Expand Down
6 changes: 3 additions & 3 deletions crates/mun_codegen/src/ir/dispatch_table.rs
Expand Up @@ -42,14 +42,14 @@ impl DispatchTable {
&self,
db: &D,
builder: &inkwell::builder::Builder,
function: &hir::Function,
function: hir::Function,
) -> PointerValue {
let function_name = function.name(db).to_string();

// Get the index of the function
let index = *self
.function_to_idx
.get(function)
.get(&function)
.expect("unknown function");

// Get the internal table reference
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<'a, D: IrDatabase> DispatchTableBuilder<'a, D> {
let expr = &body[expr];

// If this expression is a call, store it in the dispatch table
if let Expr::Call { callee, args: _ } = expr {
if let Expr::Call { callee, .. } = expr {
self.ensure_table_ref();

// Get the function from the expression
Expand Down
19 changes: 9 additions & 10 deletions crates/mun_codegen/src/ir/function.rs
Expand Up @@ -154,7 +154,7 @@ impl<'a, 'b, D: IrDatabase> BodyIrGenerator<'a, 'b, D> {
Statement::Let {
pat, initializer, ..
} => {
self.gen_let_statement(pat, initializer);
self.gen_let_statement(*pat, *initializer);
}
Statement::Expr(expr) => {
self.gen_expr(*expr);
Expand Down Expand Up @@ -202,8 +202,7 @@ impl<'a, 'b, D: IrDatabase> BodyIrGenerator<'a, 'b, D> {
) {
(BasicTypeEnum::IntType(_), Some(target @ BasicTypeEnum::FloatType(_))) => self
.builder
.build_cast(InstructionOpcode::SIToFP, value, target, "implicit_cast")
.into(),
.build_cast(InstructionOpcode::SIToFP, value, target, "implicit_cast"),
(a, Some(b)) if a == b => value,
_ => unreachable!("could not perform implicit cast"),
}
Expand All @@ -229,17 +228,17 @@ impl<'a, 'b, D: IrDatabase> BodyIrGenerator<'a, 'b, D> {
}

/// Generate IR for a let statement: `let a:int = 3`
fn gen_let_statement(&mut self, pat: &PatId, initializer: &Option<ExprId>) {
fn gen_let_statement(&mut self, pat: PatId, initializer: Option<ExprId>) {
let initializer = initializer.and_then(|expr| self.gen_expr(expr));

match &self.body[*pat] {
match &self.body[pat] {
Pat::Bind { name } => {
let builder = self.new_alloca_builder();
let ty = try_convert_any_to_basic(self.db.type_ir(self.infer[*pat].clone()))
let ty = try_convert_any_to_basic(self.db.type_ir(self.infer[pat].clone()))
.expect("expected basic type");
let ptr = builder.build_alloca(ty, &name.to_string());
self.pat_to_local.insert(*pat, ptr);
self.pat_to_name.insert(*pat, name.to_string());
self.pat_to_local.insert(pat, ptr);
self.pat_to_name.insert(pat, name.to_string());
if let Some(value) = initializer {
self.builder.build_store(ptr, value);
};
Expand Down Expand Up @@ -359,7 +358,7 @@ impl<'a, 'b, D: IrDatabase> BodyIrGenerator<'a, 'b, D> {
}

/// Generates IR for a function call.
fn gen_call(&mut self, callee: ExprId, args: &Vec<ExprId>) -> CallSiteValue {
fn gen_call(&mut self, callee: ExprId, args: &[ExprId]) -> CallSiteValue {
// Get the function value from the map
let function = self.infer[callee]
.as_function_def()
Expand All @@ -374,7 +373,7 @@ impl<'a, 'b, D: IrDatabase> BodyIrGenerator<'a, 'b, D> {
if self.should_use_dispatch_table() {
let ptr_value =
self.dispatch_table
.gen_function_lookup(self.db, &self.builder, &function);
.gen_function_lookup(self.db, &self.builder, function);
self.builder
.build_call(ptr_value, &args, &function.name(self.db).to_string())
} else {
Expand Down
2 changes: 2 additions & 0 deletions crates/mun_codegen/src/ir/module.rs
Expand Up @@ -31,6 +31,8 @@ pub(crate) fn ir_query(db: &impl IrDatabase, file_id: FileId) -> Arc<ModuleIR> {
let mut functions = HashMap::new();
let mut dispatch_table_builder = DispatchTableBuilder::new(db, &llvm_module);
for def in db.module_data(file_id).definitions() {
// TODO: Remove once we have more ModuleDef variants
#[allow(clippy::single_match)]
match def {
ModuleDef::Function(f) => {
// Construct the function signature
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_codegen/src/ir/ty.rs
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn ir_query(db: &impl IrDatabase, ty: Ty) -> AnyTypeEnum {
.collect();
let ret_ty = match db.type_ir(ty.ret().clone()) {
AnyTypeEnum::VoidType(v) => return v.fn_type(&params, false).into(),
v @ _ => try_convert_any_to_basic(v).expect("could not convert return value"),
v => try_convert_any_to_basic(v).expect("could not convert return value"),
};

ret_ty.fn_type(&params, false).into()
Expand Down
3 changes: 2 additions & 1 deletion crates/mun_codegen/src/lib.rs
Expand Up @@ -2,9 +2,10 @@
mod code_gen;
mod db;
mod ir;
mod mock;
pub(crate) mod symbols;

#[cfg(test)]
mod mock;
#[cfg(test)]
mod test;

Expand Down
4 changes: 2 additions & 2 deletions crates/mun_compiler/src/lib.rs
Expand Up @@ -213,7 +213,7 @@ fn diagnostics(db: &CompilerDatabase, file_id: FileId) -> Vec<Diagnostic> {
SyntaxKind::FUNCTION_DEF => {
ast::FunctionDef::cast(d.definition.to_node(&parse.tree().syntax()))
.map(|f| f.signature_range())
.unwrap_or(d.highlight_range())
.unwrap_or_else(|| d.highlight_range())
.into()
}
_ => d.highlight_range().into(),
Expand Down Expand Up @@ -253,7 +253,7 @@ pub fn main(options: &CompilerOptions) -> Result<Option<PathBuf>, failure::Error
let target = db.target();
let relative_path = db.file_relative_path(file_id);
let original_filename = Path::new(relative_path.file_name().unwrap());
let dll_extension = if target.options.dll_suffix.starts_with(".") {
let dll_extension = if target.options.dll_suffix.starts_with('.') {
&target.options.dll_suffix[1..]
} else {
&target.options.dll_suffix
Expand Down
5 changes: 1 addition & 4 deletions crates/mun_hir/src/ty/infer.rs
Expand Up @@ -198,10 +198,7 @@ impl<'a, D: HirDatabase> InferenceResultBuilder<'a, D> {
_ => Ty::Unknown,
},
Expr::Block { statements, tail } => self.infer_block(statements, *tail, expected),
Expr::Call {
callee: call,
args: args,
} => self.infer_call(&tgt_expr, call, args, expected),
Expr::Call { callee: call, args } => self.infer_call(&tgt_expr, call, args, expected),
Expr::Literal(lit) => match lit {
Literal::String(_) => Ty::Unknown,
Literal::Bool(_) => Ty::Unknown,
Expand Down
16 changes: 9 additions & 7 deletions crates/mun_runtime/src/assembly.rs
Expand Up @@ -48,13 +48,15 @@ impl Assembly {
let fn_ptr = runtime_dispatch_table
.get(fn_signature.name())
.map(|f| f.fn_ptr)
.ok_or(io::Error::new(
io::ErrorKind::NotFound,
format!(
"Failed to link: function '{}' is missing.",
fn_signature.name()
),
))?;
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
format!(
"Failed to link: function '{}' is missing.",
fn_signature.name()
),
)
})?;

*dispatch_ptr = fn_ptr;
}
Expand Down
32 changes: 12 additions & 20 deletions crates/mun_runtime/src/lib.rs
Expand Up @@ -49,17 +49,12 @@ impl RuntimeBuilder {
}

/// A runtime dispatch table that maps full function paths to function information.
#[derive(Default)]
pub struct DispatchTable {
functions: HashMap<String, FunctionInfo>,
}

impl DispatchTable {
pub fn new() -> Self {
Self {
functions: HashMap::new(),
}
}

pub fn get(&self, fn_path: &str) -> Option<&FunctionInfo> {
self.functions.get(fn_path)
}
Expand Down Expand Up @@ -95,7 +90,7 @@ impl MunRuntime {
let watcher: RecommendedWatcher = Watcher::new(tx, options.delay)?;
let mut runtime = MunRuntime {
assemblies: HashMap::new(),
dispatch_table: DispatchTable::new(),
dispatch_table: DispatchTable::default(),
watcher,
watcher_rx: rx,
};
Expand Down Expand Up @@ -138,21 +133,18 @@ impl MunRuntime {
pub fn update(&mut self) -> bool {
while let Ok(event) = self.watcher_rx.try_recv() {
use notify::DebouncedEvent::*;
match event {
Write(ref path) => {
if let Some(assembly) = self.assemblies.get_mut(path) {
if let Err(e) = assembly.swap(path, &mut self.dispatch_table) {
println!(
"An error occured while reloading assembly '{}': {:?}",
path.to_string_lossy(),
e
);
} else {
return true;
}
if let Write(ref path) = event {
if let Some(assembly) = self.assemblies.get_mut(path) {
if let Err(e) = assembly.swap(path, &mut self.dispatch_table) {
println!(
"An error occured while reloading assembly '{}': {:?}",
path.to_string_lossy(),
e
);
} else {
return true;
}
}
_ => {}
}
}
false
Expand Down

0 comments on commit 870e7bd

Please sign in to comment.