Skip to content
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
24 changes: 21 additions & 3 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
};
use crate::hir::resolution::errors::ResolverError;
use crate::node_interner::{ModuleAttributes, NodeInterner, ReferenceId, TypeId};
use crate::token::SecondaryAttribute;
use crate::token::{SecondaryAttribute, TestScope};
use crate::usage_tracker::{UnusedItem, UsageTracker};
use crate::{Generics, Kind, ResolvedGeneric, Type, TypeVariable};
use crate::{
Expand Down Expand Up @@ -926,7 +926,7 @@
// if it's an inline module, or the first char of a the file if it's an external module.
// - `location` will always point to the token "foo" in `mod foo` regardless of whether
// it's inline or external.
// Eventually the location put in `ModuleData` is used for codelenses about `contract`s,

Check warning on line 929 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (codelenses)
// so we keep using `location` so that it continues to work as usual.
let location = Location::new(mod_name.span(), mod_location.file);
let new_module = ModuleData::new(
Expand Down Expand Up @@ -1009,8 +1009,10 @@

let module_data = &mut def_map[module.local_id];

let is_test = function.def.attributes.is_test_function();
let is_fuzzing_harness = function.def.attributes.is_fuzzing_harness();
let test_attribute = function.def.attributes.as_test_function();
let is_test = test_attribute.is_some();
let fuzz_attribute = function.def.attributes.as_fuzzing_harness();
let is_fuzzing_harness = fuzz_attribute.is_some();
let is_entry_point_function = if module_data.is_contract {
function.attributes().is_contract_entry_point()
} else {
Expand Down Expand Up @@ -1040,6 +1042,22 @@

interner.set_doc_comments(ReferenceId::Function(func_id), doc_comments);

if let Some((test_scope, location)) = test_attribute {
if function.def.parameters.is_empty()
&& matches!(test_scope, TestScope::OnlyFailWith { .. })
{
let error = DefCollectorErrorKind::TestOnlyFailWithWithoutParameters { location };
errors.push(error.into());
}
}

if let Some((_, location)) = fuzz_attribute {
if function.def.parameters.is_empty() {
let error = DefCollectorErrorKind::FuzzingHarnessWithoutParameters { location };
errors.push(error.into());
}
}

// Add function to scope/ns of the module
let result = module_data.declare_function(name, visibility, func_id);
if let Err((first_def, second_def)) = result {
Expand Down
20 changes: 19 additions & 1 deletion compiler/noirc_frontend/src/hir/def_collector/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ pub enum DefCollectorErrorKind {
TestOnAssociatedFunction { location: Location },
#[error("The `#[export]` attribute may only be used on a non-associated function")]
ExportOnAssociatedFunction { location: Location },
#[error(
"The `#[test(only_fail_with = \"..\")]` attribute may only be used on functions with parameters"
)]
TestOnlyFailWithWithoutParameters { location: Location },
#[error("The `#[fuzz]` attribute may only be used on functions with parameters")]
FuzzingHarnessWithoutParameters { location: Location },
}

impl DefCollectorErrorKind {
Expand Down Expand Up @@ -104,7 +110,9 @@ impl DefCollectorErrorKind {
| DefCollectorErrorKind::ModuleOriginallyDefined { location, .. }
| DefCollectorErrorKind::TraitImplOrphaned { location }
| DefCollectorErrorKind::TraitMissingMethod { trait_impl_location: location, .. }
| DefCollectorErrorKind::ForeignImpl { location, .. } => *location,
| DefCollectorErrorKind::ForeignImpl { location, .. }
| DefCollectorErrorKind::TestOnlyFailWithWithoutParameters { location }
| DefCollectorErrorKind::FuzzingHarnessWithoutParameters { location } => *location,
DefCollectorErrorKind::NotATrait { not_a_trait_name: path }
| DefCollectorErrorKind::TraitNotFound { trait_path: path } => path.location,
DefCollectorErrorKind::UnsupportedNumericGenericType(
Expand Down Expand Up @@ -279,6 +287,16 @@ impl<'a> From<&'a DefCollectorErrorKind> for Diagnostic {
String::new(),
*location,
),
DefCollectorErrorKind::TestOnlyFailWithWithoutParameters { location } => Diagnostic::simple_error(
"The `#[test(only_fail_with = \"..\")]` attribute may only be used on functions with parameters".into(),
String::new(),
*location,
),
DefCollectorErrorKind::FuzzingHarnessWithoutParameters { location } => Diagnostic::simple_error(
"The `#[fuzz]` attribute may only be used on functions with parameters".into(),
String::new(),
*location,
),
}
}
}
3 changes: 2 additions & 1 deletion compiler/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ impl TestFunction {
pub fn should_fail(&self) -> bool {
match self.scope {
TestScope::ShouldFailWith { .. } => true,
TestScope::None => false,
TestScope::OnlyFailWith { .. } | TestScope::None => false,
}
}

Expand All @@ -397,6 +397,7 @@ impl TestFunction {
match &self.scope {
TestScope::None => None,
TestScope::ShouldFailWith { reason } => reason.as_deref(),
TestScope::OnlyFailWith { reason } => Some(reason.as_str()),
}
}
}
Expand Down
33 changes: 28 additions & 5 deletions compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,9 @@
/// if it fails with the specified reason. If the reason is None, then
/// the test must unconditionally fail
ShouldFailWith { reason: Option<String> },
/// If a test has a scope of OnlyFailWith, then it can only fail
/// if it fails with the specified reason.
OnlyFailWith { reason: String },
/// No scope is applied and so the test must pass
None,
}
Expand All @@ -744,11 +747,14 @@
Some(failure_reason) => write!(f, "(should_fail_with = {failure_reason:?})"),
None => write!(f, "(should_fail)"),
},
TestScope::OnlyFailWith { reason } => {
write!(f, "(only_fail_with = {reason:?})")
}
}
}
}

