lang: Represent postfix bracket lists in the AST
15b2a873865eb8675a64270e50eddac797012eba72460f5ab66b331864f8e715
Store bracket items in source order so lexical resolution can use the base to select their meaning. Preserve index and range handling for array and slice values and check that these uses have one item. Assisted-by: Codex:gpt-6 Change-Id: I7e5ccd574676a610d34b2467d49b9099e4c77e73
1 parent
67959d70
lib/std/lang/ast.rad
+7 -7
| 815 | 815 | Range(Range), |
|
| 816 | 816 | /// Array literal expression. |
|
| 817 | 817 | ArrayLit(*[*Node]), |
|
| 818 | 818 | /// Array repeat literal expression. |
|
| 819 | 819 | ArrayRepeatLit(ArrayRepeatLit), |
|
| 820 | - | /// Array subscript expression. |
|
| 821 | - | Subscript { |
|
| 822 | - | /// Array or slice. |
|
| 823 | - | container: *Node, |
|
| 824 | - | /// Index expression. |
|
| 825 | - | index: *Node, |
|
| 820 | + | /// Postfix bracket expression. |
|
| 821 | + | Bracket { |
|
| 822 | + | /// Expression before the brackets. |
|
| 823 | + | base: *Node, |
|
| 824 | + | /// Arguments in source order. |
|
| 825 | + | items: *[*Node], |
|
| 826 | 826 | }, |
|
| 827 | 827 | /// Binary operator expression. |
|
| 828 | 828 | BinOp(BinOp), |
|
| 829 | 829 | /// Unary operator expression. |
|
| 830 | 830 | UnOp(UnOp), |
| 1069 | 1069 | match node.value { |
|
| 1070 | 1070 | case |
|
| 1071 | 1071 | NodeValue::Ident(_), |
|
| 1072 | 1072 | NodeValue::ScopeAccess(_), |
|
| 1073 | 1073 | NodeValue::FieldAccess(_), |
|
| 1074 | - | NodeValue::Subscript { .. }, |
|
| 1074 | + | NodeValue::Bracket { .. }, |
|
| 1075 | 1075 | NodeValue::Deref(_) => return true, |
|
| 1076 | 1076 | else => return false, |
|
| 1077 | 1077 | } |
|
| 1078 | 1078 | } |
|
| 1079 | 1079 |
lib/std/lang/ast/printer.rad
+8 -2
| 341 | 341 | } |
|
| 342 | 342 | return sexpr::Expr::List { head: "call", tail: buf, multiline: false }; |
|
| 343 | 343 | } |
|
| 344 | 344 | case super::NodeValue::BuiltinCall { kind, args } => |
|
| 345 | 345 | return sexpr::list(a, super::builtinText(kind), nodeListToExprs(a, args)), |
|
| 346 | - | case super::NodeValue::Subscript { container, index } => |
|
| 347 | - | return sexpr::list(a, "[]", &[toExpr(a, container), toExpr(a, index)]), |
|
| 346 | + | case super::NodeValue::Bracket { base, items } => { |
|
| 347 | + | let buf = try! sexpr::allocExprs(a, items.len as u32 + 1); |
|
| 348 | + | set buf[0] = toExpr(a, base); |
|
| 349 | + | for item, i in items { |
|
| 350 | + | set buf[i + 1] = toExpr(a, item); |
|
| 351 | + | } |
|
| 352 | + | return sexpr::list(a, "[]", buf); |
|
| 353 | + | } |
|
| 348 | 354 | case super::NodeValue::FieldAccess(acc) => |
|
| 349 | 355 | return sexpr::list(a, ".", &[toExpr(a, acc.parent), toExpr(a, acc.child)]), |
|
| 350 | 356 | case super::NodeValue::ScopeAccess(acc) => |
|
| 351 | 357 | return sexpr::list(a, "::", &[toExpr(a, acc.parent), toExpr(a, acc.child)]), |
|
| 352 | 358 | case super::NodeValue::AddressOf(addr) => |
lib/std/lang/lower.rad
+11 -11
| 2455 | 2455 | return b.values.len - start; |
|
| 2456 | 2456 | } |
|
| 2457 | 2457 | } |
|
| 2458 | 2458 | } |
|
| 2459 | 2459 | match node.value { |
|
| 2460 | - | case ast::NodeValue::FieldAccess(_), ast::NodeValue::Subscript { .. } => { |
|
| 2460 | + | case ast::NodeValue::FieldAccess(_), ast::NodeValue::Bracket { .. } => { |
|
| 2461 | 2461 | if let source = self.resolver.constantNode(node) { |
|
| 2462 | 2462 | return try lowerConstDataPayloadInto(self, source, ty, dataPrefix, b); |
|
| 2463 | 2463 | } |
|
| 2464 | 2464 | } |
|
| 2465 | 2465 | else => {} |
| 7723 | 7723 | self: &mut Lowerer, |
|
| 7724 | 7724 | node: *ast::Node, |
|
| 7725 | 7725 | addr: ast::AddressOf |
|
| 7726 | 7726 | ) -> il::Val throws (LowerError) { |
|
| 7727 | 7727 | // Handle subscript range: `&ary[start..end]`. |
|
| 7728 | - | if let case ast::NodeValue::Subscript { container, index } = addr.target.value { |
|
| 7729 | - | if let case ast::NodeValue::Range(range) = index.value { |
|
| 7728 | + | if let case ast::NodeValue::Bracket { base: container, items } = addr.target.value { |
|
| 7729 | + | if let case ast::NodeValue::Range(range) = items[0].value { |
|
| 7730 | 7730 | return try lowerSliceRange(self, container, range, node); |
|
| 7731 | 7731 | } |
|
| 7732 | 7732 | } |
|
| 7733 | 7733 | // Array literals become slices when their address is taken. |
|
| 7734 | 7734 | match addr.target.value { |
| 7791 | 7791 | /// A place inside a scalar record value is addressed through the outermost |
|
| 7792 | 7792 | /// record place, because the record lowers to its field. |
|
| 7793 | 7793 | fn lowerPlaceAddress(self: &mut Lowerer, target: *ast::Node) -> il::Val throws (LowerError) { |
|
| 7794 | 7794 | let root = try scalarRecordRoot(self, target); |
|
| 7795 | 7795 | match root.value { |
|
| 7796 | - | case ast::NodeValue::Subscript { container, index } => { |
|
| 7797 | - | let result = try lowerElemPtr(self, container, index); |
|
| 7796 | + | case ast::NodeValue::Bracket { base: container, items } => { |
|
| 7797 | + | let result = try lowerElemPtr(self, container, items[0]); |
|
| 7798 | 7798 | ||
| 7799 | 7799 | return il::Val::Reg(result.elemReg); |
|
| 7800 | 7800 | } |
|
| 7801 | 7801 | case ast::NodeValue::FieldAccess(access) => { |
|
| 7802 | 7802 | let fieldRef = try lowerFieldRef(self, access); |
| 8141 | 8141 | // stripped above. The target addresses offset 0 using the |
|
| 8142 | 8142 | // resolver-assigned type. |
|
| 8143 | 8143 | let base = try emitValToReg(self, try lowerExpr(self, pointer)); |
|
| 8144 | 8144 | return FieldRef { base, offset: 0, fieldType: try typeOf(self, root) }; |
|
| 8145 | 8145 | } |
|
| 8146 | - | case ast::NodeValue::Subscript { container, index } => { |
|
| 8146 | + | case ast::NodeValue::Bracket { base: container, items } => { |
|
| 8147 | 8147 | // Array or slice element: `arr[i]`. |
|
| 8148 | - | let elem = try lowerElemPtr(self, container, index); |
|
| 8148 | + | let elem = try lowerElemPtr(self, container, items[0]); |
|
| 8149 | 8149 | return FieldRef { base: elem.elemReg, offset: 0, fieldType: elem.elemType }; |
|
| 8150 | 8150 | } |
|
| 8151 | 8151 | else => return nil, |
|
| 8152 | 8152 | } |
|
| 8153 | 8153 | } |
| 8184 | 8184 | ||
| 8185 | 8185 | /// Lower an assignment statement. |
|
| 8186 | 8186 | fn lowerAssign(self: &mut Lowerer, node: *ast::Node, a: ast::Assign) throws (LowerError) { |
|
| 8187 | 8187 | // Slice assignment: `slice[range] = value`. |
|
| 8188 | 8188 | if let info = resolver::sliceRangeInfoFor(&self.resolver, node) { |
|
| 8189 | - | let case ast::NodeValue::Subscript { container, index } = a.left.value |
|
| 8189 | + | let case ast::NodeValue::Bracket { base: container, items } = a.left.value |
|
| 8190 | 8190 | else panic "lowerAssign: slice assign without subscript"; |
|
| 8191 | - | let case ast::NodeValue::Range(range) = index.value |
|
| 8191 | + | let case ast::NodeValue::Range(range) = items[0].value |
|
| 8192 | 8192 | else panic "lowerAssign: slice assign without range"; |
|
| 8193 | 8193 | try lowerSliceAssign(self, a.right, container, range, info); |
|
| 8194 | 8194 | ||
| 8195 | 8195 | return; |
|
| 8196 | 8196 | } |
| 11321 | 11321 | set val = try lowerBinOp(self, node, binop); |
|
| 11322 | 11322 | } |
|
| 11323 | 11323 | case ast::NodeValue::UnOp(unop) => { |
|
| 11324 | 11324 | set val = try lowerUnOp(self, node, unop); |
|
| 11325 | 11325 | } |
|
| 11326 | - | case ast::NodeValue::Subscript { container, index } => { |
|
| 11327 | - | set val = try lowerSubscript(self, node, container, index); |
|
| 11326 | + | case ast::NodeValue::Bracket { base: container, items } => { |
|
| 11327 | + | set val = try lowerSubscript(self, node, container, items[0]); |
|
| 11328 | 11328 | } |
|
| 11329 | 11329 | case ast::NodeValue::BuiltinCall { kind, args } => { |
|
| 11330 | 11330 | set val = try lowerBuiltinCall(self, node, kind, args); |
|
| 11331 | 11331 | } |
|
| 11332 | 11332 | case ast::NodeValue::Call(call) => { |
lib/std/lang/parser.rad
+28 -36
| 441 | 441 | try markBroken(p, cond, beforeIf or beforeElse, ast::BrokenSite::Chain); |
|
| 442 | 442 | ||
| 443 | 443 | return cond; |
|
| 444 | 444 | } |
|
| 445 | 445 | ||
| 446 | - | /// Parse array subscript or slice expression after `[`. |
|
| 447 | - | fn parseSubscriptOrSlice(p: &mut Parser, container: *ast::Node) -> *ast::Node |
|
| 446 | + | /// Parse one bracket item, including a range with no start. |
|
| 447 | + | fn parseBracketItem(p: &mut Parser) -> *ast::Node |
|
| 448 | 448 | throws (ParseError, AllocError) |
|
| 449 | 449 | { |
|
| 450 | - | try expect(p, scanner::TokenKind::LBracket, "expected `[`"); |
|
| 451 | - | ||
| 452 | - | let mut index: *ast::Node = undefined; |
|
| 453 | - | ||
| 454 | - | if try consume(p, scanner::TokenKind::DotDot) { |
|
| 455 | - | // Either `..` or `..end`. |
|
| 456 | - | let mut endExpr: ?*ast::Node = nil; |
|
| 457 | - | if not check(p, scanner::TokenKind::RBracket) { |
|
| 458 | - | set endExpr = try parseNormalExpr(p); |
|
| 459 | - | } |
|
| 460 | - | set index = try node(p, ast::NodeValue::Range( |
|
| 461 | - | ast::Range { start: nil, end: endExpr } |
|
| 462 | - | )); |
|
| 463 | - | } else { |
|
| 464 | - | // Either `n`, `n..` or `n..end`. |
|
| 465 | - | let startExpr = try parseNormalExpr(p); |
|
| 466 | - | ||
| 467 | - | if try consume(p, scanner::TokenKind::DotDot) { |
|
| 468 | - | // Either `n..` or `n..end`. |
|
| 469 | - | let mut endExpr: ?*ast::Node = nil; |
|
| 470 | - | if not check(p, scanner::TokenKind::RBracket) { |
|
| 471 | - | set endExpr = try parseNormalExpr(p); |
|
| 472 | - | } |
|
| 473 | - | set index = try node(p, ast::NodeValue::Range( |
|
| 474 | - | ast::Range { start: startExpr, end: endExpr } |
|
| 475 | - | )); |
|
| 476 | - | } else { |
|
| 477 | - | // Just `n` - regular indexing. |
|
| 478 | - | set index = startExpr; |
|
| 479 | - | } |
|
| 450 | + | if not try consume(p, scanner::TokenKind::DotDot) { |
|
| 451 | + | return try parseNormalExpr(p); |
|
| 452 | + | } |
|
| 453 | + | let mut end: ?*ast::Node = nil; |
|
| 454 | + | if not check(p, scanner::TokenKind::RBracket) |
|
| 455 | + | and not check(p, scanner::TokenKind::Comma) |
|
| 456 | + | { |
|
| 457 | + | set end = try parseNormalExpr(p); |
|
| 480 | 458 | } |
|
| 481 | - | try expect(p, scanner::TokenKind::RBracket, "expected `]` after array index"); |
|
| 459 | + | return try node(p, ast::NodeValue::Range(ast::Range { start: nil, end })); |
|
| 460 | + | } |
|
| 482 | 461 | ||
| 483 | - | return try node(p, ast::NodeValue::Subscript { container, index }); |
|
| 462 | + | /// Parse the items in a postfix bracket expression. |
|
| 463 | + | fn parseBracket(p: &mut Parser, base: *ast::Node) -> *ast::Node |
|
| 464 | + | throws (ParseError, AllocError) |
|
| 465 | + | { |
|
| 466 | + | try advance(p); |
|
| 467 | + | let mut outer = openList(p); |
|
| 468 | + | let items = try parseItems( |
|
| 469 | + | p, scanner::TokenKind::RBracket, parseBracketItem, &mut outer |
|
| 470 | + | ); |
|
| 471 | + | try closeSoftList(p, outer, &items[..]); |
|
| 472 | + | try expect(p, scanner::TokenKind::RBracket, "expected `]` after bracket items"); |
|
| 473 | + | let bracket: *ast::Node = try node(p, ast::NodeValue::Bracket { base, items }); |
|
| 474 | + | try attachClosing(p, bracket); |
|
| 475 | + | return bracket; |
|
| 484 | 476 | } |
|
| 485 | 477 | ||
| 486 | 478 | /// Parse postfix operators (eg. field access, function call etc.) |
|
| 487 | 479 | fn parsePostfix(p: &mut Parser, expr: *ast::Node) -> *ast::Node |
|
| 488 | 480 | throws (ParseError, AllocError) |
| 506 | 498 | set result = try node(p, ast::NodeValue::ScopeAccess( |
|
| 507 | 499 | ast::Access { parent: result, child: ident } |
|
| 508 | 500 | )); |
|
| 509 | 501 | } |
|
| 510 | 502 | case scanner::TokenKind::LBracket => { |
|
| 511 | - | set result = try parseSubscriptOrSlice(p, result); |
|
| 503 | + | set result = try parseBracket(p, result); |
|
| 512 | 504 | } |
|
| 513 | 505 | case scanner::TokenKind::LParen => { |
|
| 514 | 506 | set result = try parseCall(p, result); |
|
| 515 | 507 | } |
|
| 516 | 508 | case scanner::TokenKind::LBrace if p.context <> Context::Condition => { |
| 667 | 659 | return true, |
|
| 668 | 660 | case ast::NodeValue::FieldAccess(_) => |
|
| 669 | 661 | return true, |
|
| 670 | 662 | case ast::NodeValue::ScopeAccess(_) => |
|
| 671 | 663 | return true, |
|
| 672 | - | case ast::NodeValue::Subscript { .. } => |
|
| 664 | + | case ast::NodeValue::Bracket { .. } => |
|
| 673 | 665 | return true, |
|
| 674 | 666 | case ast::NodeValue::Deref(_) => |
|
| 675 | 667 | return true, |
|
| 676 | 668 | else => |
|
| 677 | 669 | return false, |
lib/std/lang/parser/tests.rad
+41 -11
| 2127 | 2127 | } |
|
| 2128 | 2128 | ||
| 2129 | 2129 | /// Test parsing array subscript expression. |
|
| 2130 | 2130 | @test unsafe fn testParseArraySubscript() throws (testing::TestError) { |
|
| 2131 | 2131 | let root = try! parseExprStr("array[index]"); |
|
| 2132 | - | let case ast::NodeValue::Subscript { container, index } = root.value |
|
| 2132 | + | let case ast::NodeValue::Bracket { base: container, items } = root.value |
|
| 2133 | 2133 | else throw testing::TestError::Failed; |
|
| 2134 | 2134 | ||
| 2135 | 2135 | try expectIdent(container, "array"); |
|
| 2136 | - | try expectIdent(index, "index"); |
|
| 2136 | + | try testing::expect(items.len == 1); |
|
| 2137 | + | try expectIdent(items[0], "index"); |
|
| 2137 | 2138 | } |
|
| 2138 | 2139 | ||
| 2139 | 2140 | /// Test parsing array slicing expressions. |
|
| 2140 | 2141 | @test unsafe fn testParseArraySlicing() throws (testing::TestError) { |
|
| 2141 | 2142 | // Test `array[start..end]`. |
|
| 2142 | 2143 | { |
|
| 2143 | 2144 | let expr = try! parseExprStr("array[1..10]"); |
|
| 2144 | - | let case ast::NodeValue::Subscript { container: subContainer, index: subIndex } = expr.value |
|
| 2145 | + | let case ast::NodeValue::Bracket { base: subContainer, items } = expr.value |
|
| 2145 | 2146 | else throw testing::TestError::Failed; |
|
| 2146 | 2147 | try expectIdent(subContainer, "array"); |
|
| 2147 | 2148 | ||
| 2148 | - | let case ast::NodeValue::Range(range) = subIndex.value |
|
| 2149 | + | let case ast::NodeValue::Range(range) = items[0].value |
|
| 2149 | 2150 | else throw testing::TestError::Failed; |
|
| 2150 | 2151 | let start = range.start else throw testing::TestError::Failed; |
|
| 2151 | 2152 | let end = range.end else throw testing::TestError::Failed; |
|
| 2152 | 2153 | try expectNumber(start, "1"); |
|
| 2153 | 2154 | try expectNumber(end, "10"); |
|
| 2154 | 2155 | } |
|
| 2155 | 2156 | // Test `array[start..]`. |
|
| 2156 | 2157 | { |
|
| 2157 | 2158 | let expr = try! parseExprStr("array[5..]"); |
|
| 2158 | - | let case ast::NodeValue::Subscript { container: subContainer, index: subIndex } = expr.value |
|
| 2159 | + | let case ast::NodeValue::Bracket { base: subContainer, items } = expr.value |
|
| 2159 | 2160 | else throw testing::TestError::Failed; |
|
| 2160 | 2161 | try expectIdent(subContainer, "array"); |
|
| 2161 | 2162 | ||
| 2162 | - | let case ast::NodeValue::Range(range) = subIndex.value |
|
| 2163 | + | let case ast::NodeValue::Range(range) = items[0].value |
|
| 2163 | 2164 | else throw testing::TestError::Failed; |
|
| 2164 | 2165 | let start = range.start else throw testing::TestError::Failed; |
|
| 2165 | 2166 | try expectNumber(start, "5"); |
|
| 2166 | 2167 | try testing::expect(range.end == nil); |
|
| 2167 | 2168 | } |
|
| 2168 | 2169 | // Test `array[..end]`. |
|
| 2169 | 2170 | { |
|
| 2170 | 2171 | let expr = try! parseExprStr("array[..10]"); |
|
| 2171 | - | let case ast::NodeValue::Subscript { container: subContainer, index: subIndex } = expr.value |
|
| 2172 | + | let case ast::NodeValue::Bracket { base: subContainer, items } = expr.value |
|
| 2172 | 2173 | else throw testing::TestError::Failed; |
|
| 2173 | 2174 | try expectIdent(subContainer, "array"); |
|
| 2174 | 2175 | ||
| 2175 | - | let case ast::NodeValue::Range(range) = subIndex.value |
|
| 2176 | + | let case ast::NodeValue::Range(range) = items[0].value |
|
| 2176 | 2177 | else throw testing::TestError::Failed; |
|
| 2177 | 2178 | try testing::expect(range.start == nil); |
|
| 2178 | 2179 | let end = range.end else throw testing::TestError::Failed; |
|
| 2179 | 2180 | try expectNumber(end, "10"); |
|
| 2180 | 2181 | } |
|
| 2181 | 2182 | // Test `array[..]`. |
|
| 2182 | 2183 | { |
|
| 2183 | 2184 | let expr = try! parseExprStr("array[..]"); |
|
| 2184 | - | let case ast::NodeValue::Subscript { container: subContainer, index: subIndex } = expr.value |
|
| 2185 | + | let case ast::NodeValue::Bracket { base: subContainer, items } = expr.value |
|
| 2185 | 2186 | else throw testing::TestError::Failed; |
|
| 2186 | 2187 | try expectIdent(subContainer, "array"); |
|
| 2187 | 2188 | ||
| 2188 | - | let case ast::NodeValue::Range(range) = subIndex.value |
|
| 2189 | + | let case ast::NodeValue::Range(range) = items[0].value |
|
| 2189 | 2190 | else throw testing::TestError::Failed; |
|
| 2190 | 2191 | try testing::expect(range.start == nil); |
|
| 2191 | 2192 | try testing::expect(range.end == nil); |
|
| 2192 | 2193 | } |
|
| 2193 | 2194 | } |
|
| 2194 | 2195 | ||
| 2196 | + | /// Check that an open range can end at a conditional expression. |
|
| 2197 | + | @test unsafe fn testParseSliceConditionalEnd() throws (testing::TestError) { |
|
| 2198 | + | let root = try! parseExprStr("array[..n if flag else m]"); |
|
| 2199 | + | let case ast::NodeValue::Bracket { items, .. } = root.value |
|
| 2200 | + | else throw testing::TestError::Failed; |
|
| 2201 | + | let case ast::NodeValue::Range(range) = items[0].value else throw testing::TestError::Failed; |
|
| 2202 | + | let end = range.end else throw testing::TestError::Failed; |
|
| 2203 | + | let case ast::NodeValue::CondExpr(_) = end.value else throw testing::TestError::Failed; |
|
| 2204 | + | } |
|
| 2205 | + | ||
| 2195 | 2206 | /// Test parsing function call expression. |
|
| 2196 | 2207 | @test unsafe fn testParseFunctionCall() throws (testing::TestError) { |
|
| 2197 | 2208 | let root = try! parseExprStr("func(x, y)"); |
|
| 2198 | 2209 | let case ast::NodeValue::Call(call) = root.value else throw testing::TestError::Failed; |
|
| 2199 | 2210 |
| 2204 | 2215 | } |
|
| 2205 | 2216 | ||
| 2206 | 2217 | /// Test parsing chained postfix operators. |
|
| 2207 | 2218 | @test unsafe fn testParseChainedPostfix() throws (testing::TestError) { |
|
| 2208 | 2219 | let root = try! parseExprStr("obj.method(arg)[0]"); |
|
| 2209 | - | let case ast::NodeValue::Subscript { container: subContainer, index: subIndex } = root.value |
|
| 2220 | + | let case ast::NodeValue::Bracket { base: subContainer, items } = root.value |
|
| 2210 | 2221 | else throw testing::TestError::Failed; |
|
| 2211 | 2222 | ||
| 2212 | 2223 | let case ast::NodeValue::Call(call) = subContainer.value else throw testing::TestError::Failed; |
|
| 2213 | 2224 | ||
| 2214 | 2225 | let case ast::NodeValue::FieldAccess(fieldAccess) = call.callee.value |
| 3876 | 3887 | set rejected = true; |
|
| 3877 | 3888 | }; |
|
| 3878 | 3889 | assert rejected; |
|
| 3879 | 3890 | keepNodes(&parser); |
|
| 3880 | 3891 | } |
|
| 3892 | + | ||
| 3893 | + | /// Check that a bracket list can precede another postfix operator. |
|
| 3894 | + | @test unsafe fn testParseBracketList() throws (testing::TestError) { |
|
| 3895 | + | let root = try! parseExprStr("module::make[Pair[A, B], C]()"); |
|
| 3896 | + | let case ast::NodeValue::Call(call) = root.value else throw testing::TestError::Failed; |
|
| 3897 | + | let case ast::NodeValue::Bracket { base, items } = call.callee.value |
|
| 3898 | + | else throw testing::TestError::Failed; |
|
| 3899 | + | let case ast::NodeValue::ScopeAccess(access) = base.value else throw testing::TestError::Failed; |
|
| 3900 | + | try expectIdent(access.parent, "module"); |
|
| 3901 | + | try expectIdent(access.child, "make"); |
|
| 3902 | + | try testing::expect(items.len == 2); |
|
| 3903 | + | let case ast::NodeValue::Bracket { base: family, items: args } = items[0].value |
|
| 3904 | + | else throw testing::TestError::Failed; |
|
| 3905 | + | try expectIdent(family, "Pair"); |
|
| 3906 | + | try testing::expect(args.len == 2); |
|
| 3907 | + | try expectIdent(args[0], "A"); |
|
| 3908 | + | try expectIdent(args[1], "B"); |
|
| 3909 | + | try expectIdent(items[1], "C"); |
|
| 3910 | + | } |
lib/std/lang/pretty.rad
+20 -7
| 1290 | 1290 | try add(a, &mut kids, SOFT_INDENT, try group(a, nil, inner)); |
|
| 1291 | 1291 | try punct(a, &mut kids, SOFT, "]"); |
|
| 1292 | 1292 | let forced = takeMark(p, node, ast::BrokenSite::List); |
|
| 1293 | 1293 | return try forceBroken(a, try group(a, node, kids), forced); |
|
| 1294 | 1294 | } |
|
| 1295 | - | case ast::NodeValue::Subscript { container, index } => { |
|
| 1295 | + | case ast::NodeValue::Bracket { base, items } => { |
|
| 1296 | 1296 | try putOperand( |
|
| 1297 | - | a, p, &mut kids, NONE, container, ctx, LEVEL_POSTFIX |
|
| 1297 | + | a, p, &mut kids, NONE, base, ctx, LEVEL_POSTFIX |
|
| 1298 | + | ); |
|
| 1299 | + | let list = SoftSpec { |
|
| 1300 | + | source: nil, |
|
| 1301 | + | container: node, |
|
| 1302 | + | open: "[", |
|
| 1303 | + | close: "]", |
|
| 1304 | + | pad: layout::Gap::None, |
|
| 1305 | + | tail: nil, |
|
| 1306 | + | commas: Commas::Between, |
|
| 1307 | + | forced: false, |
|
| 1308 | + | }; |
|
| 1309 | + | try add( |
|
| 1310 | + | a, |
|
| 1311 | + | &mut kids, |
|
| 1312 | + | NONE, |
|
| 1313 | + | try softList( |
|
| 1314 | + | a, p, list, items, Ctx::Normal, expr |
|
| 1315 | + | ) |
|
| 1298 | 1316 | ); |
|
| 1299 | - | // An index stays whole, so that a line breaks around it. |
|
| 1300 | - | try punct(a, &mut kids, NONE, "["); |
|
| 1301 | - | let inner = try expr(a, p, index, Ctx::Normal); |
|
| 1302 | - | try add(a, &mut kids, NONE, try flattened(a, inner)); |
|
| 1303 | - | try punct(a, &mut kids, NONE, "]"); |
|
| 1304 | 1317 | } |
|
| 1305 | 1318 | case ast::NodeValue::BinOp(bin) => { |
|
| 1306 | 1319 | let forced = try chain( |
|
| 1307 | 1320 | a, p, &mut kids, node, ast::precedence(bin.op), ctx |
|
| 1308 | 1321 | ); |
lib/std/lang/resolver.rad
+61 -33
| 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 { |
|
| 633 | + | /// The base must be an array or slice. |
|
| 634 | + | Base, |
|
| 635 | + | /// A subscript must have exactly one index or range. |
|
| 636 | + | Arity, |
|
| 637 | + | } |
|
| 638 | + | ||
| 631 | 639 | /// High-level classification for semantic diagnostics. |
|
| 632 | 640 | export union ErrorKind: Copy { |
|
| 633 | 641 | /// Identifier declared more than once in the same scope. |
|
| 634 | 642 | DuplicateBinding(*[u8]), |
|
| 635 | 643 | /// Union variants have the same discriminant. |
| 722 | 730 | DerefLabeledRecord, |
|
| 723 | 731 | /// `*` on a positional record that does not have exactly one field. |
|
| 724 | 732 | DerefRecordFieldCount, |
|
| 725 | 733 | /// Expected a record type. |
|
| 726 | 734 | ExpectedRecord, |
|
| 727 | - | /// Expected an array or slice value. |
|
| 728 | - | ExpectedIndexable, |
|
| 735 | + | /// Invalid array or slice subscript. |
|
| 736 | + | Subscript(SubscriptError), |
|
| 729 | 737 | /// Expected an iterable (array, slice, or range) for a `for` loop. |
|
| 730 | 738 | ExpectedIterable, |
|
| 731 | 739 | /// Invalid `as` cast between the provided types. |
|
| 732 | 740 | InvalidAsCast(InvalidAsCast), |
|
| 733 | 741 | /// Invalid alignment value specified. |
| 1808 | 1816 | case ast::NodeValue::FieldAccess(access) => { |
|
| 1809 | 1817 | let parent = self.constantNode(access.parent) else return nil; |
|
| 1810 | 1818 | let index = recordFieldIndexFor(self, access.child) else return nil; |
|
| 1811 | 1819 | return self.constantField(parent, index); |
|
| 1812 | 1820 | } |
|
| 1813 | - | case ast::NodeValue::Subscript { container, index } => { |
|
| 1821 | + | case ast::NodeValue::Bracket { base: container, items } => { |
|
| 1822 | + | if items.len <> 1 { |
|
| 1823 | + | return nil; |
|
| 1824 | + | } |
|
| 1825 | + | let index = items[0]; |
|
| 1814 | 1826 | let case Type::Array(_) = typeFor(self, container) else return nil; |
|
| 1815 | 1827 | let parent = self.constantNode(container) else return nil; |
|
| 1816 | 1828 | let array = self.constantArray(parent) else return nil; |
|
| 1817 | 1829 | let case ConstValue::Int(value) = constValueEntry(self, index) else return nil; |
|
| 1818 | 1830 | if value.negative or value.magnitude >= array.len as u64 { |
| 1869 | 1881 | let value = self.nodeData.get(node.id).constValue; |
|
| 1870 | 1882 | if value <> nil { |
|
| 1871 | 1883 | return value; |
|
| 1872 | 1884 | } |
|
| 1873 | 1885 | match node.value { |
|
| 1874 | - | case ast::NodeValue::FieldAccess(_), ast::NodeValue::Subscript { .. } => {} |
|
| 1875 | - | else => return nil, |
|
| 1886 | + | case ast::NodeValue::FieldAccess(_), ast::NodeValue::Bracket { .. } => {} |
|
| 1887 | + | else => return nil, |
|
| 1876 | 1888 | } |
|
| 1877 | 1889 | let type = typeFor(self, node) else return nil; |
|
| 1878 | 1890 | if let case Type::Optional(_) = type { |
|
| 1879 | 1891 | return nil; |
|
| 1880 | 1892 | } |
| 3798 | 3810 | return try checkConstructed(self, node, try resolveRecordLit(self, node, &lit, hint)), |
|
| 3799 | 3811 | case ast::NodeValue::ArrayLit(items) => |
|
| 3800 | 3812 | return try checkConstructed(self, node, try resolveArrayLit(self, node, items, hint)), |
|
| 3801 | 3813 | case ast::NodeValue::ArrayRepeatLit(lit) => |
|
| 3802 | 3814 | return try checkConstructed(self, node, try resolveArrayRepeat(self, node, lit, hint)), |
|
| 3803 | - | case ast::NodeValue::Subscript { container, index } => |
|
| 3804 | - | return try resolveSubscript(self, node, container, index), |
|
| 3815 | + | case ast::NodeValue::Bracket { base, items } => |
|
| 3816 | + | return try resolveBracket(self, node, base, items), |
|
| 3805 | 3817 | case ast::NodeValue::ScopeAccess(access) => |
|
| 3806 | 3818 | return try resolveScopeAccess(self, node, access), |
|
| 3807 | 3819 | case ast::NodeValue::AddressOf(addr) => return try resolveAddressOf(self, node, addr, hint), |
|
| 3808 | 3820 | case ast::NodeValue::Deref(target) => return try resolveDeref(self, node, target, hint), |
|
| 3809 | 3821 | case ast::NodeValue::As(expr) => return try resolveAs(self, node, expr), |
| 4129 | 4141 | if let case Type::Array(_) = autoDeref(parent) { |
|
| 4130 | 4142 | return true; |
|
| 4131 | 4143 | } |
|
| 4132 | 4144 | return false; |
|
| 4133 | 4145 | } |
|
| 4134 | - | case ast::NodeValue::Subscript { .. }, ast::NodeValue::Deref(_) => |
|
| 4146 | + | case ast::NodeValue::Bracket { .. }, ast::NodeValue::Deref(_) => |
|
| 4135 | 4147 | return false, |
|
| 4136 | 4148 | else => |
|
| 4137 | 4149 | return true, |
|
| 4138 | 4150 | } |
|
| 4139 | 4151 | } |
| 4153 | 4165 | let parentTy = typeFor(self, access.parent) else return nil; |
|
| 4154 | 4166 | if let _ = getRecord(self, parentTy) { |
|
| 4155 | 4167 | return temporaryRoot(self, access.parent); |
|
| 4156 | 4168 | } |
|
| 4157 | 4169 | } |
|
| 4158 | - | case ast::NodeValue::Subscript { container, .. } => { |
|
| 4170 | + | case ast::NodeValue::Bracket { base: container, .. } => { |
|
| 4159 | 4171 | let containerTy = typeFor(self, container) else return nil; |
|
| 4160 | 4172 | if let case Type::Array(_) = containerTy { |
|
| 4161 | 4173 | return temporaryRoot(self, container); |
|
| 4162 | 4174 | } |
|
| 4163 | 4175 | } |
| 4224 | 4236 | else => {} |
|
| 4225 | 4237 | } |
|
| 4226 | 4238 | } |
|
| 4227 | 4239 | return false; |
|
| 4228 | 4240 | } |
|
| 4229 | - | case ast::NodeValue::FieldAccess(_), ast::NodeValue::Subscript { .. } => |
|
| 4241 | + | case ast::NodeValue::FieldAccess(_), ast::NodeValue::Bracket { .. } => |
|
| 4230 | 4242 | return self.nodeData.get(node.id).constValue |
|
| 4231 | 4243 | <> nil or self.constantNode(node) <> nil, |
|
| 4232 | 4244 | case ast::NodeValue::BuiltinCall { .. } => |
|
| 4233 | 4245 | return self.nodeData.get(node.id).constValue |
|
| 4234 | 4246 | <> nil, |
| 8594 | 8606 | /// Analyze an assignment expression. |
|
| 8595 | 8607 | fn resolveAssign(self: &mut Resolver, node: *ast::Node, assign: ast::Assign) -> Type |
|
| 8596 | 8608 | throws (ResolveError) |
|
| 8597 | 8609 | { |
|
| 8598 | 8610 | // Slice assignment: `slice[range] = value`. |
|
| 8599 | - | if let case ast::NodeValue::Subscript { container, index } = assign.left.value { |
|
| 8611 | + | if let case ast::NodeValue::Bracket { base: container, items } = |
|
| 8612 | + | assign.left.value; |
|
| 8613 | + | items.len == 1 |
|
| 8614 | + | { |
|
| 8615 | + | let index = items[0]; |
|
| 8600 | 8616 | if let case ast::NodeValue::Range(range) = index.value { |
|
| 8601 | 8617 | try infer(self, index); |
|
| 8602 | 8618 | let containerTy = try infer(self, container); |
|
| 8603 | 8619 | if not try canMutateThrough(self, container) { |
|
| 8604 | 8620 | throw immutableTargetError(self, container); |
| 8619 | 8635 | case Type::Array(a) => { |
|
| 8620 | 8636 | try validateArraySliceBounds(self, range, a.length, node); |
|
| 8621 | 8637 | set item = a.item; |
|
| 8622 | 8638 | set capacity = a.length; |
|
| 8623 | 8639 | } |
|
| 8624 | - | else => throw emitError(self, container, ErrorKind::ExpectedIndexable), |
|
| 8640 | + | else => |
|
| 8641 | + | throw emitError(self, container, ErrorKind::Subscript( |
|
| 8642 | + | SubscriptError::Base |
|
| 8643 | + | )), |
|
| 8625 | 8644 | } |
|
| 8626 | 8645 | } |
|
| 8627 | 8646 | // RHS is either a fill value or a source slice. |
|
| 8628 | 8647 | let rhsTy = try infer(self, assign.right); |
|
| 8629 | 8648 | if let case Type::Slice { item: sourceItem, .. } = rhsTy { |
| 8760 | 8779 | }); |
|
| 8761 | 8780 | } |
|
| 8762 | 8781 | } |
|
| 8763 | 8782 | } |
|
| 8764 | 8783 | ||
| 8765 | - | /// Analyze an array or slice subscript expression. |
|
| 8766 | - | fn resolveSubscript( |
|
| 8767 | - | self: &mut Resolver, node: *ast::Node, container: *ast::Node, indexNode: *ast::Node |
|
| 8784 | + | /// Resolve postfix brackets from the type of their base. |
|
| 8785 | + | fn resolveBracket( |
|
| 8786 | + | self: &mut Resolver, node: *ast::Node, base: *ast::Node, items: *[*ast::Node] |
|
| 8768 | 8787 | ) -> Type |
|
| 8769 | 8788 | throws (ResolveError) |
|
| 8770 | 8789 | { |
|
| 8790 | + | let containerTy = try infer(self, base); |
|
| 8791 | + | if items.len <> 1 { |
|
| 8792 | + | throw emitError(self, node, ErrorKind::Subscript(SubscriptError::Arity)); |
|
| 8793 | + | } |
|
| 8794 | + | let indexNode = items[0]; |
|
| 8771 | 8795 | // Range subscripts always require `&` to form a slice. |
|
| 8772 | 8796 | if let case ast::NodeValue::Range(range) = indexNode.value { |
|
| 8773 | 8797 | try infer(self, indexNode); |
|
| 8774 | - | try infer(self, container); |
|
| 8775 | 8798 | try checkSliceRangeIndices(self, range); |
|
| 8776 | 8799 | throw emitError(self, node, ErrorKind::SliceRequiresAddress); |
|
| 8777 | 8800 | } |
|
| 8778 | - | let containerTy = try infer(self, container); |
|
| 8779 | 8801 | if isUnsafePointerType(containerTy) { |
|
| 8780 | - | try requireUnsafe(self, container); |
|
| 8802 | + | try requireUnsafe(self, base); |
|
| 8781 | 8803 | } |
|
| 8782 | 8804 | try checkIndex(self, indexNode); |
|
| 8783 | 8805 | let subjectTy = autoDeref(containerTy); |
|
| 8784 | 8806 | if let case Type::Slice { item, .. } = subjectTy { |
|
| 8785 | 8807 | return setNodeType(self, node, *item); |
| 8787 | 8809 | match subjectTy { |
|
| 8788 | 8810 | case Type::Array(arrayInfo) => { |
|
| 8789 | 8811 | return setNodeType(self, node, *arrayInfo.item); |
|
| 8790 | 8812 | } |
|
| 8791 | 8813 | else => { |
|
| 8792 | - | throw emitError(self, container, ErrorKind::ExpectedIndexable); |
|
| 8814 | + | throw emitError(self, base, ErrorKind::Subscript(SubscriptError::Base)); |
|
| 8793 | 8815 | } |
|
| 8794 | 8816 | } |
|
| 8795 | 8817 | } |
|
| 8796 | 8818 | ||
| 8797 | 8819 | /// Find a record field by name. |
| 9323 | 9345 | /// Return the place that one access step reads from: the parent of a field |
|
| 9324 | 9346 | /// access, the container of a subscript, or the target of a dereference. |
|
| 9325 | 9347 | /// Any other node ends an access path, and gives nil. |
|
| 9326 | 9348 | fn placeBase(node: *ast::Node) -> ?*ast::Node { |
|
| 9327 | 9349 | match node.value { |
|
| 9328 | - | case ast::NodeValue::FieldAccess(access) => return access.parent, |
|
| 9329 | - | case ast::NodeValue::Subscript { container, .. } => return container, |
|
| 9330 | - | case ast::NodeValue::Deref(target) => return target, |
|
| 9331 | - | else => return nil, |
|
| 9350 | + | case ast::NodeValue::FieldAccess(access) => return access.parent, |
|
| 9351 | + | case ast::NodeValue::Bracket { base: container, .. } => return container, |
|
| 9352 | + | case ast::NodeValue::Deref(target) => return target, |
|
| 9353 | + | else => return nil, |
|
| 9332 | 9354 | } |
|
| 9333 | 9355 | } |
|
| 9334 | 9356 | ||
| 9335 | 9357 | /// Return the immutable pointer, slice, or trait object that makes the target |
|
| 9336 | 9358 | /// of `node` immutable: `node` itself, or one dereferenced on its access path, |
| 9364 | 9386 | } |
|
| 9365 | 9387 | return nil; |
|
| 9366 | 9388 | }; |
|
| 9367 | 9389 | // A subscript through a pointer to a slice also dereferences the slice. |
|
| 9368 | 9390 | // An immutable slice there is reported at the pointer. |
|
| 9369 | - | if let case ast::NodeValue::Subscript { .. } = node.value { |
|
| 9391 | + | if let case ast::NodeValue::Bracket { .. } = node.value { |
|
| 9370 | 9392 | if let ty = typeFor(self, base) { |
|
| 9371 | 9393 | if let case Type::Pointer { target, .. } = ty { |
|
| 9372 | 9394 | if let case Type::Slice { mutable: false, .. } = *target { |
|
| 9373 | 9395 | return base; |
|
| 9374 | 9396 | } |
| 9437 | 9459 | if let case SymbolData::Value { mutable, .. } = self.symbols[*sym].data { |
|
| 9438 | 9460 | return mutable; |
|
| 9439 | 9461 | } |
|
| 9440 | 9462 | return false; |
|
| 9441 | 9463 | } |
|
| 9442 | - | case ast::NodeValue::Subscript { container, .. } => { |
|
| 9464 | + | case ast::NodeValue::Bracket { base: container, .. } => { |
|
| 9443 | 9465 | let containerTy = try infer(self, container); |
|
| 9444 | 9466 | // Subscript auto-derefs pointers, so check the actual indexed type. |
|
| 9445 | 9467 | let subjectTy = autoDeref(containerTy); |
|
| 9446 | 9468 | ||
| 9447 | 9469 | if let case Type::Slice { mutable, .. } = subjectTy { |
| 9616 | 9638 | } |
|
| 9617 | 9639 | } |
|
| 9618 | 9640 | } |
|
| 9619 | 9641 | case |
|
| 9620 | 9642 | ast::NodeValue::FieldAccess(_), |
|
| 9621 | - | ast::NodeValue::Subscript { .. }, |
|
| 9643 | + | ast::NodeValue::Bracket { .. }, |
|
| 9622 | 9644 | ast::NodeValue::Deref(_) => |
|
| 9623 | 9645 | { |
|
| 9624 | 9646 | let base = placeBase(node) else panic "addressStorageClass: expected access step"; |
|
| 9625 | 9647 | if let ty = typeFor(self, base) { |
|
| 9626 | 9648 | // A subscript indexes a slice, also through a pointer to one. |
|
| 9627 | - | if let case ast::NodeValue::Subscript { .. } = node.value { |
|
| 9649 | + | if let case ast::NodeValue::Bracket { .. } = node.value { |
|
| 9628 | 9650 | if let case Type::Slice { .. } = autoDeref(ty) { |
|
| 9629 | 9651 | return pointerTargetClass(self, autoDeref(ty), base); |
|
| 9630 | 9652 | } |
|
| 9631 | 9653 | } |
|
| 9632 | 9654 | if let case Type::Pointer { .. } = ty { |
| 9721 | 9743 | if temporaryRoot(self, addr.target) == nil { |
|
| 9722 | 9744 | throw immutablePlaceError(self, addr.target); |
|
| 9723 | 9745 | } |
|
| 9724 | 9746 | } |
|
| 9725 | 9747 | } |
|
| 9726 | - | if let case ast::NodeValue::Subscript { container, index } = addr.target.value { |
|
| 9748 | + | if let case ast::NodeValue::Bracket { base: container, items } = |
|
| 9749 | + | addr.target.value; |
|
| 9750 | + | items.len == 1 |
|
| 9751 | + | { |
|
| 9752 | + | let index = items[0]; |
|
| 9727 | 9753 | if let case ast::NodeValue::Range(range) = index.value { |
|
| 9728 | 9754 | let containerTy = try infer(self, container); |
|
| 9729 | 9755 | let subjectTy = autoDeref(containerTy); |
|
| 9730 | 9756 | ||
| 9731 | 9757 | try checkSliceRangeIndices(self, range); |
| 9744 | 9770 | try validateArraySliceBounds(self, range, arrayInfo.length, node); |
|
| 9745 | 9771 | set item = arrayInfo.item; |
|
| 9746 | 9772 | set capacity = arrayInfo.length; |
|
| 9747 | 9773 | } |
|
| 9748 | 9774 | else => { |
|
| 9749 | - | throw emitError(self, container, ErrorKind::ExpectedIndexable); |
|
| 9775 | + | throw emitError(self, container, ErrorKind::Subscript( |
|
| 9776 | + | SubscriptError::Base |
|
| 9777 | + | )); |
|
| 9750 | 9778 | } |
|
| 9751 | 9779 | } |
|
| 9752 | 9780 | } |
|
| 9753 | 9781 | let class = try addressClass(self, addr, hint); |
|
| 9754 | 9782 | let sliceTy = Type::Slice { class, item, mutable: addr.mutable }; |
| 11328 | 11356 | } |
|
| 11329 | 11357 | } |
|
| 11330 | 11358 | } |
|
| 11331 | 11359 | set place.precise = false; |
|
| 11332 | 11360 | } |
|
| 11333 | - | case ast::NodeValue::Subscript { container, .. } => { |
|
| 11361 | + | case ast::NodeValue::Bracket { base: container, .. } => { |
|
| 11334 | 11362 | set place = borrowPlace(self, container); |
|
| 11335 | 11363 | if let ty = typeFor(self, container) { |
|
| 11336 | 11364 | narrowThroughStorage(&mut place, ty); |
|
| 11337 | 11365 | if let case Type::Pointer { target, .. } = ty { |
|
| 11338 | 11366 | narrowThroughStorage(&mut place, *target); |
| 12044 | 12072 | match node.value { |
|
| 12045 | 12073 | case |
|
| 12046 | 12074 | ast::NodeValue::Ident(_), |
|
| 12047 | 12075 | ast::NodeValue::ScopeAccess(_), |
|
| 12048 | 12076 | ast::NodeValue::FieldAccess(_), |
|
| 12049 | - | ast::NodeValue::Subscript { .. }, |
|
| 12077 | + | ast::NodeValue::Bracket { .. }, |
|
| 12050 | 12078 | ast::NodeValue::Deref(_) => |
|
| 12051 | 12079 | { |
|
| 12052 | 12080 | let mut exclusive = usage == LinearUse::Place; |
|
| 12053 | 12081 | if usage == LinearUse::Consume { |
|
| 12054 | 12082 | if let ty = movedType(self, node) { |
| 12225 | 12253 | case ast::NodeValue::ScopeAccess(_) => { |
|
| 12226 | 12254 | if usage == LinearUse::Consume { |
|
| 12227 | 12255 | try checkStaticMove(self, node); |
|
| 12228 | 12256 | } |
|
| 12229 | 12257 | } |
|
| 12230 | - | case ast::NodeValue::Subscript { container, index } => { |
|
| 12258 | + | case ast::NodeValue::Bracket { base: container, items } => { |
|
| 12231 | 12259 | if let resultTy = movedType(self, node) { |
|
| 12232 | 12260 | if isMoveOnly(self, resultTy) and usage == LinearUse::Consume { |
|
| 12233 | 12261 | throw emitError(self, node, ErrorKind::LinearPartialMove); |
|
| 12234 | 12262 | } |
|
| 12235 | 12263 | } |
|
| 12236 | 12264 | try checkLinearNode(self, checker, env, container, LinearUse::Locate); |
|
| 12237 | - | try checkLinearNode(self, checker, env, index, LinearUse::Consume); |
|
| 12265 | + | try checkLinearNode(self, checker, env, items[0], LinearUse::Consume); |
|
| 12238 | 12266 | } |
|
| 12239 | 12267 | case ast::NodeValue::RecordLit(lit) => { |
|
| 12240 | 12268 | let localStart = checker.regionLen; |
|
| 12241 | 12269 | for fieldNode in lit.fields { |
|
| 12242 | 12270 | let case ast::NodeValue::RecordLitField(field) = fieldNode.value |
lib/std/lang/resolver/printer.rad
+7 -2
| 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::ExpectedIndexable => { |
|
| 515 | - | io::print("expected array or slice"); |
|
| 514 | + | case super::ErrorKind::Subscript(error) => { |
|
| 515 | + | match error { |
|
| 516 | + | case super::SubscriptError::Base => |
|
| 517 | + | io::print("expected array or slice"), |
|
| 518 | + | case super::SubscriptError::Arity => |
|
| 519 | + | io::print("a subscript requires exactly one index or range"), |
|
| 520 | + | } |
|
| 516 | 521 | } |
|
| 517 | 522 | case super::ErrorKind::InvalidAlignmentValue(val) => { |
|
| 518 | 523 | io::print("invalid alignment value: "); |
|
| 519 | 524 | io::printU32(val); |
|
| 520 | 525 | } |
lib/std/lang/resolver/refs.rad
+9 -8
| 906 | 906 | return withAddress(self, view, region, positions(self, ty)); |
|
| 907 | 907 | } |
|
| 908 | 908 | } |
|
| 909 | 909 | return region; |
|
| 910 | 910 | } |
|
| 911 | - | case ast::NodeValue::Subscript { container, .. } => { |
|
| 911 | + | case ast::NodeValue::Bracket { base: container, .. } => { |
|
| 912 | 912 | let containerTy = super::typeFor(self, container) else return Region::Local; |
|
| 913 | 913 | return target(self, valueRegion(self, container), containerTy); |
|
| 914 | 914 | } |
|
| 915 | 915 | case ast::NodeValue::Deref(inner) => { |
|
| 916 | 916 | let innerTy = super::typeFor(self, inner) else return Region::Local; |
| 1070 | 1070 | /// Return the region of the storage of place `node`. Storage of the frame, and |
|
| 1071 | 1071 | /// storage behind a pointer that names no region, are local. A literal string |
|
| 1072 | 1072 | /// is static data, which fits any region. |
|
| 1073 | 1073 | fn placeRegion(self: &Resolver, node: *ast::Node) -> Region { |
|
| 1074 | 1074 | match node.value { |
|
| 1075 | - | case ast::NodeValue::String(_) => return Region::Free, |
|
| 1075 | + | case ast::NodeValue::String(_) => return Region::Free, |
|
| 1076 | 1076 | case ast::NodeValue::FieldAccess(access) => return containerRegion(self, access.parent), |
|
| 1077 | - | case ast::NodeValue::Subscript { container, .. } => return containerRegion(self, container), |
|
| 1077 | + | case ast::NodeValue::Bracket { base: container, .. } => |
|
| 1078 | + | return containerRegion(self, container), |
|
| 1078 | 1079 | case ast::NodeValue::Deref(target) => return outerRegion(self, target), |
|
| 1079 | - | case ast::NodeValue::As(cast) => return placeRegion(self, cast.value), |
|
| 1080 | + | case ast::NodeValue::As(cast) => return placeRegion(self, cast.value), |
|
| 1080 | 1081 | case ast::NodeValue::Ident(_), ast::NodeValue::ScopeAccess(_) => { |
|
| 1081 | 1082 | if let sym = super::symbolFor(self, node) { |
|
| 1082 | 1083 | if let case SymbolData::Value { scope, .. } = self.symbols[*sym].data { |
|
| 1083 | 1084 | return Region::Scoped(scope); |
|
| 1084 | 1085 | } |
| 1124 | 1125 | let ty = super::typeFor(self, node) else return nil; |
|
| 1125 | 1126 | if refersToBorrowed(self, ty) { |
|
| 1126 | 1127 | return outerRegion(self, node); |
|
| 1127 | 1128 | } |
|
| 1128 | 1129 | match node.value { |
|
| 1129 | - | case ast::NodeValue::FieldAccess(access) => return mutableView(self, access.parent), |
|
| 1130 | - | case ast::NodeValue::Subscript { container, .. } => return mutableView(self, container), |
|
| 1131 | - | else => return nil, |
|
| 1130 | + | case ast::NodeValue::FieldAccess(access) => return mutableView(self, access.parent), |
|
| 1131 | + | case ast::NodeValue::Bracket { base: container, .. } => return mutableView(self, container), |
|
| 1132 | + | else => return nil, |
|
| 1132 | 1133 | } |
|
| 1133 | 1134 | } |
|
| 1134 | 1135 | ||
| 1135 | 1136 | /// Whether a type is a direct mutable reference. |
|
| 1136 | 1137 | fn mutableReference(ty: Type) -> bool { |
| 1289 | 1290 | }, |
|
| 1290 | 1291 | self.arena |
|
| 1291 | 1292 | ); |
|
| 1292 | 1293 | } |
|
| 1293 | 1294 | } |
|
| 1294 | - | case ast::NodeValue::Subscript { container, .. } => { |
|
| 1295 | + | case ast::NodeValue::Bracket { base: container, .. } => { |
|
| 1295 | 1296 | let parent = super::typeFor(self, container) else continue; |
|
| 1296 | 1297 | let offset: u32 = 1 if super::isRefType(parent) else 0; |
|
| 1297 | 1298 | try! pending.append( |
|
| 1298 | 1299 | StorePlace { |
|
| 1299 | 1300 | node: container, position: place.position + offset |
lib/std/lang/resolver/tests/core.rad
+17 -1
| 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::ExpectedIndexable); |
|
| 1633 | + | try expectErrorKind(&result, resolver::ErrorKind::Subscript(resolver::SubscriptError::Base)); |
|
| 1634 | + | } |
|
| 1635 | + | ||
| 1636 | + | /// Check that a bracket list on an array requires one index or range. |
|
| 1637 | + | @test unsafe fn testResolveSubscriptArity() throws (testing::TestError) { |
|
| 1638 | + | let programs = [ |
|
| 1639 | + | @embed("lang/resolver/tests/fixtures/subscript/empty.rad"), |
|
| 1640 | + | @embed("lang/resolver/tests/fixtures/subscript/range_and_index.rad"), |
|
| 1641 | + | @embed("lang/resolver/tests/fixtures/subscript/store_two_indices.rad"), |
|
| 1642 | + | ]; |
|
| 1643 | + | for program in programs { |
|
| 1644 | + | let mut a = testResolver(); |
|
| 1645 | + | let result = try resolveProgramStr(&mut a, program); |
|
| 1646 | + | try expectErrorKind(&result, resolver::ErrorKind::Subscript( |
|
| 1647 | + | resolver::SubscriptError::Arity |
|
| 1648 | + | )); |
|
| 1649 | + | } |
|
| 1634 | 1650 | } |
|
| 1635 | 1651 | ||
| 1636 | 1652 | @test unsafe fn testResolveSliceFieldUnknown() throws (testing::TestError) { |
|
| 1637 | 1653 | let mut a = testResolver(); |
|
| 1638 | 1654 | let program = "let xs: [i32; 2] = [1, 2]; (&xs[0..]).unknown;"; |
lib/std/lang/resolver/tests/fixtures/subscript/empty.rad
added
+2 -0
| 1 | + | let xs: [u32; 2] = [1, 2]; |
|
| 2 | + | xs[]; |
lib/std/lang/resolver/tests/fixtures/subscript/range_and_index.rad
added
+2 -0
| 1 | + | let xs: [u32; 2] = [1, 2]; |
|
| 2 | + | &xs[0 .., 1]; |
lib/std/lang/resolver/tests/fixtures/subscript/store_two_indices.rad
added
+2 -0
| 1 | + | let mut xs: [u32; 2] = [1, 2]; |
|
| 2 | + | set xs[0, 1] = 3; |
lib/std/std.manifest
+3 -0
| 127 | 127 | "lang/gen/bitset.ras", |
|
| 128 | 128 | ], |
|
| 129 | 129 | embed: [ |
|
| 130 | 130 | "lang/manifest/tests/fixtures/kernel.manifest", |
|
| 131 | 131 | "lang/manifest/tests/fixtures/module_order.manifest", |
|
| 132 | + | "lang/resolver/tests/fixtures/subscript/empty.rad", |
|
| 133 | + | "lang/resolver/tests/fixtures/subscript/range_and_index.rad", |
|
| 134 | + | "lang/resolver/tests/fixtures/subscript/store_two_indices.rad", |
|
| 132 | 135 | ], |
|
| 133 | 136 | } |