From 0458f0a40476e5f89db95dad7323fd8e0e051962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Capucho?= Date: Wed, 27 Oct 2021 02:33:49 +0100 Subject: [PATCH] [valid] Check that the last switch case isn't falltrough (#1485) --- src/valid/function.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/valid/function.rs b/src/valid/function.rs index 14068e3e4..70980e125 100644 --- a/src/valid/function.rs +++ b/src/valid/function.rs @@ -108,6 +108,8 @@ pub enum FunctionError { ConflictingSwitchCase(i32), #[error("Multiple `default` cases are present")] MultipleDefaultCases, + #[error("The last `switch` case contains a `falltrough`")] + LastCaseFallTrough, #[error("The pointer {0:?} doesn't relate to a valid destination for a store")] InvalidStorePointer(Handle), #[error("The value {0:?} can not be stored")] @@ -469,6 +471,17 @@ impl super::Validator { } } } + if let Some(case) = cases.last() { + if case.fall_through { + return Err(FunctionError::LastCaseFallTrough.with_span_static( + case.body + .span_iter() + .next() + .map_or(Default::default(), |(_, s)| *s), + "bad switch arm here", + )); + } + } let pass_through_abilities = context.abilities & (ControlFlowAbility::RETURN | ControlFlowAbility::CONTINUE); let sub_context =