8000 codegen: `iterate` over a slice whose elements are not `base.u8` fails with "cannot convert Wuffs type" · Issue #259 · google/wuffs · GitHub
[go: up one dir, main page]

Skip to content

codegen: iterate over a slice whose elements are not base.u8 fails with "cannot convert Wuffs type" #259

Open

Description

@dio-gh

Hi,

General context of this work is in #188, including the AI-disclaimers.

No in-tree codec is affected by this issue; std never declares a variable or argument of a slice type other than slice base.u8, and never iterates over one. Claude judges the prospective risk of it as "low": generation fails, so the accepted program cannot be built.

The following is Sol 6.1's writeup of the issue. Includes a repro, the generation error, the same error for seven more element types, parameter-only and read-only-slice variants, and a base.u8 control that generates. The cause is reasoned from source.


The checker accepts the non-base.u8 slice types below and iterate over them, but cgen has a C type only for slices and tables of unrefined base.u8. A parameter or local of an unsupported slice type is enough to make generation fail, even without iterate.

Minimal repro:

pub struct repro?(
	visits : base.u32,
)

pub func repro.run!(s: slice base.u16) {
	var p : slice base.u16

	iterate (p = args.s)(length: 1, advance: 1, unroll: 1) {
		p[0] = p[0] ^ 0x1234
		this.visits ~mod+= 1
	}
}

Actual: at b36a81d3, wuffs-c gen -package_name repro repro.wuffs exits 1:

cannot convert Wuffs type "slice base.u16" to C

The same iterate fails for slices with element types base.u32, base.u64, base.i8, base.i16, base.i32, base.i64 and array[3] base.u8, with the matching slice type in the message. The parameter alone is enough: pub func repro.run!(s: slice base.u16) {} fails the same way, as does a roslice base.u16 parameter. Control: the same iterate over a slice base.u8 generates.

Expected: support slices of other element types, at least as iterate subjects, or reject them in the checker where cgen cannot lower them.

Cause: writeCTypeName has a C type only for slices and tables of unrefined base.u8, with a TODO:

wuffs/internal/cgen/expr.go

Lines 576 to 606 in b36a81d

func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, varName string) error {
// It may help to refer to http://unixwiz.net/techtips/reading-cdecl.html
// TODO: fix this, allow slices of all types, not just of base.u8's. Also
// allow arrays of slices, slices of pointers, etc.
if n.IsEitherSliceType() {
o := n.Inner()
if o.Decorator() == 0 && o.QID() == (t.QID{t.IDBase, t.IDU8}) && !o.IsRefined() {
b.writes("wuffs_base__slice_u8")
if varNamePrefix != "" {
b.writeb(' ')
b.writes(varNamePrefix)
b.writes(varName)
}
return nil
}
return fmt.Errorf("cannot convert Wuffs type %q to C", n.Str(g.tm))
}
if n.IsEitherTableType() {
o := n.Inner()
if o.Decorator() == 0 && o.QID() == (t.QID{t.IDBase, t.IDU8}) && !o.IsRefined() {
b.writes("wuffs_base__table_u8")
if varNamePrefix != "" {
b.writeb(' ')
b.writes(varNamePrefix)
b.writes(varName)
}
return nil
}
return fmt.Errorf("cannot convert Wuffs type %q to C", n.Str(g.tm))
}

#198 reports that slice expressions of wider elements are lowered with byte offsets where they do generate.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0