Skip to content

Commit

Permalink
frontend: Remove exhaustivness check in typeck
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Dec 1, 2024
1 parent f386140 commit 7ffb5fc
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 128 deletions.
2 changes: 1 addition & 1 deletion dora-frontend/src/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::typeck::control::{
check_expr_break_and_continue, check_expr_for, check_expr_if, check_expr_match,
check_expr_return, check_expr_while, get_subpatterns,
};
use crate::typeck::expr::{check_expr, read_ident, read_path, read_path_expr};
use crate::typeck::expr::{check_expr, read_path, read_path_expr};
pub use crate::typeck::expr::{compute_lit_float, compute_lit_int};
use crate::typeck::function::{
add_local, check_args_compatible, check_args_compatible_fct, check_lit_char, check_lit_float,
Expand Down
114 changes: 1 addition & 113 deletions dora-frontend/src/typeck/control.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::sync::Arc;

use dora_parser::ast;
use fixedbitset::FixedBitSet;

use crate::error::msg::ErrorMessage;
use crate::expr_always_returns;
use crate::sema::{find_impl, FctDefinitionId, ForTypeInfo};
use crate::sym::SymbolKind;
use crate::ty::{self, TraitType};
use crate::typeck::{check_expr, check_pattern, read_ident, read_path, TypeCheck};
use crate::typeck::{check_expr, check_pattern, TypeCheck};
use crate::{specialize_type, SourceType, SourceTypeArray};

pub(super) fn check_expr_while(
Expand Down Expand Up @@ -362,8 +360,6 @@ pub(super) fn check_expr_match(
ck.symtable.pop_level();
}

check_coverage(ck, node, expr_type);

ck.analysis.set_ty(node.id, result_type.clone());

result_type
Expand Down Expand Up @@ -404,114 +400,6 @@ fn check_expr_match_arm(
}
}

fn check_coverage(ck: &mut TypeCheck, node: &ast::ExprMatchType, expr_type: SourceType) {
if !expr_type.is_enum() {
ck.sa
.report(ck.file_id, node.expr.span(), ErrorMessage::EnumExpected);
return;
}

let enum_id = expr_type.enum_id().expect("enum expected");

let enum_ = ck.sa.enum_(enum_id);
let enum_variants = enum_.variants().len();

let mut used_variants = FixedBitSet::with_capacity(enum_variants);

for arm in &node.arms {
if arm.cond.is_some() {
continue;
}

let pattern = arm.pattern.as_ref();
for pattern in &pattern.alts {
match pattern.as_ref() {
ast::PatternAlt::Underscore(..) => {
let mut negated_used_variants = used_variants.clone();
negated_used_variants.toggle_range(..);

if negated_used_variants.count_ones(..) == 0 {
let msg = ErrorMessage::MatchUnreachablePattern;
ck.sa.report(ck.file_id, arm.span, msg);
}

used_variants.insert_range(..);
}

ast::PatternAlt::Rest(..) => unreachable!(),

ast::PatternAlt::LitBool(..)
| ast::PatternAlt::LitChar(..)
| ast::PatternAlt::LitString(..)
| ast::PatternAlt::LitInt(..)
| ast::PatternAlt::LitFloat(..)
| ast::PatternAlt::Tuple(..) => unreachable!(),

ast::PatternAlt::Ident(ref ident) => {
let sym = read_ident(ck, &ident.name);

match sym {
Ok(SymbolKind::EnumVariant(pattern_enum_id, variant_idx)) => {
if pattern_enum_id == enum_id {
if used_variants.contains(variant_idx as usize) {
let msg = ErrorMessage::MatchUnreachablePattern;
ck.sa.report(ck.file_id, arm.span, msg);
}

used_variants.insert(variant_idx as usize);
} else {
let msg = ErrorMessage::EnumVariantExpected;
ck.sa.report(ck.file_id, ident.span, msg);
}
}

Ok(_) => {
let msg = ErrorMessage::EnumVariantExpected;
ck.sa.report(ck.file_id, ident.span, msg);
}

Err(()) => {}
}
}

ast::PatternAlt::ClassOrStructOrEnum(ref ident) => {
let sym = read_path(ck, &ident.path);

match sym {
Ok(SymbolKind::EnumVariant(pattern_enum_id, variant_idx)) => {
if pattern_enum_id == enum_id {
if used_variants.contains(variant_idx as usize) {
let msg = ErrorMessage::MatchUnreachablePattern;
ck.sa.report(ck.file_id, arm.span, msg);
}

used_variants.insert(variant_idx as usize);
} else {
let msg = ErrorMessage::EnumVariantExpected;
ck.sa.report(ck.file_id, ident.span, msg);
}
}

Ok(_) => {
let msg = ErrorMessage::EnumVariantExpected;
ck.sa.report(ck.file_id, ident.path.span, msg);
}

Err(()) => {}
}
}
}
}
}

used_variants.toggle_range(..);

if used_variants.count_ones(..) != 0 {
let msg = ErrorMessage::MatchUncoveredVariant;
ck.sa.report(ck.file_id, node.expr.span(), msg);
}
}

pub(super) fn get_subpatterns(p: &ast::PatternAlt) -> Option<&Vec<Arc<ast::PatternField>>> {
match p {
ast::PatternAlt::Underscore(..)
Expand Down
14 changes: 0 additions & 14 deletions dora-frontend/src/typeck/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2324,20 +2324,6 @@ pub(super) fn check_type(
}
}

pub(super) fn read_ident(ck: &mut TypeCheck, ident: &ast::IdentData) -> Result<SymbolKind, ()> {
let sym = ck.symtable.get_string(ck.sa, &ident.name_as_string);

if let Some(sym) = sym {
Ok(sym)
} else {
let name = ident.name_as_string.clone();
let msg = ErrorMessage::UnknownIdentifier(name);
ck.sa.report(ck.file_id, ident.span, msg);

Err(())
}
}

pub(super) fn read_path(ck: &mut TypeCheck, path: &ast::PathData) -> Result<SymbolKind, ()> {
let names = &path.segments;
let mut sym = ck.symtable.get_string(
Expand Down

0 comments on commit 7ffb5fc

Please sign in to comment.