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
2 changes: 1 addition & 1 deletion .github/benchmark_projects.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define: &AZ_COMMIT ad837fe0687a4c9e58c43c7295ea31e39420060e
define: &AZ_COMMIT 42d5984235d2ffcaf5545e88e77eeb698e147416
projects:
private-kernel-inner:
repo: AztecProtocol/aztec-packages
Expand Down
2 changes: 1 addition & 1 deletion EXTERNAL_NOIR_LIBRARIES.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define: &AZ_COMMIT ad837fe0687a4c9e58c43c7295ea31e39420060e
define: &AZ_COMMIT 42d5984235d2ffcaf5545e88e77eeb698e147416
libraries:
noir_check_shuffle:
repo: noir-lang/noir_check_shuffle
Expand Down
16 changes: 15 additions & 1 deletion compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,21 @@ impl Elaborator<'_> {
match to {
Type::Integer(sign, bits) => Type::Integer(sign, bits),
Type::FieldElement => Type::FieldElement,
Type::Bool => Type::Bool,
Type::Bool => {
let from_is_numeric = match from_follow_bindings {
Type::Integer(..) | Type::FieldElement => true,
Type::TypeVariable(ref var) => var.is_integer() || var.is_integer_or_field(),
_ => false,
};
if from_is_numeric {
self.push_err(TypeCheckError::CannotCastNumericToBool {
typ: from_follow_bindings,
location,
});
}

Type::Bool
}
Type::Error => Type::Error,
_ => {
self.push_err(TypeCheckError::UnsupportedCast { location });
Expand Down
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub enum TypeCheckError {
InvalidCast { from: Type, location: Location, reason: String },
#[error("Casting value of type {from} to a smaller type ({to})")]
DownsizingCast { from: Type, to: Type, location: Location, reason: String },
#[error("Cannot cast `{typ}` as `bool`")]
CannotCastNumericToBool { typ: Type, location: Location },
#[error("Expected a function, but found a(n) {found}")]
ExpectedFunction { found: Type, location: Location },
#[error("Type {lhs_type} has no member named {field_name}")]
Expand Down Expand Up @@ -258,6 +260,7 @@ impl TypeCheckError {
| TypeCheckError::ArityMisMatch { location, .. }
| TypeCheckError::InvalidCast { location, .. }
| TypeCheckError::DownsizingCast { location, .. }
| TypeCheckError::CannotCastNumericToBool { location, .. }
| TypeCheckError::ExpectedFunction { location, .. }
| TypeCheckError::AccessUnknownMember { location, .. }
| TypeCheckError::ParameterCountMismatch { location, .. }
Expand Down Expand Up @@ -459,6 +462,10 @@ impl<'a> From<&'a TypeCheckError> for Diagnostic {
TypeCheckError::DownsizingCast { location, reason, .. } => {
Diagnostic::simple_warning(error.to_string(), reason.clone(), *location)
}
TypeCheckError::CannotCastNumericToBool { typ: _, location } => {
let secondary = "compare with zero instead: ` != 0`".to_string();
Diagnostic::simple_error(error.to_string(), secondary, *location)
}

TypeCheckError::ExpectedFunction { location, .. }
| TypeCheckError::AccessUnknownMember { location, .. }
Expand Down
18 changes: 13 additions & 5 deletions noir_stdlib/src/convert.nr
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,23 @@ comptime fn generate_as_primitive_impls(_: FunctionDefinition) -> Quoted {
let mut impls = &[];
for type1 in types {
for type2 in types {
let trait_impl = quote {
let body = if type1 == type2 {
quote { self }
} else if type1 == quote { bool } {
quote { self != 0 }
} else {
quote { self as $type1 }
};

impls = impls.push_back(
quote {
impl AsPrimitive<$type1> for $type2 {
fn as_(self) -> $type1 {
self as $type1
$body
}
}
};

impls = impls.push_back(trait_impl);
},
);
}
}
impls.join(quote {})
Expand Down
7 changes: 7 additions & 0 deletions test_programs/compile_failure/cast_numeric_to_bool/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "cast_numeric_to_bool"
type = "bin"
authors = [""]
compiler_version = ">=0.33.0"

[dependencies]
10 changes: 10 additions & 0 deletions test_programs/compile_failure/cast_numeric_to_bool/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
let x = 1;
let _ = x as bool;
Comment thread
vezenovm marked this conversation as resolved.

let x: i32 = 1;
let _ = x as bool;

let x: u64 = 1;
let _ = x as bool;
}
8 changes: 0 additions & 8 deletions test_programs/compile_success_empty/brillig_cast/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@
fn main() {
// Safety: testing context
unsafe {
bool_casts();
field_casts();
uint_casts();
int_casts();
mixed_casts();
}
}

unconstrained fn bool_casts() {
assert(false == 0 as bool);
assert(true == 1 as bool);
assert(true == 3 as bool);
}

unconstrained fn field_casts() {
assert(5 as u8 as Field == 5);
assert(256 as u8 as Field == 0);
Expand All @@ -39,6 +32,5 @@ unconstrained fn int_casts() {
unconstrained fn mixed_casts() {
assert(100 as u32 as i32 as u32 == 100);
assert(257 as u8 as u32 == 1);
assert(1 as u8 as bool == true);
assert(true as i8 == 1);
}
2 changes: 1 addition & 1 deletion test_programs/execution_success/assert/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main(x: Field) {
assert(x == 1);
assert(1 == conditional(x as bool));
assert(1 == conditional(x != 0));
}

fn conditional(x: bool) -> Field {
Expand Down
4 changes: 2 additions & 2 deletions test_programs/execution_success/brillig_not/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
fn main(x: Field, y: Field) {
// Safety: testing context
unsafe {
assert(false == not_operator(x as bool));
assert(true == not_operator(y as bool));
assert(false == not_operator(x != 0));
assert(true == not_operator(y != 0));
}
}

Expand Down
2 changes: 1 addition & 1 deletion test_programs/execution_success/merkle_insert/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn compute_merkle_root<let N: u32>(leaf: Field, index: Field, hash_path: [Field;
let index_bits: [u1; N] = index.to_le_bits();
let mut current = leaf;
for i in 0..N {
let path_bit = index_bits[i] as bool;
let path_bit = index_bits[i] != 0;
let (hash_left, hash_right) = if path_bit {
(hash_path[i], current)
} else {
Expand Down
4 changes: 2 additions & 2 deletions test_programs/execution_success/regression_8329/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
fn main(x: u1, y: u1, z: u1) -> pub u1 {
let p = y - z;
if p as bool {
if p != 0 {
let a = x / z;
let b = a - z;
if b as bool {
if b != 0 {
let _ = a / b;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test_programs/execution_success/simple_shield/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn compute_merkle_root<let N: u32>(leaf: Field, index: Field, hash_path: [Field;
let index_bits: [u1; N] = index.to_le_bits();
let mut current = leaf;
for i in 0..N {
let path_bit = index_bits[i] as bool;
let path_bit = index_bits[i] != 0;
let (hash_left, hash_right) = if path_bit {
(hash_path[i], current)
} else {
Expand Down

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

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

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

Loading
Loading