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

fix: keep the correct type for bitshift #2739

Merged
merged 1 commit into from
Sep 18, 2023
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: 2 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate::ssa::ir::value::ValueId;

use super::value::{Tree, Value, Values};
use fxhash::FxHashMap as HashMap;

Check warning on line 21 in compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (fxhash)

/// The FunctionContext is the main context object for translating a
/// function into SSA form during the SSA-gen pass.
Expand Down Expand Up @@ -244,6 +244,8 @@
fn insert_shift_left(&mut self, lhs: ValueId, rhs: ValueId) -> ValueId {
let base = self.builder.field_constant(FieldElement::from(2_u128));
let pow = self.pow(base, rhs);
let typ = self.builder.current_function.dfg.type_of_value(lhs);
let pow = self.builder.insert_cast(pow, typ);
self.builder.insert_binary(lhs, BinaryOp::Mul, pow)
}

Expand Down Expand Up @@ -859,7 +861,7 @@
/// and return this new id.
pub(super) fn get_or_queue_function(&self, id: ast::FuncId) -> IrFunctionId {
// Start a new block to guarantee the destructor for the map lock is released
// before map needs to be aquired again in self.functions.write() below

Check warning on line 864 in compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (aquired)
{
let map = self.functions.read().expect("Failed to read self.functions");
if let Some(existing_id) = map.get(&id) {
Expand Down
30 changes: 30 additions & 0 deletions tooling/nargo_cli/tests/execution_success/regression/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ fn enc<N>(value: [u8; N], value_length: Field) -> ([u8; 32], Field)
}
}

fn bitshift_literal_0() -> u64 {
let mut bits: u64 = 0;
bits |= 1 << 0;

bits
}
fn bitshift_literal_4() -> u64 {
let mut bits: u64 = 0;
bits |= 1 << 4;

bits
}
fn bitshift_variable(idx: u64) -> u64 {
let mut bits: u64 = 0;
bits |= 1 << idx;

bits
}


fn main(x: [u8; 5], z: Field)
{
//Issue 1144
Expand All @@ -87,4 +107,14 @@ fn main(x: [u8; 5], z: Field)
assert(enc_val1.0 == [0x94,0xb8,0x8f,0x61,0xe6,0xfb,0xda,0x83,0xfb,0xff,0xfa,0xbe,0x36,0x41,0x12,0x13,0x74,0x80,0x39,0x80,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]);
assert(enc_val1.1 == 21);

// Issue 2399
let result_0 = bitshift_literal_0();
assert(result_0 == 1);
let result_4 = bitshift_literal_4();
assert(result_4 == 16);
let result_0 = bitshift_variable(0);
assert(result_0 == 1);
let result_4 = bitshift_variable(4);
assert(result_4 == 16);

}