From 0d0c6bb15d751b263526485d6b142258ac4d319f Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 09:45:18 +0000 Subject: [PATCH] The compiler: PTR(s) (report 2026-10.34, section 6.8) Parse's ptrOf takes a slice, an array or a string, and Item's startOf makes the address: a slice's first word, read as LEN reads its second, with no check, so an empty slice gives what it holds; an array's or a string's address, refused wherever a slice of all of it is. The messages say what PTR is for and what to write: PTR(pa^) for a pointer to an array, ADR(v^[0]) through a volatile pointer, nil for PTR([]); and coerce's two mismatches on the way to it, a slice where a pointer is wanted and the other way round, say PTR(s) and LEN(s), or p[0..n]. conform/ has a run of it and ten errors, with their messages in test/msg; test/run/PtrPlaces what this compiler chooses. The editors know PTR, and COPY, which they had missed; raz(7) has it. The library can't use it yet: src/ is built by v0.19.0 against today's lib/, so Sys's bytes, which PTR would replace, waits for the release after this one. --- diff --git a/conform/err/PtrAsSlice.raz b/conform/err/PtrAsSlice.raz new file mode 100644 index 0000000..02fb5c7 --- /dev/null +++ b/conform/err/PtrAsSlice.raz @@ -0,0 +1,7 @@ +module PtrAsSlice; +proc main*; +var a: [4]U8; s: []U8; +begin + s := PTR(a); +end; +end PtrAsSlice. diff --git a/conform/err/PtrElement.raz b/conform/err/PtrElement.raz new file mode 100644 index 0000000..586286f --- /dev/null +++ b/conform/err/PtrElement.raz @@ -0,0 +1,7 @@ +module PtrElement; +proc main*; +var a: [4]U8; p: ^INT; +begin + p := PTR(a); +end; +end PtrElement. diff --git a/conform/err/PtrExport.raz b/conform/err/PtrExport.raz new file mode 100644 index 0000000..84fe94a --- /dev/null +++ b/conform/err/PtrExport.raz @@ -0,0 +1,8 @@ +module PtrExport; +import Owner; +proc main*; +var p: ^INT; +begin + p := PTR(Owner.a); +end; +end PtrExport. diff --git a/conform/err/PtrInteger.raz b/conform/err/PtrInteger.raz new file mode 100644 index 0000000..e56f3ec --- /dev/null +++ b/conform/err/PtrInteger.raz @@ -0,0 +1,8 @@ +module PtrInteger; +proc main*; +var n: INT; p: ^INT; +begin + n := 1; + p := PTR(n); +end; +end PtrInteger. diff --git a/conform/err/PtrLiteral.raz b/conform/err/PtrLiteral.raz new file mode 100644 index 0000000..3507efd --- /dev/null +++ b/conform/err/PtrLiteral.raz @@ -0,0 +1,7 @@ +module PtrLiteral; +proc main*; +var p: ^U8; +begin + p := PTR([]); +end; +end PtrLiteral. diff --git a/conform/err/PtrPacked.raz b/conform/err/PtrPacked.raz new file mode 100644 index 0000000..95235e2 --- /dev/null +++ b/conform/err/PtrPacked.raz @@ -0,0 +1,8 @@ +module PtrPacked; +type R = packed record tag: U8; words: [3]I32; end; +proc main*; +var r: R; p: ^I32; +begin + p := PTR(r.words); +end; +end PtrPacked. diff --git a/conform/err/PtrPointer.raz b/conform/err/PtrPointer.raz new file mode 100644 index 0000000..a9fb820 --- /dev/null +++ b/conform/err/PtrPointer.raz @@ -0,0 +1,9 @@ +module PtrPointer; +proc main*; +var a: [4]U8; p, q: ^U8; pa: ^[4]U8; +begin + p := ADR(a[0]); pa := ADR(a); + q := PTR(p); + q := PTR(pa); +end; +end PtrPointer. diff --git a/conform/err/PtrView.raz b/conform/err/PtrView.raz new file mode 100644 index 0000000..6d187f9 --- /dev/null +++ b/conform/err/PtrView.raz @@ -0,0 +1,8 @@ +module PtrView; +proc main*; +var w: I64; p: ^U8; +begin + w := 0; + p := PTR(w.([8]U8)); +end; +end PtrView. diff --git a/conform/err/PtrVolatile.raz b/conform/err/PtrVolatile.raz new file mode 100644 index 0000000..a8cc3ac --- /dev/null +++ b/conform/err/PtrVolatile.raz @@ -0,0 +1,9 @@ +module PtrVolatile; +var regs: [4]U32; +proc main*; +var v: ^VOLATILE [4]U32; p: ^U32; +begin + v := ADR(regs); + p := PTR(v^); +end; +end PtrVolatile. diff --git a/conform/err/SliceAsPtr.raz b/conform/err/SliceAsPtr.raz new file mode 100644 index 0000000..3023bb5 --- /dev/null +++ b/conform/err/SliceAsPtr.raz @@ -0,0 +1,9 @@ +module SliceAsPtr; +proc put(_p: ^U8; _n: INT); begin end; +proc main*; +var a: [4]U8; s: []U8; +begin + s := a[0..2]; + put(s, LEN(s)); +end; +end SliceAsPtr. diff --git a/conform/run/Ptr.expect b/conform/run/Ptr.expect new file mode 100644 index 0000000..f978e33 --- /dev/null +++ b/conform/run/Ptr.expect @@ -0,0 +1,13 @@ +hello +el +a slice: ADR(s[0]) +an array: ADR(a[0]) +a slice of it: ADR(a[2]) +a call's slice +a slice as it starts: nil +[]: nil +0 bytes back +lo +a const parameter: in place +10 20 30 +4 diff --git a/conform/run/Ptr.raz b/conform/run/Ptr.raz new file mode 100644 index 0000000..7e6476a --- /dev/null +++ b/conform/run/Ptr.raz @@ -0,0 +1,65 @@ +(* PTR(s), the address a slice starts at (report section 6.8): ADR(s[0]) + when s isn't empty; nil for a slice as it starts and for []; and, of + an array, the address of its first element. *) +module Ptr; +import Out; + +type Value = record n: INT; p: ^U8; end; + +const Digits: [4]INT = [1, 2, 3, 4]; + +var kept: []U8; + +(* a slice as a pointer and a length, with no test for the empty one *) +proc valueOf(s: []U8): Value; +begin + return {n: LEN(s), p: PTR(s)}; +end; + +(* and back: empty when n is 0, whatever p is *) +proc bytes(v: Value): []U8; +begin + return v.p[0..v.n]; +end; + +proc same(s: []U8): []U8; +begin + return s; +end; + +proc first(const a: [3]I64): ^I64; +begin + return PTR(a); +end; + +proc main*; +var + a: [6]U8; s, none: []U8; v: Value; w: [3]I64; ws: []I64; q: ^I64; + d: ^INT; i: INT; +begin + a := [104, 101, 108, 108, 111, 10]; + s := a[0..6]; + Out.str(bytes(valueOf(s))); + Out.str(bytes(valueOf(s[1..3]))); Out.str("\n"); + if PTR(s) = ADR(s[0]) then Out.str("a slice: ADR(s[0])\n"); end; + if PTR(a) = ADR(a[0]) then Out.str("an array: ADR(a[0])\n"); end; + if PTR(s[2..5]) = ADR(a[2]) then Out.str("a slice of it: ADR(a[2])\n"); end; + if PTR(same(s)) = ADR(a[0]) then Out.str("a call's slice\n"); end; + v := valueOf(none); + if v.p = nil then Out.str("a slice as it starts: nil\n"); end; + v := valueOf([]); + if v.p = nil then Out.str("[]: nil\n"); end; + Out.int(LEN(bytes(v))); Out.str(" bytes back\n"); + kept := s[3..5]; + Out.str(PTR(kept)[0..2]); Out.str("\n"); + w := [10, 20, 30]; + if first(w) = ADR(w[0]) then Out.str("a const parameter: in place\n"); end; + ws := w[0..3]; + q := PTR(ws); + for i := 0 to 2 do Out.int(q[i]); Out.str(" "); end; + Out.str("\n"); + d := PTR(Digits); + Out.int(d[3]); Out.str("\n"); +end; + +end Ptr. diff --git a/doc/compiler.md b/doc/compiler.md index d324274..800a924 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1594,6 +1594,22 @@ parses its own arguments, since `SIZE` takes a type and `INC` a variable. +`PTR(s)` (report 2026-10.34) is `ptrOf`, which refuses what isn't a +slice, an array or a string with a message that says what PTR is for +(and, for a pointer, what to write), and Item's `startOf`, which makes +it. Of a slice it is the first of its two words, read as `LEN` reads +the second, with no check, so that an empty slice gives what it holds; +a call's slice, in two words on the stack (`mWords`), drops its length. +Of an array or a string it is the address, refused wherever `slice` +would refuse the slice of all of it: + + ADR(s[0]) check 0 < LEN(s), then s's first word (stops on "") + PTR(s) s's first word (no check) + +Two mismatches in `coerce` that people meet on the way to it, a slice +where its element's pointer is wanted and a pointer where a slice is, +come with help that says what to write (`mismatchHelp`). + **Register variables.** One pass can't know whether a variable's address will be taken before its code is made, so `chooseRegs` reads the body ahead, as `instancesFor` does, and counts each variable's diff --git a/doc/raz.7 b/doc/raz.7 index a760c19..ea3c782 100644 --- a/doc/raz.7 +++ b/doc/raz.7 @@ -419,6 +419,17 @@ .Ar x . .It Fn LEN a The length of an array, a string constant or a slice. +.It Fn PTR s +The address the slice +.Ar s +starts at, a +.Li ^T +for a +.Li []T , +unchecked: for an empty slice what it holds, +.Dv nil +for one never set. +Of an array, the address of its first element. .It Fn NEW p a .Ar p set to a fresh, zeroed diff --git a/editor/emacs/raz-mode.el b/editor/emacs/raz-mode.el index 1470e28..5050b7d 100644 --- a/editor/emacs/raz-mode.el +++ b/editor/emacs/raz-mode.el @@ -32,8 +32,8 @@ '("I8" "I16" "I32" "I64" "INT" "UINT" "U8" "U16" "U32" "U64" "BOOL" "F32" "F64" "ERROR")) (defconst raz-builtins - '("SIZE" "ADR" "LEN" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" - "ARGC" "ARGV" "INC" "DEC")) + '("SIZE" "ADR" "LEN" "PTR" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" + "ARGC" "ARGV" "INC" "DEC" "COPY")) (defvar raz-mode-syntax-table (let ((st (make-syntax-table))) diff --git a/editor/vim/syntax/raz.vim b/editor/vim/syntax/raz.vim index b91176d..8863ccf 100644 --- a/editor/vim/syntax/raz.vim +++ b/editor/vim/syntax/raz.vim @@ -14,8 +14,8 @@ syntax keyword razKeyword repeat return set shl shr then to try type until syntax keyword razKeyword var variant VOLATILE while xor syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR -syntax keyword razBuiltin SIZE ADR LEN NEW MSG SYSCALL FENCE CAS XCHG XADD -syntax keyword razBuiltin ARGC ARGV INC DEC +syntax keyword razBuiltin SIZE ADR LEN PTR NEW MSG SYSCALL FENCE CAS XCHG XADD +syntax keyword razBuiltin ARGC ARGV INC DEC COPY syntax keyword razConstant true false nil " 123, 0FFH, 1.5, 2.0E-3 diff --git a/src/Item.raz b/src/Item.raz index 234ce1b..b28e33a 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -564,17 +564,24 @@ return nil; end; -(* An error in x's type; if x is an argument, a note at the parameter - it goes to. *) -proc mismatch(x: Item; text: []U8): ERROR; +(* An error in x's type, with help that says what to write instead + unless hint is empty; if x is an argument, a note at the parameter it + goes to. *) +proc mismatchHelp(x: Item; text, hint: []U8): ERROR; begin Diag.error(x.pos, text); + if LEN(hint) > 0 then Diag.help(hint); end; if (passing # nil) and (x.pos.f = passingAt.f) and (x.pos.a = passingAt.a) and (x.pos.b = passingAt.b) then Diag.note(passing^.pos, say1("it goes to {}, declared here", Scan.nameOf(passing^.name))); end; return Scan.Failed; end; +proc mismatch(x: Item; text: []U8): ERROR; +begin + return mismatchHelp(x, text, ""); +end; + (* An error at x, which is a view, or part of one, and so has no address: text, and help that says what to do instead. *) proc noAddress*(x: Item; text: []U8): ERROR; @@ -583,7 +590,7 @@ end; proc coerce*(var x: Item; t: ^Sym.Type): ERROR; -var s: ^Sym.Type; tmp: I64; inFrame: BOOL; +var s: ^Sym.Type; tmp: I64; inFrame: BOOL; hint: []U8; begin if x.mode = mCond then try value(x); end; s := x.typ; @@ -641,6 +648,15 @@ return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), Diag.say("{} widens to {} only as written, {}({})", [Fmt.str(Sym.typeName(s)), Fmt.str(Sym.typeName(t)), Fmt.str(Sym.typeName(t)), Fmt.str(Diag.source(x.pos))])); + elsif (s^.form in {Sym.fSlice, Sym.fArray}) and (t^.form = Sym.fPtr) and Sym.same(s^.base, t^.base) then + (* the pointer and the length of a slice are two things *) + return mismatchHelp(x, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), + Diag.say("PTR({}) is the address it starts at, a {}, and LEN({}) how many it has", + [Fmt.str(Diag.source(x.pos)), Fmt.str(Sym.typeName(t)), Fmt.str(Diag.source(x.pos))])); + elsif (s^.form = Sym.fPtr) and (t^.form = Sym.fSlice) and Sym.same(s^.base, t^.base) then + if x.mode = mVar then hint := say1("{}[0..n]", Diag.source(x.pos)); else hint := "p[0..n], for a pointer p,"; end; + return mismatchHelp(x, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), + say1("a pointer has no length: {} is the slice of the n elements it starts", hint)); elsif widens(t, s) then (* the other way, an I64 into an INT, is written: it may drop bits *) return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), @@ -810,6 +826,50 @@ st^.base := t^.base; tempItem(x, st, tmp); x.frame := inFrame; + return nil; +end; + +(* PTR(x), the address the slice x starts at, as the pointer type t + (report section 6.8). Of a slice it is its first word, read with no + check, so that an empty one gives what it holds, nil or an end, where + ADR(x[0]) would stop the program: + + PTR(s) s's first word, as LEN(s) is its second + + Of an array or a string it is the address of its first element, and + may be taken where slice above would make a slice of all of it. *) +proc startOf*(var x: Item; t: ^Sym.Type): ERROR; +begin + if x.typ^.form = Sym.fSlice then + if x.mode = mWords then + Gen.drop(); (* a call's two words: the length, on top, goes *) + x.mode := mStack; + else + if x.mode # mVar then try copyToTemp(x); end; + x.typ := t; + try value(x); + end; + x.typ := t; + x.frame := false; + return nil; + end; + if x.vol then + return Scan.markHelp(x.pos, "PTR can't take an array through a volatile pointer", + Diag.say("ADR({}[0]) is the address of its first element, a ^VOLATILE {}", + [Fmt.str(Diag.source(x.pos)), Fmt.str(Sym.typeName(t^.base))])); + end; + if x.odd then return Scan.markAt(x.pos, "PTR can't take a field of a packed record, which has no address"); end; + if x.view then return noAddress(x, "PTR can't take a view"); end; + if x.ro and not onlyRead(x) then + (* as in slice: another module's array isn't lent to be written *) + _ := notReadOnly(x); + Diag.help("a pointer to it could change it, as a slice of it could: copy it to a variable here first"); + return Scan.Failed; + end; + try address(x); + x.mode := mStack; + x.typ := t; + x.frame := false; return nil; end; diff --git a/src/Parse.raz b/src/Parse.raz index 88eb65e..c99e979 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -960,6 +960,30 @@ return "INC"; end; +(* PTR(x): the item x, a slice, an array or a string, made the address + its elements start at by Item's startOf. Anything else is an error + that says what PTR is for, and for a pointer what to write. *) +proc ptrOf(var x: TItem): ERROR; +var e: PType; s: []U8; +begin + if x.mode = Item.mStr then + e := Sym.tU8; + elsif x.typ^.form in {Sym.fSlice, Sym.fArray} then + e := x.typ^.base; + else + s := say1("PTR needs a slice or an array, found {}", Item.what(x)); + if (x.typ^.form = Sym.fPtr) and (x.typ^.base # nil) and (x.typ^.base^.form = Sym.fArray) then + return Scan.markHelp(x.pos, s, Diag.say("write PTR({}^) for the address of the array's first element, a ^{}", + [Fmt.str(Diag.source(x.pos)), Fmt.str(Sym.typeName(x.typ^.base^.base))])); + elsif x.typ^.form = Sym.fPtr then + return Scan.markHelp(x.pos, s, say1("{} is a pointer already, which is what PTR gives of a slice or an array", + Diag.source(x.pos))); + end; + return Scan.markAt(x.pos, s); + end; + return Item.startOf(x, newPtr(e, false)); +end; + proc builtin(var x: TItem): ERROR; var which, n: INT; y: TItem; t: PType; begin @@ -1009,6 +1033,15 @@ else return Scan.markAt(y.pos, say1("LEN needs an array or a slice, found {}", Item.what(y))); end; + | Sym.bPtr: + if Scan.tok = Scan.sLbrak then + (* a literal takes its type from where it stands, and here nothing gives one *) + return Scan.markHelp(Scan.tokPos(), "PTR can't take an array literal, which has no type here", + "PTR([]) would be nil, the empty slice's pointer; give any other literal a variable first"); + end; + try expression(y, nil); + try ptrOf(y); + x := y; | Sym.bSyscall: n := 0; repeat diff --git a/src/Raz.raz b/src/Raz.raz index ec3d496..95580d0 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -31,7 +31,7 @@ import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; const - Report = "2026-10.33"; (* the version of doc/raz.md it follows *) + Report = "2026-10.34"; (* the version of doc/raz.md it follows *) MemfdCreate = 319; (* the system call *) MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) Execve = 59; diff --git a/src/Sym.raz b/src/Sym.raz index 7b45572..eb21387 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -131,7 +131,7 @@ (* built-in procedures *) bSize* = 0; bAdr* = 1; bLen* = 2; bNew* = 3; bMsg* = 4; bSyscall* = 5; bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11; - bInc* = 12; bDec* = 13; bCopy* = 14; + bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; MaxDepth* = 64; (* scopes open at once *) @@ -697,6 +697,7 @@ enter("SIZE", oBuiltin, nil, bSize); enter("ADR", oBuiltin, nil, bAdr); enter("LEN", oBuiltin, nil, bLen); + enter("PTR", oBuiltin, nil, bPtr); enter("NEW", oBuiltin, nil, bNew); enter("MSG", oBuiltin, nil, bMsg); enter("SYSCALL", oBuiltin, nil, bSyscall); diff --git a/test/msg/PtrAsSlice.err b/test/msg/PtrAsSlice.err new file mode 100644 index 0000000..0cacfea --- /dev/null +++ b/test/msg/PtrAsSlice.err @@ -0,0 +1,5 @@ +conform/err/PtrAsSlice.raz:5:10: error: can't use ^U8 as []U8 + 5 | s := PTR(a); + | ^^^^^^ +help: a pointer has no length: p[0..n], for a pointer p, is the slice of the n elements it starts +raz: 1 error diff --git a/test/msg/PtrElement.err b/test/msg/PtrElement.err new file mode 100644 index 0000000..2ea46d0 --- /dev/null +++ b/test/msg/PtrElement.err @@ -0,0 +1,4 @@ +conform/err/PtrElement.raz:5:10: error: can't use ^U8 as ^INT + 5 | p := PTR(a); + | ^^^^^^ +raz: 1 error diff --git a/test/msg/PtrExport.err b/test/msg/PtrExport.err new file mode 100644 index 0000000..524b6b4 --- /dev/null +++ b/test/msg/PtrExport.err @@ -0,0 +1,5 @@ +conform/err/PtrExport.raz:6:14: error: Owner.a is Owner's, and can't be changed outside it + 6 | p := PTR(Owner.a); + | ^^^^^^^ +help: a pointer to it could change it, as a slice of it could: copy it to a variable here first +raz: 1 error diff --git a/test/msg/PtrInteger.err b/test/msg/PtrInteger.err new file mode 100644 index 0000000..a5e4b2c --- /dev/null +++ b/test/msg/PtrInteger.err @@ -0,0 +1,4 @@ +conform/err/PtrInteger.raz:6:14: error: PTR needs a slice or an array, found INT + 6 | p := PTR(n); + | ^ +raz: 1 error diff --git a/test/msg/PtrLiteral.err b/test/msg/PtrLiteral.err new file mode 100644 index 0000000..adf0059 --- /dev/null +++ b/test/msg/PtrLiteral.err @@ -0,0 +1,5 @@ +conform/err/PtrLiteral.raz:5:14: error: PTR can't take an array literal, which has no type here + 5 | p := PTR([]); + | ^ +help: PTR([]) would be nil, the empty slice's pointer; give any other literal a variable first +raz: 1 error diff --git a/test/msg/PtrPacked.err b/test/msg/PtrPacked.err new file mode 100644 index 0000000..e07535b --- /dev/null +++ b/test/msg/PtrPacked.err @@ -0,0 +1,4 @@ +conform/err/PtrPacked.raz:6:14: error: PTR can't take a field of a packed record, which has no address + 6 | p := PTR(r.words); + | ^^^^^^^ +raz: 1 error diff --git a/test/msg/PtrPointer.err b/test/msg/PtrPointer.err new file mode 100644 index 0000000..17cfdcf --- /dev/null +++ b/test/msg/PtrPointer.err @@ -0,0 +1,9 @@ +conform/err/PtrPointer.raz:6:14: error: PTR needs a slice or an array, found ^U8 + 6 | q := PTR(p); + | ^ +help: p is a pointer already, which is what PTR gives of a slice or an array +conform/err/PtrPointer.raz:7:14: error: PTR needs a slice or an array, found ^[4]U8 + 7 | q := PTR(pa); + | ^^ +help: write PTR(pa^) for the address of the array's first element, a ^U8 +raz: 2 errors diff --git a/test/msg/PtrView.err b/test/msg/PtrView.err new file mode 100644 index 0000000..b0084fd --- /dev/null +++ b/test/msg/PtrView.err @@ -0,0 +1,5 @@ +conform/err/PtrView.raz:6:14: error: PTR can't take a view + 6 | p := PTR(w.([8]U8)); + | ^^^^^^^^^ +help: a view has no address, since it may be misaligned: copy it to a variable +raz: 1 error diff --git a/test/msg/PtrVolatile.err b/test/msg/PtrVolatile.err new file mode 100644 index 0000000..881ed80 --- /dev/null +++ b/test/msg/PtrVolatile.err @@ -0,0 +1,5 @@ +conform/err/PtrVolatile.raz:7:14: error: PTR can't take an array through a volatile pointer + 7 | p := PTR(v^); + | ^^ +help: ADR(v^[0]) is the address of its first element, a ^VOLATILE U32 +raz: 1 error diff --git a/test/msg/SliceAsPtr.err b/test/msg/SliceAsPtr.err new file mode 100644 index 0000000..b7e791e --- /dev/null +++ b/test/msg/SliceAsPtr.err @@ -0,0 +1,8 @@ +conform/err/SliceAsPtr.raz:7:9: error: can't use []U8 as ^U8 + 7 | put(s, LEN(s)); + | ^ +help: PTR(s) is the address it starts at, a ^U8, and LEN(s) how many it has +conform/err/SliceAsPtr.raz:2:10: note: it goes to _p, declared here + 2 | proc put(_p: ^U8; _n: INT); begin end; + | ^^ +raz: 1 error diff --git a/test/run/PtrPlaces.expect b/test/run/PtrPlaces.expect new file mode 100644 index 0000000..32222e1 --- /dev/null +++ b/test/run/PtrPlaces.expect @@ -0,0 +1,3 @@ +cut empty: at its place +"": not nil +6 diff --git a/test/run/PtrPlaces.raz b/test/run/PtrPlaces.raz new file mode 100644 index 0000000..454bae7 --- /dev/null +++ b/test/run/PtrPlaces.raz @@ -0,0 +1,22 @@ +(* PTR(s) where the report leaves the address to the implementation: + an empty slice cut from another keeps its place there, and a string + constant, even "", has a place in read-only data. A slice kept in + registers in a loop gives its pointer's register. *) +module PtrPlaces; +import Out; + +proc main*; +var a: [4]U8; s, e: []U8; p: ^U8; i, n: INT; +begin + a := [1, 2, 3, 4]; + s := a[0..4]; + e := s[2..2]; + if PTR(e) = PTR(s) + 2 then Out.str("cut empty: at its place\n"); end; + p := PTR(""); + if p # nil then Out.str("\"\": not nil\n"); end; + n := 0; + for i := 0 to 3 do s := a[i..4]; n := n + INT(PTR(s) - PTR(a)); end; + Out.int(n); Out.str("\n"); +end; + +end PtrPlaces.