Skip to content

Commit

Permalink
feat: Issue warning for signed integers (#2185)
Browse files Browse the repository at this point in the history
Issue warning for signed integers
  • Loading branch information
jfecher authored Aug 7, 2023
1 parent 08683e1 commit 1be1bcc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
9 changes: 7 additions & 2 deletions crates/noirc_frontend/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub enum ParserErrorReason {
EarlyReturn,
#[error("Patterns aren't allowed in a trait's function declarations")]
PatternInTraitFunctionParameter,
#[error("Traits are an experimental feature that are not yet in a working state")]
TraitsAreExperimental,
#[error("{0} are experimental and aren't fully supported yet")]
ExperimentalFeature(&'static str),
}

/// Represents a parsing error, or a parsing error in the making.
Expand Down Expand Up @@ -117,6 +117,11 @@ impl From<ParserError> for Diagnostic {
"The 'constrain' keyword has been deprecated. Please use the 'assert' function instead.".into(),
error.span,
),
ParserErrorReason::ExperimentalFeature(_) => Diagnostic::simple_warning(
reason.to_string(),
"".into(),
error.span,
),
reason @ ParserErrorReason::ExpectedPatternButFoundType(ty) => {
Diagnostic::simple_error(reason.to_string(), format!("{ty} is a type and cannot be used as a variable name"), error.span)
}
Expand Down
19 changes: 13 additions & 6 deletions crates/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ fn trait_definition() -> impl NoirParser<TopLevelStatement> {
.then(trait_body())
.then_ignore(just(Token::RightBrace))
.validate(|((name, generics), items), span, emit| {
emit(ParserError::with_reason(ParserErrorReason::TraitsAreExperimental, span));
emit(ParserError::with_reason(ParserErrorReason::ExperimentalFeature("Traits"), span));
TopLevelStatement::Trait(NoirTrait { name, generics, items })
})
}
Expand Down Expand Up @@ -467,7 +467,7 @@ fn trait_implementation() -> impl NoirParser<TopLevelStatement> {
let (((impl_generics, trait_name), trait_generics), (object_type, object_type_span)) =
other_args;

emit(ParserError::with_reason(ParserErrorReason::TraitsAreExperimental, span));
emit(ParserError::with_reason(ParserErrorReason::ExperimentalFeature("Traits"), span));
TopLevelStatement::TraitImpl(TraitImpl {
impl_generics,
trait_name,
Expand Down Expand Up @@ -499,7 +499,7 @@ fn where_clause() -> impl NoirParser<Vec<TraitConstraint>> {
.then(ident())
.then(generic_type_args(parse_type()))
.validate(|((typ, trait_name), trait_generics), span, emit| {
emit(ParserError::with_reason(ParserErrorReason::TraitsAreExperimental, span));
emit(ParserError::with_reason(ParserErrorReason::ExperimentalFeature("Traits"), span));
TraitConstraint { typ, trait_name, trait_generics }
});

Expand Down Expand Up @@ -878,7 +878,14 @@ fn int_type() -> impl NoirParser<UnresolvedType> {
Err(ParserError::expected_label(ParsingRuleLabel::IntegerType, unexpected, span))
}
}))
.map(UnresolvedType::from_int_token)
.validate(|token, span, emit| {
let typ = UnresolvedType::from_int_token(token);
if let UnresolvedType::Integer(_, crate::Signedness::Signed, _) = &typ {
let reason = ParserErrorReason::ExperimentalFeature("Signed integer types");
emit(ParserError::with_reason(reason, span));
}
typ
})
}

fn named_type(type_parser: impl NoirParser<UnresolvedType>) -> impl NoirParser<UnresolvedType> {
Expand Down Expand Up @@ -1530,7 +1537,7 @@ mod test {
"y[x+a]",
" foo [foo+5]",
"baz[bar]",
"foo.bar[3] as Field .baz as i32 [7]",
"foo.bar[3] as Field .baz as u32 [7]",
];
parse_all(atom_or_right_unary(expression(), expression_no_constructors(), true), valid);
}
Expand Down Expand Up @@ -1943,7 +1950,7 @@ mod test {

#[test]
fn parse_member_access() {
let cases = vec!["a.b", "a + b.c", "foo.bar as i32"];
let cases = vec!["a.b", "a + b.c", "foo.bar as u32"];
parse_all(expression(), cases);
}

Expand Down

0 comments on commit 1be1bcc

Please sign in to comment.