change #[hdl] if to only accept Expr<T> instead of accepting bool, this prevents using == by accident

This commit is contained in:
Jacob Lifshay 2024-07-22 00:52:48 -07:00
parent 79031ccf88
commit 37d03cec33
Signed by: programmerjake
SSH key fingerprint: SHA256:B1iRVvUJkvd7upMIiMqn6OyxvD2SgJkAH3ZnUOj6z+c

View file

@ -11,7 +11,7 @@ use crate::{
ops::VariantAccess, Expr, Flow, Target, TargetBase, TargetPathArrayElement, ops::VariantAccess, Expr, Flow, Target, TargetBase, TargetPathArrayElement,
TargetPathBundleField, TargetPathElement, ToExpr, TargetPathBundleField, TargetPathElement, ToExpr,
}, },
int::{DynUInt, DynUIntType, FixedOrDynIntType, UInt}, int::{DynUInt, DynUIntType, FixedOrDynIntType, IntValue, UInt},
intern::{Intern, Interned}, intern::{Intern, Interned},
memory::{Mem, MemBuilder, MemBuilderTarget, PortName}, memory::{Mem, MemBuilder, MemBuilderTarget, PortName},
reg::Reg, reg::Reg,
@ -2259,8 +2259,10 @@ where
) -> RegBuilder<'a, (), (), ()> { ) -> RegBuilder<'a, (), (), ()> {
self.reg_builder_with_loc(implicit_name.0, SourceLocation::caller()) self.reg_builder_with_loc(implicit_name.0, SourceLocation::caller())
} }
// intentionally takes Expr instead of impl ToExpr to help prevent
// `#[hdl] if a == b {}` instead of using `cmp_eq`
#[track_caller] #[track_caller]
pub fn if_<Ty>(&mut self, cond: impl ToExpr<Type = Ty>) -> IfScope pub fn if_<Ty>(&mut self, cond: Expr<IntValue<Ty>>) -> IfScope
where where
Ty: FixedOrDynIntType< Ty: FixedOrDynIntType<
1, 1,
@ -2273,7 +2275,7 @@ where
} }
pub fn if_with_loc<Ty>( pub fn if_with_loc<Ty>(
&mut self, &mut self,
cond: impl ToExpr<Type = Ty>, cond: Expr<IntValue<Ty>>,
source_location: SourceLocation, source_location: SourceLocation,
) -> IfScope ) -> IfScope
where where
@ -2284,7 +2286,7 @@ where
CanonicalValue = DynUInt, CanonicalValue = DynUInt,
>, >,
{ {
let cond = cond.to_expr().as_bool(); let cond = cond.as_bool();
let outer_block = self.block_stack.top(); let outer_block = self.block_stack.top();
let then_block = self.new_block(); let then_block = self.new_block();
let else_block = self.new_block(); let else_block = self.new_block();