fix: no need to check exhaustiveness during if/is

This commit is contained in:
rvcas 2024-06-11 19:58:32 -04:00 committed by Lucas
parent 5024bd3f9c
commit 579abb7d3d
2 changed files with 13 additions and 10 deletions

View File

@ -1672,8 +1672,9 @@ pub enum Warning {
}, },
#[error( #[error(
"I found an {} that checks an expression with a known type.", "I found an {} {}",
"if/is".if_supports_color(Stderr, |s| s.purple()) "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( #[diagnostic(
code("if_is_on_non_data"), code("if_is_on_non_data"),
@ -1684,7 +1685,7 @@ pub enum Warning {
)] )]
UseWhenInstead { UseWhenInstead {
#[label( #[label(
"use {} instead", "use {}",
"when/is".if_supports_color(Stderr, |s| s.purple()) "when/is".if_supports_color(Stderr, |s| s.purple())
)] )]
location: Span, location: Span,

View File

@ -510,7 +510,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
location: _, location: _,
} = patterns.into_vec().swap_remove(0); } = 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 { UntypedExpr::Trace {
@ -1182,6 +1182,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
kind: UntypedAssignmentKind, kind: UntypedAssignmentKind,
annotation: &Option<Annotation>, annotation: &Option<Annotation>,
location: Span, location: Span,
check_exhaustiveness: bool,
) -> Result<TypedExpr, Error> { ) -> Result<TypedExpr, Error> {
let typed_value = self.infer(untyped_value.clone())?; let typed_value = self.infer(untyped_value.clone())?;
let mut value_typ = typed_value.tipo(); 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 // 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. // error we emit a warning which explains that using `expect` is unnecessary.
match kind { match kind {
AssignmentKind::Let { .. } => { AssignmentKind::Let { .. } if check_exhaustiveness => self
self.environment .environment
.check_exhaustiveness(&[&pattern], location, true)? .check_exhaustiveness(&[&pattern], location, true)?,
}
AssignmentKind::Expect { .. } => { AssignmentKind::Expect { .. } if check_exhaustiveness => {
let is_exaustive_pattern = self let is_exaustive_pattern = self
.environment .environment
.check_exhaustiveness(&[&pattern], location, false) .check_exhaustiveness(&[&pattern], location, false)
@ -1292,6 +1292,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
}); });
} }
} }
_ => (),
} }
Ok(TypedExpr::Assignment { Ok(TypedExpr::Assignment {
@ -1751,6 +1752,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
AssignmentKind::expect(), AssignmentKind::expect(),
&annotation, &annotation,
location, location,
false,
)? )?
else { else {
unreachable!() unreachable!()
@ -1758,7 +1760,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
if !value.tipo().is_data() { if !value.tipo().is_data() {
typer.environment.warnings.push(Warning::UseWhenInstead { typer.environment.warnings.push(Warning::UseWhenInstead {
location: branch.location, location: branch.condition.location().union(location),
}) })
} }