compiler: Add module-private opaque records
c8d7b8906e8d8d4adce50eef376f4a62177aeca1e0d3d0d0b2b6b9697dc798f7
1 parent
f6905c51
lib/std/lang/ast.rad
+2 -0
| 60 | 60 | Test = 0b1000, |
|
| 61 | 61 | /// Compiler intrinsic attribute. |
|
| 62 | 62 | Intrinsic = 0b10000, |
|
| 63 | 63 | /// Declaration may perform unsafe pointer operations. |
|
| 64 | 64 | Unsafe = 0b100000, |
|
| 65 | + | /// Record representation is accessible only in its defining module. |
|
| 66 | + | Opaque = 0b1000000, |
|
| 65 | 67 | } |
|
| 66 | 68 | ||
| 67 | 69 | /// Ordered collection of attribute nodes applied to a declaration. |
|
| 68 | 70 | export record Attributes: Copy { |
|
| 69 | 71 | /// Attribute nodes in declaration order. |
lib/std/lang/ast/printer.rad
+7 -2
| 344 | 344 | case super::Attribute::Default => return sexpr::sym("@default"), |
|
| 345 | 345 | case super::Attribute::Extern => return sexpr::sym("@extern"), |
|
| 346 | 346 | case super::Attribute::Test => return sexpr::sym("@test"), |
|
| 347 | 347 | case super::Attribute::Intrinsic => return sexpr::sym("@intrinsic"), |
|
| 348 | 348 | case super::Attribute::Unsafe => return sexpr::sym("@unsafe"), |
|
| 349 | + | case super::Attribute::Opaque => return sexpr::sym("@opaque"), |
|
| 349 | 350 | } |
|
| 350 | 351 | } |
|
| 351 | 352 | case super::NodeValue::Try(t) => { |
|
| 352 | 353 | let mut head = "try"; |
|
| 353 | 354 | if t.shouldPanic { set head = "try!"; } |
| 487 | 488 | } |
|
| 488 | 489 | case super::NodeValue::Mod(m) => return sexpr::list(a, "mod", &[toExpr(a, m.name)]), |
|
| 489 | 490 | case super::NodeValue::Use(u_) => return sexpr::list(a, "use", &[toExpr(a, u_.path)]), |
|
| 490 | 491 | case super::NodeValue::RecordDecl(r) => { |
|
| 491 | 492 | let children = fieldListToExprs(a, r.fields); |
|
| 493 | + | let mut head = "record"; |
|
| 494 | + | if let attrs = r.attrs; super::attributesContains(&attrs, super::Attribute::Opaque) { |
|
| 495 | + | set head = "opaque-record"; |
|
| 496 | + | } |
|
| 492 | 497 | if r.regions.len > 0 or r.derives.len > 0 { |
|
| 493 | - | return sexpr::block(a, "record", &[ |
|
| 498 | + | return sexpr::block(a, head, &[ |
|
| 494 | 499 | toExpr(a, r.name), |
|
| 495 | 500 | sexpr::list(a, "regions", nodeListToExprs(a, r.regions)), |
|
| 496 | 501 | sexpr::list(a, "derives", nodeListToExprs(a, r.derives)), |
|
| 497 | 502 | ], children); |
|
| 498 | 503 | } |
|
| 499 | - | return sexpr::block(a, "record", &[toExpr(a, r.name)], children); |
|
| 504 | + | return sexpr::block(a, head, &[toExpr(a, r.name)], children); |
|
| 500 | 505 | } |
|
| 501 | 506 | case super::NodeValue::RecordField { field, type, value } => { |
|
| 502 | 507 | return fieldToExpr(a, field, type, value); |
|
| 503 | 508 | } |
|
| 504 | 509 | case super::NodeValue::UnionDecl(u_) => { |
lib/std/lang/lower.rad
+1 -1
| 7337 | 7337 | ||
| 7338 | 7338 | // Get the receiver as a pointer. |
|
| 7339 | 7339 | let parentTy = try typeOf(self, access.parent); |
|
| 7340 | 7340 | let receiverVal = try lowerReceiver(self, access.parent, parentTy); |
|
| 7341 | 7341 | ||
| 7342 | - | let qualName = instanceMethodName(self.low, nil, method.concreteTypeName, method.name); |
|
| 7342 | + | let qualName = instanceMethodName(self.low, method.moduleId, method.concreteTypeName, method.name); |
|
| 7343 | 7343 | let fnInfo = method.fullFnType; |
|
| 7344 | 7344 | ||
| 7345 | 7345 | // Build args: optional return param slot + receiver + user args. |
|
| 7346 | 7346 | let mut args = callArgs(self, fnInfo); |
|
| 7347 | 7347 | args.append(receiverVal, self.allocator); |
lib/std/lang/parser.rad
+15 -3
| 891 | 891 | } |
|
| 892 | 892 | let allowed: bool = |
|
| 893 | 893 | p.current.kind == scanner::TokenKind::Fn or |
|
| 894 | 894 | p.current.kind == scanner::TokenKind::Union or |
|
| 895 | 895 | p.current.kind == scanner::TokenKind::Record or |
|
| 896 | + | p.current.kind == scanner::TokenKind::Opaque or |
|
| 896 | 897 | p.current.kind == scanner::TokenKind::Mod or |
|
| 897 | 898 | p.current.kind == scanner::TokenKind::Static or |
|
| 898 | 899 | p.current.kind == scanner::TokenKind::Constant or |
|
| 899 | 900 | p.current.kind == scanner::TokenKind::Use or |
|
| 900 | 901 | p.current.kind == scanner::TokenKind::Trait; |
| 970 | 971 | return try parseFnDecl(p, attrs); |
|
| 971 | 972 | } |
|
| 972 | 973 | case scanner::TokenKind::Union => { |
|
| 973 | 974 | return try parseUnionDecl(p, attrs); |
|
| 974 | 975 | } |
|
| 975 | - | case scanner::TokenKind::Record => { |
|
| 976 | + | case scanner::TokenKind::Record, scanner::TokenKind::Opaque => { |
|
| 976 | 977 | return try parseRecordDecl(p, attrs); |
|
| 977 | 978 | } |
|
| 978 | 979 | case scanner::TokenKind::Use => { |
|
| 979 | 980 | if isSessionBlock(p) { |
|
| 980 | 981 | return try parseSessionBlock(p); |
| 1886 | 1887 | /// Parse a named record declaration. |
|
| 1887 | 1888 | /// `record Point { x: i32, y: i32 }`, or `record Pair(i32, i32);` |
|
| 1888 | 1889 | unsafe fn parseRecordDecl 'pool (p: &mut Parser 'pool, attrs: ?ast::Attributes) -> *ast::Node |
|
| 1889 | 1890 | throws (ParseError) |
|
| 1890 | 1891 | { |
|
| 1892 | + | let mut recordAttrs = attrs; |
|
| 1893 | + | if consume(p, scanner::TokenKind::Opaque) { |
|
| 1894 | + | let mut list = ast::nodeSlice(p.arena, 4); |
|
| 1895 | + | if let a = attrs { |
|
| 1896 | + | for attr in a.list { |
|
| 1897 | + | list.append(attr, p.allocator); |
|
| 1898 | + | } |
|
| 1899 | + | } |
|
| 1900 | + | list.append(nodeAttribute(p, ast::Attribute::Opaque), p.allocator); |
|
| 1901 | + | set recordAttrs = ast::Attributes { list }; |
|
| 1902 | + | } |
|
| 1891 | 1903 | try expect(p, scanner::TokenKind::Record, "expected `record`"); |
|
| 1892 | 1904 | ||
| 1893 | 1905 | let name = try parseIdent(p, "expected record name"); |
|
| 1894 | 1906 | let mut regions: *mut [*ast::Node] = &mut []; |
|
| 1895 | 1907 | let mut derives: *mut [*ast::Node] = &mut []; |
| 1899 | 1911 | if consume(p, scanner::TokenKind::LParen) { |
|
| 1900 | 1912 | let fields = try parseRecordFields(p, RecordFieldMode::Unlabeled); |
|
| 1901 | 1913 | try expect(p, scanner::TokenKind::Semicolon, "expected `;` after record"); |
|
| 1902 | 1914 | return node(p, ast::NodeValue::RecordDecl( |
|
| 1903 | 1915 | ast::RecordDecl { |
|
| 1904 | - | name, fields, attrs, regions, derives, labeled: false, |
|
| 1916 | + | name, fields, attrs: recordAttrs, regions, derives, labeled: false, |
|
| 1905 | 1917 | } |
|
| 1906 | 1918 | )); |
|
| 1907 | 1919 | } else { |
|
| 1908 | 1920 | try expect(p, scanner::TokenKind::LBrace, "expected `{` before record body"); |
|
| 1909 | 1921 | let fields = try parseRecordFields(p, RecordFieldMode::Labeled); |
|
| 1910 | 1922 | return node(p, ast::NodeValue::RecordDecl( |
|
| 1911 | 1923 | ast::RecordDecl { |
|
| 1912 | - | name, fields, attrs, regions, derives, labeled: true, |
|
| 1924 | + | name, fields, attrs: recordAttrs, regions, derives, labeled: true, |
|
| 1913 | 1925 | } |
|
| 1914 | 1926 | )); |
|
| 1915 | 1927 | } |
|
| 1916 | 1928 | } |
|
| 1917 | 1929 |
lib/std/lang/parser/tests.rad
+33 -0
| 955 | 955 | try expectIntType(itemType, 4, ast::Signedness::Signed); |
|
| 956 | 956 | try expectNumber(length, "4"); |
|
| 957 | 957 | } |
|
| 958 | 958 | ||
| 959 | 959 | /// Test parsing a named record declaration without derives. |
|
| 960 | + | /// Opaque record modifiers preserve exports, regions, and ownership markers. |
|
| 961 | + | @test unsafe fn testParseOpaqueRecord() throws (testing::TestError) { |
|
| 962 | + | for source, i in [ |
|
| 963 | + | "opaque record R { value: u32 }", |
|
| 964 | + | "export opaque record R: 'r + Copy { value: &'r u32 }", |
|
| 965 | + | "opaque record R: Copy(u32);", |
|
| 966 | + | ] { |
|
| 967 | + | let node = try! parseStmtStr(source); |
|
| 968 | + | let case ast::NodeValue::RecordDecl(decl) = node.value else throw testing::TestError::Failed; |
|
| 969 | + | let attrs = decl.attrs else throw testing::TestError::Failed; |
|
| 970 | + | assert ast::attributesContains(&attrs, ast::Attribute::Opaque); |
|
| 971 | + | assert ast::attributesContains(&attrs, ast::Attribute::Export) == (i == 1); |
|
| 972 | + | assert decl.labeled == (i <> 2); |
|
| 973 | + | assert decl.fields.len == 1; |
|
| 974 | + | assert decl.regions.len == (1 if i == 1 else 0); |
|
| 975 | + | assert decl.derives.len == (0 if i == 0 else 1); |
|
| 976 | + | static PRINT_STORAGE: [u8; 4096] = [0; 4096]; |
|
| 977 | + | let mut printArena = alloc::new(&mut PRINT_STORAGE[..]); |
|
| 978 | + | let printed = printer::toExpr(&mut printArena, node); |
|
| 979 | + | let case sexpr::Expr::Block { name, .. } = printed else throw testing::TestError::Failed; |
|
| 980 | + | assert name == "opaque-record"; |
|
| 981 | + | } |
|
| 982 | + | for source in [ |
|
| 983 | + | "opaque fn f() {}", |
|
| 984 | + | "opaque union R { Value }", |
|
| 985 | + | "opaque opaque record R {}", |
|
| 986 | + | "opaque export record R {}", |
|
| 987 | + | ] { |
|
| 988 | + | let parsed = try? parseStmtStr(source); |
|
| 989 | + | assert parsed == nil; |
|
| 990 | + | } |
|
| 991 | + | } |
|
| 992 | + | ||
| 960 | 993 | @test unsafe fn testParseRecordDecl() throws (testing::TestError) { |
|
| 961 | 994 | let node = try! parseStmtStr("record R { x: bool, y: i32 }"); |
|
| 962 | 995 | let case ast::NodeValue::RecordDecl(decl) = node.value |
|
| 963 | 996 | else throw testing::TestError::Failed; |
|
| 964 | 997 |
lib/std/lang/resolver.rad
+30 -0
| 6 | 6 | export mod printer; |
|
| 7 | 7 | ||
| 8 | 8 | /// Unit tests for the resolver. |
|
| 9 | 9 | @test mod tests; |
|
| 10 | 10 | ||
| 11 | + | /// Tests for module-owned record representations. |
|
| 12 | + | @test mod opaqueTests; |
|
| 13 | + | ||
| 11 | 14 | // TODO: Move to raw vectors to reduce list duplication? |
|
| 12 | 15 | // TODO: When a function declaration fails to typecheck, it should still "exist". |
|
| 13 | 16 | // TODO: `ensureNominalResolved` should just run when you call `typeFor`. |
|
| 14 | 17 | // TODO: Have different types for positional vs. named field records. |
|
| 15 | 18 |
| 103 | 106 | methods: *unsafe mut [*unsafe mut Symbol], |
|
| 104 | 107 | } |
|
| 105 | 108 | ||
| 106 | 109 | /// An entry in the method registry. |
|
| 107 | 110 | export record MethodEntry: Copy { |
|
| 111 | + | /// Module where the method is defined. |
|
| 112 | + | moduleId: u16, |
|
| 108 | 113 | /// Concrete type that owns the method. |
|
| 109 | 114 | concreteType: Type, |
|
| 110 | 115 | /// Name of the concrete type. |
|
| 111 | 116 | concreteTypeName: *[u8], |
|
| 112 | 117 | /// Method name. |
| 179 | 184 | length: u32, |
|
| 180 | 185 | } |
|
| 181 | 186 | ||
| 182 | 187 | /// Record nominal type. |
|
| 183 | 188 | export record RecordType: Copy { |
|
| 189 | + | /// Module that can access the representation, or `nil` for public fields. |
|
| 190 | + | privateModule: ?u16, |
|
| 184 | 191 | /// Region parameters of the source declaration. |
|
| 185 | 192 | regions: ?*RegionScope, |
|
| 186 | 193 | /// Exact region arguments, if this is an applied type. |
|
| 187 | 194 | application: ?*unsafe NominalApplication, |
|
| 188 | 195 | fields: *unsafe [RecordField], |
| 509 | 516 | TypeMismatch(TypeMismatch), |
|
| 510 | 517 | /// Numeric literal does not fit within the required range. |
|
| 511 | 518 | NumericLiteralOverflow, |
|
| 512 | 519 | /// Record literal omitted a required field. |
|
| 513 | 520 | RecordFieldMissing(*[u8]), |
|
| 521 | + | /// Record representation is private to another module. |
|
| 522 | + | OpaqueRecordAccess, |
|
| 514 | 523 | /// Record literal referenced a field that does not exist. |
|
| 515 | 524 | RecordFieldUnknown(*[u8]), |
|
| 516 | 525 | /// Brace syntax used on unlabeled record. |
|
| 517 | 526 | RecordFieldStyleMismatch, |
|
| 518 | 527 | /// Record literal supplied the wrong number of fields. |
| 1477 | 1486 | fieldType, |
|
| 1478 | 1487 | offset: field.offset, |
|
| 1479 | 1488 | }, allocator); |
|
| 1480 | 1489 | } |
|
| 1481 | 1490 | set *applied.view = NominalType::Record(RecordType { |
|
| 1491 | + | privateModule: body.privateModule, |
|
| 1482 | 1492 | regions: body.regions, |
|
| 1483 | 1493 | application: applied, |
|
| 1484 | 1494 | fields: (&fields[..]) as *unsafe [RecordField], |
|
| 1485 | 1495 | labeled: body.labeled, |
|
| 1486 | 1496 | layout: body.layout, |
| 4881 | 4891 | let recordLayout = Layout { |
|
| 4882 | 4892 | size: mem::alignUp(layout.size, layout.alignment), |
|
| 4883 | 4893 | alignment: layout.alignment |
|
| 4884 | 4894 | }; |
|
| 4885 | 4895 | return RecordType { |
|
| 4896 | + | privateModule: nil, |
|
| 4886 | 4897 | regions: nil, |
|
| 4887 | 4898 | application: nil, |
|
| 4888 | 4899 | fields: (&result[..]) as *unsafe [RecordField], |
|
| 4889 | 4900 | labeled, |
|
| 4890 | 4901 | layout: allocLayout(self, recordLayout), |
| 4940 | 4951 | let mut recordType = try resolveRecordFields(self, node, decl.fields, decl.labeled) catch error { |
|
| 4941 | 4952 | set *nominalTy = NominalType::Placeholder(node); |
|
| 4942 | 4953 | throw error; |
|
| 4943 | 4954 | }; |
|
| 4944 | 4955 | set recordType.regions = self.regionScope; |
|
| 4956 | + | if ast::hasAttribute(sym.attrs, ast::Attribute::Opaque) { |
|
| 4957 | + | set recordType.privateModule = self.currentMod; |
|
| 4958 | + | } |
|
| 4945 | 4959 | if markers.copy { |
|
| 4946 | 4960 | for field in recordType.fields { |
|
| 4947 | 4961 | if not isCopy(field.fieldType) { |
|
| 4948 | 4962 | throw emitError(self, node, ErrorKind::CopyContainsNonCopy); |
|
| 4949 | 4963 | } |
| 5637 | 5651 | // Register in the method table. |
|
| 5638 | 5652 | if self.methodsLen >= MAX_METHODS { |
|
| 5639 | 5653 | throw emitError(self, node, ErrorKind::Internal); |
|
| 5640 | 5654 | } |
|
| 5641 | 5655 | set self.methods[self.methodsLen] = MethodEntry { |
|
| 5656 | + | moduleId: self.currentMod, |
|
| 5642 | 5657 | concreteType, |
|
| 5643 | 5658 | concreteTypeName: typeName, |
|
| 5644 | 5659 | name: methodName, |
|
| 5645 | 5660 | fnType: allocFnType(self, checkFnType), |
|
| 5646 | 5661 | mutable: receiverMut, |
| 6680 | 6695 | } |
|
| 6681 | 6696 | setNodeType(self, pattern, subjectTy); |
|
| 6682 | 6697 | try bindRecordPatternFields(self, pattern, body, matchBy); |
|
| 6683 | 6698 | } |
|
| 6684 | 6699 | ||
| 6700 | + | /// Require access to a record's module-owned representation. |
|
| 6701 | + | fn requireRecordAccess 'arena (self: &mut Resolver 'arena, node: *ast::Node, recordType: RecordType) |
|
| 6702 | + | throws (ResolveError) |
|
| 6703 | + | { |
|
| 6704 | + | if let owner = recordType.privateModule; owner <> self.currentMod { |
|
| 6705 | + | throw emitError(self, node, ErrorKind::OpaqueRecordAccess); |
|
| 6706 | + | } |
|
| 6707 | + | } |
|
| 6708 | + | ||
| 6685 | 6709 | /// Bind record pattern fields to variables in the current scope. |
|
| 6686 | 6710 | unsafe fn bindRecordPatternFields 'arena ( |
|
| 6687 | 6711 | self: &mut Resolver 'arena, |
|
| 6688 | 6712 | pattern: *ast::Node, |
|
| 6689 | 6713 | recInfo: RecordType, |
|
| 6690 | 6714 | matchBy: MatchBy |
|
| 6691 | 6715 | ) throws (ResolveError) { |
|
| 6716 | + | try requireRecordAccess(self, pattern, recInfo); |
|
| 6692 | 6717 | match pattern.value { |
|
| 6693 | 6718 | case ast::NodeValue::Call(call) => { |
|
| 6694 | 6719 | // Unlabeled patterns: `S(x, y)`. |
|
| 6695 | 6720 | try checkRecordArity(self, CountMismatch { expected: recInfo.fields.len, actual: call.args.len }, pattern); |
|
| 6696 | 6721 |
| 7986 | 8011 | throws (ResolveError) |
|
| 7987 | 8012 | { |
|
| 7988 | 8013 | let case NominalType::Record(recInfo) = *recordType |
|
| 7989 | 8014 | else panic "resolveRecordConstructorCall: not a record type"; |
|
| 7990 | 8015 | ||
| 8016 | + | try requireRecordAccess(self, node, recInfo); |
|
| 7991 | 8017 | try checkRecordConstructorArgs(self, node, call.args, recInfo); |
|
| 7992 | 8018 | return setNodeType(self, node, Type::Nominal(recordType)); |
|
| 7993 | 8019 | } |
|
| 7994 | 8020 | ||
| 7995 | 8021 | /// Resolve the type name of a record literal, handling both record types and |
| 8073 | 8099 | // Lazily resolve record body if not yet done. |
|
| 8074 | 8100 | try ensureNominalResolved(self, tyInfo, typeIdent); |
|
| 8075 | 8101 | let case NominalType::Record(recordType) = *tyInfo |
|
| 8076 | 8102 | else throw emitError(self, node, ErrorKind::ExpectedRecord); |
|
| 8077 | 8103 | ||
| 8104 | + | try requireRecordAccess(self, node, recordType); |
|
| 8078 | 8105 | // Unlabeled records must use constructor call syntax `R(...)`, not brace syntax. |
|
| 8079 | 8106 | if not recordType.labeled { |
|
| 8080 | 8107 | throw emitError(self, node, ErrorKind::RecordFieldStyleMismatch); |
|
| 8081 | 8108 | } |
|
| 8082 | 8109 | // Check field count. With `{ .. }` syntax, fewer fields are allowed. |
| 8132 | 8159 | } |
|
| 8133 | 8160 | let targetInfo = hintInfo else { |
|
| 8134 | 8161 | throw emitError(self, node, ErrorKind::CannotInferType); |
|
| 8135 | 8162 | }; |
|
| 8136 | 8163 | ||
| 8164 | + | try requireRecordAccess(self, node, targetInfo); |
|
| 8137 | 8165 | // Check field count. |
|
| 8138 | 8166 | if lit.fields.len <> targetInfo.fields.len { |
|
| 8139 | 8167 | if lit.fields.len < targetInfo.fields.len { |
|
| 8140 | 8168 | let missingName = targetInfo.fields[lit.fields.len].name else panic; |
|
| 8141 | 8169 | throw emitError(self, node, ErrorKind::RecordFieldMissing(missingName)); |
| 8351 | 8379 | match subjectTy { |
|
| 8352 | 8380 | case Type::Nominal(NominalType::Record(recordType)) => { |
|
| 8353 | 8381 | let fieldNode = access.child; |
|
| 8354 | 8382 | let fieldName = try nodeName(self, fieldNode); |
|
| 8355 | 8383 | if let fieldIndex = findRecordField(&recordType.fields[..], fieldName) { |
|
| 8384 | + | try requireRecordAccess(self, node, recordType); |
|
| 8356 | 8385 | let fieldTy = recordType.fields[fieldIndex].fieldType; |
|
| 8357 | 8386 | setRecordFieldIndex(self, fieldNode, fieldIndex); |
|
| 8358 | 8387 | return setNodeType(self, node, fieldTy); |
|
| 8359 | 8388 | } |
|
| 8360 | 8389 | // Not a field: check for a standalone method. |
| 8904 | 8933 | return setNodeType(self, node, *target); |
|
| 8905 | 8934 | } |
|
| 8906 | 8935 | // Auto-deref for single-field unlabeled records. |
|
| 8907 | 8936 | if let case Type::Nominal(NominalType::Record(recInfo)) = operandTy { |
|
| 8908 | 8937 | if not recInfo.labeled and recInfo.fields.len == 1 { |
|
| 8938 | + | try requireRecordAccess(self, node, recInfo); |
|
| 8909 | 8939 | let fieldTy = recInfo.fields[0].fieldType; |
|
| 8910 | 8940 | setRecordFieldIndex(self, node, 0); |
|
| 8911 | 8941 | return setNodeType(self, node, fieldTy); |
|
| 8912 | 8942 | } |
|
| 8913 | 8943 | } |
lib/std/lang/resolver/opaqueTests.rad
added
+98 -0
| 1 | + | //! Module-owned record representation tests. |
|
| 2 | + | ||
| 3 | + | use std::testing; |
|
| 4 | + | use std::io; |
|
| 5 | + | use std::lang::ast; |
|
| 6 | + | use std::lang::resolver; |
|
| 7 | + | use super::tests; |
|
| 8 | + | ||
| 9 | + | /// Types used to check access to module-owned record representations. |
|
| 10 | + | constant OPAQUE_RECORD_MODEL: *[u8] = "export opaque record Value: Copy { number: u32 } export opaque record Word: Copy(u32); export opaque record Empty: Copy {} export opaque record View: 'r + Copy { number: &'r cell u32 } opaque record Hidden: Copy { number: u32 } export fn hidden() -> Hidden { return Hidden { number: 42 }; } export union Wrapped: Copy { Value(Value) }"; |
|
| 11 | + | ||
| 12 | + | /// External record operations cannot expose or construct private fields. |
|
| 13 | + | @test unsafe fn testOpaqueRecordAccess() throws (testing::TestError) { |
|
| 14 | + | for program in [ |
|
| 15 | + | "mod model; fn f(p: model::Value) -> u32 { return p.number; }", |
|
| 16 | + | "mod model; fn f(p: &model::Value) -> u32 { return p.number; }", |
|
| 17 | + | "mod model; fn f(p: &mut model::Value) { set p.number = 1; }", |
|
| 18 | + | "mod model; fn f(p: &mut model::Value) { set p.number += 1; }", |
|
| 19 | + | "mod model; fn f(p: &model::Value) { let field = &p.number; }", |
|
| 20 | + | "mod model; fn f(p: &mut model::Value) { let field = &mut p.number; }", |
|
| 21 | + | "mod model; unsafe fn f(p: model::Value) -> u32 { return p.number; }", |
|
| 22 | + | "mod model; fn f() -> u32 { return model::hidden().number; }", |
|
| 23 | + | "mod model; fn f() -> model::Value { return model::Value { number: 1 }; }", |
|
| 24 | + | "mod model; fn f() -> model::Value { return { number: 1 }; }", |
|
| 25 | + | "mod model; fn f() -> ?model::Value { return { number: 1 }; }", |
|
| 26 | + | "mod model; fn f() -> model::Value { return model::Value { .. }; }", |
|
| 27 | + | "mod model; fn f() -> model::Empty { return model::Empty {}; }", |
|
| 28 | + | "mod model; fn f() -> model::Word { return model::Word(1); }", |
|
| 29 | + | "mod model; fn f(p: model::Value) { let case model::Value { number } = p else panic; }", |
|
| 30 | + | "mod model; fn f(p: &model::Value) { let case model::Value { number } = p else panic; }", |
|
| 31 | + | "mod model; fn f(p: model::Value) { let case model::Value { .. } = p else panic; }", |
|
| 32 | + | "mod model; fn f(p: model::Value) { if let case model::Value { number } = p; number > 0 {} }", |
|
| 33 | + | "mod model; fn f(p: model::Value) { while let case model::Value { number } = p { break; } }", |
|
| 34 | + | "mod model; fn f(p: model::Value) { match p { case model::Value { number } => {} } }", |
|
| 35 | + | "mod model; fn f(p: model::Wrapped) { match p { case model::Wrapped::Value(model::Value { number }) => {} } }", |
|
| 36 | + | "mod model; fn f(p: model::Word) { let case model::Word(number) = p else panic; }", |
|
| 37 | + | "mod model; fn f(p: model::Word) -> u32 { return *p; }", |
|
| 38 | + | "mod model; fn f(p: &mut model::Word) { set **p = 2; }", |
|
| 39 | + | "mod model; fn f(p: model::Word) { let field = &*p; }", |
|
| 40 | + | "mod model; fn f 'r (p: &model::View 'r) -> u32 { return *p.number; }", |
|
| 41 | + | "mod model; fn f 'r (p: &model::View 'r) { set *p.number = 1; }", |
|
| 42 | + | "mod model; fn f 'r (number: &'r cell u32) -> model::View 'r { return model::View 'r { number }; }", |
|
| 43 | + | "mod model; fn f 'r (number: &'r cell u32) -> model::View 'r { return { number }; }", |
|
| 44 | + | "mod model; fn f 'r (p: model::View 'r) { let case model::View 'r { number } = p else panic; }", |
|
| 45 | + | "mod model; use root::model::*; fn f(p: Value) -> u32 { return p.number; }", |
|
| 46 | + | ] { |
|
| 47 | + | let mut storage = tests::testArena(); |
|
| 48 | + | let memory: 'test = &mut storage in { |
|
| 49 | + | let mut res = tests::testResolver(memory); |
|
| 50 | + | let mut arena = ast::nodeArena(&mut tests::AST_ARENA[..]); |
|
| 51 | + | let root = try tests::registerModule(&mut tests::MODULE_GRAPH, nil, "root", program, &mut arena); |
|
| 52 | + | let model = try tests::registerModule(&mut tests::MODULE_GRAPH, root, "model", OPAQUE_RECORD_MODEL, &mut arena); |
|
| 53 | + | let result = try tests::resolveModuleTree(&mut res, root); |
|
| 54 | + | try tests::expectErrorKind(&result, resolver::ErrorKind::OpaqueRecordAccess) catch error { |
|
| 55 | + | io::print(program); |
|
| 56 | + | io::print("\n"); |
|
| 57 | + | resolver::printer::printDiagnostics(&result.diagnostics, &res); |
|
| 58 | + | throw error; |
|
| 59 | + | }; |
|
| 60 | + | } |
|
| 61 | + | } |
|
| 62 | + | } |
|
| 63 | + | ||
| 64 | + | /// Child and sibling modules cannot access an opaque record's fields. |
|
| 65 | + | @test unsafe fn testOpaqueRecordModuleBoundary() throws (testing::TestError) { |
|
| 66 | + | for child in [false, true] { |
|
| 67 | + | let mut storage = tests::testArena(); |
|
| 68 | + | let memory: 'test = &mut storage in { |
|
| 69 | + | let mut res = tests::testResolver(memory); |
|
| 70 | + | let mut arena = ast::nodeArena(&mut tests::AST_ARENA[..]); |
|
| 71 | + | let root = try tests::registerModule(&mut tests::MODULE_GRAPH, nil, "root", "export mod model; mod client;", &mut arena); |
|
| 72 | + | let model = try tests::registerModule(&mut tests::MODULE_GRAPH, root, "model", "mod child; export opaque record Value: Copy { number: u32 }", &mut arena); |
|
| 73 | + | let program = "use root::model; fn f(p: model::Value) -> u32 { return p.number; }"; |
|
| 74 | + | let nested = try tests::registerModule(&mut tests::MODULE_GRAPH, model, "child", program if child else "", &mut arena); |
|
| 75 | + | let client = try tests::registerModule(&mut tests::MODULE_GRAPH, root, "client", "" if child else program, &mut arena); |
|
| 76 | + | let result = try tests::resolveModuleTree(&mut res, root); |
|
| 77 | + | try tests::expectErrorKind(&result, resolver::ErrorKind::OpaqueRecordAccess) catch error { |
|
| 78 | + | resolver::printer::printDiagnostics(&result.diagnostics, &res); |
|
| 79 | + | throw error; |
|
| 80 | + | }; |
|
| 81 | + | } |
|
| 82 | + | } |
|
| 83 | + | } |
|
| 84 | + | ||
| 85 | + | /// Defining modules can access opaque representations through all source forms. |
|
| 86 | + | @test unsafe fn testOpaqueRecordLocalAccess() throws (testing::TestError) { |
|
| 87 | + | let mut storage = tests::testArena(); |
|
| 88 | + | let memory: 'test = &mut storage in { |
|
| 89 | + | let mut res = tests::testResolver(memory); |
|
| 90 | + | let result = try tests::resolveProgramStr(&mut res, |
|
| 91 | + | "opaque record Value: Copy { number: u32 } opaque record Word: Copy(u32); opaque record View: 'r + Copy { number: &'r cell u32 } fn f() { let mut p = Value { number: 1 }; set p.number += 2; let q: Value = { number: p.number }; let r: ?Value = { number: 4 }; let case Value { number } = q else panic; if let case Value { number: n } = p; n > 0 {} while let case Value { number: n } = p { break; } match p { case Value { number: n } => {} else => {} } let mut w = Word(1); set *w = 2; { let field = &mut *w; set *field = 3; } let case Word(n) = w else panic; } fn g 'r (number: &'r cell u32) -> View 'r { let p = View 'r { number }; set *p.number = 42; let case View 'r { number: value } = p else panic; return { number: value }; }" |
|
| 92 | + | ); |
|
| 93 | + | try tests::expectNoErrors(&result) catch error { |
|
| 94 | + | resolver::printer::printDiagnostics(&result.diagnostics, &res); |
|
| 95 | + | throw error; |
|
| 96 | + | }; |
|
| 97 | + | } |
|
| 98 | + | } |
lib/std/lang/resolver/printer.rad
+3 -0
| 631 | 631 | io::print("record field out of order"); |
|
| 632 | 632 | } |
|
| 633 | 633 | case super::ErrorKind::OpaqueTypeNotAllowed => { |
|
| 634 | 634 | io::print("opaque type not allowed"); |
|
| 635 | 635 | } |
|
| 636 | + | case super::ErrorKind::OpaqueRecordAccess => { |
|
| 637 | + | io::print("opaque record representation is private to its defining module"); |
|
| 638 | + | } |
|
| 636 | 639 | case super::ErrorKind::OpaqueTypeDeref => { |
|
| 637 | 640 | io::print("opaque types cannot be dereferenced"); |
|
| 638 | 641 | } |
|
| 639 | 642 | case super::ErrorKind::OpaquePointerArithmetic => { |
|
| 640 | 643 | io::print("opaque pointer arithmetic is not allowed"); |
lib/std/lang/resolver/tests.rad
+4 -4
| 368 | 368 | } |
|
| 369 | 369 | } |
|
| 370 | 370 | } |
|
| 371 | 371 | ||
| 372 | 372 | /// AST arena storage used by resolver tests. |
|
| 373 | - | static AST_ARENA: [u8; 2097152] = [0; 2097152]; |
|
| 373 | + | export static AST_ARENA: [u8; 2097152] = [0; 2097152]; |
|
| 374 | 374 | ||
| 375 | 375 | /// Resolver arena storage used by resolver tests. |
|
| 376 | 376 | static ARENA_STORAGE: [u8; 2097152] = [0; 2097152]; |
|
| 377 | 377 | ||
| 378 | 378 | /// Node metadata storage used by resolver tests. |
| 386 | 386 | ||
| 387 | 387 | /// Module entries used by resolver tests. |
|
| 388 | 388 | unsafe static MODULE_ENTRIES: [?*module::ModuleEntry; 8] = [nil; 8]; |
|
| 389 | 389 | ||
| 390 | 390 | /// Module graph used by resolver tests. |
|
| 391 | - | unsafe static MODULE_GRAPH: module::ModuleGraph = undefined; |
|
| 391 | + | export unsafe static MODULE_GRAPH: module::ModuleGraph = undefined; |
|
| 392 | 392 | ||
| 393 | 393 | /// Module AST arena storage used by resolver tests. |
|
| 394 | 394 | static MODULE_ARENA_STORAGE: [u8; 16384] = [0; 16384]; |
|
| 395 | 395 | ||
| 396 | 396 | /// Module AST arena used by resolver tests. |
| 519 | 519 | root: analysis.root, |
|
| 520 | 520 | }; |
|
| 521 | 521 | } |
|
| 522 | 522 | ||
| 523 | 523 | /// Resolve a module with the full resolution process. |
|
| 524 | - | unsafe fn resolveModuleTree 'arena ( |
|
| 524 | + | export unsafe fn resolveModuleTree 'arena ( |
|
| 525 | 525 | res: &mut super::Resolver 'arena, |
|
| 526 | 526 | rootId: u16 |
|
| 527 | 527 | ) -> TestResult throws (testing::TestError) { |
|
| 528 | 528 | let root = module::get(&MODULE_GRAPH, rootId) |
|
| 529 | 529 | else throw testing::TestError::Failed; |
| 539 | 539 | return TestResult { diagnostics, root: rootAst }; |
|
| 540 | 540 | } |
|
| 541 | 541 | ||
| 542 | 542 | /// Register a module in the graph and attach a parsed AST to it. |
|
| 543 | 543 | /// If parentId is nil, registers as a root module. |
|
| 544 | - | unsafe fn registerModule( |
|
| 544 | + | export unsafe fn registerModule( |
|
| 545 | 545 | graph: &mut module::ModuleGraph, |
|
| 546 | 546 | parentId: ?u16, |
|
| 547 | 547 | name: *[u8], |
|
| 548 | 548 | code: *[u8], |
|
| 549 | 549 | arena: &mut ast::NodeArena |
std.lib.test
+1 -0
| 17 | 17 | lib/std/arch/rv64/image/tests.rad |
|
| 18 | 18 | lib/std/arch/rv64/shared/tests.rad |
|
| 19 | 19 | lib/std/arch/rv64/bounds.rad |
|
| 20 | 20 | lib/std/arch/rv64/atomicTests.rad |
|
| 21 | 21 | lib/std/lang/resolver/tests/regions.rad |
|
| 22 | + | lib/std/lang/resolver/opaqueTests.rad |
test/tests/record.opaque.graph.rad
added
+20 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | //! session-support |
|
| 3 | + | //! Exercise cyclic construction and a consuming freeze API. |
|
| 4 | + | use std::lang::alloc; |
|
| 5 | + | mod model; |
|
| 6 | + | ||
| 7 | + | /// Build and freeze a cycle while its allocation session is live. |
|
| 8 | + | @default fn main() -> u32 { |
|
| 9 | + | // Backing storage for the two-node graph. |
|
| 10 | + | static DATA: [u8; 1024] = [0; 1024]; |
|
| 11 | + | let mut arena = alloc::new(&mut DATA[..]); |
|
| 12 | + | use arena as graph in { |
|
| 13 | + | let mut builder = try! model::create(&graph); |
|
| 14 | + | model::complete(&mut builder); |
|
| 15 | + | let frozen = model::freeze(builder); |
|
| 16 | + | assert model::read(&frozen) == 42; |
|
| 17 | + | } |
|
| 18 | + | alloc::reset(&mut arena); |
|
| 19 | + | return 0; |
|
| 20 | + | } |
test/tests/record.opaque.graph/model.rad
added
+63 -0
| 1 | + | //! Pointer-based cyclic definitions with a consuming builder API. |
|
| 2 | + | use std::lang::alloc; |
|
| 3 | + | ||
| 4 | + | /// State of one definition in the graph. |
|
| 5 | + | union State: 'g + Copy { |
|
| 6 | + | /// Forward reference that has not been completed. |
|
| 7 | + | Pending, |
|
| 8 | + | /// Value and next definition. |
|
| 9 | + | Ready { |
|
| 10 | + | /// Number stored by this definition. |
|
| 11 | + | value: u32, |
|
| 12 | + | /// Next definition in the cycle. |
|
| 13 | + | next: &'g cell State 'g, |
|
| 14 | + | }, |
|
| 15 | + | } |
|
| 16 | + | ||
| 17 | + | /// Affine construction capability for two linked definitions. |
|
| 18 | + | export opaque record Builder: 'g { |
|
| 19 | + | /// First definition slot. |
|
| 20 | + | first: &'g cell State 'g, |
|
| 21 | + | /// Second definition slot. |
|
| 22 | + | second: &'g cell State 'g, |
|
| 23 | + | /// Exclusive capability consumed by freeze. |
|
| 24 | + | permit: &'g mut u8, |
|
| 25 | + | } |
|
| 26 | + | ||
| 27 | + | /// Shared access to completed definitions. |
|
| 28 | + | export opaque record Frozen: 'g + Copy { |
|
| 29 | + | /// Entry point into the graph. |
|
| 30 | + | root: &'g cell State 'g, |
|
| 31 | + | /// Shared lifetime witness for the graph capability. |
|
| 32 | + | permit: &'g u8, |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Allocate forward-reference slots and a unique construction capability. |
|
| 36 | + | export fn create 'g (session: &Session 'g) -> Builder 'g throws (alloc::AllocError) { |
|
| 37 | + | let first = try session.new(State 'g::Pending); |
|
| 38 | + | let second = try session.new(State 'g::Pending); |
|
| 39 | + | let permit = try session.new(0 as u8); |
|
| 40 | + | return Builder 'g { first: &cell *first, second: &cell *second, permit }; |
|
| 41 | + | } |
|
| 42 | + | ||
| 43 | + | /// Complete the two-node cycle through the construction capability. |
|
| 44 | + | export fn complete 'g (builder: &mut Builder 'g) { |
|
| 45 | + | set *builder.first = State 'g::Ready { value: 20, next: builder.second }; |
|
| 46 | + | set *builder.second = State 'g::Ready { value: 22, next: builder.first }; |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | /// Consume construction authority after verifying the cycle is complete. |
|
| 50 | + | export fn freeze 'g (builder: Builder 'g) -> Frozen 'g { |
|
| 51 | + | let case State 'g::Ready { next, .. } = *builder.first else panic; |
|
| 52 | + | let case State 'g::Ready { next: back, .. } = *builder.second else panic; |
|
| 53 | + | assert next == builder.second and back == builder.first; |
|
| 54 | + | let case Builder 'g { first, second: _, permit } = builder else panic; |
|
| 55 | + | return Frozen 'g { root: first, permit }; |
|
| 56 | + | } |
|
| 57 | + | ||
| 58 | + | /// Read the values reachable from a frozen root. |
|
| 59 | + | export fn read 'g (graph: &Frozen 'g) -> u32 { |
|
| 60 | + | let case State 'g::Ready { value, next } = *graph.root else return 0; |
|
| 61 | + | let case State 'g::Ready { value: other, .. } = *next else return 0; |
|
| 62 | + | return value + other; |
|
| 63 | + | } |
test/tests/record.opaque.rad
added
+21 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | //! Opaque records expose operations across module boundaries. |
|
| 3 | + | ||
| 4 | + | mod model; |
|
| 5 | + | ||
| 6 | + | /// Exercise construction, mutation, copying, and region-bearing wrappers. |
|
| 7 | + | @default fn main() -> u32 { |
|
| 8 | + | let mut value = model::create(20); |
|
| 9 | + | assert value.read() == 20; |
|
| 10 | + | value.add(22); |
|
| 11 | + | let copy = value; |
|
| 12 | + | assert copy.read() == 42; |
|
| 13 | + | assert model::unpack(copy) == 42; |
|
| 14 | + | assert model::readWord(model::word(42)) == 42; |
|
| 15 | + | let number: u32 = 42; |
|
| 16 | + | let scope: 'r = &number in { |
|
| 17 | + | let view = model::view(scope); |
|
| 18 | + | assert model::readView(&view) == 42; |
|
| 19 | + | } |
|
| 20 | + | return 0; |
|
| 21 | + | } |
test/tests/record.opaque/model.rad
added
+57 -0
| 1 | + | //! Module-owned record representations and their public operations. |
|
| 2 | + | ||
| 3 | + | /// Number accessed through module operations. |
|
| 4 | + | export opaque record Value: Copy { |
|
| 5 | + | /// Stored number. |
|
| 6 | + | number: u32, |
|
| 7 | + | } |
|
| 8 | + | ||
| 9 | + | /// Positional number wrapper. |
|
| 10 | + | export opaque record Word: Copy(u32); |
|
| 11 | + | ||
| 12 | + | /// Number borrowed for a region. |
|
| 13 | + | export opaque record View: 'r + Copy { |
|
| 14 | + | /// Borrowed number. |
|
| 15 | + | number: &'r u32, |
|
| 16 | + | } |
|
| 17 | + | ||
| 18 | + | /// Construct a number. |
|
| 19 | + | export fn create(number: u32) -> Value { |
|
| 20 | + | return { number }; |
|
| 21 | + | } |
|
| 22 | + | ||
| 23 | + | /// Read the stored number. |
|
| 24 | + | export fn (self: &Value) read() -> u32 { |
|
| 25 | + | return self.number; |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | /// Add to the stored number. |
|
| 29 | + | export fn (self: &mut Value) add(amount: u32) { |
|
| 30 | + | set self.number += amount; |
|
| 31 | + | } |
|
| 32 | + | ||
| 33 | + | /// Read a number through a record pattern. |
|
| 34 | + | export fn unpack(value: Value) -> u32 { |
|
| 35 | + | let case Value { number } = value else panic; |
|
| 36 | + | return number; |
|
| 37 | + | } |
|
| 38 | + | ||
| 39 | + | /// Construct a positional wrapper. |
|
| 40 | + | export fn word(number: u32) -> Word { |
|
| 41 | + | return Word(number); |
|
| 42 | + | } |
|
| 43 | + | ||
| 44 | + | /// Read a positional wrapper. |
|
| 45 | + | export fn readWord(value: Word) -> u32 { |
|
| 46 | + | return *value; |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | /// Wrap a regional borrow. |
|
| 50 | + | export fn view 'r (number: &'r u32) -> View 'r { |
|
| 51 | + | return View 'r { number }; |
|
| 52 | + | } |
|
| 53 | + | ||
| 54 | + | /// Read a regional wrapper. |
|
| 55 | + | export fn readView 'r (value: &View 'r) -> u32 { |
|
| 56 | + | return *value.number; |
|
| 57 | + | } |