From 579abb7d3d784ad1d0e4e8750e726473ad5b65b2 Mon Sep 17 00:00:00 2001 From: rvcas Date: Tue, 11 Jun 2024 19:58:32 -0400 Subject: [PATCH] fix: no need to check exhaustiveness during if/is --- crates/aiken-lang/src/tipo/error.rs | 7 ++++--- crates/aiken-lang/src/tipo/expr.rs | 16 +++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/crates/aiken-lang/src/tipo/error.rs b/crates/aiken-lang/src/tipo/error.rs index bbb72dc2..dde1b294 100644 --- a/crates/aiken-lang/src/tipo/error.rs +++ b/crates/aiken-lang/src/tipo/error.rs @@ -1672,8 +1672,9 @@ pub enum Warning { }, #[error( - "I found an {} that checks an expression with a known type.", - "if/is".if_supports_color(Stderr, |s| s.purple()) + "I found an {} {}", + "if/is".if_supports_color(Stderr, |s| s.purple()), + "that checks an expression with a known type.".if_supports_color(Stderr, |s| s.yellow()) )] #[diagnostic( code("if_is_on_non_data"), @@ -1684,7 +1685,7 @@ pub enum Warning { )] UseWhenInstead { #[label( - "use {} instead", + "use {}", "when/is".if_supports_color(Stderr, |s| s.purple()) )] location: Span, diff --git a/crates/aiken-lang/src/tipo/expr.rs b/crates/aiken-lang/src/tipo/expr.rs index 9d5d3994..aeaa9327 100644 --- a/crates/aiken-lang/src/tipo/expr.rs +++ b/crates/aiken-lang/src/tipo/expr.rs @@ -510,7 +510,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> { location: _, } = patterns.into_vec().swap_remove(0); - self.infer_assignment(pattern, *value, kind, &annotation, location) + self.infer_assignment(pattern, *value, kind, &annotation, location, true) } UntypedExpr::Trace { @@ -1182,6 +1182,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> { kind: UntypedAssignmentKind, annotation: &Option, location: Span, + check_exhaustiveness: bool, ) -> Result { let typed_value = self.infer(untyped_value.clone())?; let mut value_typ = typed_value.tipo(); @@ -1239,12 +1240,11 @@ impl<'a, 'b> ExprTyper<'a, 'b> { // If `expect` is explicitly used, we still check exhaustiveness but instead of returning an // error we emit a warning which explains that using `expect` is unnecessary. match kind { - AssignmentKind::Let { .. } => { - self.environment - .check_exhaustiveness(&[&pattern], location, true)? - } + AssignmentKind::Let { .. } if check_exhaustiveness => self + .environment + .check_exhaustiveness(&[&pattern], location, true)?, - AssignmentKind::Expect { .. } => { + AssignmentKind::Expect { .. } if check_exhaustiveness => { let is_exaustive_pattern = self .environment .check_exhaustiveness(&[&pattern], location, false) @@ -1292,6 +1292,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> { }); } } + _ => (), } Ok(TypedExpr::Assignment { @@ -1751,6 +1752,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> { AssignmentKind::expect(), &annotation, location, + false, )? else { unreachable!() @@ -1758,7 +1760,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> { if !value.tipo().is_data() { typer.environment.warnings.push(Warning::UseWhenInstead { - location: branch.location, + location: branch.condition.location().union(location), }) }