Skip to content

Commit

Permalink
Cleanup and distinguish unused types
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Apr 17, 2024
1 parent 3db6bc8 commit 8acd693
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 68 deletions.
19 changes: 14 additions & 5 deletions crates/rue-compiler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@ pub enum DiagnosticKind {

#[derive(Debug, Error, Clone, PartialEq, Eq, Hash)]
pub enum WarningKind {
#[error("redundant optional type")]
RedundantOptional,
#[error("marking optional types as optional again has no effect")]
UselessOptional,

#[error("redundant check against same type `{0}`")]
#[error("redundant type check against `{0}`, value is already that type")]
RedundantTypeGuard(String),

#[error("unused symbol `{0}`")]
UnusedSymbol(String),
#[error("unused function `{0}`")]
UnusedFunction(String),

#[error("unused parameter `{0}`")]
UnusedParameter(String),

#[error("unused constant `{0}`")]
UnusedConst(String),

#[error("unused let binding `{0}`")]
UnusedLet(String),

#[error("unused type `{0}`")]
UnusedType(String),
Expand Down
2 changes: 2 additions & 0 deletions crates/rue-compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(clippy::needless_pass_by_ref_mut)]

use clvmr::{Allocator, NodePtr};
use codegen::Codegen;
use lowerer::Lowerer;
Expand Down
5 changes: 1 addition & 4 deletions crates/rue-compiler/src/lowerer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1922,10 +1922,7 @@ impl<'a> Lowerer<'a> {
.unwrap_or(self.unknown_type);

if let Type::Optional(inner) = self.db.ty_raw(ty).clone() {
self.warning(
WarningKind::RedundantOptional,
optional.syntax().text_range(),
);
self.warning(WarningKind::UselessOptional, optional.syntax().text_range());
return inner;
}

Expand Down
116 changes: 59 additions & 57 deletions crates/rue-compiler/src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,37 +91,49 @@ impl<'a> Optimizer<'a> {
}
}

fn compute_captures_entrypoint(
fn check_unused(
&mut self,
scope_id: ScopeId,
inherits_from: Option<ScopeId>,
hir_id: HirId,
used_symbols: HashSet<SymbolId>,
used_types: HashSet<TypeId>,
) {
if self.environments.contains_key(&scope_id) {
return;
}

self.environments.insert(scope_id, Environment::default());
let used_types = self.db.scope(scope_id).used_types().clone();
self.env_mut(scope_id).used_types.extend(used_types);
self.env_mut(scope_id).inherits_from = inherits_from;
self.compute_captures_hir(scope_id, hir_id);

let unused_symbols: Vec<SymbolId> = self
.db
.scope(scope_id)
.local_symbols()
.iter()
.filter(|&symbol_id| !self.env(scope_id).used_symbols.contains(symbol_id))
.filter(|&symbol_id| !used_symbols.contains(symbol_id))
.copied()
.collect();

for symbol_id in unused_symbols {
if let Some(token) = self.db.symbol_token(symbol_id) {
self.warning(
WarningKind::UnusedSymbol(token.to_string()),
token.text_range(),
);
match self.db.symbol(symbol_id).clone() {
Symbol::Function { .. } => {
self.warning(
WarningKind::UnusedFunction(token.to_string()),
token.text_range(),
);
}
Symbol::Parameter { .. } => {
self.warning(
WarningKind::UnusedParameter(token.to_string()),
token.text_range(),
);
}
Symbol::LetBinding { .. } => {
self.warning(
WarningKind::UnusedLet(token.to_string()),
token.text_range(),
);
}
Symbol::ConstBinding { .. } => {
self.warning(
WarningKind::UnusedConst(token.to_string()),
token.text_range(),
);
}
}
}
}

Expand All @@ -130,7 +142,7 @@ impl<'a> Optimizer<'a> {
.scope(scope_id)
.local_types()
.iter()
.filter(|&type_id| !self.env(scope_id).used_types.contains(type_id))
.filter(|&type_id| !used_types.contains(type_id))
.copied()
.collect();

Expand All @@ -144,6 +156,29 @@ impl<'a> Optimizer<'a> {
}
}

fn compute_captures_entrypoint(
&mut self,
scope_id: ScopeId,
inherits_from: Option<ScopeId>,
hir_id: HirId,
) {
if self.environments.contains_key(&scope_id) {
return;
}

self.environments.insert(scope_id, Environment::default());
let used_types = self.db.scope(scope_id).used_types().clone();
self.env_mut(scope_id).used_types.extend(used_types);
self.env_mut(scope_id).inherits_from = inherits_from;
self.compute_captures_hir(scope_id, hir_id);

self.check_unused(
scope_id,
self.env(scope_id).used_symbols.clone(),
self.env(scope_id).used_types.clone(),
);
}

fn compute_captures_hir(&mut self, scope_id: ScopeId, hir_id: HirId) {
match self.db.hir(hir_id).clone() {
Hir::Unknown | Hir::Atom(_) => {}
Expand Down Expand Up @@ -240,46 +275,13 @@ impl<'a> Optimizer<'a> {

self.compute_captures_entrypoint(scope_id, None, hir_id);

let unused_symbols: Vec<SymbolId> = self
.db
.scope(root_scope_id)
.local_symbols()
.iter()
.filter(|&symbol_id| {
!self.env(scope_id).used_symbols.contains(symbol_id) && *symbol_id != main
})
.copied()
.collect();
let mut used_symbols = self.env(scope_id).used_symbols.clone();
used_symbols.insert(main);

for symbol_id in unused_symbols {
if let Some(token) = self.db.symbol_token(symbol_id) {
self.warning(
WarningKind::UnusedSymbol(token.to_string()),
token.text_range(),
);
}
}

let unused_types: Vec<TypeId> = self
.db
.scope(root_scope_id)
.local_types()
.iter()
.filter(|&type_id| {
!self.env(scope_id).used_types.contains(type_id)
&& !self.db.scope(root_scope_id).used_types().contains(type_id)
})
.copied()
.collect();
let mut used_types = self.env(scope_id).used_types.clone();
used_types.extend(self.db.scope(root_scope_id).used_types().clone());

for type_id in unused_types {
if let Some(token) = self.db.type_token(type_id) {
self.warning(
WarningKind::UnusedType(token.to_string()),
token.text_range(),
);
}
}
self.check_unused(root_scope_id, used_symbols, used_types);

for symbol_id in self.db.scope(scope_id).local_symbols() {
if self.db.symbol(symbol_id).is_parameter() {
Expand Down
4 changes: 2 additions & 2 deletions tests.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[list_types]
parser_errors = []
compiler_errors = [
"unused symbol `empty_hinted` at 3:9",
"unused symbol `empty` at 2:9",
"unused let binding `empty_hinted` at 3:9",
"unused let binding `empty` at 2:9",
]

[pair_types]
Expand Down

0 comments on commit 8acd693

Please sign in to comment.