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: improve malformed test attribute error #6414

Merged
merged 3 commits into from
Oct 30, 2024
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
8 changes: 8 additions & 0 deletions compiler/noirc_frontend/src/lexer/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub enum LexerErrorKind {
IntegerLiteralTooLarge { span: Span, limit: String },
#[error("{:?} is not a valid attribute", found)]
MalformedFuncAttribute { span: Span, found: String },
#[error("Malformed test attribute")]
MalformedTestAttribute { span: Span },
#[error("{:?} is not a valid inner attribute", found)]
InvalidInnerAttribute { span: Span, found: String },
#[error("Logical and used instead of bitwise and")]
Expand Down Expand Up @@ -61,6 +63,7 @@ impl LexerErrorKind {
LexerErrorKind::InvalidIntegerLiteral { span, .. } => *span,
LexerErrorKind::IntegerLiteralTooLarge { span, .. } => *span,
LexerErrorKind::MalformedFuncAttribute { span, .. } => *span,
LexerErrorKind::MalformedTestAttribute { span, .. } => *span,
LexerErrorKind::InvalidInnerAttribute { span, .. } => *span,
LexerErrorKind::LogicalAnd { span } => *span,
LexerErrorKind::UnterminatedBlockComment { span } => *span,
Expand Down Expand Up @@ -109,6 +112,11 @@ impl LexerErrorKind {
format!(" {found} is not a valid attribute"),
*span,
),
LexerErrorKind::MalformedTestAttribute { span } => (
"Malformed test attribute".to_string(),
"The test attribute can be written in one of these forms: `#[test]`, `#[test(should_fail)]` or `#[test(should_fail_with = \"message\")]`".to_string(),
*span,
),
LexerErrorKind::InvalidInnerAttribute { span, found } => (
"Invalid inner attribute".to_string(),
format!(" {found} is not a valid inner attribute"),
Expand Down
8 changes: 1 addition & 7 deletions compiler/noirc_frontend/src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
done: false,
skip_comments: true,
skip_whitespaces: true,
max_integer: BigInt::from_biguint(num_bigint::Sign::Plus, FieldElement::modulus())

Check warning on line 50 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (biguint)
- BigInt::one(),
}
}
Expand Down Expand Up @@ -935,13 +935,7 @@
Err(err) => err,
};

// Check if error is MalformedFuncAttribute and found is "foo"
let sub_string = match err {
LexerErrorKind::MalformedFuncAttribute { found, .. } => found,
_ => panic!("expected malformed func attribute error"),
};

assert_eq!(sub_string, "test(invalid_scope)");
assert!(matches!(err, LexerErrorKind::MalformedTestAttribute { .. }));
}

#[test]
Expand Down Expand Up @@ -1279,7 +1273,7 @@
// (expected_token_discriminator, strings_to_lex)
// expected_token_discriminator matches a given token when
// std::mem::discriminant returns the same discriminant for both.
fn blns_base64_to_statements(base64_str: String) -> Vec<(Option<Token>, Vec<String>)> {

Check warning on line 1276 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (blns)
use base64::engine::general_purpose;
use std::borrow::Cow;
use std::io::Cursor;
Expand Down Expand Up @@ -1337,13 +1331,13 @@
fn test_big_list_of_naughty_strings() {
use std::mem::discriminant;

let blns_contents = include_str!("./blns/blns.base64.json");

Check warning on line 1334 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (blns)

Check warning on line 1334 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (blns)

Check warning on line 1334 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (blns)
let blns_base64: Vec<String> =

Check warning on line 1335 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (blns)
serde_json::from_str(blns_contents).expect("BLNS json invalid");

Check warning on line 1336 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (BLNS)
for blns_base64_str in blns_base64 {
let statements = blns_base64_to_statements(blns_base64_str);
for (token_discriminator_opt, blns_program_strs) in statements {

Check warning on line 1339 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (strs)
for blns_program_str in blns_program_strs {

Check warning on line 1340 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (strs)
let mut expected_token_found = false;
let mut lexer = Lexer::new(&blns_program_str);
let mut result_tokens = Vec::new();
Expand Down Expand Up @@ -1429,7 +1423,7 @@

#[test]
fn test_non_ascii_comments() {
let cases = vec!["// 🙂", "// schön", "/* in the middle 🙂 of a comment */"];

Check warning on line 1426 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (schön)

for source in cases {
let mut lexer = Lexer::new(source);
Expand Down
20 changes: 13 additions & 7 deletions compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,18 @@ impl Attribute {
contents_span: Span,
is_tag: bool,
) -> Result<Attribute, LexerErrorKind> {
let word_segments: Vec<&str> = word
.split(|c| c == '(' || c == ')')
.filter(|string_segment| !string_segment.is_empty())
.collect();
// See if we can parse the word into "name ( contents )".
// We first split into "first_segment ( rest".
let word_segments = if let Some((first_segment, rest)) = word.trim().split_once('(') {
// Now we try to remove the final ")" (it must be at the end, if it exists)
if let Some(middle) = rest.strip_suffix(')') {
vec![first_segment.trim(), middle.trim()]
} else {
vec![word]
}
} else {
vec![word]
};

let validate = |slice: &str| {
let is_valid = slice
Expand Down Expand Up @@ -799,11 +807,9 @@ impl Attribute {
["inline_always"] => Attribute::Function(FunctionAttribute::InlineAlways),
["test", name] => {
validate(name)?;
let malformed_scope =
LexerErrorKind::MalformedFuncAttribute { span, found: word.to_owned() };
match TestScope::lookup_str(name) {
Some(scope) => Attribute::Function(FunctionAttribute::Test(scope)),
None => return Err(malformed_scope),
None => return Err(LexerErrorKind::MalformedTestAttribute { span }),
}
}
["field", name] => {
Expand Down
9 changes: 9 additions & 0 deletions tooling/lsp/src/requests/completion/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ impl<'a> NodeFinder<'a> {
let one_argument_attributes = &["abi", "field", "foreign", "oracle"];
self.suggest_one_argument_attributes(prefix, one_argument_attributes);

if name_matches("test", prefix) || name_matches("should_fail", prefix) {
self.completion_items.push(snippet_completion_item(
"test(should_fail)",
CompletionItemKind::METHOD,
"test(should_fail)",
None,
));
}

if name_matches("test", prefix) || name_matches("should_fail_with", prefix) {
self.completion_items.push(snippet_completion_item(
"test(should_fail_with = \"...\")",
Expand Down
Loading