Hdl21 icon indicating copy to clipboard operation
Hdl21 copied to clipboard

`BundleInstance.__setattr__` (and maybe a few others?) should probably fail

Open dan-fritchman opened this issue 1 year ago • 4 comments

Some new-user intuition about how to use Bundles:

import hdl21 as h 

@h.module
class Inner:
    d = h.Diff(port=True)

@h.module
class Outer:
    # Make an `Inner` instance, and a `Diff` connected to it
    d = h.Diff()
    i = Inner(d=d)

    # This is the problem part here: 
    x, y = 2 * h.Signal()
    d.p = x 
    d.n = y

    # And connect Signals `x` and `y` to other stuff 
    v = h.Vdc()(p=x, n=y)

Which, I get why that might be your intuition. We allow LHS-assignment/ __setattr__ of Instance-ports; bundles probably seem kinda similar.

But they're not. And that produces a netlist like so:

subckt Outer 
  + // No ports 
  + // No parameters 
  
  i
  + // Ports: 
  + ( d_p d_n )
  +  Inner 
  + // No parameters 
  
  v
  + // Ports: 
  + ( x y )
  +  vsource 
  +  dc=0 type=dc mag=0 

Which is bad in that:

  • (a) x and y don't connect to the d signals at all, and
  • (b) We didn't offer any indication this looked like a problem

All that's happening: BundleInstance.__setattr__ is just the "regular" object.__setattr__. It stores fields named p and n, which are Signals, which never get looked at again.

This should probably fail at __setattr__. There may be some other "struct-like" types on which we want to add similar behavior; Slice and Concat come to mind.

dan-fritchman avatar Jun 13 '23 17:06 dan-fritchman