lang: Use conditional result type context
96692761a9ea15428b94f02f96c1c21b34c24d62d6aaad3e74829eaa212b7d29
A nil-first conditional could not use its optional annotation to type both arms. Pass context into each arm and use the expected result type when neither arm supplies a common type. Cover absent and present branches, nested conditionals and mismatched arms. Preserve the inferred input type of numeric casts. Assisted-by: Codex:gpt-6
1 parent
746b9a09
lib/std/lang/resolver.rad
+18 -13
| 3605 | 3605 | return try resolveFnDecl(self, node, decl), |
|
| 3606 | 3606 | case ast::NodeValue::ConstDecl(decl) => |
|
| 3607 | 3607 | return try resolveConst( |
|
| 3608 | 3608 | self, node, decl.ident, decl.type, decl.value, decl.attrs |
|
| 3609 | 3609 | ), |
|
| 3610 | - | case ast::NodeValue::StaticDecl(decl) => return try resolveStatic(self, node, decl), |
|
| 3611 | - | case ast::NodeValue::FnParam(param) => return try resolveFnParam(self, node, param), |
|
| 3612 | - | case ast::NodeValue::If(cond) => return try resolveIf(self, node, cond), |
|
| 3613 | - | case ast::NodeValue::CondExpr(cond) => return try resolveCondExpr(self, node, cond), |
|
| 3614 | - | case ast::NodeValue::IfLet(cond) => return try resolveIfLet(self, node, cond), |
|
| 3615 | - | case ast::NodeValue::While(loopNode) => return try resolveWhile(self, node, loopNode), |
|
| 3610 | + | case ast::NodeValue::StaticDecl(decl) => return try resolveStatic(self, node, decl), |
|
| 3611 | + | case ast::NodeValue::FnParam(param) => return try resolveFnParam(self, node, param), |
|
| 3612 | + | case ast::NodeValue::If(cond) => return try resolveIf(self, node, cond), |
|
| 3613 | + | case ast::NodeValue::CondExpr(cond) => return try resolveCondExpr(self, node, cond, hint), |
|
| 3614 | + | case ast::NodeValue::IfLet(cond) => return try resolveIfLet(self, node, cond), |
|
| 3615 | + | case ast::NodeValue::While(loopNode) => return try resolveWhile(self, node, loopNode), |
|
| 3616 | 3616 | case ast::NodeValue::WhileLet(loopNode) => return try resolveWhileLet(self, node, loopNode), |
|
| 3617 | - | case ast::NodeValue::For(loopNode) => return try resolveFor(self, node, loopNode), |
|
| 3617 | + | case ast::NodeValue::For(loopNode) => return try resolveFor(self, node, loopNode), |
|
| 3618 | 3618 | case ast::NodeValue::Loop { body } => { |
|
| 3619 | 3619 | let loopType = try visitLoop(self, body); |
|
| 3620 | 3620 | return setNodeType(self, node, loopType); |
|
| 3621 | 3621 | } |
|
| 3622 | 3622 | case ast::NodeValue::Break => { |
| 5075 | 5075 | let receiverTy = *fnType.paramTypes[0]; |
|
| 5076 | 5076 | let sym = try bindValueIdent(self, receiver, receiver, receiverTy, false, 0, 0); |
|
| 5077 | 5077 | if let fnNode = self.currentFnNode { |
|
| 5078 | 5078 | if let case ast::NodeValue::MethodDecl { receiverType, .. } = fnNode.value { |
|
| 5079 | 5079 | let declared = try declaredRegion(self, receiverType, receiverTy); |
|
| 5080 | - | refs::setSymbolRegion( |
|
| 5081 | - | self, sym, refs::parameterRegion(self, sym, declared, receiverTy) |
|
| 5082 | - | ); |
|
| 5080 | + | refs::setSymbolRegion(self, sym, refs::parameterRegion( |
|
| 5081 | + | self, sym, declared, receiverTy |
|
| 5082 | + | )); |
|
| 5083 | 5083 | } |
|
| 5084 | 5084 | } |
|
| 5085 | 5085 | // Bind the remaining parameters from the signature. |
|
| 5086 | 5086 | for paramNode in params { |
|
| 5087 | 5087 | try infer(self, paramNode); |
| 6348 | 6348 | ||
| 6349 | 6349 | return setNodeType(self, node, unifyBranches(thenTy, elseTy)); |
|
| 6350 | 6350 | } |
|
| 6351 | 6351 | ||
| 6352 | 6352 | /// Analyze a conditional expression. |
|
| 6353 | - | fn resolveCondExpr(self: &mut Resolver, node: *ast::Node, cond: ast::CondExpr) -> Type |
|
| 6353 | + | fn resolveCondExpr(self: &mut Resolver, node: *ast::Node, cond: ast::CondExpr, hint: Type) -> Type |
|
| 6354 | 6354 | throws (ResolveError) |
|
| 6355 | 6355 | { |
|
| 6356 | 6356 | try checkBoolean(self, cond.condition); |
|
| 6357 | - | let thenTy = try infer(self, cond.thenExpr); |
|
| 6358 | - | let elseTy = try infer(self, cond.elseExpr); |
|
| 6357 | + | let thenTy = try visit(self, cond.thenExpr, hint); |
|
| 6358 | + | let elseTy = try visit(self, cond.elseExpr, hint); |
|
| 6359 | 6359 | ||
| 6360 | 6360 | // Either branch may supply the concrete type for an otherwise context- |
|
| 6361 | 6361 | // dependent expression, such as an unsuffixed integer or `nil`. |
|
| 6362 | 6362 | if let coercion = isAssignable(self, thenTy, elseTy, cond.elseExpr) { |
|
| 6363 | 6363 | setNodeCoercion(self, cond.elseExpr, coercion); |
| 6365 | 6365 | } |
|
| 6366 | 6366 | if let coercion = isAssignable(self, elseTy, thenTy, cond.thenExpr) { |
|
| 6367 | 6367 | setNodeCoercion(self, cond.thenExpr, coercion); |
|
| 6368 | 6368 | return setNodeType(self, node, elseTy); |
|
| 6369 | 6369 | } |
|
| 6370 | + | if hint <> Type::Unknown and hint <> Type::Void { |
|
| 6371 | + | try expectAssignable(self, hint, thenTy, cond.thenExpr); |
|
| 6372 | + | try expectAssignable(self, hint, elseTy, cond.elseExpr); |
|
| 6373 | + | return setNodeType(self, node, hint); |
|
| 6374 | + | } |
|
| 6370 | 6375 | try expectAssignable(self, thenTy, elseTy, cond.elseExpr); |
|
| 6371 | 6376 | ||
| 6372 | 6377 | return setNodeType(self, node, thenTy); |
|
| 6373 | 6378 | } |
|
| 6374 | 6379 |
lib/std/lang/resolver/tests/structure.rad
+22 -0
| 3898 | 3898 | let case resolver::ErrorKind::TestSignature(_) = err.kind else throw testing::TestError::Failed; |
|
| 3899 | 3899 | let node = err.node else throw testing::TestError::Failed; |
|
| 3900 | 3900 | let case ast::NodeValue::Ident(name) = node.value else throw testing::TestError::Failed; |
|
| 3901 | 3901 | assert mem::eq(name, "bad"); |
|
| 3902 | 3902 | } |
|
| 3903 | + | ||
| 3904 | + | /// An optional annotation applies to both arms of a nil-first conditional. |
|
| 3905 | + | @test unsafe fn testNilFirstConditionalAnnotation() throws (testing::TestError) { |
|
| 3906 | + | try expectAnalyzeOk( |
|
| 3907 | + | "union Rule: Copy { Return, Bounds } fn f(condition: bool) -> ?Rule { let rule: ?Rule = nil if condition else Rule::Return; return rule; }" |
|
| 3908 | + | ); |
|
| 3909 | + | } |
|
| 3910 | + | ||
| 3911 | + | /// Both annotated conditional arms must fit their common result type. |
|
| 3912 | + | @test unsafe fn testConditionalAnnotationArms() throws (testing::TestError) { |
|
| 3913 | + | try expectAnalyzeOk( |
|
| 3914 | + | "fn f(c: bool) -> ?u8 { let a: ?u8 = 7 if c else nil; return nil if c else a; }" |
|
| 3915 | + | ); |
|
| 3916 | + | let mut a = testResolver(); |
|
| 3917 | + | let result = try resolveProgramStr( |
|
| 3918 | + | &mut a, "fn f(c: bool) { let a: ?u8 = nil if c else true; }" |
|
| 3919 | + | ); |
|
| 3920 | + | let error = try expectError(&result); |
|
| 3921 | + | let case resolver::ErrorKind::TypeMismatch(_) = error.kind else { |
|
| 3922 | + | throw testing::TestError::Failed; |
|
| 3923 | + | }; |
|
| 3924 | + | } |
test/tests/cond.optional.annotation.rad
added
+33 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | ||
| 3 | + | /// Value returned by an optional conditional. |
|
| 4 | + | union Rule: Copy { |
|
| 5 | + | /// A return instruction. |
|
| 6 | + | Return, |
|
| 7 | + | /// A bounds check. |
|
| 8 | + | Bounds, |
|
| 9 | + | } |
|
| 10 | + | ||
| 11 | + | /// Select an optional value with an absent first arm. |
|
| 12 | + | fn choose(condition: bool) -> ?Rule { |
|
| 13 | + | let rule: ?Rule = nil if condition else Rule::Return; |
|
| 14 | + | return rule; |
|
| 15 | + | } |
|
| 16 | + | ||
| 17 | + | /// Nested conditionals use the declared optional result type. |
|
| 18 | + | fn nested(a: bool, b: bool) -> ?u8 { |
|
| 19 | + | return (nil if b else 7) if a else 9; |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | /// Both arms build the declared optional representation. |
|
| 23 | + | @default fn main() -> u32 { |
|
| 24 | + | assert choose(true) == nil; |
|
| 25 | + | let rule = choose(false) else panic; |
|
| 26 | + | assert rule == Rule::Return; |
|
| 27 | + | assert nested(true, true) == nil; |
|
| 28 | + | let inner = nested(true, false) else panic; |
|
| 29 | + | assert inner == 7; |
|
| 30 | + | let outer = nested(false, true) else panic; |
|
| 31 | + | assert outer == 9; |
|
| 32 | + | return 0; |
|
| 33 | + | } |