/// FuzzingScopr is used to specify additional annotations for fuzzing harnesses

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Scopr)
#[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)]
pub enum FuzzingScope {
/// If the fuzzing harness has a scope of ShouldFailWith, then it should only pass
Expand Down Expand Up @@ -812,14 +818,31 @@
}

pub fn is_test_function(&self) -> bool {
matches!(self.function().map(|attr| &attr.kind), Some(FunctionAttributeKind::Test(_)))
self.as_test_function().is_some()
}

pub fn as_test_function(&self) -> Option<(&TestScope, Location)> {
self.function().and_then(|attr| {
if let FunctionAttributeKind::Test(scope) = &attr.kind {
Some((scope, attr.location))
} else {
None
}
})
}

pub fn is_fuzzing_harness(&self) -> bool {
matches!(
self.function().map(|attr| &attr.kind),
Some(FunctionAttributeKind::FuzzingHarness(_))
)
self.as_fuzzing_harness().is_some()
}

pub fn as_fuzzing_harness(&self) -> Option<(&FuzzingScope, Location)> {
self.function().and_then(|attr| {
if let FunctionAttributeKind::FuzzingHarness(scope) = &attr.kind {
Some((scope, attr.location))
} else {
None
}
})
}

/// True if these attributes mean the given function is an entry point function if it was
Expand Down
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/parser/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum ParsingRuleLabel {
Path,
Pattern,
Statement,
String,
Term,
TraitBound,
TraitImplItem,
Expand Down Expand Up @@ -54,6 +55,7 @@ impl fmt::Display for ParsingRuleLabel {
ParsingRuleLabel::Path => write!(f, "path"),
ParsingRuleLabel::Pattern => write!(f, "pattern"),
ParsingRuleLabel::Statement => write!(f, "statement"),
ParsingRuleLabel::String => write!(f, "string"),
ParsingRuleLabel::Term => write!(f, "term"),
ParsingRuleLabel::TraitBound => write!(f, "trait bound"),
ParsingRuleLabel::TraitImplItem => write!(f, "trait impl item"),
Expand Down
4 changes: 4 additions & 0 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,10 @@ impl<'a> Parser<'a> {
self.expected_label(ParsingRuleLabel::Identifier);
}

fn expected_string(&mut self) {
self.expected_label(ParsingRuleLabel::String);
}

fn expected_token(&mut self, token: Token) {
self.errors.push(ParserError::expected_token(
token,
Expand Down
17 changes: 17 additions & 0 deletions compiler/noirc_frontend/src/parser/parser/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ impl Parser<'_> {
Some(TestScope::ShouldFailWith { reason: None })
}
}
"only_fail_with" => {
self.eat_or_error(Token::Assign);
if let Some(reason) = self.eat_str() {
Some(TestScope::OnlyFailWith { reason })
} else {
self.expected_string();
None
}
}
_ => None,
}
} else {
Expand Down Expand Up @@ -687,6 +696,14 @@ mod tests {
parse_function_attribute_no_errors(src, expected);
}

#[test]
fn parses_attribute_test_only_fail_with() {
let src = "#[test(only_fail_with = \"reason\")]";
let reason = "reason".to_string();
let expected = FunctionAttributeKind::Test(TestScope::OnlyFailWith { reason });
parse_function_attribute_no_errors(src, expected);
}

#[test]
fn parses_meta_attribute_single_identifier_no_arguments() {
let src = "#[foo]";
Expand Down
11 changes: 9 additions & 2 deletions docs/docs/tooling/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn test_basic(a: Field, b: Field) {
}
```
The test above is not expected to fail. By default, the fuzzer will run for 1 second and use 100000 executions (whichever comes first). All available threads will be used for each fuzz test.
The fuzz tests also work with `#[test(should_fail)]` and `#[test(should_fail_with = "<the reason for failure>")]`. For example:
The fuzz tests also work with `#[test(should_fail)]`, `#[test(should_fail_with = "<the reason for failure>")]` and `#[test(only_fail_with = "<the reason for failure>")]`. For example:

```rust
#[test(should_fail)]
Expand All @@ -105,7 +105,7 @@ or

```rust
#[test(should_fail_with = "This is the message that will be checked for")]
fn fuzz_should_fail_with(a: [bool; 32]) {
fn test_should_fail_with(a: [bool; 32]) {
let mut or_sum= false;
for i in 0..32 {
or_sum=or_sum|(a[i]==((i&1)as bool));
Expand All @@ -115,6 +115,13 @@ fn fuzz_should_fail_with(a: [bool; 32]) {
}
```

```rust
#[test(only_fail_with = "This is the message that will be checked for")]
fn test_add(a: u64, b: u64) {
assert((a+b-15)!=(a-b+30), "This is the message that will be checked for");
}
```

The underlying fuzzing mechanism is described in the [Fuzzing](../tooling/fuzzing) documentation.

There are some fuzzing-specific options that can be used with `nargo test`:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_and_fuzz_attribute_errors"
type = "bin"
authors = [""]
compiler_version = ">=0.26.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This is an error as we can't fuzz if there are no arguments
#[fuzz]
fn fuzz_no_arguments() {}

// This is an error as `only_fail_with` implies fuzzing, but there are no arguments
#[test(only_fail_with = "error")]
fn test() {}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: The `#[fuzz]` attribute may only be used on functions with parameters
┌─ src/main.nr:2:1
2 │ #[fuzz]
│ -------

error: The `#[test(only_fail_with = "..")]` attribute may only be used on functions with parameters
┌─ src/main.nr:6:1
6 │ #[test(only_fail_with = "error")]
│ ---------------------------------

Aborting due to 2 previous errors
1 change: 1 addition & 0 deletions tooling/nargo/src/ops/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ where
TestScope::ShouldFailWith { reason } => {
FuzzingScope::ShouldFailWith { reason: reason.clone() }
}
TestScope::OnlyFailWith { reason } => FuzzingScope::OnlyFailWith { reason: reason.clone() },
TestScope::None => FuzzingScope::None,
};
let location = test_function.location;
Expand Down
13 changes: 10 additions & 3 deletions tooling/nargo_fmt/src/formatter/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ impl Formatter<'_> {
self.write_current_token_and_bump(); // should_fail
self.write_right_paren(); // )
}
TestScope::ShouldFailWith { reason: Some(..) } => {
TestScope::ShouldFailWith { reason: Some(..) } | TestScope::OnlyFailWith { .. } => {
self.write_left_paren(); // (
self.skip_comments_and_whitespace();
self.write_current_token_and_bump(); // should_fail_with
self.write_current_token_and_bump(); // should_fail_with | only_fail_with
self.write_space();
self.write_token(Token::Assign);
self.write_space();
Expand Down Expand Up @@ -389,6 +389,13 @@ mod tests {
assert_format_attribute(src, expected);
}

#[test]
fn format_test_only_fail_with_reason_attribute() {
let src = " #[ test ( only_fail_with=\"reason\" )] ";
let expected = "#[test(only_fail_with = \"reason\")]";
assert_format_attribute(src, expected);
}

#[test]
fn format_fuzz_attribute() {
let src = " #[ fuzz ] ";
Expand All @@ -397,7 +404,7 @@ mod tests {
}

#[test]
fn format_test_only_fail_with_reason_attribute() {
fn format_fuzz_only_fail_with_reason_attribute() {
let src = " #[ fuzz ( only_fail_with=\"reason\" )] ";
let expected = "#[fuzz(only_fail_with = \"reason\")]";
assert_format_attribute(src, expected);
Expand Down
Loading