Skip to content

Commit 94c0bd8

Browse files
committed
validate for braces
1 parent fd4cdcf commit 94c0bd8

2 files changed

Lines changed: 36 additions & 17 deletions

File tree

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ pub enum Error {
437437
ModuleRedefined(ModuleName),
438438
ArgumentMissing(WitnessName),
439439
ArgumentTypeMismatch(WitnessName, ResolvedType, ResolvedType),
440+
IfThenArmMissingBraces,
441+
IfElseArmMissingBraces,
440442
}
441443

442444
#[rustfmt::skip]
@@ -582,6 +584,8 @@ impl fmt::Display for Error {
582584
f,
583585
"Parameter `{name}` was declared with type `{declared}` but its assigned argument is of type `{assigned}`"
584586
),
587+
Error::IfThenArmMissingBraces | Error::IfElseArmMissingBraces => write!(f, "If statement must have enclosing braces")
588+
585589
}
586590
}
587591
}

src/parse.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ impl Expression {
288288
span,
289289
}
290290
}
291+
292+
pub fn is_single(&self) -> bool {
293+
match &self.inner {
294+
ExpressionInner::Block(..) => false,
295+
ExpressionInner::Single(..) => true,
296+
}
297+
}
298+
299+
pub fn is_block(&self) -> bool {
300+
match &self.inner {
301+
ExpressionInner::Block(..) => true,
302+
ExpressionInner::Single(..) => false,
303+
}
304+
}
291305
}
292306

293307
impl_eq_hash!(Expression; inner);
@@ -1828,29 +1842,30 @@ impl If {
18281842
{
18291843
let scrutinee = expr.clone().map(Arc::new);
18301844

1831-
let true_arm = delimited_with_recovery(
1832-
expr.clone().map(Arc::new),
1833-
Token::LBrace,
1834-
Token::RBrace,
1835-
|span| Arc::new(Expression::empty(span)),
1836-
);
1837-
let false_arm = delimited_with_recovery(
1838-
expr.clone().map(Arc::new),
1839-
Token::LBrace,
1840-
Token::RBrace,
1841-
|span| Arc::new(Expression::empty(span)),
1842-
);
1845+
let true_arm = expr.clone().map(Arc::new);
1846+
let false_arm = expr.clone().map(Arc::new);
18431847

18441848
just(Token::If)
18451849
.ignore_then(scrutinee)
18461850
.then(true_arm)
18471851
.then_ignore(just(Token::Else))
18481852
.then(false_arm)
1849-
.map_with(|((s, t), el), extra| Self {
1850-
scrutinee: s,
1851-
then_arm: t,
1852-
else_arm: el,
1853-
span: extra.span(),
1853+
.validate(|((scrutinee, then), else_), extra, emit| {
1854+
// Do not allow single statements, only blocks. I.E. `then` and `else` arms must be
1855+
// wrapped in braces.
1856+
if !then.is_block() {
1857+
emit.emit(Error::IfThenArmMissingBraces.with_span(extra.span()));
1858+
}
1859+
if !else_.is_block() {
1860+
emit.emit(Error::IfElseArmMissingBraces.with_span(extra.span()))
1861+
}
1862+
1863+
Self {
1864+
scrutinee,
1865+
then_arm: then,
1866+
else_arm: else_,
1867+
span: extra.span(),
1868+
}
18541869
})
18551870
}
18561871
}

0 commit comments

Comments
 (0)