lang: Parse generic declaration binders
8ffa307ca5c1e1d096881b96fd38dcf6f15a78def1eb4f887ddac4e30245c541
Keep declaration binders and type arguments in source order. Parse bounds as trait paths with arguments and print declaration binders in the positions where they are declared. Assisted-by: Codex:gpt-6 Change-Id: I8d8df134794a43a0aaea6238e1214ee6ccefc732
1 parent
15b2a873
compiler/radiance/radiance.rad
+5 -1
| 1302 | 1302 | try attrList.append(attrNode, arena.heap); |
|
| 1303 | 1303 | try attrList.append(unsafeAttr, arena.heap); |
|
| 1304 | 1304 | let fnAttrs = ast::Attributes { list: attrList }; |
|
| 1305 | 1305 | ||
| 1306 | 1306 | return try ast::synthNode(arena, ast::NodeValue::FnDecl(ast::FnDecl { |
|
| 1307 | - | name: fnName, sig: fnSig, body: fnBody, attrs: fnAttrs |
|
| 1307 | + | name: fnName, |
|
| 1308 | + | binders: &[], |
|
| 1309 | + | sig: try ast::declSignature(arena, fnSig), |
|
| 1310 | + | body: fnBody, |
|
| 1311 | + | attrs: fnAttrs, |
|
| 1308 | 1312 | })); |
|
| 1309 | 1313 | } |
|
| 1310 | 1314 | ||
| 1311 | 1315 | /// Build a module node with a declaration appended to its statement list. |
|
| 1312 | 1316 | fn injectIntoModule( |
lib/std/lang/ast.rad
+36 -14
| 367 | 367 | }, |
|
| 368 | 368 | /// Nominal type, points to identifier node. |
|
| 369 | 369 | Nominal { |
|
| 370 | 370 | /// Type name path. |
|
| 371 | 371 | path: *Node, |
|
| 372 | - | /// Region arguments in declaration order. |
|
| 373 | - | regions: *[*Node], |
|
| 372 | + | /// Type and region arguments in declaration order. |
|
| 373 | + | args: *[*Node], |
|
| 374 | 374 | }, |
|
| 375 | 375 | /// Inline record type for union variant payloads. |
|
| 376 | 376 | Record { |
|
| 377 | 377 | /// Field declaration nodes. |
|
| 378 | 378 | fields: *[*Node], |
| 671 | 671 | ||
| 672 | 672 | /// Record declaration. |
|
| 673 | 673 | export record RecordDecl: Copy { |
|
| 674 | 674 | /// Identifier naming the record. |
|
| 675 | 675 | name: *Node, |
|
| 676 | - | /// Region parameters in declaration order. |
|
| 677 | - | regions: *[*Node], |
|
| 676 | + | /// Type and region binders in declaration order. |
|
| 677 | + | binders: *[*Node], |
|
| 678 | 678 | /// Field declaration nodes. |
|
| 679 | 679 | fields: *[*Node], |
|
| 680 | 680 | /// Optional attribute list applied to the record. |
|
| 681 | 681 | attrs: ?Attributes, |
|
| 682 | 682 | /// Trait derivations attached to the record. |
| 687 | 687 | ||
| 688 | 688 | /// Union declarations. |
|
| 689 | 689 | export record UnionDecl: Copy { |
|
| 690 | 690 | /// Identifier naming the union. |
|
| 691 | 691 | name: *Node, |
|
| 692 | - | /// Region parameters in declaration order. |
|
| 693 | - | regions: *[*Node], |
|
| 692 | + | /// Type and region binders in declaration order. |
|
| 693 | + | binders: *[*Node], |
|
| 694 | 694 | /// Variant nodes making up the union. |
|
| 695 | 695 | variants: *[*Node], |
|
| 696 | 696 | /// Optional attribute list applied to the union. |
|
| 697 | 697 | attrs: ?Attributes, |
|
| 698 | 698 | /// Trait derivations attached to the union. |
| 713 | 713 | ||
| 714 | 714 | /// Function declaration. |
|
| 715 | 715 | export record FnDecl: Copy { |
|
| 716 | 716 | /// Identifier naming the function. |
|
| 717 | 717 | name: *Node, |
|
| 718 | + | /// Type and region binders in declaration order. |
|
| 719 | + | binders: *[*Node], |
|
| 718 | 720 | /// Function type signature. |
|
| 719 | - | sig: FnSig, |
|
| 721 | + | sig: *FnSig, |
|
| 720 | 722 | /// Executable body, absent for provider and foreign declarations. |
|
| 721 | 723 | body: ?*Node, |
|
| 722 | 724 | /// Optional attribute list applied to the function. |
|
| 723 | 725 | attrs: ?Attributes, |
|
| 724 | 726 | } |
| 948 | 950 | /// Alignment value. |
|
| 949 | 951 | value: *Node, |
|
| 950 | 952 | }, |
|
| 951 | 953 | /// Catch clause within a try expression. |
|
| 952 | 954 | CatchClause(CatchClause), |
|
| 955 | + | /// Type parameter in a declaration's binder list. |
|
| 956 | + | TypeBinder { |
|
| 957 | + | /// Identifier naming the parameter. |
|
| 958 | + | name: *Node, |
|
| 959 | + | /// The optional trait application that bounds the parameter. |
|
| 960 | + | bound: ?*Node, |
|
| 961 | + | }, |
|
| 953 | 962 | /// Trait declaration. |
|
| 954 | 963 | TraitDecl { |
|
| 955 | 964 | /// Trait name identifier. |
|
| 956 | 965 | name: *Node, |
|
| 966 | + | /// Type and region binders in declaration order. |
|
| 967 | + | binders: *[*Node], |
|
| 957 | 968 | /// Supertrait name nodes. |
|
| 958 | 969 | supertraits: *[*Node], |
|
| 959 | 970 | /// Method signature nodes ([`TraitMethodSig`]). |
|
| 960 | 971 | methods: *[*Node], |
|
| 961 | 972 | /// Optional attributes. |
| 966 | 977 | /// Method name identifier. |
|
| 967 | 978 | name: *Node, |
|
| 968 | 979 | /// Receiver type node (eg. `*mut Allocator`). |
|
| 969 | 980 | receiver: *Node, |
|
| 970 | 981 | /// Function signature. |
|
| 971 | - | sig: FnSig, |
|
| 982 | + | sig: *FnSig, |
|
| 972 | 983 | /// Optional declaration modifiers. |
|
| 973 | 984 | attrs: ?Attributes, |
|
| 974 | 985 | }, |
|
| 975 | 986 | /// Instance block. |
|
| 976 | 987 | InstanceDecl { |
|
| 988 | + | /// Type and region binders in declaration order. |
|
| 989 | + | binders: *[*Node], |
|
| 977 | 990 | /// Trait name identifier. |
|
| 978 | 991 | traitName: *Node, |
|
| 979 | 992 | /// Target type identifier. |
|
| 980 | 993 | targetType: *Node, |
|
| 981 | 994 | /// Method definition nodes ([`MethodDecl`]). |
| 984 | 997 | /// Method definition with a receiver. |
|
| 985 | 998 | /// Used both inside `instance` blocks and as standalone methods. |
|
| 986 | 999 | MethodDecl { |
|
| 987 | 1000 | /// Method name identifier. |
|
| 988 | 1001 | name: *Node, |
|
| 1002 | + | /// Type and region binders in declaration order. |
|
| 1003 | + | binders: *[*Node], |
|
| 989 | 1004 | /// Receiver binding name ([`Ident`] node). |
|
| 990 | 1005 | receiverName: *Node, |
|
| 991 | 1006 | /// Receiver type node (eg. `*mut Arena`). |
|
| 992 | 1007 | receiverType: *Node, |
|
| 993 | 1008 | /// Function signature. |
|
| 994 | - | sig: FnSig, |
|
| 1009 | + | sig: *FnSig, |
|
| 995 | 1010 | /// Executable body, absent in a provider interface. |
|
| 996 | 1011 | body: ?*Node, |
|
| 997 | 1012 | /// Optional attribute list. |
|
| 998 | 1013 | attrs: ?Attributes, |
|
| 999 | 1014 | }, |
| 1052 | 1067 | case TypeSig::Slice { region, .. } => return region, |
|
| 1053 | 1068 | else => return nil, |
|
| 1054 | 1069 | } |
|
| 1055 | 1070 | } |
|
| 1056 | 1071 | ||
| 1057 | - | /// Return the region arguments of a nominal type node. |
|
| 1058 | - | export fn nominalRegions(type: *Node) -> *[*Node] { |
|
| 1072 | + | /// Return the arguments of a nominal type node. |
|
| 1073 | + | export fn nominalArgs(type: *Node) -> *[*Node] { |
|
| 1059 | 1074 | let case NodeValue::TypeSig(sig) = type.value else return &[]; |
|
| 1060 | - | let case TypeSig::Nominal { regions, .. } = sig else return &[]; |
|
| 1061 | - | return regions; |
|
| 1075 | + | let case TypeSig::Nominal { args, .. } = sig else return &[]; |
|
| 1076 | + | return args; |
|
| 1062 | 1077 | } |
|
| 1063 | 1078 | ||
| 1064 | 1079 | /// Check whether a node is a place expression. |
|
| 1065 | 1080 | /// |
|
| 1066 | 1081 | /// Place expressions have persistent storage and can appear on the left side |
| 1091 | 1106 | ||
| 1092 | 1107 | set arena.nextId = nodeId + 1; |
|
| 1093 | 1108 | return &mut nodes[0]; |
|
| 1094 | 1109 | } |
|
| 1095 | 1110 | ||
| 1111 | + | /// Store a declaration signature in the node arena. |
|
| 1112 | + | export fn declSignature(arena: &mut NodeArena, sig: FnSig) -> *FnSig throws (AllocError) { |
|
| 1113 | + | let mut signatures: *mut [FnSig] = &mut []; |
|
| 1114 | + | try signatures.append(sig, arena.heap); |
|
| 1115 | + | return &signatures[0]; |
|
| 1116 | + | } |
|
| 1117 | + | ||
| 1096 | 1118 | /// Allocate a synthetic AST node with a zero-length span. |
|
| 1097 | 1119 | /// Throws `AllocError` if the arena is full. |
|
| 1098 | 1120 | export fn synthNode(arena: &mut NodeArena, value: NodeValue) -> *mut Node throws (AllocError) { |
|
| 1099 | 1121 | return try allocNode(arena, Span { offset: 0, length: 0 }, value); |
|
| 1100 | 1122 | } |
| 1176 | 1198 | let fnSig = FnSig { params, returnType: nil, throwList }; |
|
| 1177 | 1199 | let fnBody: *Node = try synthNode(arena, NodeValue::Block( |
|
| 1178 | 1200 | Block { statements: bodyStmts, isUnsafe: false } |
|
| 1179 | 1201 | )); |
|
| 1180 | 1202 | let fnDecl = try synthNode(arena, NodeValue::FnDecl(FnDecl { |
|
| 1181 | - | name: fnName, sig: fnSig, body: fnBody, attrs: nil |
|
| 1203 | + | name: fnName, binders: &[], sig: try declSignature(arena, fnSig), body: fnBody, attrs: nil |
|
| 1182 | 1204 | })); |
|
| 1183 | 1205 | let mut rootStmts: *mut [*Node] = &mut []; |
|
| 1184 | 1206 | try rootStmts.append(fnDecl, arena.heap); |
|
| 1185 | 1207 | let modBody = try synthNode(arena, NodeValue::Module( |
|
| 1186 | 1208 | Module { statements: rootStmts } |
lib/std/lang/ast/printer.rad
+40 -24
| 38 | 38 | let head = pointerClassHead(class, "ptr", "ref", "unsafe-ptr"); |
|
| 39 | 39 | return sexpr::list(a, head, pointerTail(a, region, mutable, toExpr(a, valueType))); |
|
| 40 | 40 | } |
|
| 41 | 41 | case super::TypeSig::Optional { valueType } => |
|
| 42 | 42 | return sexpr::list(a, "?", &[toExpr(a, valueType)]), |
|
| 43 | - | case super::TypeSig::Nominal { path, regions } => { |
|
| 44 | - | if regions.len == 0 { |
|
| 43 | + | case super::TypeSig::Nominal { path, args } => { |
|
| 44 | + | if args.len == 0 { |
|
| 45 | 45 | return toExpr(a, path); |
|
| 46 | 46 | } |
|
| 47 | 47 | let mut parts: *mut [sexpr::Expr] = &mut []; |
|
| 48 | 48 | try! parts.append(toExpr(a, path), a); |
|
| 49 | - | for region in regions { |
|
| 50 | - | try! parts.append(sexpr::list(a, "region", &[toExpr(a, region)]), a); |
|
| 49 | + | for arg in args { |
|
| 50 | + | let head = "region" if isRegionName(arg) else "type"; |
|
| 51 | + | try! parts.append(sexpr::list(a, head, &[toExpr(a, arg)]), a); |
|
| 51 | 52 | } |
|
| 52 | 53 | return sexpr::list(a, "nominal", &parts[..]); |
|
| 53 | 54 | } |
|
| 54 | 55 | case super::TypeSig::Record { fields, labeled } => |
|
| 55 | 56 | return sexpr::list(a, "record" if labeled else "tuple", nodeListToExprs( |
| 339 | 340 | for arg, i in c.args { |
|
| 340 | 341 | set buf[i + 1] = toExpr(a, arg); |
|
| 341 | 342 | } |
|
| 342 | 343 | return sexpr::Expr::List { head: "call", tail: buf, multiline: false }; |
|
| 343 | 344 | } |
|
| 345 | + | case super::NodeValue::TypeBinder { name, bound } => |
|
| 346 | + | return sexpr::list(a, "binder", &[toExpr(a, name), toExprOrNull(a, bound)]), |
|
| 344 | 347 | case super::NodeValue::BuiltinCall { kind, args } => |
|
| 345 | 348 | return sexpr::list(a, super::builtinText(kind), nodeListToExprs(a, args)), |
|
| 346 | 349 | case super::NodeValue::Bracket { base, items } => { |
|
| 347 | 350 | let buf = try! sexpr::allocExprs(a, items.len as u32 + 1); |
|
| 348 | 351 | set buf[0] = toExpr(a, base); |
| 573 | 576 | case super::NodeValue::ExprStmt(e) => return toExpr(a, e), |
|
| 574 | 577 | else => panic "statementToExpr: expected a statement", |
|
| 575 | 578 | } |
|
| 576 | 579 | } |
|
| 577 | 580 | ||
| 581 | + | /// Check if a parameter or argument names a region. |
|
| 582 | + | fn isRegionName(node: *super::Node) -> bool { |
|
| 583 | + | let case super::NodeValue::Ident(_) = node.value else return false; |
|
| 584 | + | return true; |
|
| 585 | + | } |
|
| 586 | + | ||
| 587 | + | /// Attach an ordered list of binders to a declaration name. |
|
| 588 | + | fn withBinders(a: &heap::Heap, name: sexpr::Expr, binders: *[*super::Node]) -> sexpr::Expr { |
|
| 589 | + | let mut result = name; |
|
| 590 | + | for binder in binders { |
|
| 591 | + | let head = "region" if isRegionName(binder) else "type"; |
|
| 592 | + | set result = sexpr::list(a, head, &[result, toExpr(a, binder)]); |
|
| 593 | + | } |
|
| 594 | + | return result; |
|
| 595 | + | } |
|
| 596 | + | ||
| 578 | 597 | /// Convert a declaration node to an S-expression. |
|
| 579 | 598 | fn declarationToExpr(a: &heap::Heap, node: *super::Node) -> sexpr::Expr { |
|
| 580 | 599 | match node.value { |
|
| 581 | 600 | case super::NodeValue::ConstDecl(decl) => |
|
| 582 | 601 | return sexpr::list( |
| 598 | 617 | ), |
|
| 599 | 618 | case super::NodeValue::FnDecl(f) => { |
|
| 600 | 619 | let params = sexpr::list(a, "params", nodeListToExprs(a, f.sig.params)); |
|
| 601 | 620 | let ret = toExprOrNull(a, f.sig.returnType); |
|
| 602 | 621 | let attrs = attributesToExpr(a, f.attrs); |
|
| 603 | - | let thrown = throwsToExpr(a, &f.sig); |
|
| 604 | - | let name = toExpr(a, f.name); |
|
| 622 | + | let thrown = throwsToExpr(a, f.sig); |
|
| 623 | + | let name = withBinders(a, toExpr(a, f.name), f.binders); |
|
| 605 | 624 | if let body = f.body { |
|
| 606 | 625 | return sexpr::block(a, "fn", &[name, params, ret, attrs, thrown], &[ |
|
| 607 | 626 | toExpr(a, body) |
|
| 608 | 627 | ]); |
|
| 609 | 628 | } |
| 629 | 648 | } |
|
| 630 | 649 | let derives = nodeListToExprs(a, r.derives); |
|
| 631 | 650 | let unlabeled = sexpr::Expr::Null |
|
| 632 | 651 | if r.labeled |
|
| 633 | 652 | else sexpr::sym("tuple"); |
|
| 634 | - | let mut name = toExpr(a, r.name); |
|
| 635 | - | for region in r.regions { |
|
| 636 | - | set name = sexpr::list(a, "region", &[name, toExpr(a, region)]); |
|
| 637 | - | } |
|
| 653 | + | let name = withBinders(a, toExpr(a, r.name), r.binders); |
|
| 638 | 654 | return sexpr::block( |
|
| 639 | 655 | a, |
|
| 640 | 656 | head, |
|
| 641 | 657 | &[ |
|
| 642 | 658 | name, attributesToExpr(a, r.attrs), |
| 648 | 664 | case super::NodeValue::RecordField { field, type, value } => { |
|
| 649 | 665 | return fieldToExpr(a, field, type, value); |
|
| 650 | 666 | } |
|
| 651 | 667 | case super::NodeValue::UnionDecl(u_) => { |
|
| 652 | 668 | let children = variantListToExprs(a, u_.variants); |
|
| 653 | - | let mut name = toExpr(a, u_.name); |
|
| 654 | - | for region in u_.regions { |
|
| 655 | - | set name = sexpr::list(a, "region", &[name, toExpr(a, region)]); |
|
| 656 | - | } |
|
| 669 | + | let name = withBinders(a, toExpr(a, u_.name), u_.binders); |
|
| 657 | 670 | return sexpr::block( |
|
| 658 | 671 | a, |
|
| 659 | 672 | "union", |
|
| 660 | 673 | &[ |
|
| 661 | 674 | name, attributesToExpr(a, u_.attrs), |
| 666 | 679 | } |
|
| 667 | 680 | case super::NodeValue::UnionDeclVariant(v) => { |
|
| 668 | 681 | return variantToExpr(a, v.name, v.type, v.value); |
|
| 669 | 682 | } |
|
| 670 | 683 | case super::NodeValue::TraitDecl { |
|
| 671 | - | name, supertraits, methods, attrs |
|
| 684 | + | name, binders, supertraits, methods, attrs |
|
| 672 | 685 | } => { |
|
| 673 | 686 | let children = nodeListToExprs(a, methods); |
|
| 674 | 687 | let supers = sexpr::list(a, "supertraits", nodeListToExprs(a, supertraits)); |
|
| 675 | 688 | return sexpr::block( |
|
| 676 | 689 | a, |
|
| 677 | 690 | "trait", |
|
| 678 | 691 | &[ |
|
| 679 | - | toExpr(a, name), supers, attributesToExpr(a, attrs) |
|
| 692 | + | withBinders(a, toExpr(a, name), binders), supers, attributesToExpr(a, attrs) |
|
| 680 | 693 | ], |
|
| 681 | 694 | children |
|
| 682 | 695 | ); |
|
| 683 | 696 | } |
|
| 684 | 697 | case super::NodeValue::TraitMethodSig { name, receiver, sig, attrs } => { |
| 688 | 701 | return sexpr::list( |
|
| 689 | 702 | a, |
|
| 690 | 703 | "methodSig", |
|
| 691 | 704 | &[ |
|
| 692 | 705 | attributes, toExpr(a, receiver), toExpr(a, name), params, |
|
| 693 | - | ret, throwsToExpr(a, &sig), |
|
| 706 | + | ret, throwsToExpr(a, sig), |
|
| 694 | 707 | ] |
|
| 695 | 708 | ); |
|
| 696 | 709 | } |
|
| 697 | - | case super::NodeValue::InstanceDecl { traitName, targetType, methods } => { |
|
| 710 | + | case super::NodeValue::InstanceDecl { binders, traitName, targetType, methods } => { |
|
| 698 | 711 | let children = nodeListToExprs(a, methods); |
|
| 699 | 712 | return sexpr::block( |
|
| 700 | - | a, "instance", &[toExpr(a, traitName), toExpr(a, targetType)], children |
|
| 713 | + | a, |
|
| 714 | + | "instance", |
|
| 715 | + | &[withBinders(a, toExpr(a, traitName), binders), toExpr(a, targetType)], |
|
| 716 | + | children |
|
| 701 | 717 | ); |
|
| 702 | 718 | } |
|
| 703 | 719 | case super::NodeValue::MethodDecl { |
|
| 704 | - | name, receiverName, receiverType, sig, body, attrs |
|
| 720 | + | name, binders, receiverName, receiverType, sig, body, attrs |
|
| 705 | 721 | } => { |
|
| 706 | 722 | let params = sexpr::list(a, "params", nodeListToExprs(a, sig.params)); |
|
| 707 | 723 | let ret = toExprOrNull(a, sig.returnType); |
|
| 708 | 724 | let attributes = attributesToExpr(a, attrs); |
|
| 709 | 725 | return sexpr::block( |
|
| 710 | 726 | a, |
|
| 711 | 727 | "method", |
|
| 712 | 728 | &[ |
|
| 713 | 729 | attributes, |
|
| 714 | - | toExpr(a, receiverType), |
|
| 715 | - | toExpr(a, receiverName), |
|
| 716 | - | toExpr(a, name), |
|
| 730 | + | toExpr(a, receiverType), |
|
| 731 | + | toExpr(a, receiverName), |
|
| 732 | + | withBinders(a, toExpr(a, name), binders), |
|
| 717 | 733 | params, |
|
| 718 | 734 | ret, |
|
| 719 | - | throwsToExpr(a, &sig), |
|
| 735 | + | throwsToExpr(a, sig), |
|
| 720 | 736 | ], |
|
| 721 | 737 | &[toExprOrNull(a, body)] |
|
| 722 | 738 | ); |
|
| 723 | 739 | } |
|
| 724 | 740 | else => panic "declarationToExpr: expected a declaration", |
lib/std/lang/lower.rad
+4 -4
| 1279 | 1279 | try lowerDataDecl(low, node, decl.value, true, declLinkage(decl.attrs)); |
|
| 1280 | 1280 | } |
|
| 1281 | 1281 | case ast::NodeValue::StaticDecl(decl) => { |
|
| 1282 | 1282 | try lowerDataDecl(low, node, decl.value, false, declLinkage(decl.attrs)); |
|
| 1283 | 1283 | } |
|
| 1284 | - | case ast::NodeValue::InstanceDecl { traitName, targetType, methods } => { |
|
| 1284 | + | case ast::NodeValue::InstanceDecl { traitName, targetType, methods, .. } => { |
|
| 1285 | 1285 | try lowerInstanceDecl(low, node, traitName, targetType, methods, sink); |
|
| 1286 | 1286 | } |
|
| 1287 | 1287 | case ast::NodeValue::MethodDecl { |
|
| 1288 | - | name, receiverName, receiverType, sig, body, attrs |
|
| 1288 | + | name, receiverName, receiverType, sig, body, attrs, .. |
|
| 1289 | 1289 | } => { |
|
| 1290 | 1290 | let executable = body else throw LowerError::MissingMetadata; |
|
| 1291 | - | if let f = try lowerMethodDecl(low, node, name, receiverName, &sig, executable) { |
|
| 1291 | + | if let f = try lowerMethodDecl(low, node, name, receiverName, sig, executable) { |
|
| 1292 | 1292 | try emitFunction(low, f, FnRole::Normal, declLinkage(attrs), sink); |
|
| 1293 | 1293 | } |
|
| 1294 | 1294 | } |
|
| 1295 | 1295 | else => {} |
|
| 1296 | 1296 | } |
| 1986 | 1986 | let case ast::NodeValue::Ident(mName) = name.value else { |
|
| 1987 | 1987 | throw LowerError::ExpectedIdentifier; |
|
| 1988 | 1988 | }; |
|
| 1989 | 1989 | let qualName = try instanceMethodName(self, nil, typeName, mName); |
|
| 1990 | 1990 | let executable = body else throw LowerError::MissingMetadata; |
|
| 1991 | - | let func = try lowerMethod(self, methodNode, qualName, receiverName, &sig, executable) |
|
| 1991 | + | let func = try lowerMethod(self, methodNode, qualName, receiverName, sig, executable) |
|
| 1992 | 1992 | else continue; |
|
| 1993 | 1993 | try emitFunction(self, func, FnRole::Normal, Linkage::Local, sink); |
|
| 1994 | 1994 | ||
| 1995 | 1995 | let method = resolver::findTraitMethod(&self.resolver, traitInfo, mName) |
|
| 1996 | 1996 | else panic "lowerInstanceDecl: method not found in trait"; |
lib/std/lang/parser.rad
+93 -43
| 102 | 102 | ||
| 103 | 103 | /// A function signature with the trivia before the `)` that ends its |
|
| 104 | 104 | /// parameters, and whether its `throws` clause starts a line. |
|
| 105 | 105 | record ParsedSig: Copy { |
|
| 106 | 106 | /// The function signature. |
|
| 107 | - | sig: ast::FnSig, |
|
| 107 | + | sig: *ast::FnSig, |
|
| 108 | 108 | /// Trivia before the `)` of the parameters. The node that holds the |
|
| 109 | 109 | /// signature attaches it as closing trivia once created. |
|
| 110 | 110 | closing: *[Pending], |
|
| 111 | 111 | /// Whether the `throws` clause starts on a later line than the token before |
|
| 112 | 112 | /// it. Always false when trivia is discarded. |
| 1978 | 1978 | throw failParsing(p, "reserved keyword cannot be used as an identifier"); |
|
| 1979 | 1979 | } |
|
| 1980 | 1980 | throw failParsing(p, err); |
|
| 1981 | 1981 | } |
|
| 1982 | 1982 | ||
| 1983 | - | /// Parse a list of region names, eg. `['r, 's]`, from the current `[`. |
|
| 1984 | - | fn parseRegions(p: &mut Parser) -> *[*ast::Node] throws (ParseError, AllocError) { |
|
| 1985 | - | try advance(p); |
|
| 1986 | - | let mut regions: *mut [*ast::Node] = &mut []; |
|
| 1987 | - | while check(p, scanner::TokenKind::Region) { |
|
| 1988 | - | try regions.append(try parseRegion(p), p.arena.heap); |
|
| 1983 | + | /// Parse one binder in a declaration's parameter list. |
|
| 1984 | + | fn parseBinder(p: &mut Parser) -> *ast::Node throws (ParseError, AllocError) { |
|
| 1985 | + | if check(p, scanner::TokenKind::Region) { |
|
| 1986 | + | return try parseRegion(p); |
|
| 1987 | + | } |
|
| 1988 | + | let name = try parseIdent(p, "expected type or region parameter"); |
|
| 1989 | + | let mut bound: ?*ast::Node = nil; |
|
| 1990 | + | if try consume(p, scanner::TokenKind::Colon) { |
|
| 1991 | + | set bound = try parseAppliedPath(p); |
|
| 1992 | + | } |
|
| 1993 | + | return try node(p, ast::NodeValue::TypeBinder { name, bound }); |
|
| 1994 | + | } |
|
| 1989 | 1995 | ||
| 1990 | - | if not try consume(p, scanner::TokenKind::Comma) { |
|
| 1996 | + | /// Parse a declaration's optional ordered type and region binders. |
|
| 1997 | + | fn parseBinders(p: &mut Parser) -> *[*ast::Node] |
|
| 1998 | + | throws (ParseError, AllocError) |
|
| 1999 | + | { |
|
| 2000 | + | if p.current.kind <> scanner::TokenKind::LBracket { |
|
| 2001 | + | return &[]; |
|
| 2002 | + | } |
|
| 2003 | + | try advance(p); |
|
| 2004 | + | let mut binders: *mut [*ast::Node] = &mut []; |
|
| 2005 | + | loop { |
|
| 2006 | + | try binders.append(try parseBinder(p), p.arena.heap); |
|
| 2007 | + | if not try consume(p, scanner::TokenKind::Comma) |
|
| 2008 | + | or check(p, scanner::TokenKind::RBracket) |
|
| 2009 | + | { |
|
| 1991 | 2010 | break; |
|
| 1992 | 2011 | } |
|
| 1993 | 2012 | } |
|
| 1994 | - | if regions.len == 0 { |
|
| 1995 | - | throw failParsing(p, "expected region name"); |
|
| 1996 | - | } |
|
| 1997 | - | try expect(p, scanner::TokenKind::RBracket, "expected `]` after region names"); |
|
| 1998 | - | ||
| 1999 | - | return regions; |
|
| 2013 | + | try expect(p, scanner::TokenKind::RBracket, "expected `]` after parameters"); |
|
| 2014 | + | return binders; |
|
| 2000 | 2015 | } |
|
| 2001 | 2016 | ||
| 2002 | 2017 | /// Parse the current region token, eg. `'r`, into an identifier node |
|
| 2003 | 2018 | /// holding its name with the quote, which keeps it apart from value names. |
|
| 2004 | 2019 | fn parseRegion(p: &mut Parser) -> *ast::Node throws (AllocError) { |
| 2184 | 2199 | set recordAttrs = ast::Attributes { list }; |
|
| 2185 | 2200 | } |
|
| 2186 | 2201 | try expect(p, scanner::TokenKind::Record, "expected `record`"); |
|
| 2187 | 2202 | ||
| 2188 | 2203 | let name = try parseIdent(p, "expected record name"); |
|
| 2189 | - | let mut regions: *[*ast::Node] = &[]; |
|
| 2190 | - | if check(p, scanner::TokenKind::LBracket) { |
|
| 2191 | - | set regions = try parseRegions(p); |
|
| 2192 | - | } |
|
| 2204 | + | let binders = try parseBinders(p); |
|
| 2193 | 2205 | if try consume(p, scanner::TokenKind::LParen) { |
|
| 2194 | 2206 | let fields = try parseRecordFields(p, RecordFieldMode::Unlabeled); |
|
| 2195 | 2207 | let derives = try parseDerives(p); |
|
| 2196 | 2208 | try expect(p, scanner::TokenKind::Semicolon, "expected `;` after record"); |
|
| 2197 | 2209 | let decl: *ast::Node = try node(p, ast::NodeValue::RecordDecl( |
|
| 2198 | - | ast::RecordDecl { name, regions, fields, attrs: recordAttrs, derives, labeled: false } |
|
| 2210 | + | ast::RecordDecl { name, binders, fields, attrs: recordAttrs, derives, labeled: false } |
|
| 2199 | 2211 | )); |
|
| 2200 | 2212 | try attachClosing(p, decl); |
|
| 2201 | 2213 | ||
| 2202 | 2214 | return decl; |
|
| 2203 | 2215 | } |
|
| 2204 | 2216 | let derives = try parseDerives(p); |
|
| 2205 | 2217 | try expect(p, scanner::TokenKind::LBrace, "expected `{` before record body"); |
|
| 2206 | 2218 | let fields = try parseRecordFields(p, RecordFieldMode::Labeled); |
|
| 2207 | 2219 | let decl: *ast::Node = try node(p, ast::NodeValue::RecordDecl( |
|
| 2208 | - | ast::RecordDecl { name, regions, fields, attrs: recordAttrs, derives, labeled: true } |
|
| 2220 | + | ast::RecordDecl { name, binders, fields, attrs: recordAttrs, derives, labeled: true } |
|
| 2209 | 2221 | )); |
|
| 2210 | 2222 | try attachClosing(p, decl); |
|
| 2211 | 2223 | ||
| 2212 | 2224 | return decl; |
|
| 2213 | 2225 | } |
| 2218 | 2230 | throws (ParseError, AllocError) |
|
| 2219 | 2231 | { |
|
| 2220 | 2232 | try expect(p, scanner::TokenKind::Union, "expected `union`"); |
|
| 2221 | 2233 | ||
| 2222 | 2234 | let name = try parseIdent(p, "expected union name"); |
|
| 2223 | - | let mut regions: *[*ast::Node] = &[]; |
|
| 2224 | - | if check(p, scanner::TokenKind::LBracket) { |
|
| 2225 | - | set regions = try parseRegions(p); |
|
| 2226 | - | } |
|
| 2235 | + | let binders = try parseBinders(p); |
|
| 2227 | 2236 | let derives = try parseDerives(p); |
|
| 2228 | 2237 | ||
| 2229 | 2238 | try expect(p, scanner::TokenKind::LBrace, "expected `{` before union body"); |
|
| 2230 | 2239 | let outer = openList(p); |
|
| 2231 | 2240 |
| 2281 | 2290 | } |
|
| 2282 | 2291 | } |
|
| 2283 | 2292 | try closeList(p, outer, &variants[..]); |
|
| 2284 | 2293 | try expect(p, scanner::TokenKind::RBrace, "expected `}`"); |
|
| 2285 | 2294 | let decl: *ast::Node = try node(p, ast::NodeValue::UnionDecl( |
|
| 2286 | - | ast::UnionDecl { name, regions, variants, attrs, derives } |
|
| 2295 | + | ast::UnionDecl { name, binders, variants, attrs, derives } |
|
| 2287 | 2296 | )); |
|
| 2288 | 2297 | try attachClosing(p, decl); |
|
| 2289 | 2298 | ||
| 2290 | 2299 | return decl; |
|
| 2291 | 2300 | } |
| 2385 | 2394 | let stacked = check(p, scanner::TokenKind::Throws) and startsLine(p); |
|
| 2386 | 2395 | let mut brokenThrows = false; |
|
| 2387 | 2396 | let throwList = try parseThrowList(p, &mut brokenThrows); |
|
| 2388 | 2397 | ||
| 2389 | 2398 | return ParsedSig { |
|
| 2390 | - | sig: ast::FnSig { params, returnType, throwList }, |
|
| 2399 | + | sig: try ast::declSignature(&mut p.arena, ast::FnSig { params, returnType, throwList }), |
|
| 2391 | 2400 | closing, |
|
| 2392 | 2401 | stacked, |
|
| 2393 | 2402 | brokenThrows, |
|
| 2394 | 2403 | }; |
|
| 2395 | 2404 | } |
| 2451 | 2460 | fn parseFnDecl(p: &mut Parser, attrs: ?ast::Attributes) -> *ast::Node |
|
| 2452 | 2461 | throws (ParseError, AllocError) |
|
| 2453 | 2462 | { |
|
| 2454 | 2463 | try expect(p, scanner::TokenKind::Fn, "expected `fn`"); |
|
| 2455 | 2464 | ||
| 2456 | - | // Method syntax: `fn (recv: *Type) name(params) { body }`. |
|
| 2457 | - | if check(p, scanner::TokenKind::LParen) { |
|
| 2465 | + | // A method's binders precede its receiver. |
|
| 2466 | + | if check(p, scanner::TokenKind::LBracket) |
|
| 2467 | + | or check(p, scanner::TokenKind::LParen) |
|
| 2468 | + | { |
|
| 2469 | + | let binders = try parseBinders(p); |
|
| 2458 | 2470 | if let attrs = attrs; ast::attributesContains(&attrs, ast::Attribute::Constant) { |
|
| 2459 | 2471 | throw failParsing(p, "expected constant function name"); |
|
| 2460 | 2472 | } |
|
| 2461 | - | return try parseMethodDecl(p, attrs); |
|
| 2473 | + | return try parseMethodDecl(p, attrs, binders); |
|
| 2462 | 2474 | } |
|
| 2463 | 2475 | let name = try parseIdent(p, "expected function name"); |
|
| 2476 | + | let binders = try parseBinders(p); |
|
| 2464 | 2477 | let parsed = try parseFnTypeSig(p); |
|
| 2465 | 2478 | let mut body: ?*ast::Node = nil; |
|
| 2466 | 2479 | let mut fnAttrs = attrs; |
|
| 2467 | 2480 | ||
| 2468 | 2481 | let mut intrinsic = false; |
| 2492 | 2505 | throw failParsing(p, "expected `;` after interface function signature"); |
|
| 2493 | 2506 | } |
|
| 2494 | 2507 | set body = try parseBlock(p); |
|
| 2495 | 2508 | } |
|
| 2496 | 2509 | let decl: *ast::Node = try node(p, ast::NodeValue::FnDecl( |
|
| 2497 | - | ast::FnDecl { name, sig: parsed.sig, body, attrs: fnAttrs } |
|
| 2510 | + | ast::FnDecl { name, binders, sig: parsed.sig, body, attrs: fnAttrs } |
|
| 2498 | 2511 | )); |
|
| 2499 | 2512 | try attachSig(p, decl, parsed); |
|
| 2500 | 2513 | ||
| 2501 | 2514 | return decl; |
|
| 2502 | 2515 | } |
| 2538 | 2551 | if try consume(p, scanner::TokenKind::Opaque) { |
|
| 2539 | 2552 | if check(p, scanner::TokenKind::Ident) or check(p, scanner::TokenKind::Super) { |
|
| 2540 | 2553 | if region <> nil { |
|
| 2541 | 2554 | throw failParsing(p, "trait object references cannot name a region"); |
|
| 2542 | 2555 | } |
|
| 2543 | - | let traitName = try parseTypePath(p); |
|
| 2556 | + | let traitName = try parseAppliedPath(p); |
|
| 2544 | 2557 | return try node(p, ast::NodeValue::TypeSig( |
|
| 2545 | 2558 | ast::TypeSig::TraitObject { class, traitName, mutable } |
|
| 2546 | 2559 | )); |
|
| 2547 | 2560 | } |
|
| 2548 | 2561 | // Plain opaque target. |
| 2593 | 2606 | )); |
|
| 2594 | 2607 | } |
|
| 2595 | 2608 | return path; |
|
| 2596 | 2609 | } |
|
| 2597 | 2610 | ||
| 2611 | + | /// Parse a path with optional type or region arguments. |
|
| 2612 | + | fn parseAppliedPath(p: &mut Parser) -> *ast::Node |
|
| 2613 | + | throws (ParseError, AllocError) |
|
| 2614 | + | { |
|
| 2615 | + | let base = try parseTypePath(p); |
|
| 2616 | + | let items = try parseTypeArgs(p); |
|
| 2617 | + | if items.len == 0 { |
|
| 2618 | + | return base; |
|
| 2619 | + | } |
|
| 2620 | + | return try node(p, ast::NodeValue::Bracket { base, items }); |
|
| 2621 | + | } |
|
| 2622 | + | ||
| 2623 | + | /// Parse the optional arguments of a type or trait name. |
|
| 2624 | + | fn parseTypeArgs(p: &mut Parser) -> *[*ast::Node] |
|
| 2625 | + | throws (ParseError, AllocError) |
|
| 2626 | + | { |
|
| 2627 | + | if p.current.kind <> scanner::TokenKind::LBracket { |
|
| 2628 | + | return &[]; |
|
| 2629 | + | } |
|
| 2630 | + | try advance(p); |
|
| 2631 | + | let mut args: *mut [*ast::Node] = &mut []; |
|
| 2632 | + | loop { |
|
| 2633 | + | let item = try parseRegion(p) |
|
| 2634 | + | if check(p, scanner::TokenKind::Region) |
|
| 2635 | + | else try parseType(p); |
|
| 2636 | + | try args.append(item, p.arena.heap); |
|
| 2637 | + | if not try consume(p, scanner::TokenKind::Comma) |
|
| 2638 | + | or check(p, scanner::TokenKind::RBracket) |
|
| 2639 | + | { |
|
| 2640 | + | break; |
|
| 2641 | + | } |
|
| 2642 | + | } |
|
| 2643 | + | try expect(p, scanner::TokenKind::RBracket, "expected `]` after type arguments"); |
|
| 2644 | + | return args; |
|
| 2645 | + | } |
|
| 2646 | + | ||
| 2598 | 2647 | /// Parse a type annotation. |
|
| 2599 | 2648 | export fn parseType(p: &mut Parser) -> *ast::Node |
|
| 2600 | 2649 | throws (ParseError, AllocError) |
|
| 2601 | 2650 | { |
|
| 2602 | 2651 | match p.current.kind { |
| 2622 | 2671 | case scanner::TokenKind::LBracket => { |
|
| 2623 | 2672 | return try parseArrayType(p); |
|
| 2624 | 2673 | } |
|
| 2625 | 2674 | case scanner::TokenKind::Super, scanner::TokenKind::Ident => { |
|
| 2626 | 2675 | let path = try parseTypePath(p); |
|
| 2627 | - | let mut regions: *[*ast::Node] = &[]; |
|
| 2628 | - | if check(p, scanner::TokenKind::LBracket) { |
|
| 2629 | - | set regions = try parseRegions(p); |
|
| 2630 | - | } |
|
| 2676 | + | let args = try parseTypeArgs(p); |
|
| 2631 | 2677 | return try node(p, ast::NodeValue::TypeSig( |
|
| 2632 | - | ast::TypeSig::Nominal { path, regions } |
|
| 2678 | + | ast::TypeSig::Nominal { path, args } |
|
| 2633 | 2679 | )); |
|
| 2634 | 2680 | } |
|
| 2635 | 2681 | case scanner::TokenKind::U8 => { |
|
| 2636 | 2682 | try advance(p); |
|
| 2637 | 2683 | return try nodeTypeInt(p, 1, ast::Signedness::Unsigned); |
| 3014 | 3060 | fn parseTraitDecl(p: &mut Parser, attrs: ?ast::Attributes) -> *ast::Node |
|
| 3015 | 3061 | throws (ParseError, AllocError) |
|
| 3016 | 3062 | { |
|
| 3017 | 3063 | try expect(p, scanner::TokenKind::Trait, "expected `trait`"); |
|
| 3018 | 3064 | let name = try parseIdent(p, "expected trait name"); |
|
| 3065 | + | let binders = try parseBinders(p); |
|
| 3019 | 3066 | let supertraits = try parseDerives(p); |
|
| 3020 | 3067 | try expect(p, scanner::TokenKind::LBrace, "expected `{` after trait name"); |
|
| 3021 | 3068 | let outer = openList(p); |
|
| 3022 | 3069 | ||
| 3023 | 3070 | let mut methods = try ast::nodeSlice(&mut p.arena, ast::MAX_TRAIT_METHODS); |
| 3030 | 3077 | try methods.append(method, p.arena.heap); |
|
| 3031 | 3078 | } |
|
| 3032 | 3079 | try closeList(p, outer, &methods[..]); |
|
| 3033 | 3080 | try expect(p, scanner::TokenKind::RBrace, "expected `}` after trait methods"); |
|
| 3034 | 3081 | let decl: *ast::Node = try node(p, ast::NodeValue::TraitDecl { |
|
| 3035 | - | name, supertraits, methods, attrs |
|
| 3082 | + | name, binders, supertraits, methods, attrs |
|
| 3036 | 3083 | }); |
|
| 3037 | 3084 | try attachClosing(p, decl); |
|
| 3038 | 3085 | ||
| 3039 | 3086 | return decl; |
|
| 3040 | 3087 | } |
| 3070 | 3117 | /// Visibility is determined by the trait declaration itself. |
|
| 3071 | 3118 | fn parseInstanceDecl(p: &mut Parser) -> *ast::Node |
|
| 3072 | 3119 | throws (ParseError, AllocError) |
|
| 3073 | 3120 | { |
|
| 3074 | 3121 | try expect(p, scanner::TokenKind::Instance, "expected `instance`"); |
|
| 3075 | - | let traitName = try parseTypePath(p); |
|
| 3122 | + | let binders = try parseBinders(p); |
|
| 3123 | + | let traitName = try parseAppliedPath(p); |
|
| 3076 | 3124 | try expect(p, scanner::TokenKind::For, "expected `for` after trait name"); |
|
| 3077 | - | let targetType = try parseTypePath(p); |
|
| 3125 | + | let targetType = try parseAppliedPath(p); |
|
| 3078 | 3126 | try expect(p, scanner::TokenKind::LBrace, "expected `{` after target type"); |
|
| 3079 | 3127 | let outer = openList(p); |
|
| 3080 | 3128 | ||
| 3081 | 3129 | let mut methods = try ast::nodeSlice(&mut p.arena, ast::MAX_TRAIT_METHODS); |
|
| 3082 | 3130 |
| 3084 | 3132 | and not check(p, scanner::TokenKind::Eof) |
|
| 3085 | 3133 | { |
|
| 3086 | 3134 | let leading = try takePending(p, &methods[..]); |
|
| 3087 | 3135 | let attrs = try parseAttributes(p); |
|
| 3088 | 3136 | try expect(p, scanner::TokenKind::Fn, "expected `fn`"); |
|
| 3089 | - | let method = try parseMethodDecl(p, attrs); |
|
| 3137 | + | let method = try parseMethodDecl(p, attrs, &[]); |
|
| 3090 | 3138 | ||
| 3091 | 3139 | try attachLeading(p, method, leading); |
|
| 3092 | 3140 | try methods.append(method, p.arena.heap); |
|
| 3093 | 3141 | } |
|
| 3094 | 3142 | try closeList(p, outer, &methods[..]); |
|
| 3095 | 3143 | try expect(p, scanner::TokenKind::RBrace, "expected `}` after instance methods"); |
|
| 3096 | 3144 | let decl: *ast::Node = try node(p, ast::NodeValue::InstanceDecl { |
|
| 3097 | - | traitName, targetType, methods |
|
| 3145 | + | binders, traitName, targetType, methods |
|
| 3098 | 3146 | }); |
|
| 3099 | 3147 | try attachClosing(p, decl); |
|
| 3100 | 3148 | ||
| 3101 | 3149 | return decl; |
|
| 3102 | 3150 | } |
| 3104 | 3152 | /// Parse a method declaration with a receiver. |
|
| 3105 | 3153 | /// Syntax: `fn (t: *mut Type) fnord(<params>) -> ReturnType { body }` |
|
| 3106 | 3154 | /// |
|
| 3107 | 3155 | /// Used both inside `instance` blocks and as standalone methods at the top level. |
|
| 3108 | 3156 | /// Expects the `fn` token to have already been consumed. |
|
| 3109 | - | fn parseMethodDecl(p: &mut Parser, attrs: ?ast::Attributes) -> *ast::Node |
|
| 3157 | + | fn parseMethodDecl( |
|
| 3158 | + | p: &mut Parser, attrs: ?ast::Attributes, binders: *[*ast::Node] |
|
| 3159 | + | ) -> *ast::Node |
|
| 3110 | 3160 | throws (ParseError, AllocError) |
|
| 3111 | 3161 | { |
|
| 3112 | 3162 | try expect(p, scanner::TokenKind::LParen, "expected `(` before receiver"); |
|
| 3113 | 3163 | ||
| 3114 | 3164 | let receiverName = try parseIdent(p, "expected receiver name"); |
| 3127 | 3177 | ); |
|
| 3128 | 3178 | } else { |
|
| 3129 | 3179 | set body = try parseBlock(p); |
|
| 3130 | 3180 | } |
|
| 3131 | 3181 | let method: *ast::Node = try node(p, ast::NodeValue::MethodDecl { |
|
| 3132 | - | name, receiverName, receiverType, sig: parsed.sig, body, attrs |
|
| 3182 | + | name, binders, receiverName, receiverType, sig: parsed.sig, body, attrs |
|
| 3133 | 3183 | }); |
|
| 3134 | 3184 | try attachSig(p, method, parsed); |
|
| 3135 | 3185 | ||
| 3136 | 3186 | return method; |
|
| 3137 | 3187 | } |
lib/std/lang/parser/tests.rad
+55 -15
| 3659 | 3659 | try testing::expect(failed); |
|
| 3660 | 3660 | } |
|
| 3661 | 3661 | ||
| 3662 | 3662 | /// Assert that a type string parses to a nominal type with the given region. |
|
| 3663 | 3663 | fn expectNominalRegion(node: *ast::Node, region: *[u8]) throws (testing::TestError) { |
|
| 3664 | - | let regions = ast::nominalRegions(node); |
|
| 3664 | + | let regions = ast::nominalArgs(node); |
|
| 3665 | 3665 | assert regions.len == 1; |
|
| 3666 | 3666 | let r = regions[0]; |
|
| 3667 | 3667 | try expectIdent(r, region); |
|
| 3668 | 3668 | } |
|
| 3669 | 3669 |
| 3671 | 3671 | @test unsafe fn testParseRegionParamDecls() throws (testing::TestError) { |
|
| 3672 | 3672 | let labeled = try! parseStmtStr( |
|
| 3673 | 3673 | "record Request['in]: Copy { method: &'in [u8], body: &'in [u8] }" |
|
| 3674 | 3674 | ); |
|
| 3675 | 3675 | let case ast::NodeValue::RecordDecl(rec) = labeled.value else throw testing::TestError::Failed; |
|
| 3676 | - | assert rec.regions.len == 1; |
|
| 3677 | - | let r1 = rec.regions[0]; |
|
| 3676 | + | assert rec.binders.len == 1; |
|
| 3677 | + | let r1 = rec.binders[0]; |
|
| 3678 | 3678 | try expectIdent(r1, "'in"); |
|
| 3679 | 3679 | try expectIdent(rec.name, "Request"); |
|
| 3680 | 3680 | try testing::expect(rec.labeled); |
|
| 3681 | 3681 | try testing::expect(rec.derives.len == 1); |
|
| 3682 | 3682 | try testing::expect(rec.fields.len == 2); |
|
| 3683 | 3683 | ||
| 3684 | 3684 | let positional = try! parseStmtStr("record Pair['r](&'r u8, u32);"); |
|
| 3685 | 3685 | let case ast::NodeValue::RecordDecl(pair) = positional.value |
|
| 3686 | 3686 | else throw testing::TestError::Failed; |
|
| 3687 | - | assert pair.regions.len == 1; |
|
| 3688 | - | let r2 = pair.regions[0]; |
|
| 3687 | + | assert pair.binders.len == 1; |
|
| 3688 | + | let r2 = pair.binders[0]; |
|
| 3689 | 3689 | try expectIdent(r2, "'r"); |
|
| 3690 | 3690 | try testing::expect(not pair.labeled); |
|
| 3691 | 3691 | try testing::expect(pair.fields.len == 2); |
|
| 3692 | 3692 | ||
| 3693 | 3693 | let plain = try! parseStmtStr("record Plain { x: u32 }"); |
|
| 3694 | 3694 | let case ast::NodeValue::RecordDecl(plainRec) = plain.value |
|
| 3695 | 3695 | else throw testing::TestError::Failed; |
|
| 3696 | - | try testing::expect(plainRec.regions.len == 0); |
|
| 3696 | + | try testing::expect(plainRec.binders.len == 0); |
|
| 3697 | 3697 | ||
| 3698 | 3698 | let union_ = try! parseStmtStr("union Token['src]: Copy { Ident(&'src [u8]), Number(u64) }"); |
|
| 3699 | 3699 | let case ast::NodeValue::UnionDecl(tok) = union_.value else throw testing::TestError::Failed; |
|
| 3700 | - | assert tok.regions.len == 1; |
|
| 3701 | - | let r3 = tok.regions[0]; |
|
| 3700 | + | assert tok.binders.len == 1; |
|
| 3701 | + | let r3 = tok.binders[0]; |
|
| 3702 | 3702 | try expectIdent(r3, "'src"); |
|
| 3703 | 3703 | try testing::expect(tok.derives.len == 1); |
|
| 3704 | 3704 | try testing::expect(tok.variants.len == 2); |
|
| 3705 | 3705 | } |
|
| 3706 | 3706 |
| 3729 | 3729 | let case ast::NodeValue::TypeSig(ast::TypeSig::Array { itemType, .. }) = array.value |
|
| 3730 | 3730 | else throw testing::TestError::Failed; |
|
| 3731 | 3731 | try expectNominalRegion(itemType, "'b"); |
|
| 3732 | 3732 | ||
| 3733 | 3733 | let plain = try! parseTypeStr("Plain"); |
|
| 3734 | - | try testing::expect(ast::nominalRegions(plain).len == 0); |
|
| 3734 | + | try testing::expect(ast::nominalArgs(plain).len == 0); |
|
| 3735 | 3735 | ||
| 3736 | 3736 | let declNode = try! parseStmtStr( |
|
| 3737 | 3737 | "fn parse(input: &'in [u8]) -> Request['in] throws (ParseError) {}" |
|
| 3738 | 3738 | ); |
|
| 3739 | 3739 | let case ast::NodeValue::FnDecl(decl) = declNode.value else throw testing::TestError::Failed; |
| 3745 | 3745 | let case ast::NodeValue::FnParam(param) = fnDecl.sig.params[0].value |
|
| 3746 | 3746 | else throw testing::TestError::Failed; |
|
| 3747 | 3747 | try expectNominalRegion(param.type, "'b"); |
|
| 3748 | 3748 | } |
|
| 3749 | 3749 | ||
| 3750 | - | /// Region parameter and argument lists contain region names. |
|
| 3750 | + | /// Bracketed binders and type arguments must contain at least one item. |
|
| 3751 | 3751 | @test unsafe fn testParseRegionParamErrors() throws (testing::TestError) { |
|
| 3752 | 3752 | let mut failed = false; |
|
| 3753 | 3753 | try parseStmtStr("record R[] { x: u32 }") catch { |
|
| 3754 | 3754 | set failed = true; |
|
| 3755 | 3755 | }; |
|
| 3756 | 3756 | try testing::expect(failed); |
|
| 3757 | 3757 | ||
| 3758 | 3758 | set failed = false; |
|
| 3759 | - | try parseTypeStr("R[x]") catch { |
|
| 3759 | + | try parseTypeStr("R[]") catch { |
|
| 3760 | 3760 | set failed = true; |
|
| 3761 | 3761 | }; |
|
| 3762 | 3762 | try testing::expect(failed); |
|
| 3763 | 3763 | } |
|
| 3764 | 3764 | ||
| 3765 | 3765 | /// Multiple nominal region arguments retain their declared order. |
|
| 3766 | 3766 | @test unsafe fn testParseMultipleRegions() throws (testing::TestError) { |
|
| 3767 | 3767 | let node = try! parseStmtStr("record P['a, 'b] { x: &'b [u8], y: &'a [u8] }"); |
|
| 3768 | 3768 | let case ast::NodeValue::RecordDecl(decl) = node.value else throw testing::TestError::Failed; |
|
| 3769 | - | assert decl.regions.len == 2; |
|
| 3770 | - | try expectIdent(decl.regions[0], "'a"); |
|
| 3771 | - | try expectIdent(decl.regions[1], "'b"); |
|
| 3769 | + | assert decl.binders.len == 2; |
|
| 3770 | + | try expectIdent(decl.binders[0], "'a"); |
|
| 3771 | + | try expectIdent(decl.binders[1], "'b"); |
|
| 3772 | 3772 | let type = try! parseTypeStr("P['b, 'a]"); |
|
| 3773 | - | let regions = ast::nominalRegions(type); |
|
| 3773 | + | let regions = ast::nominalArgs(type); |
|
| 3774 | 3774 | assert regions.len == 2; |
|
| 3775 | 3775 | try expectIdent(regions[0], "'b"); |
|
| 3776 | 3776 | try expectIdent(regions[1], "'a"); |
|
| 3777 | 3777 | } |
|
| 3778 | 3778 |
| 3906 | 3906 | try testing::expect(args.len == 2); |
|
| 3907 | 3907 | try expectIdent(args[0], "A"); |
|
| 3908 | 3908 | try expectIdent(args[1], "B"); |
|
| 3909 | 3909 | try expectIdent(items[1], "C"); |
|
| 3910 | 3910 | } |
|
| 3911 | + | ||
| 3912 | + | /// Check declaration binders and nested arguments in type positions. |
|
| 3913 | + | @test unsafe fn testParseGenericDeclarations() throws (testing::TestError) { |
|
| 3914 | + | for source in [ |
|
| 3915 | + | "fn pair[A, B](first: A, second: B) -> Pair[A, B] {}", |
|
| 3916 | + | "fn[K, V] (m: &Map[K, V]) len() -> u32 {}", |
|
| 3917 | + | "record View['r, T] { item: &'r T }", |
|
| 3918 | + | "union Either[A, B] { Left(A), Right(B) }", |
|
| 3919 | + | "trait Widget[M] { fn (&mut Widget[M]) width(model: &M) -> u32; }", |
|
| 3920 | + | "instance[M, W: Widget[M]] Widget[M] for Padding[M, W] {}", |
|
| 3921 | + | ] { |
|
| 3922 | + | try parseStmtStr(source) catch { |
|
| 3923 | + | throw testing::TestError::Failed; |
|
| 3924 | + | }; |
|
| 3925 | + | } |
|
| 3926 | + | } |
|
| 3927 | + | ||
| 3928 | + | /// Check the kinds and order of mixed binders and a parameterized bound. |
|
| 3929 | + | @test unsafe fn testParseGenericBinderOrder() throws (testing::TestError) { |
|
| 3930 | + | let node = try! parseStmtStr( |
|
| 3931 | + | "record View['r, M, W: widgets::Widget[M]] { item: &'r W }" |
|
| 3932 | + | ); |
|
| 3933 | + | let case ast::NodeValue::RecordDecl(decl) = node.value else throw testing::TestError::Failed; |
|
| 3934 | + | try testing::expect(decl.binders.len == 3); |
|
| 3935 | + | try expectIdent(decl.binders[0], "'r"); |
|
| 3936 | + | let case ast::NodeValue::TypeBinder { name: model, bound: nil } = decl.binders[1].value |
|
| 3937 | + | else throw testing::TestError::Failed; |
|
| 3938 | + | try expectIdent(model, "M"); |
|
| 3939 | + | let case ast::NodeValue::TypeBinder { name, bound } = decl.binders[2].value |
|
| 3940 | + | else throw testing::TestError::Failed; |
|
| 3941 | + | try expectIdent(name, "W"); |
|
| 3942 | + | let constraint = bound else throw testing::TestError::Failed; |
|
| 3943 | + | let case ast::NodeValue::Bracket { base, items } = constraint.value |
|
| 3944 | + | else throw testing::TestError::Failed; |
|
| 3945 | + | let case ast::NodeValue::ScopeAccess(path) = base.value else throw testing::TestError::Failed; |
|
| 3946 | + | try expectIdent(path.parent, "widgets"); |
|
| 3947 | + | try expectIdent(path.child, "Widget"); |
|
| 3948 | + | try testing::expect(items.len == 1); |
|
| 3949 | + | try expectTypeIdent(items[0], "M"); |
|
| 3950 | + | } |
lib/std/lang/pretty.rad
+30 -19
| 1461 | 1461 | ctx, |
|
| 1462 | 1462 | recordLitField |
|
| 1463 | 1463 | ) |
|
| 1464 | 1464 | ); |
|
| 1465 | 1465 | } |
|
| 1466 | + | case ast::NodeValue::TypeBinder { name, bound } => { |
|
| 1467 | + | try put(a, p, &mut kids, NONE, name, Ctx::Normal); |
|
| 1468 | + | if let traitName = bound { |
|
| 1469 | + | try punct(a, &mut kids, NONE, ":"); |
|
| 1470 | + | try put(a, p, &mut kids, SPACE, traitName, Ctx::Normal); |
|
| 1471 | + | } |
|
| 1472 | + | } |
|
| 1466 | 1473 | case ast::NodeValue::TypeSig(sig) => |
|
| 1467 | 1474 | return try typeSig(a, p, node, sig), |
|
| 1468 | 1475 | case ast::NodeValue::Align { value } => { |
|
| 1469 | 1476 | try keyword(a, &mut kids, NONE, "align"); |
|
| 1470 | 1477 | try punct(a, &mut kids, NONE, "("); |
| 1653 | 1660 | region: *ast::Node |
|
| 1654 | 1661 | ) throws (alloc::AllocError) { |
|
| 1655 | 1662 | try put(a, p, kids, at, region, Ctx::Normal); |
|
| 1656 | 1663 | } |
|
| 1657 | 1664 | ||
| 1658 | - | /// Append bracketed region parameters or arguments when present. |
|
| 1659 | - | fn regionParam( |
|
| 1665 | + | /// Append bracketed parameters or arguments when present. |
|
| 1666 | + | fn typeList( |
|
| 1660 | 1667 | a: &heap::Heap, |
|
| 1661 | 1668 | p: &mut Printer, |
|
| 1662 | 1669 | kids: &mut *mut [layout::Child], |
|
| 1663 | - | regions: *[*ast::Node] |
|
| 1670 | + | items: *[*ast::Node] |
|
| 1664 | 1671 | ) throws (alloc::AllocError) { |
|
| 1665 | - | if regions.len == 0 { |
|
| 1672 | + | if items.len == 0 { |
|
| 1666 | 1673 | return; |
|
| 1667 | 1674 | } |
|
| 1668 | 1675 | try punct(a, kids, NONE, "["); |
|
| 1669 | - | for region, i in regions { |
|
| 1670 | - | try regionName(a, p, kids, NONE if i == 0 else SPACE, region); |
|
| 1671 | - | if i + 1 < regions.len { |
|
| 1676 | + | for item, i in items { |
|
| 1677 | + | try put(a, p, kids, NONE if i == 0 else SPACE, item, Ctx::Normal); |
|
| 1678 | + | if i + 1 < items.len { |
|
| 1672 | 1679 | try punct(a, kids, NONE, ","); |
|
| 1673 | 1680 | } |
|
| 1674 | 1681 | } |
|
| 1675 | 1682 | try punct(a, kids, NONE, "]"); |
|
| 1676 | 1683 | } |
| 1763 | 1770 | } |
|
| 1764 | 1771 | case ast::TypeSig::Optional { valueType } => { |
|
| 1765 | 1772 | try operator(a, &mut kids, NONE, "?"); |
|
| 1766 | 1773 | try put(a, p, &mut kids, NONE, valueType, Ctx::Normal); |
|
| 1767 | 1774 | } |
|
| 1768 | - | case ast::TypeSig::Nominal { path, regions } => { |
|
| 1769 | - | if regions.len == 0 { |
|
| 1775 | + | case ast::TypeSig::Nominal { path, args } => { |
|
| 1776 | + | if args.len == 0 { |
|
| 1770 | 1777 | return try expr(a, p, path, Ctx::Normal); |
|
| 1771 | 1778 | } |
|
| 1772 | 1779 | try put(a, p, &mut kids, NONE, path, Ctx::Normal); |
|
| 1773 | - | try regionParam(a, p, &mut kids, regions); |
|
| 1780 | + | try typeList(a, p, &mut kids, args); |
|
| 1774 | 1781 | } |
|
| 1775 | 1782 | case ast::TypeSig::Record { .. } => |
|
| 1776 | 1783 | panic "typeSig: record type outside a variant", |
|
| 1777 | 1784 | case ast::TypeSig::Fn { sig: fnSig, isUnsafe } => { |
|
| 1778 | 1785 | if isUnsafe { |
| 2522 | 2529 | fn fnDecl( |
|
| 2523 | 2530 | a: &heap::Heap, p: &mut Printer, node: *ast::Node |
|
| 2524 | 2531 | ) -> *layout::Layout throws (alloc::AllocError) { |
|
| 2525 | 2532 | let mut head: *mut [layout::Child] = &mut []; |
|
| 2526 | 2533 | let mut fnBody: ?*ast::Node = nil; |
|
| 2527 | - | let mut sig: ast::FnSig = undefined; |
|
| 2534 | + | let mut sig: *ast::FnSig = undefined; |
|
| 2528 | 2535 | match node.value { |
|
| 2529 | 2536 | case ast::NodeValue::FnDecl(f) => { |
|
| 2530 | 2537 | if p.role == Role::Interface { |
|
| 2531 | 2538 | if let attrs = |
|
| 2532 | 2539 | f.attrs; |
| 2537 | 2544 | } |
|
| 2538 | 2545 | } |
|
| 2539 | 2546 | try attributes(a, &mut head, f.attrs); |
|
| 2540 | 2547 | try spaced(a, &mut head, "fn", layout::Role::Keyword); |
|
| 2541 | 2548 | try put(a, p, &mut head, SPACE, f.name, Ctx::Normal); |
|
| 2549 | + | try typeList(a, p, &mut head, f.binders); |
|
| 2542 | 2550 | set fnBody = f.body; |
|
| 2543 | 2551 | set sig = f.sig; |
|
| 2544 | 2552 | } |
|
| 2545 | 2553 | case ast::NodeValue::MethodDecl { |
|
| 2546 | - | name, receiverName, receiverType, sig: s, body: b, attrs |
|
| 2554 | + | name, binders, receiverName, receiverType, sig: s, body: b, attrs |
|
| 2547 | 2555 | } => { |
|
| 2548 | 2556 | let mut recv: *mut [layout::Child] = &mut []; |
|
| 2549 | 2557 | try punct(a, &mut recv, NONE, "("); |
|
| 2550 | 2558 | try put(a, p, &mut recv, NONE, receiverName, Ctx::Normal); |
|
| 2551 | 2559 | try punct(a, &mut recv, NONE, ":"); |
|
| 2552 | 2560 | try put(a, p, &mut recv, SPACE, receiverType, Ctx::Normal); |
|
| 2553 | 2561 | try punct(a, &mut recv, NONE, ")"); |
|
| 2554 | 2562 | try attributes(a, &mut head, attrs); |
|
| 2555 | 2563 | try spaced(a, &mut head, "fn", layout::Role::Keyword); |
|
| 2564 | + | try typeList(a, p, &mut head, binders); |
|
| 2556 | 2565 | try add(a, &mut head, SPACE, try group(a, nil, recv)); |
|
| 2557 | 2566 | try put(a, p, &mut head, SPACE, name, Ctx::Normal); |
|
| 2558 | 2567 | set fnBody = b; |
|
| 2559 | 2568 | set sig = s; |
|
| 2560 | 2569 | } |
| 2578 | 2587 | &mut head, |
|
| 2579 | 2588 | NONE, |
|
| 2580 | 2589 | try softList(a, p, parens(node), sig.params, Ctx::Normal, paramItem) |
|
| 2581 | 2590 | ); |
|
| 2582 | 2591 | let mut tail: *mut [layout::Child] = &mut []; |
|
| 2583 | - | try signatureTail(a, p, &mut tail, &sig, marked( |
|
| 2592 | + | try signatureTail(a, p, &mut tail, sig, marked( |
|
| 2584 | 2593 | p, node, ast::BrokenSite::Throws |
|
| 2585 | 2594 | )); |
|
| 2586 | 2595 | let stacked = isStacked(p, node); |
|
| 2587 | 2596 | let forced = marked(p, node, ast::BrokenSite::List); |
|
| 2588 | 2597 | let mut kids: *mut [layout::Child] = &mut []; |
| 2594 | 2603 | let mut at = SPACE; |
|
| 2595 | 2604 | if sig.throwList.len > 0 { |
|
| 2596 | 2605 | set at = SOFT_SPACE; |
|
| 2597 | 2606 | } |
|
| 2598 | 2607 | try punct(a, &mut tail, at, brace(p, blk.statements.len, b)); |
|
| 2599 | - | let text = try signature(a, &sig, stacked, forced, head, tail); |
|
| 2608 | + | let text = try signature(a, sig, stacked, forced, head, tail); |
|
| 2600 | 2609 | try add(a, &mut kids, NONE, text); |
|
| 2601 | 2610 | set p.fnDepth += 1; |
|
| 2602 | 2611 | try body(a, p, &mut kids, blk.statements, b, stmt); |
|
| 2603 | 2612 | set p.fnDepth -= 1; |
|
| 2604 | 2613 | } else { |
|
| 2605 | 2614 | try punct(a, &mut tail, NONE, ";"); |
|
| 2606 | - | let text = try signature(a, &sig, stacked, forced, head, tail); |
|
| 2615 | + | let text = try signature(a, sig, stacked, forced, head, tail); |
|
| 2607 | 2616 | try add(a, &mut kids, NONE, text); |
|
| 2608 | 2617 | } |
|
| 2609 | 2618 | return try group(a, node, kids); |
|
| 2610 | 2619 | } |
|
| 2611 | 2620 |
| 2831 | 2840 | match node.value { |
|
| 2832 | 2841 | case ast::NodeValue::RecordDecl(r) => { |
|
| 2833 | 2842 | try attributes(a, &mut head, r.attrs); |
|
| 2834 | 2843 | try spaced(a, &mut head, "record", layout::Role::Keyword); |
|
| 2835 | 2844 | try put(a, p, &mut head, SPACE, r.name, Ctx::Normal); |
|
| 2836 | - | try regionParam(a, p, &mut head, r.regions); |
|
| 2845 | + | try typeList(a, p, &mut head, r.binders); |
|
| 2837 | 2846 | if r.labeled and isCompact(p, node, r.fields.len) { |
|
| 2838 | 2847 | try derives(a, p, &mut head, r.derives); |
|
| 2839 | 2848 | try compactFields(a, p, &mut head, node, r.fields); |
|
| 2840 | 2849 | try add(a, &mut kids, NONE, try group(a, nil, head)); |
|
| 2841 | 2850 | } else if r.labeled { |
| 2864 | 2873 | } |
|
| 2865 | 2874 | case ast::NodeValue::UnionDecl(u) => { |
|
| 2866 | 2875 | try attributes(a, &mut head, u.attrs); |
|
| 2867 | 2876 | try spaced(a, &mut head, "union", layout::Role::Keyword); |
|
| 2868 | 2877 | try put(a, p, &mut head, SPACE, u.name, Ctx::Normal); |
|
| 2869 | - | try regionParam(a, p, &mut head, u.regions); |
|
| 2878 | + | try typeList(a, p, &mut head, u.binders); |
|
| 2870 | 2879 | try derives(a, p, &mut head, u.derives); |
|
| 2871 | 2880 | try punct(a, &mut head, SPACE, brace(p, u.variants.len, node)); |
|
| 2872 | 2881 | try add(a, &mut kids, NONE, try group(a, nil, head)); |
|
| 2873 | 2882 | try body(a, p, &mut kids, u.variants, node, variantItem); |
|
| 2874 | 2883 | } |
|
| 2875 | 2884 | case ast::NodeValue::TraitDecl { |
|
| 2876 | - | name, supertraits, methods, attrs |
|
| 2885 | + | name, binders, supertraits, methods, attrs |
|
| 2877 | 2886 | } => { |
|
| 2878 | 2887 | try attributes(a, &mut head, attrs); |
|
| 2879 | 2888 | try spaced(a, &mut head, "trait", layout::Role::Keyword); |
|
| 2880 | 2889 | try put(a, p, &mut head, SPACE, name, Ctx::Normal); |
|
| 2890 | + | try typeList(a, p, &mut head, binders); |
|
| 2881 | 2891 | try derives(a, p, &mut head, supertraits); |
|
| 2882 | 2892 | try punct(a, &mut head, SPACE, brace(p, methods.len, node)); |
|
| 2883 | 2893 | try add(a, &mut kids, NONE, try group(a, nil, head)); |
|
| 2884 | 2894 | try body(a, p, &mut kids, methods, node, fnDecl); |
|
| 2885 | 2895 | } |
|
| 2886 | 2896 | case ast::NodeValue::InstanceDecl { |
|
| 2887 | - | traitName, targetType, methods |
|
| 2897 | + | binders, traitName, targetType, methods |
|
| 2888 | 2898 | } => { |
|
| 2889 | 2899 | try spaced(a, &mut head, "instance", layout::Role::Keyword); |
|
| 2900 | + | try typeList(a, p, &mut head, binders); |
|
| 2890 | 2901 | try put(a, p, &mut head, SPACE, traitName, Ctx::Normal); |
|
| 2891 | 2902 | try keyword(a, &mut head, SPACE, "for"); |
|
| 2892 | 2903 | try put(a, p, &mut head, SPACE, targetType, Ctx::Normal); |
|
| 2893 | 2904 | try punct(a, &mut head, SPACE, brace(p, methods.len, node)); |
|
| 2894 | 2905 | try add(a, &mut kids, NONE, try group(a, nil, head)); |
lib/std/lang/resolver.rad
+69 -36
| 626 | 626 | EmbedPath, |
|
| 627 | 627 | /// The package does not declare a file with this `@embed` path. |
|
| 628 | 628 | EmbedNotFound(*[u8]), |
|
| 629 | 629 | } |
|
| 630 | 630 | ||
| 631 | - | /// Invalid shape or base of an array or slice subscript. |
|
| 632 | - | export union SubscriptError: Copy { |
|
| 631 | + | /// Invalid type application or subscript. |
|
| 632 | + | export union BracketError: Copy { |
|
| 633 | 633 | /// The base must be an array or slice. |
|
| 634 | 634 | Base, |
|
| 635 | 635 | /// A subscript must have exactly one index or range. |
|
| 636 | 636 | Arity, |
|
| 637 | + | /// The compiler cannot resolve a generic declaration or application. |
|
| 638 | + | Generic, |
|
| 637 | 639 | } |
|
| 638 | 640 | ||
| 639 | 641 | /// High-level classification for semantic diagnostics. |
|
| 640 | 642 | export union ErrorKind: Copy { |
|
| 641 | 643 | /// Identifier declared more than once in the same scope. |
| 730 | 732 | DerefLabeledRecord, |
|
| 731 | 733 | /// `*` on a positional record that does not have exactly one field. |
|
| 732 | 734 | DerefRecordFieldCount, |
|
| 733 | 735 | /// Expected a record type. |
|
| 734 | 736 | ExpectedRecord, |
|
| 735 | - | /// Invalid array or slice subscript. |
|
| 736 | - | Subscript(SubscriptError), |
|
| 737 | + | /// Invalid type application or subscript. |
|
| 738 | + | Bracket(BracketError), |
|
| 737 | 739 | /// Expected an iterable (array, slice, or range) for a `for` loop. |
|
| 738 | 740 | ExpectedIterable, |
|
| 739 | 741 | /// Invalid `as` cast between the provided types. |
|
| 740 | 742 | InvalidAsCast(InvalidAsCast), |
|
| 741 | 743 | /// Invalid alignment value specified. |
| 3580 | 3582 | // fields are known. |
|
| 3581 | 3583 | if let ty = typeFor(self, decl.type) { |
|
| 3582 | 3584 | try requireConstNoMutablePointer(self, decl.type, ty); |
|
| 3583 | 3585 | } |
|
| 3584 | 3586 | } |
|
| 3585 | - | case ast::NodeValue::InstanceDecl { traitName, targetType, methods } => { |
|
| 3587 | + | case ast::NodeValue::InstanceDecl { binders, traitName, targetType, methods } => { |
|
| 3588 | + | if binders.len > 0 { |
|
| 3589 | + | throw emitError(self, binders[0], ErrorKind::Bracket(BracketError::Generic)); |
|
| 3590 | + | } |
|
| 3586 | 3591 | try resolveInstanceDecl(self, node, traitName, targetType, methods); |
|
| 3587 | 3592 | } |
|
| 3588 | - | case ast::NodeValue::MethodDecl { name, receiverName, receiverType, sig, body, attrs } => { |
|
| 3593 | + | case ast::NodeValue::MethodDecl { |
|
| 3594 | + | name, binders, receiverName, receiverType, sig, attrs, .. |
|
| 3595 | + | } => { |
|
| 3596 | + | if binders.len > 0 { |
|
| 3597 | + | throw emitError(self, binders[0], ErrorKind::Bracket(BracketError::Generic)); |
|
| 3598 | + | } |
|
| 3589 | 3599 | try resolveMethodDecl(self, node, name, receiverName, receiverType, sig, attrs); |
|
| 3590 | 3600 | } |
|
| 3591 | 3601 | else => { |
|
| 3592 | 3602 | // Ignore non-declaration nodes. |
|
| 3593 | 3603 | } |
| 3662 | 3672 | } |
|
| 3663 | 3673 | case ast::NodeValue::InstanceDecl { methods, .. } => { |
|
| 3664 | 3674 | try resolveInstanceMethodBodies(self, methods); |
|
| 3665 | 3675 | } |
|
| 3666 | 3676 | case ast::NodeValue::MethodDecl { receiverName, receiverType, sig, body, .. } => { |
|
| 3667 | - | try resolveMethodBody(self, node, receiverName, receiverType, &sig, body); |
|
| 3677 | + | let _ = symbolFor(self, node) else return; |
|
| 3678 | + | try resolveMethodBody(self, node, receiverName, receiverType, sig, body); |
|
| 3668 | 3679 | } |
|
| 3669 | 3680 | else => { |
|
| 3670 | 3681 | // FIXME: This allows module-level statements that should |
|
| 3671 | 3682 | // normally only be valid inside function bodies. We currently |
|
| 3672 | 3683 | // need this because of how tests are written, but it should |
| 5087 | 5098 | ||
| 5088 | 5099 | /// Analyze a function declaration signature and bind the function name. |
|
| 5089 | 5100 | fn resolveFnDecl(self: &mut Resolver, node: *ast::Node, decl: ast::FnDecl) -> Type |
|
| 5090 | 5101 | throws (ResolveError) |
|
| 5091 | 5102 | { |
|
| 5103 | + | if decl.binders.len > 0 { |
|
| 5104 | + | throw emitError(self, decl.binders[0], ErrorKind::Bracket(BracketError::Generic)); |
|
| 5105 | + | } |
|
| 5092 | 5106 | let attrMask = resolveAttributes(self, decl.attrs); |
|
| 5093 | 5107 | let mut fnType = FnType { |
|
| 5094 | 5108 | paramTypes: &[], |
|
| 5095 | 5109 | returnType: allocType(self, Type::Void), |
|
| 5096 | 5110 | throwList: &[], |
| 5100 | 5114 | stores: nil, |
|
| 5101 | 5115 | }; |
|
| 5102 | 5116 | // The body reuses the function scope, so the regions and parameters of |
|
| 5103 | 5117 | // the signature stay in scope there. |
|
| 5104 | 5118 | enterFn(self, node, &fnType); |
|
| 5105 | - | try resolveFnSig(self, node, &decl.sig, &mut fnType) catch e { |
|
| 5119 | + | try resolveFnSig(self, node, decl.sig, &mut fnType) catch e { |
|
| 5106 | 5120 | exitFn(self); |
|
| 5107 | 5121 | throw e; |
|
| 5108 | 5122 | }; |
|
| 5109 | 5123 | exitFn(self); |
|
| 5110 | 5124 |
| 5437 | 5451 | /// Enter a scope for a declaration's content parameters and return the |
|
| 5438 | 5452 | /// enclosing declaration's parameters. |
|
| 5439 | 5453 | fn enterDeclRegion(self: &mut Resolver, node: *ast::Node, regions: *[*ast::Node]) -> *[*ast::Node] |
|
| 5440 | 5454 | throws (ResolveError) |
|
| 5441 | 5455 | { |
|
| 5456 | + | for region in regions { |
|
| 5457 | + | if let case ast::NodeValue::TypeBinder { .. } = region.value { |
|
| 5458 | + | throw emitError(self, region, ErrorKind::Bracket(BracketError::Generic)); |
|
| 5459 | + | } |
|
| 5460 | + | } |
|
| 5442 | 5461 | let outer = self.declRegions; |
|
| 5443 | 5462 | set self.declRegions = regions; |
|
| 5444 | 5463 | // Content parameters occupy a separate declaration scope, including when |
|
| 5445 | 5464 | // another declaration resolves this type on demand. |
|
| 5446 | 5465 | if regions.len > 0 { |
| 5483 | 5502 | throws (ResolveError) |
|
| 5484 | 5503 | { |
|
| 5485 | 5504 | if not isUnresolvedDecl(self, node) { |
|
| 5486 | 5505 | return; |
|
| 5487 | 5506 | } |
|
| 5488 | - | let outer = try enterDeclRegion(self, node, decl.regions); |
|
| 5507 | + | let outer = try enterDeclRegion(self, node, decl.binders); |
|
| 5489 | 5508 | try resolveRecordShape(self, node, decl) catch e { |
|
| 5490 | - | exitDeclRegion(self, decl.regions, outer); |
|
| 5509 | + | exitDeclRegion(self, decl.binders, outer); |
|
| 5491 | 5510 | throw e; |
|
| 5492 | 5511 | }; |
|
| 5493 | - | exitDeclRegion(self, decl.regions, outer); |
|
| 5512 | + | exitDeclRegion(self, decl.binders, outer); |
|
| 5494 | 5513 | } |
|
| 5495 | 5514 | ||
| 5496 | 5515 | /// Resolve the fields of a named record declaration in the scope of its region. |
|
| 5497 | 5516 | fn resolveRecordShape(self: &mut Resolver, node: *ast::Node, decl: ast::RecordDecl) |
|
| 5498 | 5517 | throws (ResolveError) |
| 5516 | 5535 | } |
|
| 5517 | 5536 | } |
|
| 5518 | 5537 | } |
|
| 5519 | 5538 | set recordType.declaredLinear = markers.linear; |
|
| 5520 | 5539 | set recordType.declaredCopy = markers.copy; |
|
| 5521 | - | set recordType.borrowed = decl.regions.len > 0; |
|
| 5522 | - | set recordType.regions = decl.regions; |
|
| 5540 | + | set recordType.borrowed = decl.binders.len > 0; |
|
| 5541 | + | set recordType.regions = decl.binders; |
|
| 5523 | 5542 | if ast::hasAttribute(self.symbols[*sym].attrs, ast::Attribute::Opaque) { |
|
| 5524 | 5543 | set recordType.privateModule = self.currentMod; |
|
| 5525 | 5544 | } |
|
| 5526 | 5545 | set self.nominals[*nominalTy] = NominalType::Record(recordType); |
|
| 5527 | 5546 | } |
| 5673 | 5692 | fn resolveTraitBody( |
|
| 5674 | 5693 | self: &mut Resolver, node: *ast::Node, supertraits: *[*ast::Node], methods: *[*ast::Node] |
|
| 5675 | 5694 | ) |
|
| 5676 | 5695 | throws (ResolveError) |
|
| 5677 | 5696 | { |
|
| 5697 | + | let case ast::NodeValue::TraitDecl { binders, .. } = node.value else { |
|
| 5698 | + | panic "resolveTraitBody: expected trait declaration"; |
|
| 5699 | + | }; |
|
| 5700 | + | if binders.len > 0 { |
|
| 5701 | + | throw emitError(self, binders[0], ErrorKind::Bracket(BracketError::Generic)); |
|
| 5702 | + | } |
|
| 5678 | 5703 | let sym = symbolFor(self, node) else return; |
|
| 5679 | 5704 | let case SymbolData::Trait(traitType) = self.symbols[*sym].data else return; |
|
| 5680 | 5705 | if self.traits[*traitType].methods.len > 0 { |
|
| 5681 | 5706 | return; |
|
| 5682 | 5707 | } |
| 5748 | 5773 | class: receiverClass, valueType: receiverValueType, mutable, .. |
|
| 5749 | 5774 | } = |
|
| 5750 | 5775 | typeSig else throw emitError(self, receiver, ErrorKind::TraitReceiverMismatch); |
|
| 5751 | 5776 | let case ast::NodeValue::TypeSig(innerSig) = receiverValueType.value |
|
| 5752 | 5777 | else throw emitError(self, receiver, ErrorKind::TraitReceiverMismatch); |
|
| 5753 | - | let case ast::TypeSig::Nominal { path: nameNode, .. } = innerSig |
|
| 5778 | + | let case ast::TypeSig::Nominal { path: nameNode, args } = innerSig |
|
| 5754 | 5779 | else throw emitError(self, receiver, ErrorKind::TraitReceiverMismatch); |
|
| 5780 | + | if args.len > 0 { |
|
| 5781 | + | throw emitError(self, args[0], ErrorKind::Bracket(BracketError::Generic)); |
|
| 5782 | + | } |
|
| 5755 | 5783 | let receiverTargetName = try nodeName(self, nameNode); |
|
| 5756 | 5784 | ||
| 5757 | 5785 | if receiverTargetName <> self.traits[*traitType].name { |
|
| 5758 | 5786 | throw emitError(self, receiver, ErrorKind::TraitReceiverMismatch); |
|
| 5759 | 5787 | } |
| 5829 | 5857 | } |
|
| 5830 | 5858 | case ast::NodeValue::ScopeAccess(access) => { |
|
| 5831 | 5859 | let scope = self.scope; |
|
| 5832 | 5860 | return try resolveAccess(self, node, access, scope); |
|
| 5833 | 5861 | } |
|
| 5862 | + | case ast::NodeValue::Bracket { .. } => |
|
| 5863 | + | throw emitError(self, node, ErrorKind::Bracket(BracketError::Generic)), |
|
| 5834 | 5864 | else => { |
|
| 5835 | 5865 | throw emitError(self, node, ErrorKind::ExpectedIdentifier); |
|
| 5836 | 5866 | } |
|
| 5837 | 5867 | } |
|
| 5838 | 5868 | } |
| 5884 | 5914 | let mut covered: [bool; ast::MAX_TRAIT_METHODS] = [false; ast::MAX_TRAIT_METHODS]; |
|
| 5885 | 5915 | ||
| 5886 | 5916 | // Match each instance method to a trait method. |
|
| 5887 | 5917 | for methodNode in methods { |
|
| 5888 | 5918 | let case ast::NodeValue::MethodDecl { |
|
| 5889 | - | name, receiverName, receiverType, sig, body, attrs |
|
| 5919 | + | name, receiverName, receiverType, sig, body, attrs, .. |
|
| 5890 | 5920 | } = |
|
| 5891 | 5921 | methodNode.value else continue; |
|
| 5892 | 5922 | ||
| 5893 | 5923 | let methodName = try nodeName(self, name); |
|
| 5894 | 5924 | let attrMask = resolveAttributes(self, attrs); |
| 6068 | 6098 | ||
| 6069 | 6099 | // Symbol may be absent if [`resolveInstanceDecl`] reported an error |
|
| 6070 | 6100 | // for this method (eg. unknown method name). Skip gracefully. |
|
| 6071 | 6101 | let _ = symbolFor(self, methodNode) else continue; |
|
| 6072 | 6102 | ||
| 6073 | - | try resolveMethodBody(self, methodNode, receiverName, receiverType, &sig, body); |
|
| 6103 | + | try resolveMethodBody(self, methodNode, receiverName, receiverType, sig, body); |
|
| 6074 | 6104 | } |
|
| 6075 | 6105 | } |
|
| 6076 | 6106 | ||
| 6077 | 6107 | /// Resolve a method body shared by instance methods and standalone methods. |
|
| 6078 | 6108 | /// Binds the receiver and parameters, then type-checks the body. |
| 6121 | 6151 | self: &mut Resolver, |
|
| 6122 | 6152 | node: *ast::Node, |
|
| 6123 | 6153 | name: *ast::Node, |
|
| 6124 | 6154 | receiverName: *ast::Node, |
|
| 6125 | 6155 | receiverType: *ast::Node, |
|
| 6126 | - | sig: ast::FnSig, |
|
| 6156 | + | sig: *ast::FnSig, |
|
| 6127 | 6157 | attrs: ?ast::Attributes |
|
| 6128 | 6158 | ) throws (ResolveError) { |
|
| 6129 | 6159 | // The body reuses the method scope, so the region of the receiver stays |
|
| 6130 | 6160 | // in scope there. |
|
| 6131 | 6161 | enterScope(self, node); |
|
| 6132 | - | try declareMethod(self, node, name, receiverType, &sig, attrs) catch e { |
|
| 6162 | + | try declareMethod(self, node, name, receiverType, sig, attrs) catch e { |
|
| 6133 | 6163 | exitScope(self); |
|
| 6134 | 6164 | throw e; |
|
| 6135 | 6165 | }; |
|
| 6136 | 6166 | exitScope(self); |
|
| 6137 | 6167 | } |
| 6319 | 6349 | throws (ResolveError) |
|
| 6320 | 6350 | { |
|
| 6321 | 6351 | if not isUnresolvedDecl(self, node) { |
|
| 6322 | 6352 | return; |
|
| 6323 | 6353 | } |
|
| 6324 | - | let outer = try enterDeclRegion(self, node, decl.regions); |
|
| 6354 | + | let outer = try enterDeclRegion(self, node, decl.binders); |
|
| 6325 | 6355 | try resolveUnionShape(self, node, decl) catch e { |
|
| 6326 | - | exitDeclRegion(self, decl.regions, outer); |
|
| 6356 | + | exitDeclRegion(self, decl.binders, outer); |
|
| 6327 | 6357 | throw e; |
|
| 6328 | 6358 | }; |
|
| 6329 | - | exitDeclRegion(self, decl.regions, outer); |
|
| 6359 | + | exitDeclRegion(self, decl.binders, outer); |
|
| 6330 | 6360 | } |
|
| 6331 | 6361 | ||
| 6332 | 6362 | /// Resolve union variant types after all type names are bound (Phase 2 of type resolution). |
|
| 6333 | 6363 | fn resolveUnionShape(self: &mut Resolver, node: *ast::Node, decl: ast::UnionDecl) |
|
| 6334 | 6364 | throws (ResolveError) |
| 6356 | 6386 | layout: Layout { size: 0, alignment: 0 }, |
|
| 6357 | 6387 | valOffset: 0, |
|
| 6358 | 6388 | isAllVoid: true, |
|
| 6359 | 6389 | declaredLinear: markers.linear, |
|
| 6360 | 6390 | declaredCopy: markers.copy, |
|
| 6361 | - | borrowed: decl.regions.len > 0, |
|
| 6362 | - | regions: decl.regions, |
|
| 6391 | + | borrowed: decl.binders.len > 0, |
|
| 6392 | + | regions: decl.binders, |
|
| 6363 | 6393 | }); |
|
| 6364 | 6394 | ||
| 6365 | 6395 | if decl.variants.len > MAX_UNION_VARIANTS { |
|
| 6366 | 6396 | throw emitError(self, decl.variants[MAX_UNION_VARIANTS], ErrorKind::LimitExceeded( |
|
| 6367 | 6397 | "too many variants in one union" |
| 6401 | 6431 | // Create a symbol for this variant. |
|
| 6402 | 6432 | let data = SymbolData::Variant { type: variantType, decl: node, ordinal: i, index: tag }; |
|
| 6403 | 6433 | let variantSym = allocSymbol(self, data, variantName, variantNode, 0); |
|
| 6404 | 6434 | let mut regions: *[u32] = &[]; |
|
| 6405 | 6435 | if let payload = variantDecl.type { |
|
| 6406 | - | set regions = refs::declarationMap(self, payload, decl.regions); |
|
| 6436 | + | set regions = refs::declarationMap(self, payload, decl.binders); |
|
| 6407 | 6437 | } |
|
| 6408 | 6438 | try! variants.append( |
|
| 6409 | 6439 | UnionVariant { |
|
| 6410 | 6440 | name: variantName, |
|
| 6411 | 6441 | valueType: variantType, |
| 6431 | 6461 | layout: info.layout, |
|
| 6432 | 6462 | valOffset: info.valOffset, |
|
| 6433 | 6463 | isAllVoid: info.isAllVoid, |
|
| 6434 | 6464 | declaredLinear: markers.linear, |
|
| 6435 | 6465 | declaredCopy: markers.copy, |
|
| 6436 | - | borrowed: decl.regions.len > 0, |
|
| 6437 | - | regions: decl.regions, |
|
| 6466 | + | borrowed: decl.binders.len > 0, |
|
| 6467 | + | regions: decl.binders, |
|
| 6438 | 6468 | }; |
|
| 6439 | 6469 | set unionType.mixed = mixedPayload(self, unionType); |
|
| 6440 | 6470 | set self.nominals[*nominalTy] = NominalType::Union(unionType); |
|
| 6441 | 6471 | } |
|
| 6442 | 6472 |
| 8636 | 8666 | try validateArraySliceBounds(self, range, a.length, node); |
|
| 8637 | 8667 | set item = a.item; |
|
| 8638 | 8668 | set capacity = a.length; |
|
| 8639 | 8669 | } |
|
| 8640 | 8670 | else => |
|
| 8641 | - | throw emitError(self, container, ErrorKind::Subscript( |
|
| 8642 | - | SubscriptError::Base |
|
| 8671 | + | throw emitError(self, container, ErrorKind::Bracket( |
|
| 8672 | + | BracketError::Base |
|
| 8643 | 8673 | )), |
|
| 8644 | 8674 | } |
|
| 8645 | 8675 | } |
|
| 8646 | 8676 | // RHS is either a fill value or a source slice. |
|
| 8647 | 8677 | let rhsTy = try infer(self, assign.right); |
| 8787 | 8817 | ) -> Type |
|
| 8788 | 8818 | throws (ResolveError) |
|
| 8789 | 8819 | { |
|
| 8790 | 8820 | let containerTy = try infer(self, base); |
|
| 8791 | 8821 | if items.len <> 1 { |
|
| 8792 | - | throw emitError(self, node, ErrorKind::Subscript(SubscriptError::Arity)); |
|
| 8822 | + | throw emitError(self, node, ErrorKind::Bracket(BracketError::Arity)); |
|
| 8793 | 8823 | } |
|
| 8794 | 8824 | let indexNode = items[0]; |
|
| 8795 | 8825 | // Range subscripts always require `&` to form a slice. |
|
| 8796 | 8826 | if let case ast::NodeValue::Range(range) = indexNode.value { |
|
| 8797 | 8827 | try infer(self, indexNode); |
| 8809 | 8839 | match subjectTy { |
|
| 8810 | 8840 | case Type::Array(arrayInfo) => { |
|
| 8811 | 8841 | return setNodeType(self, node, *arrayInfo.item); |
|
| 8812 | 8842 | } |
|
| 8813 | 8843 | else => { |
|
| 8814 | - | throw emitError(self, base, ErrorKind::Subscript(SubscriptError::Base)); |
|
| 8844 | + | throw emitError(self, base, ErrorKind::Bracket(BracketError::Base)); |
|
| 8815 | 8845 | } |
|
| 8816 | 8846 | } |
|
| 8817 | 8847 | } |
|
| 8818 | 8848 | ||
| 8819 | 8849 | /// Find a record field by name. |
| 9770 | 9800 | try validateArraySliceBounds(self, range, arrayInfo.length, node); |
|
| 9771 | 9801 | set item = arrayInfo.item; |
|
| 9772 | 9802 | set capacity = arrayInfo.length; |
|
| 9773 | 9803 | } |
|
| 9774 | 9804 | else => { |
|
| 9775 | - | throw emitError(self, container, ErrorKind::Subscript( |
|
| 9776 | - | SubscriptError::Base |
|
| 9805 | + | throw emitError(self, container, ErrorKind::Bracket( |
|
| 9806 | + | BracketError::Base |
|
| 9777 | 9807 | )); |
|
| 9778 | 9808 | } |
|
| 9779 | 9809 | } |
|
| 9780 | 9810 | } |
|
| 9781 | 9811 | let class = try addressClass(self, addr, hint); |
| 10662 | 10692 | } |
|
| 10663 | 10693 | case ast::TypeSig::Optional { valueType } => { |
|
| 10664 | 10694 | let payload = try infer(self, valueType); |
|
| 10665 | 10695 | return Type::Optional(allocType(self, payload)); |
|
| 10666 | 10696 | } |
|
| 10667 | - | case ast::TypeSig::Nominal { path: name, regions } => { |
|
| 10697 | + | case ast::TypeSig::Nominal { path: name, args } => { |
|
| 10668 | 10698 | let ty = try resolveTypeName(self, name); |
|
| 10669 | - | for region in regions { |
|
| 10670 | - | try checkRegion(self, region); |
|
| 10699 | + | for arg in args { |
|
| 10700 | + | if let case ast::NodeValue::TypeSig(_) = arg.value { |
|
| 10701 | + | throw emitError(self, arg, ErrorKind::Bracket(BracketError::Generic)); |
|
| 10702 | + | } |
|
| 10703 | + | try checkRegion(self, arg); |
|
| 10671 | 10704 | } |
|
| 10672 | - | if refs::nominalRegions(self, ty).len <> regions.len { |
|
| 10705 | + | if refs::nominalRegions(self, ty).len <> args.len { |
|
| 10673 | 10706 | throw emitError(self, node, ErrorKind::Region(RegionError::Argument)); |
|
| 10674 | 10707 | } |
|
| 10675 | 10708 | return Type::Nominal(ty); |
|
| 10676 | 10709 | } |
|
| 10677 | 10710 | case ast::TypeSig::Record { fields, labeled } => { |
lib/std/lang/resolver/printer.rad
+5 -3
| 509 | 509 | io::print("an owned receiver must be owned storage, not a local or a borrowed place"); |
|
| 510 | 510 | } |
|
| 511 | 511 | case super::ErrorKind::ExpectedRecord => { |
|
| 512 | 512 | io::print("expected record type"); |
|
| 513 | 513 | } |
|
| 514 | - | case super::ErrorKind::Subscript(error) => { |
|
| 514 | + | case super::ErrorKind::Bracket(error) => { |
|
| 515 | 515 | match error { |
|
| 516 | - | case super::SubscriptError::Base => |
|
| 516 | + | case super::BracketError::Base => |
|
| 517 | 517 | io::print("expected array or slice"), |
|
| 518 | - | case super::SubscriptError::Arity => |
|
| 518 | + | case super::BracketError::Arity => |
|
| 519 | 519 | io::print("a subscript requires exactly one index or range"), |
|
| 520 | + | case super::BracketError::Generic => |
|
| 521 | + | io::print("generic declarations and types cannot be resolved"), |
|
| 520 | 522 | } |
|
| 521 | 523 | } |
|
| 522 | 524 | case super::ErrorKind::InvalidAlignmentValue(val) => { |
|
| 523 | 525 | io::print("invalid alignment value: "); |
|
| 524 | 526 | io::printU32(val); |
lib/std/lang/resolver/refs.rad
+7 -5
| 540 | 540 | if let ty = super::typeFor(self, type); positions(self, ty) == 0 { |
|
| 541 | 541 | return; |
|
| 542 | 542 | } |
|
| 543 | 543 | let case ast::NodeValue::TypeSig(sig) = type.value else return; |
|
| 544 | 544 | match sig { |
|
| 545 | - | case ast::TypeSig::Nominal { regions, .. } => { |
|
| 546 | - | for region in regions { |
|
| 547 | - | try! out.append(region, self.arena); |
|
| 545 | + | case ast::TypeSig::Nominal { args, .. } => { |
|
| 546 | + | for arg in args { |
|
| 547 | + | if let case ast::NodeValue::Ident(_) = arg.value { |
|
| 548 | + | try! out.append(arg, self.arena); |
|
| 549 | + | } |
|
| 548 | 550 | } |
|
| 549 | 551 | } |
|
| 550 | 552 | case ast::TypeSig::Optional { valueType } => collectNames(self, valueType, out), |
|
| 551 | 553 | case ast::TypeSig::Array { itemType, .. } => collectNames(self, itemType, out), |
|
| 552 | 554 | case ast::TypeSig::Pointer { class, valueType, region, .. } => { |
| 571 | 573 | match self.nominals[*id] { |
|
| 572 | 574 | case NominalType::Record(r) => return r.regions, |
|
| 573 | 575 | case NominalType::Union(u) => return u.regions, |
|
| 574 | 576 | case NominalType::Placeholder(node) => { |
|
| 575 | 577 | match node.value { |
|
| 576 | - | case ast::NodeValue::RecordDecl(r) => return r.regions, |
|
| 577 | - | case ast::NodeValue::UnionDecl(u) => return u.regions, |
|
| 578 | + | case ast::NodeValue::RecordDecl(r) => return r.binders, |
|
| 579 | + | case ast::NodeValue::UnionDecl(u) => return u.binders, |
|
| 578 | 580 | else => return &[], |
|
| 579 | 581 | } |
|
| 580 | 582 | } |
|
| 581 | 583 | } |
|
| 582 | 584 | } |
lib/std/lang/resolver/tests/core.rad
+3 -3
| 1628 | 1628 | ||
| 1629 | 1629 | @test unsafe fn testResolveIndexNonIndexable() throws (testing::TestError) { |
|
| 1630 | 1630 | let mut a = testResolver(); |
|
| 1631 | 1631 | let program = "let flag: bool = true; flag[0];"; |
|
| 1632 | 1632 | let result = try resolveProgramStr(&mut a, program); |
|
| 1633 | - | try expectErrorKind(&result, resolver::ErrorKind::Subscript(resolver::SubscriptError::Base)); |
|
| 1633 | + | try expectErrorKind(&result, resolver::ErrorKind::Bracket(resolver::BracketError::Base)); |
|
| 1634 | 1634 | } |
|
| 1635 | 1635 | ||
| 1636 | 1636 | /// Check that a bracket list on an array requires one index or range. |
|
| 1637 | 1637 | @test unsafe fn testResolveSubscriptArity() throws (testing::TestError) { |
|
| 1638 | 1638 | let programs = [ |
| 1641 | 1641 | @embed("lang/resolver/tests/fixtures/subscript/store_two_indices.rad"), |
|
| 1642 | 1642 | ]; |
|
| 1643 | 1643 | for program in programs { |
|
| 1644 | 1644 | let mut a = testResolver(); |
|
| 1645 | 1645 | let result = try resolveProgramStr(&mut a, program); |
|
| 1646 | - | try expectErrorKind(&result, resolver::ErrorKind::Subscript( |
|
| 1647 | - | resolver::SubscriptError::Arity |
|
| 1646 | + | try expectErrorKind(&result, resolver::ErrorKind::Bracket( |
|
| 1647 | + | resolver::BracketError::Arity |
|
| 1648 | 1648 | )); |
|
| 1649 | 1649 | } |
|
| 1650 | 1650 | } |
|
| 1651 | 1651 | ||
| 1652 | 1652 | @test unsafe fn testResolveSliceFieldUnknown() throws (testing::TestError) { |