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
15 changes: 15 additions & 0 deletions compiler/noirc_frontend/src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::ast::PathSegment;
use crate::parse_program;
use crate::parser::{ParsedModule, ParsedSubModule};
use crate::signed_field::SignedField;
use crate::token::FunctionAttributeKind;
use crate::{ast, ast::Path, parser::ItemKind};
use fm::FileId;
use noirc_errors::debug_info::{DebugFnId, DebugFunction};
Expand Down Expand Up @@ -107,6 +108,20 @@ impl DebugInstrumenter {
}

fn walk_fn(&mut self, func: &mut ast::FunctionDefinition) {
// Don't instrument functions that are not supposed to have a body
if let Some((func, _)) = &func.attributes.function {
match func.kind {
FunctionAttributeKind::Foreign(_)
| FunctionAttributeKind::Builtin(_)
| FunctionAttributeKind::Oracle(_) => return,
FunctionAttributeKind::Test(..)
| FunctionAttributeKind::Fold
| FunctionAttributeKind::NoPredicates
| FunctionAttributeKind::InlineAlways
| FunctionAttributeKind::FuzzingHarness(..) => (),
}
}

let func_name = func.name.to_string();
let func_args =
func.parameters.iter().map(|param| pattern_to_string(&param.pattern)).collect();
Expand Down
18 changes: 16 additions & 2 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,22 @@ impl<'context> Elaborator<'context> {
let (hir_func, body_type) = match kind {
FunctionKind::Builtin
| FunctionKind::LowLevel
| FunctionKind::Oracle
| FunctionKind::TraitFunctionWithoutBody => (HirFunction::empty(), Type::Error),
| FunctionKind::TraitFunctionWithoutBody => {
if !body.statements.is_empty() {
panic!(
"Builtin, low-level, and trait function declarations cannot have a body"
);
}
(HirFunction::empty(), Type::Error)
}
FunctionKind::Oracle => {
if !body.statements.is_empty() {
self.push_err(ResolverError::OracleWithBody {
location: func_meta.name.location,
});
}
(HirFunction::empty(), Type::Error)
}
FunctionKind::Normal => {
let return_type = func_meta.return_type();
let (block, body_type) = self.elaborate_block(body, Some(return_type));
Expand Down
12 changes: 11 additions & 1 deletion compiler/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ pub enum ResolverError {
WildcardTypeDisallowed { location: Location },
#[error("References are not allowed in globals")]
ReferencesNotAllowedInGlobals { location: Location },
#[error("Functions marked with #[oracle] must have no body")]
OracleWithBody { location: Location },
}

impl ResolverError {
Expand Down Expand Up @@ -255,7 +257,8 @@ impl ResolverError {
| ResolverError::AssociatedItemConstraintsNotAllowedInGenerics { location }
| ResolverError::AmbiguousAssociatedType { location, .. }
| ResolverError::WildcardTypeDisallowed { location }
| ResolverError::ReferencesNotAllowedInGlobals { location } => *location,
| ResolverError::ReferencesNotAllowedInGlobals { location }
| ResolverError::OracleWithBody { location } => *location,
ResolverError::UnusedVariable { ident }
| ResolverError::UnusedItem { ident, .. }
| ResolverError::DuplicateField { field: ident }
Expand Down Expand Up @@ -808,6 +811,13 @@ impl<'a> From<&'a ResolverError> for Diagnostic {
*location,
)
}
ResolverError::OracleWithBody { location } => {
Diagnostic::simple_error(
"Functions marked with #[oracle] must have no body".to_string(),
"This function body will never be run so should be removed".to_string(),
*location,
)
}
}
}
}
14 changes: 1 addition & 13 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod enums;
mod imports;
mod metaprogramming;
mod name_shadowing;
mod oracles;
mod references;
mod traits;
mod turbofish;
Expand Down Expand Up @@ -1321,19 +1322,6 @@ fn deny_fold_attribute_on_unconstrained() {
check_errors!(src);
}

#[test]
fn deny_oracle_attribute_on_non_unconstrained() {
let src = r#"
#[oracle(foo)]
^^^^^^^^^^^^^^ Usage of the `#[oracle]` function attribute is only valid on unconstrained functions
pub fn foo(x: Field, y: Field) {
~~~ Oracle functions must have the `unconstrained` keyword applied
assert(x != y);
}
"#;
check_errors!(src);
}

#[test]
fn deny_abi_attribute_outside_of_contract() {
let src = r#"
Expand Down
26 changes: 26 additions & 0 deletions compiler/noirc_frontend/src/tests/oracles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::check_errors;

#[test]
fn deny_oracle_attribute_on_non_unconstrained() {
let src = r#"
#[oracle(foo)]
^^^^^^^^^^^^^^ Usage of the `#[oracle]` function attribute is only valid on unconstrained functions
pub fn foo(x: Field, y: Field) {
~~~ Oracle functions must have the `unconstrained` keyword applied
}
"#;
check_errors!(src);
}

#[test]
fn errors_if_oracle_declaration_has_function_body() {
let src = r#"
#[oracle(oracle_call)]
pub unconstrained fn oracle_call() {
^^^^^^^^^^^ Functions marked with #[oracle] must have no body
~~~~~~~~~~~ This function body will never be run so should be removed
assert(true);
}
"#;
check_errors!(src);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#[oracle(oracleName)]
#[builtin(builtinName)]
fn main(x: Field) -> pub Field {
x + 1
}
fn main(x: Field) -> pub Field {}
7 changes: 0 additions & 7 deletions test_programs/compile_failure/unconstrained_oracle/Nargo.toml

This file was deleted.

This file was deleted.

22 changes: 11 additions & 11 deletions tooling/ast_fuzzer/src/compare/comptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,23 @@ impl CompareComptime {
// Include the print part of stdlib for the elaborator to be able to use the print oracle
let import_print = r#"
#[oracle(print)]
unconstrained fn print_oracle<T>(with_newline: bool, input: T) {{}}
unconstrained fn print_oracle<T>(with_newline: bool, input: T) {}

unconstrained fn print_unconstrained<T>(with_newline: bool, input: T) {{
unconstrained fn print_unconstrained<T>(with_newline: bool, input: T) {
print_oracle(with_newline, input);
}}
}

pub fn println<T>(input: T) {{
unsafe {{
pub fn println<T>(input: T) {
unsafe {
print_unconstrained(true, input);
}}
}}
}
}

pub fn print<T>(input: T) {{
unsafe {{
pub fn print<T>(input: T) {
unsafe {
print_unconstrained(false, input);
}}
}}
}
}
"#;

// Add comptime modifier for main
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Loading