Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Issue warning for signed integers #2185

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ impl Context {

#[cfg(test)]
mod tests {
use std::{rc::Rc, collections::HashMap};
use std::{collections::HashMap, rc::Rc};

use acvm::{
acir::{
Expand Down
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