Skip to content

Commit a992dd6

Browse files
committed
flag for skipping argument type checking
1 parent 6904fe8 commit a992dd6

File tree

12 files changed

+28
-27
lines changed

12 files changed

+28
-27
lines changed

src/circuit_writer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<B: Backend> CircuitWriter<B> {
142142
let main_fn_info = circuit_writer.main_info()?;
143143

144144
let function = match &main_fn_info.kind {
145-
crate::imports::FnKind::BuiltIn(_, _) => unreachable!(),
145+
crate::imports::FnKind::BuiltIn(_, _, _) => unreachable!(),
146146
crate::imports::FnKind::Native(fn_sig) => fn_sig.clone(),
147147
};
148148

src/circuit_writer/writer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ impl<B: Backend> CircuitWriter<B> {
424424

425425
let res = match &fn_info.kind {
426426
// assert() <-- for example
427-
FnKind::BuiltIn(sig, handle) => {
427+
FnKind::BuiltIn(sig, handle, _) => {
428428
let res = handle(self, &sig.generics, &vars, expr.span);
429429
res.map(|r| r.map(VarOrRef::Var))
430430
}

src/imports.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ where
7474
B: Backend,
7575
{
7676
/// A built-in is just a handle to a function written in Rust.
77-
BuiltIn(FnSig, FnHandle<B>),
77+
/// The boolean flag indicates whether to skip argument type checking for builtins
78+
BuiltIn(FnSig, FnHandle<B>, bool),
7879

7980
/// A native function is represented as an AST.
8081
Native(FunctionDef),
@@ -96,7 +97,7 @@ mod fn_kind_serde {
9697
S: Serializer,
9798
{
9899
let surrogate = match self {
99-
FnKind::BuiltIn(sig, _handle) => {
100+
FnKind::BuiltIn(sig, _handle, _) => {
100101
FnKindSurrogate::BuiltIn(sig.clone(), "native function".to_string())
101102
}
102103
FnKind::Native(def) => FnKindSurrogate::Native(def.clone()),

src/mast/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<B: Backend> FnInfo<B> {
188188
ctx: &mut MastCtx<B>,
189189
) -> Result<FnSig> {
190190
match self.kind {
191-
FnKind::BuiltIn(ref mut sig, _) => {
191+
FnKind::BuiltIn(ref mut sig, _, _) => {
192192
sig.resolve_generic_values(observed_args, ctx)?;
193193
}
194194
FnKind::Native(ref mut func) => {
@@ -1363,9 +1363,9 @@ pub fn instantiate_fn_call<B: Backend>(
13631363

13641364
// construct the monomorphized function AST
13651365
let (func_def, mono_info) = match fn_info.kind {
1366-
FnKind::BuiltIn(_, handle) => (
1366+
FnKind::BuiltIn(_, handle, ignore_arg_types) => (
13671367
FnInfo {
1368-
kind: FnKind::BuiltIn(sig_typed, handle),
1368+
kind: FnKind::BuiltIn(sig_typed, handle, ignore_arg_types),
13691369
..fn_info
13701370
},
13711371
// todo: we will need to propagate the constant value from builtin function as well

src/negative_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ fn test_hint_builtin_fn(qualified: &FullyQualified, code: &str) -> Result<usize>
623623
}
624624

625625
let fn_info = FnInfo {
626-
kind: FnKind::BuiltIn(sig, mocked_builtin_fn::<R1csBackend>),
626+
kind: FnKind::BuiltIn(sig, mocked_builtin_fn::<R1csBackend>, false),
627627
is_hint: true,
628628
span: Span::default(),
629629
};

src/stdlib/bits.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ pub struct BitsLib {}
2222
impl Module for BitsLib {
2323
const MODULE: &'static str = "bits";
2424

25-
fn get_fns<B: Backend>() -> Vec<(&'static str, FnInfoType<B>)> {
25+
fn get_fns<B: Backend>() -> Vec<(&'static str, FnInfoType<B>, bool)> {
2626
vec![
27-
(NTH_BIT_FN, nth_bit),
28-
(CHECK_FIELD_SIZE_FN, check_field_size),
27+
(NTH_BIT_FN, nth_bit, false),
28+
(CHECK_FIELD_SIZE_FN, check_field_size, false),
2929
]
3030
}
3131
}

src/stdlib/builtins.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,19 @@ pub const BUILTIN_FN_NAMES: [&str; 3] = ["assert", "assert_eq", "log"];
2323

2424
const ASSERT_FN: &str = "assert(condition: Bool)";
2525
const ASSERT_EQ_FN: &str = "assert_eq(lhs: Field, rhs: Field)";
26-
// todo: currently only supports a single field var
27-
// to support all the types, we can bypass the type check for this log function for now
2826
const LOG_FN: &str = "log(var: Field)";
2927

3028
pub struct BuiltinsLib {}
3129

3230
impl Module for BuiltinsLib {
3331
const MODULE: &'static str = "builtins";
3432

35-
fn get_fns<B: Backend>() -> Vec<(&'static str, FnInfoType<B>)> {
33+
fn get_fns<B: Backend>() -> Vec<(&'static str, FnInfoType<B>, bool)> {
3634
vec![
37-
(ASSERT_FN, assert_fn),
38-
(ASSERT_EQ_FN, assert_eq_fn),
39-
(LOG_FN, log_fn),
35+
(ASSERT_FN, assert_fn, false),
36+
(ASSERT_EQ_FN, assert_eq_fn, false),
37+
// true -> skip argument type checking for log
38+
(LOG_FN, log_fn, true),
4039
]
4140
}
4241
}

src/stdlib/crypto.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub struct CryptoLib {}
88
impl Module for CryptoLib {
99
const MODULE: &'static str = "crypto";
1010

11-
fn get_fns<B: Backend>() -> Vec<(&'static str, FnInfoType<B>)> {
12-
vec![(POSEIDON_FN, B::poseidon())]
11+
fn get_fns<B: Backend>() -> Vec<(&'static str, FnInfoType<B>, bool)> {
12+
vec![(POSEIDON_FN, B::poseidon(), false)]
1313
}
1414
}

src/stdlib/int.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub struct IntLib {}
2020
impl Module for IntLib {
2121
const MODULE: &'static str = "int";
2222

23-
fn get_fns<B: Backend>() -> Vec<(&'static str, FnInfoType<B>)> {
24-
vec![(DIVMOD_FN, divmod_fn)]
23+
fn get_fns<B: Backend>() -> Vec<(&'static str, FnInfoType<B>, bool)> {
24+
vec![(DIVMOD_FN, divmod_fn, false)]
2525
}
2626
}
2727

src/stdlib/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@ trait Module {
7171
/// e.g. "crypto"
7272
const MODULE: &'static str;
7373

74-
fn get_fns<B: Backend>() -> Vec<(&'static str, FnInfoType<B>)>;
74+
fn get_fns<B: Backend>() -> Vec<(&'static str, FnInfoType<B>, bool)>;
7575

7676
fn get_parsed_fns<B: Backend>() -> Vec<FnInfo<B>> {
7777
let fns = Self::get_fns();
7878
let mut res = Vec::with_capacity(fns.len());
79-
for (code, fn_handle) in fns {
79+
for (code, fn_handle, ignore_arg_types) in fns {
8080
let ctx = &mut ParserCtx::default();
8181
// TODO: we should try to point to real noname files here (not 0)
8282
let mut tokens = Token::parse(0, code).unwrap();
8383
let sig = FnSig::parse(ctx, &mut tokens).unwrap();
8484
res.push(FnInfo {
85-
kind: FnKind::BuiltIn(sig, fn_handle),
85+
kind: FnKind::BuiltIn(sig, fn_handle, ignore_arg_types),
8686
is_hint: false,
8787
span: Span::default(),
8888
});

src/type_checker/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<B: Backend> TypeChecker<B> {
377377

378378
// check it is a builtin function
379379
let fn_handle = match builtin_fn {
380-
FnKind::BuiltIn(_, fn_handle) => fn_handle,
380+
FnKind::BuiltIn(_, fn_handle, _) => fn_handle,
381381
_ => {
382382
return Err(Error::new(
383383
"type-checker",
@@ -392,7 +392,8 @@ impl<B: Backend> TypeChecker<B> {
392392
qualified,
393393
FnInfo {
394394
is_hint: true,
395-
kind: FnKind::BuiltIn(function.sig.clone(), fn_handle),
395+
// todo: is there a case where we want to ignore argument types for hint functions?
396+
kind: FnKind::BuiltIn(function.sig.clone(), fn_handle, false),
396397
span: function.span,
397398
},
398399
);

src/witness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<B: Backend> CompiledCircuit<B> {
6666
// get info on main
6767
let main_info = self.main_info();
6868
let main_sig = match &main_info.kind {
69-
crate::imports::FnKind::BuiltIn(_, _) => unreachable!(),
69+
crate::imports::FnKind::BuiltIn(_, _, _) => unreachable!(),
7070
crate::imports::FnKind::Native(fn_sig) => &fn_sig.sig,
7171
};
7272

0 commit comments

Comments
 (0)