summaryrefslogtreecommitdiff
path: root/chapter18/src
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-09-28 09:23:27 -0700
committerCliff Click <cliffc@acm.org>2026-09-28 09:23:27 -0700
commit8212a66ff27e56531e0b48807088f1c544d75756 (patch)
treed1a1cdbf1ae790ddd8378714d4325a84b90bfc17 /chapter18/src
parent3d226b49e1cd23b41fd9baf3917f751973c1ad11 (diff)
downloadSimple-8212a66ff27e56531e0b48807088f1c544d75756.tar.gz
Simple-8212a66ff27e56531e0b48807088f1c544d75756.zip
Check un-init not-null constructor fields
Propagate BOT inputs; Arith, Load. CallEnd inlining, the FUnPtr dies early. Load asserts when loading against uninit not-null field. Keep not-nill state for storage class. Clean up Node.peep as constant.
Diffstat (limited to 'chapter18/src')
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/StoreNode.java2
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter10Test.java6
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter16Test.java13
3 files changed, 12 insertions, 9 deletions
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/StoreNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/StoreNode.java
index 8f0725bb..870b8447 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/StoreNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/StoreNode.java
@@ -116,6 +116,8 @@ public class StoreNode extends MemOpNode {
public Parser.ParseException err() {
Parser.ParseException err = super.err();
if( err != null ) return err;
+ if( ptr()._type == Type.TOP )
+ return null; // Dead store
TypeMemPtr tmp = (TypeMemPtr)ptr()._type;
if( tmp._obj.field(_name)._final && !"[]".equals(_name) )
return Parser.error("Cannot modify final field '"+_name+"'",_loc);
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter10Test.java
index 378e4f40..daeea36d 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter10Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter10Test.java
@@ -46,6 +46,7 @@ public class Chapter10Test {
public void testNullGuardErrors() {
for( String body : new String[] {
"return p.x;",
+ "if (p == null) return p.x; return -1;",
"if (!!points) return p.x; return -1;",
"if (!!p) { int x = p.x; } return p.x;"
} ) {
@@ -53,7 +54,10 @@ public class Chapter10Test {
new CodeGen(NULLABLE_POINT_SOURCE+body).parse().opto().typeCheck();
fail(body);
} catch( RuntimeException e ) {
- assertEquals(body,"Might be null accessing 'x'",e.getMessage());
+ // Known null is rejected during parsing in these chapters.
+ String expected = body.startsWith("if (p == null)")
+ ? "Accessing unknown field 'x' from 'null'" : "Might be null accessing 'x'";
+ assertEquals(body,expected,e.getMessage());
}
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter16Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter16Test.java
index ad3dc690..83d34b2e 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter16Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter16Test.java
@@ -144,14 +144,11 @@ return p;
@Test
public void testStructFinal3() {
- CodeGen code = new CodeGen("""
-struct Point { var x; var y; };
-Point p = new Point;
-p.x++;
-return p;
-""");
- try { code.parse().opto(); fail(); }
- catch( Exception e ) { assertEquals("'Point' is not fully initialized, field 'x' needs to be set in a constructor",e.getMessage()); }
+ for( String fields : new String[] { "var x; var y;", "int[] x;", "int[] !x;" } ) {
+ String src = "struct Point { "+fields+" }; Point p = new Point; p.x++; return p;";
+ try { new CodeGen(src).parse().opto(); fail(fields); }
+ catch( Exception e ) { assertEquals("'Point' is not fully initialized, field 'x' needs to be set in a constructor",e.getMessage()); }
+ }
}
@Test