From 065335aa294855015d3a9303b352d5ee06d578c5 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:06:30 +0100 Subject: [PATCH] Parse: a named record or array constant as an element of a constant --- diff --git a/src/Parse.raz b/src/Parse.raz index 9fa9ab6..4eb284a 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3261,7 +3261,7 @@ (* The part of type t at offset off of the constant *) proc constPart(t: PType; off: INT): ERROR; -var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st: INT; +var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st, fx: INT; begin start := Scan.tokPos(); if (t^.form = Sym.fSlice) and (Scan.tok = Scan.sLbrak) and (literalLength() = 0) then @@ -3331,7 +3331,17 @@ elsif t^.form = Sym.fVariant then return Scan.markAt(e.pos, say1("expected a case of {}", Sym.typeName(t))); elsif (t^.form = Sym.fArray) or (t^.form = Sym.fRecord) then - return Scan.markAt(e.pos, say1("expected a literal of {}", Sym.typeName(t))); + (* a literal, or the whole of a constant of the type: its bytes and addresses are copied *) + if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst) or (e.typ # e.obj^.typ) + or not Sym.same(e.typ, t) then + return Scan.markAt(e.pos, say1("expected a literal of {}", Sym.typeName(t))); + end; + st := Chunk.chunks[e.chunk].start; + COPY(constBytes[off..off + INT(t^.size)], Chunk.rodata[st..st + INT(t^.size)]); + fx := Chunk.chunks[e.chunk].firstFix; + for k := fx to fx + Chunk.chunks[e.chunk].numFix - 1 do + try constFix(off + Chunk.dataFixups[k].at, Chunk.dataFixups[k].chunk); + end; else try Item.coerce(e, t); if (e.mode = Item.mProc) and not e.obj^.ext then diff --git a/test/run/ConstOfConst.expect b/test/run/ConstOfConst.expect new file mode 100644 index 0000000..1d202ca --- /dev/null +++ b/test/run/ConstOfConst.expect @@ -0,0 +1 @@ +onetwo diff --git a/test/run/ConstOfConst.raz b/test/run/ConstOfConst.raz new file mode 100644 index 0000000..76426bd --- /dev/null +++ b/test/run/ConstOfConst.raz @@ -0,0 +1,20 @@ +(* A named record or array constant as an element of an array constant, + and a constant with an address in it. *) +module ConstOfConst; +import Sys; + +type R = record a: INT; s: []U8; end; + +const X: R = {a: 1, s: "one"}; +const Row: [2]INT = [3, 4]; +const Y: [2]R = [X, {a: 2, s: "two"}]; +const Grid: [2][2]INT = [Row, [5, 6]]; + +proc main*; +begin + _ := Sys.write(Sys.stdout, Y[0].s); + _ := Sys.write(Sys.stdout, Y[1].s); + _ := Sys.write(Sys.stdout, "\n"); + if (Y[0].a # 1) or (Y[1].a # 2) or (Grid[0][1] # 4) or (Grid[1][0] # 5) then Sys.exit(1); end; +end; +end ConstOfConst.