Skip to content

Commit fb5136e

Browse files
author
jfecher
authored
fix: bit shifting type checking (#5824)
# Description ## Problem\* Resolves #5823 ## Summary\* We check `rhs` differently than `lhs` in left and right bit shifts in a series of hacky checks. In the case of type variables, `lhs` and `rhs` may be swapped causing the unification to be placed on lhs instead of rhs. I've reordered the cases to prevent this. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
1 parent 806af24 commit fb5136e

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

compiler/noirc_frontend/src/elaborator/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,6 @@ impl<'context> Elaborator<'context> {
10411041
// Matches on TypeVariable must be first so that we follow any type
10421042
// bindings.
10431043
(TypeVariable(int, _), other) | (other, TypeVariable(int, _)) => {
1044-
if let TypeBinding::Bound(binding) = &*int.borrow() {
1045-
return self.infix_operand_type_rules(binding, op, other, span);
1046-
}
10471044
if op.kind == BinaryOpKind::ShiftLeft || op.kind == BinaryOpKind::ShiftRight {
10481045
self.unify(
10491046
rhs_type,
@@ -1058,6 +1055,9 @@ impl<'context> Elaborator<'context> {
10581055
};
10591056
return Ok((lhs_type.clone(), use_impl));
10601057
}
1058+
if let TypeBinding::Bound(binding) = &*int.borrow() {
1059+
return self.infix_operand_type_rules(binding, op, other, span);
1060+
}
10611061
let use_impl = self.bind_type_variables_for_infix(lhs_type, op, rhs_type, span);
10621062
Ok((other.clone(), use_impl))
10631063
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "regression_5823"
3+
type = "bin"
4+
authors = [""]
5+
compiler_version = ">=0.33.0"
6+
7+
[dependencies]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let x = 1 as u64;
3+
let y = 2 as u8;
4+
assert_eq(x << y, 4);
5+
}

0 commit comments

Comments
 (0)