Skip to content

Commit

Permalink
fix: bit shifting type checking (#5824)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
jfecher authored Aug 26, 2024
1 parent 806af24 commit fb5136e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,6 @@ impl<'context> Elaborator<'context> {
// Matches on TypeVariable must be first so that we follow any type
// bindings.
(TypeVariable(int, _), other) | (other, TypeVariable(int, _)) => {
if let TypeBinding::Bound(binding) = &*int.borrow() {
return self.infix_operand_type_rules(binding, op, other, span);
}
if op.kind == BinaryOpKind::ShiftLeft || op.kind == BinaryOpKind::ShiftRight {
self.unify(
rhs_type,
Expand All @@ -1058,6 +1055,9 @@ impl<'context> Elaborator<'context> {
};
return Ok((lhs_type.clone(), use_impl));
}
if let TypeBinding::Bound(binding) = &*int.borrow() {
return self.infix_operand_type_rules(binding, op, other, span);
}
let use_impl = self.bind_type_variables_for_infix(lhs_type, op, rhs_type, span);
Ok((other.clone(), use_impl))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "regression_5823"
type = "bin"
authors = [""]
compiler_version = ">=0.33.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let x = 1 as u64;
let y = 2 as u8;
assert_eq(x << y, 4);
}

0 comments on commit fb5136e

Please sign in to comment.