| | @@ -54,6 +54,9 @@ |
| 54 | 54 | of one, which only that module changes *) |
| 55 | 55 | elemRo*: BOOL; (* mVar: an element of a []const slice, or part of |
| 56 | 56 | one, read through it and not written *) |
| 57 | + viewOver*: BOOL; (* a view over a variable that holds more than |
| 58 | + integers and reals, written only in a |
| 59 | + module that imports SYSTEM *) |
| 57 | 60 | deref*: BOOL; (* mVar: at an address from a pointer, which may be |
| 58 | 61 | nil and isn't tested yet (nilTest) *) |
| 59 | 62 | obj*: ^Sym.Obj; (* mProc, mBuiltin, mCase; mVar: the variable named *) |
| | @@ -80,6 +83,8 @@ |
| 80 | 83 | procedure is compiled again, the variable in memory *) |
| 81 | 84 | checks*: BOOL; (* the module's index and slice checks are made: it |
| 82 | 85 | doesn't say check off *) |
| 86 | + strict*: BOOL; (* raz -strict: report 2026-10.37 held whole, as v0.24.0 |
| 87 | + will hold it; until then, its old forms are taken *) |
| 83 | 88 | result: ^Sym.Type; (* the result of the procedure being compiled, or nil *) |
| 84 | 89 | passing: ^Sym.Obj; (* the parameter the argument at passingAt goes to *) |
| 85 | 90 | passingAt: Diag.Pos; |
| | @@ -94,6 +99,11 @@ |
| 94 | 99 | checks := yes; |
| 95 | 100 | end; |
| 96 | 101 | |
| 102 | +proc setStrict*(yes: BOOL); |
| 103 | +begin |
| 104 | + strict := yes; |
| 105 | +end; |
| 106 | + |
| 97 | 107 | (* The comparison that holds when op doesn't *) |
| 98 | 108 | proc negated(op: Scan.Sym): Scan.Sym; |
| 99 | 109 | begin |
| | @@ -127,6 +137,18 @@ |
| 127 | 137 | return Diag.say(f, [Fmt.str(a)]); |
| 128 | 138 | end; |
| 129 | 139 | |
| 140 | +(* Under -strict, what reaches memory other than through a variable, |
| 141 | + what, is refused at pos in a module that doesn't import SYSTEM |
| 142 | + (report 3) *) |
| 143 | +proc needSystem*(pos: Diag.Pos; what: []U8): ERROR; |
| 144 | +begin |
| 145 | + if strict and not Sym.curModule^.system then |
| 146 | + return Scan.markHelp(pos, say1("{} needs import SYSTEM", what), |
| 147 | + "it reaches memory other than through a variable: say import SYSTEM after the module's imports"); |
| 148 | + end; |
| 149 | + return nil; |
| 150 | +end; |
| 151 | + |
| 130 | 152 | proc say2(f, a, b: []U8): []U8; |
| 131 | 153 | begin |
| 132 | 154 | return Diag.say(f, [Fmt.str(a), Fmt.str(b)]); |
| | @@ -550,6 +572,12 @@ |
| 550 | 572 | return (x.mode = mVar) and x.ro and (x.obj # nil) and x.obj^.isConst; |
| 551 | 573 | end; |
| 552 | 574 | |
| 575 | +(* Whether x is a constant, or part of one, held in read-only data *) |
| 576 | +proc ofConst(const x: Item): BOOL; |
| 577 | +begin |
| 578 | + return (x.mode = mVar) and (x.obj # nil) and (x.obj^.cls = Sym.oConst); |
| 579 | +end; |
| 580 | + |
| 553 | 581 | (* Another module's exported variable can be read, not changed, nor |
| 554 | 582 | handed to anything that could change it (report section 3); nor can |
| 555 | 583 | what is only read, above. *) |
| | @@ -559,6 +587,7 @@ |
| 559 | 587 | return Scan.markHelp(x.pos, say1("{} is read through a []const slice, and can't be changed", Diag.source(x.pos)), |
| 560 | 588 | "a []const slice's elements are only read: write them through a []T"); |
| 561 | 589 | end; |
| 590 | + if x.view and x.viewOver then try needSystem(x.pos, "writing a view over what holds more than integers and reals"); end; |
| 562 | 591 | if onlyRead(x) then |
| 563 | 592 | return Scan.markHelp(x.pos, say1("{} is only read here, and can't be changed", Diag.source(x.pos)), |
| 564 | 593 | "a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it"); |
| | @@ -611,9 +640,14 @@ |
| 611 | 640 | if x.mode = mCond then try value(x); end; |
| 612 | 641 | s := x.typ; |
| 613 | 642 | if x.mode = mStr then |
| 643 | + if strict and (t^.form = Sym.fSlice) and (t^.base = Sym.tU8) and not t^.rdonly then |
| 644 | + return mismatchHelp(x, say1("a string is a []const U8, not {}", Sym.typeName(t)), |
| 645 | + "its bytes are constant: take a []const U8 where they are only read, or copy them"); |
| 646 | + end; |
| 614 | 647 | if (t^.form = Sym.fSlice) and (t^.base = Sym.tU8) then |
| 615 | 648 | try address(x); sliceOf(x, t, x.val); |
| 616 | 649 | elsif (t^.form = Sym.fPtr) and (t^.base = Sym.tU8) then |
| 650 | + try needSystem(x.pos, "a string as a ^U8"); |
| 617 | 651 | try address(x); x.mode := mStack; x.typ := t; |
| 618 | 652 | elsif (t^.form = Sym.fArray) and (t^.base = Sym.tU8) and (t^.len >= x.val) then |
| 619 | 653 | tmp := Gen.allocTemp(t^.size, 1); |
| | @@ -651,6 +685,10 @@ |
| 651 | 685 | (* a slice of what is only read may be made, to read, as of a |
| 652 | 686 | constant; another module's array, or one read through a |
| 653 | 687 | []const, only as a []const *) |
| 688 | + if strict and (onlyRead(x) or ofConst(x)) and not t^.rdonly then |
| 689 | + return mismatchHelp(x, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), |
| 690 | + say1("{} is only read: a slice of it is a []const", Diag.source(x.pos))); |
| 691 | + end; |
| 654 | 692 | if x.ro and not onlyRead(x) and not t^.rdonly then |
| 655 | 693 | return Scan.markHelp(x.pos, Diag.say("{} is {}'s, and can't be changed outside it", |
| 656 | 694 | [Fmt.str(Diag.source(x.pos)), Fmt.str(Scan.nameOf(x.obj^.home^.name))]), |
| | @@ -765,7 +803,9 @@ |
| 765 | 803 | proc indexPrep*(var x: Item): ERROR; |
| 766 | 804 | begin |
| 767 | 805 | nilTest(x); |
| 768 | | - if x.mode = mStr then try coerce(x, Sym.tBytes); end; |
| 806 | + if x.mode = mStr then |
| 807 | + if strict then try coerce(x, Sym.tConstBytes); else try coerce(x, Sym.tBytes); end; |
| 808 | + end; |
| 769 | 809 | if (x.typ^.form = Sym.fPtr) and (x.typ^.base^.form = Sym.fArray) then |
| 770 | 810 | return Scan.markAt(x.pos, "a pointer to an array is indexed as p^[i]"); |
| 771 | 811 | end; |
| | @@ -794,6 +834,7 @@ |
| 794 | 834 | return Scan.markAt(y.pos, say1("an index is an integer, found {}", what(y))); |
| 795 | 835 | end; |
| 796 | 836 | if t^.form = Sym.fPtr then |
| 837 | + try needSystem(Diag.join(x.pos, y.pos), "indexing through a pointer"); |
| 797 | 838 | isConst := y.mode = mConst; |
| 798 | 839 | try value(y); |
| 799 | 840 | if isConst or not ((s = 1) or (s = 2) or (s = 4) or (s = 8)) then |
| | @@ -848,7 +889,10 @@ |
| 848 | 889 | (* a slice of another module's array, or of one read through a |
| 849 | 890 | []const, is a []const; of a const parameter's, a slice to read *) |
| 850 | 891 | roSlice := (t^.form = Sym.fSlice) and t^.rdonly; |
| 851 | | - if t^.form = Sym.fArray then roSlice := (x.ro and not onlyRead(x)) or x.elemRo; end; |
| 892 | + if t^.form = Sym.fArray then |
| 893 | + roSlice := (x.ro and not onlyRead(x)) or x.elemRo or (strict and (onlyRead(x) or ofConst(x))); |
| 894 | + end; |
| 895 | + if t^.form = Sym.fPtr then try needSystem(x.pos, "a slice of a pointer"); end; |
| 852 | 896 | try value(hi); |
| 853 | 897 | inFrame := x.frame and (t^.form # Sym.fPtr); |
| 854 | 898 | tmp := Gen.allocTemp(16, 8); |
| | @@ -952,7 +996,10 @@ |
| 952 | 996 | "a view may be misaligned, which can't be one access: reach a device through a ^VOLATILE pointer to a record"); |
| 953 | 997 | end; |
| 954 | 998 | u := notPlain(t); |
| 955 | | - if u = nil then return nil; end; |
| 999 | + if u = nil then |
| 1000 | + if not Sym.isPlain(t) then try needSystem(x.pos, say1("a view as {}, which holds an address,", Sym.typeName(t))); end; |
| 1001 | + return nil; |
| 1002 | + end; |
| 956 | 1003 | if u # t then |
| 957 | 1004 | text := say2("can't view bytes as {}, which holds {}", Sym.typeName(t), Sym.typeName(u)); |
| 958 | 1005 | else |
| | @@ -1015,6 +1062,7 @@ |
| 1015 | 1062 | inFrame := x.frame; |
| 1016 | 1063 | if t^.form = Sym.fSlice then |
| 1017 | 1064 | try bytesType(x, t^.base); |
| 1065 | + if not t^.rdonly and not Sym.isPlain(t^.base) then try needSystem(x.pos, "BYTES of what holds an address"); end; |
| 1018 | 1066 | if (x.mode # mVar) or (x.base = Gen.bStack) then try copyToTemp(x); end; |
| 1019 | 1067 | y := x; |
| 1020 | 1068 | y.typ := Sym.tInt; |
| | @@ -1050,6 +1098,7 @@ |
| 1050 | 1098 | if x.odd then return Scan.markAt(x.pos, "BYTES can't take a field of a packed record, which has no address"); end; |
| 1051 | 1099 | if x.view then return noAddress(x, "BYTES can't take a view"); end; |
| 1052 | 1100 | try bytesType(x, t); |
| 1101 | + if not Sym.isPlain(t) then try needSystem(x.pos, "BYTES of what holds an address"); end; |
| 1053 | 1102 | try address(x); |
| 1054 | 1103 | sliceOf(x, Sym.tBytes, t^.size); |
| 1055 | 1104 | x.frame := inFrame; |
| | @@ -1068,13 +1117,16 @@ |
| 1068 | 1117 | |
| 1069 | 1118 | (* v.(T), where v is not an element: T must fit in v itself. *) |
| 1070 | 1119 | proc view*(var x: Item; t: ^Sym.Type): ERROR; |
| 1120 | +var over: BOOL; |
| 1071 | 1121 | begin |
| 1072 | 1122 | try viewable(x, t); |
| 1123 | + over := not Sym.isPlain(x.typ); |
| 1073 | 1124 | if t^.size > x.typ^.size then |
| 1074 | 1125 | return Scan.markAt(x.pos, Diag.say("a view of {} bytes doesn't fit in {}, of {}", |
| 1075 | 1126 | [Fmt.int(t^.size), Fmt.str(Sym.typeName(x.typ)), Fmt.int(x.typ^.size)])); |
| 1076 | 1127 | end; |
| 1077 | 1128 | becomeView(x, t); |
| 1129 | + x.viewOver := over; |
| 1078 | 1130 | return nil; |
| 1079 | 1131 | end; |
| 1080 | 1132 | |
| | @@ -1093,7 +1145,7 @@ |
| 1093 | 1145 | below it, is at least k. Through a pointer, no check: p[i] has none |
| 1094 | 1146 | (report section 6.9). *) |
| 1095 | 1147 | proc indexView*(var x, y: Item; t: ^Sym.Type): ERROR; |
| 1096 | | -var at: ^Sym.Type; s, k: I64; |
| 1148 | +var at: ^Sym.Type; s, k: I64; over: BOOL; |
| 1097 | 1149 | begin |
| 1098 | 1150 | at := x.typ; |
| 1099 | 1151 | s := at^.base^.size; |
| | @@ -1129,7 +1181,9 @@ |
| 1129 | 1181 | try index(x, y); |
| 1130 | 1182 | end; |
| 1131 | 1183 | try viewable(x, t); |
| 1184 | + over := not Sym.isPlain(x.typ); (* the element's *) |
| 1132 | 1185 | becomeView(x, t); |
| 1186 | + x.viewOver := over; |
| 1133 | 1187 | return nil; |
| 1134 | 1188 | end; |
| 1135 | 1189 | |
| | @@ -1542,6 +1596,7 @@ |
| 1542 | 1596 | if (op # Scan.sEql) and (op # Scan.sNeq) then return doesntApply(op, x, y, xt); end; |
| 1543 | 1597 | t := Sym.tBool; |
| 1544 | 1598 | elsif (xt^.form = Sym.fPtr) and Sym.isInt(yt) and ((op = Scan.sPlus) or (op = Scan.sMinus)) then |
| 1599 | + try needSystem(Diag.join(x.pos, y.pos), "a pointer's arithmetic"); |
| 1545 | 1600 | t := xt; scale := INT(xt^.base^.size); isPtr := true; |
| 1546 | 1601 | if y.mode = mConst then y.typ := Sym.tInt; end; |
| 1547 | 1602 | elsif (xt^.form = Sym.fPtr) and (yt^.form = Sym.fPtr) and (op = Scan.sMinus) then |
| | @@ -1845,8 +1900,10 @@ |
| 1845 | 1900 | try value(x); Gen.realToInt(Gen.kindOf(s)); Gen.narrow(INT(t^.size), t^.signed); |
| 1846 | 1901 | end; |
| 1847 | 1902 | elsif (isWord(s) or (s^.form in {Sym.fConst, Sym.fPtr, Sym.fProc, Sym.fNil})) and (t^.form in {Sym.fPtr, Sym.fProc}) then |
| 1903 | + if s^.form # Sym.fNil then try needSystem(x.pos, say1("a conversion to {}", Sym.typeName(t))); end; |
| 1848 | 1904 | if x.mode # mConst then try value(x); end; |
| 1849 | 1905 | elsif (s^.form in {Sym.fPtr, Sym.fProc}) and isWord(t) then |
| 1906 | + try needSystem(x.pos, say1("a conversion of a pointer to {}", Sym.typeName(t))); |
| 1850 | 1907 | try value(x); |
| 1851 | 1908 | elsif Sym.isInt(s) and (t^.form = Sym.fBool) then |
| 1852 | 1909 | return Scan.markHelp(x.pos, say1("can't convert {} to BOOL", Sym.typeName(s)), |
| | @@ -2494,7 +2551,7 @@ |
| 2494 | 2551 | try value(x); |
| 2495 | 2552 | s := Gen.allocTemp(16, 8); |
| 2496 | 2553 | Gen.errorText(s); |
| 2497 | | - tempItem(x, Sym.tBytes, s); |
| 2554 | + if strict then tempItem(x, Sym.tConstBytes, s); else tempItem(x, Sym.tBytes, s); end; (* the message is constant *) |
| 2498 | 2555 | return nil; |
| 2499 | 2556 | end; |
| 2500 | 2557 | |