Stream: compiler development

Topic: zig compiler - `var` fields on Can structs


view this post on Zulip Jared Ramirez (Jun 26 2025 at 13:37):

When looking at Luke's last PR, I had a question on how we store type variables for numbers. It looks like the number CIR nodes contain struct fields to hold num_vars:

    num: struct {
        num_var: TypeVar,
        literal: StringLiteral.Idx,
        value: IntValue,
        bound: types.Num.Int.Precision,
        region: Region,
    },
    int: struct {
        num_var: TypeVar,
        requirements: types.Num.Int.Requirements,
        value: IntValue,
        region: Region,
    },

My question is this:

Is num_var supposed to represent the root Num(a(b)) type var, or the sub (Int(Signed8))) type var, or something else?

If it's intended to represent the root Num, then is there a reason we can't use the CIR Expr.Idx for that? Or is there something that having this as a sub-field gets us?

view this post on Zulip Jared Ramirez (Jun 26 2025 at 13:38):

This rust compiler also has this field, but I'm wondering in the Zig compilers "CIR node Idxs as Type Vars" design makes this unnecessary

view this post on Zulip Jared Ramirez (Jun 26 2025 at 13:48):

It looks like there's a similar thing on tuple as well:

const expr_idx = self.can_ir.store.addExpr(CIR.Expr{
  .tuple = .{
    .elems = elems_span,
    .tuple_var = tuple_type_var,
    .region = region,
  },
)

I think here tuple_var is meant to reference the type of whole tuple, but I think we could assign the type at expr_idx?

view this post on Zulip Anthony Bullard (Jun 26 2025 at 17:58):

i think the type var depends. For num it's the range included the type like Int(Signed8) and for int it would be whatever fills the a in Integer(a)

view this post on Zulip Richard Feldman (Jun 26 2025 at 19:02):

yeah I was thinking the same thing

view this post on Zulip Richard Feldman (Jun 26 2025 at 19:02):

I don't think that field is necessary anymore

view this post on Zulip Richard Feldman (Jun 26 2025 at 19:03):

also I have a PR that gets rid of bound and literal

view this post on Zulip Luke Boswell (Jun 26 2025 at 20:07):

We had record_var inside the record CIR.Expr and I removed that and used the expression id instead for the type of the whole record.

view this post on Zulip Luke Boswell (Jun 26 2025 at 22:13):

Richard Feldman said:

I don't think that field is necessary anymore

@Richard Feldman are you looking at removing them in your PR?

view this post on Zulip Luke Boswell (Jun 26 2025 at 22:14):

I can see you have removed the requirements field

view this post on Zulip Richard Feldman (Jun 26 2025 at 22:14):

I have not removed it

view this post on Zulip Richard Feldman (Jun 26 2025 at 22:14):

feel free to though!


Last updated: Jul 06 2025 at 12:14 UTC