Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use System.Exception | |
| 3 | ||
| 4 | use Logging | |
| 5 | ||
| 6 | use Semantic.Types.Type | |
| 7 | ||
| 8 | use IR.Values | |
| 9 | ||
| 10 | // Compiles bindings, assignments and returns: `let` statements, | |
| 11 | // assignment statements and their simple-left targets, `let in` | |
| 12 | // expressions, expression statements and `return`. Split out of | |
| 13 | // COMPILE_EXPRESSIONS, which delegates the matching pre / visit | |
| 14 | // methods here. The `super.pre` / `super.visit` base-visitor | |
| 15 | // calls stay in the visitor's thin stubs; the methods here are | |
| 16 | // the enclosed logic. | |
| 17 | class COMPILE_BINDINGS is | |
| 18 | _logger: Logger | |
| 19 | _symbol_table: Semantic.SYMBOL_TABLE | |
| 20 | _symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS | |
| 21 | _innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup | |
| 22 | _task_conversion: Semantic.TASK_CONVERSION | |
| 23 | _awaitable_resolver: Semantic.AWAITABLE_RESOLVER | |
| 24 | _flow: NARROWING_FLOW | |
| 25 | _build_flags: Compiler.GLOBAL_BUILD_FLAGS | |
| 26 | _value_boxer: IR.VALUE_BOXER | |
| 27 | _pure_slots: PURE_SLOT_CHECK | |
| 28 | _visitor: COMPILE_EXPRESSIONS | |
| 29 | _match_propagator: Semantic.MATCH_PROPAGATOR | |
| 30 | ||
| 31 | // Read-only accessor: COMPILE_EXPRESSIONS (the | |
| 32 | // expression-body return path) goes through `_bindings` as | |
| 33 | // its single shared facade and reaches the TASK helpers | |
| 34 | // via this property. | |
| 35 | task_conversion: Semantic.TASK_CONVERSION => _task_conversion | |
| 36 | ||
| 37 | init( | |
| 38 | logger: Logger, | |
| 39 | symbol_table: Semantic.SYMBOL_TABLE, | |
| 40 | symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS, | |
| 41 | innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup, | |
| 42 | task_conversion: Semantic.TASK_CONVERSION, | |
| 43 | awaitable_resolver: Semantic.AWAITABLE_RESOLVER, | |
| 44 | flow: NARROWING_FLOW, | |
| 45 | build_flags: Compiler.GLOBAL_BUILD_FLAGS, | |
| 46 | value_boxer: IR.VALUE_BOXER, | |
| 47 | pure_slots: PURE_SLOT_CHECK, | |
| 48 | visitor: COMPILE_EXPRESSIONS | |
| 49 | ) is | |
| 50 | super.init() | |
| 51 | ||
| 52 | _logger = logger | |
| 53 | _symbol_table = symbol_table | |
| 54 | _symbol_use_locations = symbol_use_locations | |
| 55 | _innate_symbol_lookup = innate_symbol_lookup | |
| 56 | _task_conversion = task_conversion | |
| 57 | _awaitable_resolver = awaitable_resolver | |
| 58 | _flow = flow | |
| 59 | _build_flags = build_flags | |
| 60 | _value_boxer = value_boxer | |
| 61 | _pure_slots = pure_slots | |
| 62 | _visitor = visitor | |
| 63 | _match_propagator = Semantic.MATCH_PROPAGATOR(logger) | |
| 64 | si | |
| 65 | ||
| 66 | pre_let(`let: Trees.Statements.LET) -> bool is | |
| 67 | // Reset is_defined on each LHS variable at the start of | |
| 68 | // every body-retry walk of the let. Without this reset, | |
| 69 | // an earlier walk's define() call leaves is_defined=true, | |
| 70 | // and check_is_defined in `load_captured_value` no longer | |
| 71 | // fires for a forward self-reference inside the RHS | |
| 72 | // lambda on subsequent walks (`let f = ... f ...` is the | |
| 73 | // canonical case). The error gets rolled back per iter | |
| 74 | // via mark_consumed_any rolling back diagnostics, but | |
| 75 | // the final iter's error survives so the user-visible | |
| 76 | // diagnostic is preserved. | |
| 77 | for v in `let.variables do | |
| 78 | for name in v.names do | |
| 79 | let symbol = _visitor.find(name) | |
| 80 | if symbol? /\ isa Semantic.Symbols.Variable(symbol) then | |
| 81 | let variable = symbol | |
| 82 | variable.is_defined = false | |
| 83 | fi | |
| 84 | od | |
| 85 | od | |
| 86 | ||
| 87 | return false | |
| 88 | si | |
| 89 | ||
| 90 | visit_let(`let: Trees.Statements.LET) is | |
| 91 | // A non-mut local must carry an initializer — an | |
| 92 | // immutable local variable without a value at its | |
| 93 | // declaration could only ever be set by a later | |
| 94 | // assignment, and those are rejected at the store site | |
| 95 | // below. Catching it here keeps the diagnostic on the | |
| 96 | // declaration that is missing the `= expr`. | |
| 97 | for v in `let.variables do | |
| 98 | if !v.initializer? /\ !v.is_mutable_marked then | |
| 99 | _logger.error(v.location, "local value must be initialized") | |
| 100 | fi | |
| 101 | od | |
| 102 | ||
| 103 | // Definite assignment: a `let` binding with no | |
| 104 | // initializer introduces a deferred-init local. Until it | |
| 105 | // is assigned, a read of it is a use-before-assignment. | |
| 106 | if !_build_flags.no_warn_definite_assignment then | |
| 107 | for v in `let.variables do | |
| 108 | if !v.initializer? then | |
| 109 | for name in v.names do | |
| 110 | let symbol = _visitor.find(name) | |
| 111 | ||
| 112 | if symbol? /\ isa Semantic.Symbols.Variable(symbol) then | |
| 113 | _flow.track_deferred(symbol) | |
| 114 | fi | |
| 115 | od | |
| 116 | fi | |
| 117 | od | |
| 118 | fi | |
| 119 | ||
| 120 | if `let.want_dispose then | |
| 121 | let idisposable = _innate_symbol_lookup.get_idisposable_type() | |
| 122 | ||
| 123 | for v in `let.variables do | |
| 124 | // What is disposed is the value the local started | |
| 125 | // with, so a local declared this way cannot be given | |
| 126 | // another. `let x mut = use E` says the same thing | |
| 127 | // and can be. | |
| 128 | if v.is_mutable_marked then | |
| 129 | _logger.error(v.location, "local declared with let use cannot be mut") | |
| 130 | fi | |
| 131 | ||
| 132 | for name in v.names do | |
| 133 | let symbol = _visitor.find(name) | |
| 134 | ||
| 135 | if symbol? /\ isa Semantic.Symbols.Variable(symbol) then | |
| 136 | let variable = symbol | |
| 137 | variable.is_disposed = true | |
| 138 | ||
| 139 | let type = variable.type | |
| 140 | ||
| 141 | if type? /\ !idisposable.is_assignable_from(type) then | |
| 142 | _logger.error(name.location, "not disposable") | |
| 143 | fi | |
| 144 | ||
| 145 | _visitor.current_statement_list.add_variable_to_dispose(variable) | |
| 146 | fi | |
| 147 | od | |
| 148 | od | |
| 149 | fi | |
| 150 | si | |
| 151 | ||
| 152 | pre_simple_left(left: Trees.Expressions.SIMPLE_LEFT_EXPRESSION) -> bool is | |
| 153 | if let left.value? then | |
| 154 | if !value.is_consumable then | |
| 155 | _logger.error(left.location, "cannot use this here") | |
| 156 | return false | |
| 157 | fi | |
| 158 | ||
| 159 | left.expression.compile_expressions_state.value = Need.STORE(value) | |
| 160 | fi | |
| 161 | ||
| 162 | return false | |
| 163 | si | |
| 164 | ||
| 165 | visit_simple_left(left: Trees.Expressions.SIMPLE_LEFT_EXPRESSION) is | |
| 166 | let value = left.value | |
| 167 | let expression_value = left.expression.value | |
| 168 | ||
| 169 | if !value? \/ !value.type? \/ !expression_value? \/ !expression_value.type? then | |
| 170 | return | |
| 171 | fi | |
| 172 | ||
| 173 | // Snapshot the presence-narrowed type before the queries | |
| 174 | // below cross the fact it was read through. | |
| 175 | let value_type = value.type | |
| 176 | ||
| 177 | if !expression_value.is_consumable then | |
| 178 | _logger.error(left.location, "cannot assign to this") | |
| 179 | return | |
| 180 | fi | |
| 181 | ||
| 182 | // Bypass narrowing for assignability — the LHS value's | |
| 183 | // type may be a narrowed view, but assignment must | |
| 184 | // typecheck against the variable's declared type, or | |
| 185 | // `if isa T(x) then x = wider_value` (idiomatic across | |
| 186 | // the codebase) would fail at the narrow. | |
| 187 | let target_variable = _visitor.try_get_narrowing_target(left.expression) | |
| 188 | ||
| 189 | // A member of the enclosing type is recorded as written | |
| 190 | // whether or not the narrowing target agrees - see | |
| 191 | // try_get_member_assigned. Locals are left to the | |
| 192 | // mark_assigned below, which the narrowing target covers. | |
| 193 | let member_target = _visitor.try_get_member_assigned(left.expression) | |
| 194 | ||
| 195 | if member_target? then | |
| 196 | _flow.mark_assigned(member_target) | |
| 197 | fi | |
| 198 | ||
| 199 | let lhs_type = | |
| 200 | if target_variable? then | |
| 201 | // A local assigned before the declaration that types | |
| 202 | // it - an assignment inside the local's own | |
| 203 | // initializer - has no declared type to check | |
| 204 | // against, and the read that reached it is already | |
| 205 | // reported. What the target compiled to is then all | |
| 206 | // there is to judge by. | |
| 207 | _flow.declared_type_of(target_variable) ?? expression_value.type! | |
| 208 | else | |
| 209 | expression_value.type! | |
| 210 | fi | |
| 211 | ||
| 212 | if !lhs_type.is_assignable_from(value_type) then | |
| 213 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(value_type, lhs_type) | |
| 214 | _logger.error(left.assign_location, "{value_type} is not assignable to {lhs_type}") | |
| 215 | return | |
| 216 | fi | |
| 217 | ||
| 218 | // Read the RHS static type and non-optional-ness off the | |
| 219 | // RHS expression value before the overwrite below replaces | |
| 220 | // `left.value` with the Store.SYMBOL IR whose type is the | |
| 221 | // variable's declared type — so a `BOX? mut` slot would mask | |
| 222 | // a non-null RHS like `BOX(1)`. | |
| 223 | let rhs_type = value_type | |
| 224 | let rhs_is_non_optional = _visitor.is_non_optional_value(value) | |
| 225 | ||
| 226 | left.compile_expressions_state.value = expression_value | |
| 227 | ||
| 228 | // Reassignment invalidates any narrow for the target — | |
| 229 | // the new value may not satisfy it, so subsequent reads | |
| 230 | // observe the declared type — and makes the target | |
| 231 | // definitely assigned from here on. | |
| 232 | if target_variable? then | |
| 233 | // Kill the invalidated facts and re-narrow to the RHS | |
| 234 | // static type when it is more specific than the | |
| 235 | // declared type, so a later read sees DOG after | |
| 236 | // `pet = DOG()`. The transfer also emits the editor | |
| 237 | // hint for whichever of kill / re-narrow applied. | |
| 238 | let narrowed = _flow.on_assignment(target_variable, left.assign_location, rhs_type) | |
| 239 | ||
| 240 | _flow.mark_assigned(target_variable) | |
| 241 | ||
| 242 | // A non-optional RHS leaves the target known to hold | |
| 243 | // a value — so `_field = arg; _field.method()` does | |
| 244 | // not warn. Keyed off the declared type, not the | |
| 245 | // possibly-narrowed view above: the presence fact lives | |
| 246 | // in its own lattice channel and must still be recorded | |
| 247 | // when a branch also type-narrows, so it composes at a | |
| 248 | // join with a sibling branch that carries only presence. | |
| 249 | let declared_target = _flow.declared_type_of(target_variable) | |
| 250 | let want_presence = | |
| 251 | rhs_is_non_optional /\ | |
| 252 | declared_target? /\ | |
| 253 | declared_target.is_optional | |
| 254 | ||
| 255 | if want_presence then | |
| 256 | _flow.mark_non_null(target_variable) | |
| 257 | ||
| 258 | // A type narrow's hint has already been emitted by | |
| 259 | // the transfer; the presence-only view is a hint of | |
| 260 | // its own only when no narrow applied. | |
| 261 | if !narrowed? then | |
| 262 | _flow.report_narrowing_site( | |
| 263 | left.assign_location, | |
| 264 | "narrowing-assign", | |
| 265 | "►", | |
| 266 | INLAY_TYPE.render(target_variable.type!.as_non_optional()) | |
| 267 | ) | |
| 268 | fi | |
| 269 | fi | |
| 270 | fi | |
| 271 | si | |
| 272 | ||
| 273 | visit_let_in(let_in: Trees.Expressions.LET_IN) is | |
| 274 | if let_in.want_dispose then | |
| 275 | _dispose_let_in_variables(let_in) | |
| 276 | fi | |
| 277 | ||
| 278 | if let let_in.expression.value? /\ value.check_is_consumable_allow_void(_logger, let_in.expression.location) then | |
| 279 | let_in.compile_expressions_state.value = value | |
| 280 | else | |
| 281 | let_in.compile_expressions_state.value = IR.Values.DUMMY(Semantic.Types.ERROR(), let_in.location) | |
| 282 | fi | |
| 283 | si | |
| 284 | ||
| 285 | // A `let use ... in` local is disposed when the statement or the | |
| 286 | // expression body holding the expression completes, the nearest | |
| 287 | // point where a disposal region can begin and end with nothing else | |
| 288 | // on the evaluation stack. | |
| 289 | _dispose_let_in_variables(let_in: Trees.Expressions.LET_IN) is | |
| 290 | let host = _visitor.disposal_host | |
| 291 | ||
| 292 | if !host? then | |
| 293 | _logger.error(let_in.location, "cannot dispose a local declared here") | |
| 294 | return | |
| 295 | fi | |
| 296 | ||
| 297 | let idisposable = _innate_symbol_lookup.get_idisposable_type() | |
| 298 | ||
| 299 | for v in let_in.variables do | |
| 300 | for name in v.names do | |
| 301 | if let variable: Semantic.Symbols.Variable = _visitor.find(name) then | |
| 302 | variable.is_disposed = true | |
| 303 | ||
| 304 | if variable.type? /\ !idisposable.is_assignable_from(variable.type) then | |
| 305 | _logger.error(name.location, "not disposable") | |
| 306 | fi | |
| 307 | ||
| 308 | host.add_let_in_disposal(variable) | |
| 309 | fi | |
| 310 | od | |
| 311 | od | |
| 312 | si | |
| 313 | ||
| 314 | pre_assignment(assignment: Trees.Statements.ASSIGNMENT) -> bool is | |
| 315 | // A member / index target reads its receiver before the | |
| 316 | // right-hand side runs, so a call there must not drop a field | |
| 317 | // narrow the receiver relies on. Shield that field across the | |
| 318 | // whole assignment; once the target is walked, drop it iff a | |
| 319 | // call actually occurred, so later statements still see the | |
| 320 | // post-call state. | |
| 321 | let receiver_field = _try_get_assignment_receiver_field(assignment.left) | |
| 322 | let shield_frame = _flow.push_shield(receiver_field) | |
| 323 | ||
| 324 | // Push the LHS type down to the RHS as a constraint. The | |
| 325 | // RHS expression node is responsible for either consuming | |
| 326 | // it (FUNCTION uses it for argument-type inference, | |
| 327 | // SEQUENCE / TUPLE forward to their elements) or ignoring | |
| 328 | // it (literals, identifiers — Expression's default | |
| 329 | // set_constraint is a no-op). The post-walk assignability | |
| 330 | // check still verifies type correctness for the cases | |
| 331 | // where the RHS doesn't act on the constraint. | |
| 332 | // | |
| 333 | // When the LHS is itself a not-yet-resolved placeholder | |
| 334 | // (`let l; ...; l = X` shape), the LHS type is not | |
| 335 | // useful as a constraint to push *down*. Instead, after | |
| 336 | // walking the RHS, push the RHS's type back to the | |
| 337 | // LHS variable's symbol via add_constraint — the | |
| 338 | // existing iterative-inference machinery will collapse | |
| 339 | // the LUB across all assignments on the next retry- | |
| 340 | // loop iteration. | |
| 341 | let lhs_type = _try_get_assignment_left_type(assignment.left) | |
| 342 | ||
| 343 | // `!is_inferred` (not !is_sentinel or is_settled): | |
| 344 | // we're asking "is the LHS anything other than a | |
| 345 | // bare placeholder?". ERROR is pre-filtered by | |
| 346 | // _try_get_assignment_left_type. A composite-with- | |
| 347 | // placeholder LHS like Function[placeholder, int] | |
| 348 | // *is* useful as an RHS constraint — the placeholder | |
| 349 | // arg slot is filled by the lambda's body inference, | |
| 350 | // and the return slot does its job. | |
| 351 | if lhs_type? /\ !lhs_type.is_inferred /\ !lhs_type.is_error then | |
| 352 | assignment.right.set_expected_type(lhs_type, "{{0}} is not assignable to {{1}}") | |
| 353 | fi | |
| 354 | ||
| 355 | assignment.right.walk(_visitor) | |
| 356 | ||
| 357 | let right_value = assignment.right.value | |
| 358 | ||
| 359 | if !right_value? then | |
| 360 | return true | |
| 361 | fi | |
| 362 | ||
| 363 | if !right_value.is_consumable then | |
| 364 | _logger.error(assignment.right.location, "cannot use this here") | |
| 365 | assignment.left.compile_expressions_state.value = IR.Values.DUMMY(Semantic.Types.ERROR(), assignment.right.location) | |
| 366 | else | |
| 367 | assignment.left.compile_expressions_state.value = right_value | |
| 368 | ||
| 369 | _visitor.check_non_optional(lhs_type, assignment.right, assignment.right.location) | |
| 370 | ||
| 371 | _pure_slots.check_store(assignment.right.location, lhs_type, right_value) | |
| 372 | fi | |
| 373 | ||
| 374 | // Back-feed the RHS type as a constraint onto a | |
| 375 | // placeholder-typed LHS variable (`let l; l = X` etc.). | |
| 376 | // The next retry-loop iteration of the enclosing | |
| 377 | // function-body walk picks up the LUB via | |
| 378 | // try_get_inferred_type at visit(SIMPLE_VARIABLE_LEFT). | |
| 379 | // | |
| 380 | // `!is_settled` (not is_inferred or contains_inferred): a | |
| 381 | // provisional composite LHS like `Func[List[int], | |
| 382 | // INFERRED_RETURN_TYPE]` — recorded on an early iter when | |
| 383 | // the lambda's body was walked before the return type | |
| 384 | // resolved — still needs refining once the placeholder | |
| 385 | // inside settles. Same goes for an ERROR-carrying | |
| 386 | // composite like `(ERROR, int)` left over from an iter | |
| 387 | // where a sub-expression poisoned to DUMMY(ERROR): | |
| 388 | // a later iter may produce a clean `(int, int)` value, | |
| 389 | // and without the match propagation the LUB never picks it up | |
| 390 | // and the per-position merge prefers the stale ERROR | |
| 391 | // slot. `is_settled` ("no placeholders, no errors") | |
| 392 | // captures both senses in one predicate. | |
| 393 | if | |
| 394 | lhs_type? /\ | |
| 395 | (!lhs_type.is_settled \/ _is_joining_local(assignment.left)) /\ | |
| 396 | right_value.type? /\ | |
| 397 | _try_propagate_assignment_lhs(assignment.left, right_value.type) | |
| 398 | then | |
| 399 | _logger.mark_consumed_any() | |
| 400 | fi | |
| 401 | ||
| 402 | assignment.left.assign_location = assignment.location | |
| 403 | ||
| 404 | assignment.left.walk(_visitor) | |
| 405 | ||
| 406 | // The target walk compiles the target's name, which the | |
| 407 | // narrowing flow sees as a read — but a store target is | |
| 408 | // written, not read, so any load it recorded is moot. | |
| 409 | ||
| 410 | if _flow.release_shield(shield_frame) /\ receiver_field? then | |
| 411 | _flow.forget(receiver_field) | |
| 412 | fi | |
| 413 | ||
| 414 | // A store to anything but a local can change what a | |
| 415 | // property getter returns, so property facts must not | |
| 416 | // survive it. Field facts do — a store to one field | |
| 417 | // cannot alter another, and the assignment transfer | |
| 418 | // above already handled the target itself. | |
| 419 | if _is_heap_store_target(assignment.left) then | |
| 420 | _flow.on_heap_store(assignment.location) | |
| 421 | fi | |
| 422 | ||
| 423 | assignment.compile_expressions_state.value = assignment.left.value | |
| 424 | ||
| 425 | return true | |
| 426 | si | |
| 427 | ||
| 428 | // True when an assignment target writes the heap: a member, | |
| 429 | // index, field or property target — anything except a plain | |
| 430 | // local variable or parameter. Destructuring stores to the | |
| 431 | // heap when any element does. | |
| 432 | ||
| 433 | _is_heap_store_target(left: Trees.Expressions.AssignmentLeftExpression?) -> bool is | |
| 434 | if !left? then | |
| 435 | return true | |
| 436 | fi | |
| 437 | ||
| 438 | if isa Trees.Expressions.DESTRUCTURING_LEFT_EXPRESSION(left) then | |
| 439 | for element in (cast Trees.Expressions.DESTRUCTURING_LEFT_EXPRESSION(left)).elements do | |
| 440 | if _is_heap_store_target(element) then | |
| 441 | return true | |
| 442 | fi | |
| 443 | od | |
| 444 | ||
| 445 | return false | |
| 446 | fi | |
| 447 | ||
| 448 | if !isa Trees.Expressions.SIMPLE_LEFT_EXPRESSION(left) then | |
| 449 | return true | |
| 450 | fi | |
| 451 | ||
| 452 | let expression = (cast Trees.Expressions.SIMPLE_LEFT_EXPRESSION(left)).expression | |
| 453 | ||
| 454 | if !isa Trees.Expressions.IDENTIFIER(expression) then | |
| 455 | return true | |
| 456 | fi | |
| 457 | ||
| 458 | let identifier = (cast Trees.Expressions.IDENTIFIER(expression)).identifier | |
| 459 | ||
| 460 | if identifier.is_qualified then | |
| 461 | return true | |
| 462 | fi | |
| 463 | ||
| 464 | let symbol = _visitor.try_find(identifier) | |
| 465 | ||
| 466 | return | |
| 467 | !symbol? \/ | |
| 468 | !(isa Semantic.Symbols.LOCAL_VARIABLE(symbol) \/ isa Semantic.Symbols.LOCAL_ARGUMENT(symbol)) | |
| 469 | si | |
| 470 | ||
| 471 | // The field variable a member / index assignment target reads | |
| 472 | // as its receiver — the shield subject in `pre_assignment`. | |
| 473 | // Null unless the target is `field.member` / `field[index]` | |
| 474 | // rooted at a bare field identifier. | |
| 475 | _try_get_assignment_receiver_field(left: Trees.Expressions.AssignmentLeftExpression) -> Semantic.Symbols.Symbol? is | |
| 476 | let simple = cast Trees.Expressions.SIMPLE_LEFT_EXPRESSION?(left) | |
| 477 | ||
| 478 | if !simple? then | |
| 479 | return null | |
| 480 | fi | |
| 481 | ||
| 482 | let target = simple.expression | |
| 483 | let receiver = | |
| 484 | if isa Trees.Expressions.MEMBER(target) then | |
| 485 | (cast Trees.Expressions.MEMBER(target)).left | |
| 486 | elif isa Trees.Expressions.INDEX(target) then | |
| 487 | (cast Trees.Expressions.INDEX(target)).left | |
| 488 | else | |
| 489 | null | |
| 490 | fi | |
| 491 | ||
| 492 | if !receiver? then | |
| 493 | return null | |
| 494 | fi | |
| 495 | ||
| 496 | return _visitor.try_get_narrowing_target(receiver) | |
| 497 | si | |
| 498 | ||
| 499 | // If the assignment's LHS is a simple identifier referring | |
| 500 | // to a Variable whose current type is an inference | |
| 501 | // placeholder, push the supplied RHS type onto the | |
| 502 | // variable's LUB accumulator. Returns true when a real | |
| 503 | // constraint actually landed (so the caller can signal | |
| 504 | // progress to the retry loop). | |
| 505 | // A `mut` local whose type is the join of its initializer and its | |
| 506 | // assignments: each assignment is a bound on that join even after | |
| 507 | // the local's type has settled. | |
| 508 | _is_joining_local(left: Trees.Expressions.AssignmentLeftExpression) -> bool is | |
| 509 | if | |
| 510 | let simple: Trees.Expressions.SIMPLE_LEFT_EXPRESSION = left, | |
| 511 | identifier: Trees.Expressions.IDENTIFIER = simple.expression | |
| 512 | then | |
| 513 | if let variable: Semantic.Symbols.Variable = _visitor.find(identifier.identifier) then | |
| 514 | return variable.joins_assignments | |
| 515 | fi | |
| 516 | fi | |
| 517 | ||
| 518 | return false | |
| 519 | si | |
| 520 | ||
| 521 | _try_propagate_assignment_lhs( | |
| 522 | left: Trees.Expressions.AssignmentLeftExpression, | |
| 523 | rhs_type: Semantic.Types.Type | |
| 524 | ) -> bool is | |
| 525 | let simple = cast Trees.Expressions.SIMPLE_LEFT_EXPRESSION?(left) | |
| 526 | ||
| 527 | if !simple? then | |
| 528 | return false | |
| 529 | fi | |
| 530 | ||
| 531 | let identifier = cast Trees.Expressions.IDENTIFIER?(simple.expression) | |
| 532 | ||
| 533 | if !identifier? then | |
| 534 | return false | |
| 535 | fi | |
| 536 | ||
| 537 | let symbol = _visitor.find(identifier.identifier) | |
| 538 | ||
| 539 | if !symbol? \/ !isa Semantic.Symbols.Variable(symbol) then | |
| 540 | return false | |
| 541 | fi | |
| 542 | ||
| 543 | let variable = symbol | |
| 544 | ||
| 545 | // A value typed over another function's type parameter - the | |
| 546 | // `T[]` a `collect[T]` call still resolving reports - is | |
| 547 | // no type the variable could take; a later walk supplies the | |
| 548 | // one it settles to. | |
| 549 | if rhs_type.has_function_generic_argument_foreign_to(_symbol_table.current_scope) then | |
| 550 | return false | |
| 551 | fi | |
| 552 | ||
| 553 | // A value still being inferred is no bound yet: the assignment | |
| 554 | // waits for it, and joins once a later walk has typed it, which | |
| 555 | // can be after the local has settled on its other assignments. | |
| 556 | if Semantic.AWAITED_LOCAL.is_awaited_local(rhs_type) then | |
| 557 | variable.joins_assignments = true | |
| 558 | Semantic.OBLIGATIONS.defer("assignment", rhs_type, left.location) | |
| 559 | return false | |
| 560 | fi | |
| 561 | ||
| 562 | return Semantic.INFERENCE_TRACE.add_lower_bound("bindings.assignment_lhs", variable, rhs_type) | |
| 563 | si | |
| 564 | ||
| 565 | // The type a SIMPLE_LEFT_EXPRESSION's target stores. Destructuring | |
| 566 | // lefts are skipped — extracting their effective type needs a | |
| 567 | // tuple-shape construction we don't have yet. | |
| 568 | _try_get_assignment_left_type(left: Trees.Expressions.AssignmentLeftExpression) -> Type? is | |
| 569 | let simple = cast Trees.Expressions.SIMPLE_LEFT_EXPRESSION?(left) | |
| 570 | ||
| 571 | if !simple? then | |
| 572 | return null | |
| 573 | fi | |
| 574 | ||
| 575 | let (resolved, type) = _resolve_assignment_target_type(simple.expression) | |
| 576 | ||
| 577 | if resolved then | |
| 578 | return type | |
| 579 | fi | |
| 580 | ||
| 581 | return _probe_assignment_left_type(simple) | |
| 582 | si | |
| 583 | ||
| 584 | // Read the target's type off its symbol without compiling it, | |
| 585 | // where the target names one: a local, field or readable | |
| 586 | // property, `self.member`, or the pointee of a `ref` local. | |
| 587 | // Resolving the name can report it missing, so the logger is | |
| 588 | // rolled back to keep the lookup invisible, as the probe is. | |
| 589 | _resolve_assignment_target_type(target: Trees.Expressions.Expression) -> (bool, Type?) is | |
| 590 | let mark = _logger.mark() | |
| 591 | ||
| 592 | _logger.speculate() | |
| 593 | ||
| 594 | try | |
| 595 | return _resolve_assignment_target_type_silently(target) | |
| 596 | finally | |
| 597 | _logger.roll_back() | |
| 598 | _logger.release(mark) | |
| 599 | yrt | |
| 600 | si | |
| 601 | ||
| 602 | _resolve_assignment_target_type_silently(target: Trees.Expressions.Expression) -> (bool, Type?) is | |
| 603 | let symbol = _find_assignment_target_symbol(target) | |
| 604 | ||
| 605 | if !symbol? then | |
| 606 | return (false, null) | |
| 607 | fi | |
| 608 | ||
| 609 | let type = _flow.declared_type_of(symbol) | |
| 610 | ||
| 611 | if !type? then | |
| 612 | return (false, null) | |
| 613 | fi | |
| 614 | ||
| 615 | if isa Trees.Expressions.UNWRAP(target) then | |
| 616 | if !type.is_ref then | |
| 617 | return (false, null) | |
| 618 | fi | |
| 619 | ||
| 620 | let element_type = type.get_element_type() | |
| 621 | ||
| 622 | if !element_type? then | |
| 623 | return (false, null) | |
| 624 | fi | |
| 625 | ||
| 626 | return (true, element_type) | |
| 627 | fi | |
| 628 | ||
| 629 | return (true, type) | |
| 630 | si | |
| 631 | ||
| 632 | _find_assignment_target_symbol(target: Trees.Expressions.Expression) -> Semantic.Symbols.Symbol? is | |
| 633 | if let unwrap: Trees.Expressions.UNWRAP = target then | |
| 634 | return _visitor.try_get_narrowing_target(unwrap.left) | |
| 635 | fi | |
| 636 | ||
| 637 | let narrowing_target = _visitor.try_get_narrowing_target(target) | |
| 638 | ||
| 639 | if narrowing_target? then | |
| 640 | return narrowing_target | |
| 641 | fi | |
| 642 | ||
| 643 | let member = cast Trees.Expressions.MEMBER?(target) | |
| 644 | ||
| 645 | if | |
| 646 | !member? \/ | |
| 647 | !isa Trees.Expressions.SELF(member.left) \/ | |
| 648 | member.is_coalesce \/ | |
| 649 | member.identifier.is_qualified | |
| 650 | then | |
| 651 | return null | |
| 652 | fi | |
| 653 | ||
| 654 | let context = _visitor.current_instance_context | |
| 655 | ||
| 656 | if !context? then | |
| 657 | return null | |
| 658 | fi | |
| 659 | ||
| 660 | let symbol = context.find_direct(member.identifier.name) | |
| 661 | ||
| 662 | if isa Semantic.Symbols.Variable(symbol) then | |
| 663 | return symbol | |
| 664 | fi | |
| 665 | ||
| 666 | if let property: Semantic.Symbols.Property = symbol /\ property.read_function? then | |
| 667 | return property | |
| 668 | fi | |
| 669 | ||
| 670 | return null | |
| 671 | si | |
| 672 | ||
| 673 | // For targets whose type depends on compiling them - an indexer, | |
| 674 | // a member reached through anything but `self` - speculatively | |
| 675 | // walk the target as a regular load, read its type, and roll | |
| 676 | // back the logger so the probe is invisible to later passes. | |
| 677 | // The probe walks the live expression rather than a copy: the | |
| 678 | // subsequent normal walk through pre(SIMPLE_LEFT_EXPRESSION) | |
| 679 | // overwrites the inner expression's value with the STORE form | |
| 680 | // anyway, so the probe's read-form value never escapes. | |
| 681 | _probe_assignment_left_type(simple: Trees.Expressions.SIMPLE_LEFT_EXPRESSION) -> Type? is | |
| 682 | let mark = _logger.mark() | |
| 683 | let type: Type? mut = _ | |
| 684 | ||
| 685 | // The probe's walk must be invisible to the symbol-use | |
| 686 | // maps as well as to the logger: it compiles the target | |
| 687 | // as a read, and the read-shaped use it would record | |
| 688 | // out-ranks the store-shaped one the real walk records | |
| 689 | // at the same location (first recorded wins a hover tie). | |
| 690 | _symbol_use_locations.begin_suppress() | |
| 691 | ||
| 692 | try | |
| 693 | let use retry_site = RETRY_SITE_STATS.enter("bindings.assignment_target_probe") | |
| 694 | _logger.speculate() | |
| 695 | ||
| 696 | simple.expression.walk(_visitor) | |
| 697 | ||
| 698 | let expression_value = simple.expression.value | |
| 699 | ||
| 700 | // is_error left unfiltered here: an ERROR-bearing | |
| 701 | // composite (e.g. `(ERROR, int)` set by an earlier | |
| 702 | // iter on a delayed-init variable whose first-seen | |
| 703 | // RHS poisoned to DUMMY(ERROR)) is exactly the case | |
| 704 | // the assignment match propagation below needs to recognise | |
| 705 | // — without the LHS type, the match propagation gate | |
| 706 | // gets null, no refinement constraint lands, and | |
| 707 | // the LUB stays frozen. Call sites that don't want | |
| 708 | // to pass ERROR onward (the set_constraint push to | |
| 709 | // RHS) gate it themselves. | |
| 710 | if expression_value? /\ expression_value.type? then | |
| 711 | let target_variable = _visitor.try_get_narrowing_target(simple.expression) | |
| 712 | ||
| 713 | type = | |
| 714 | if target_variable? then | |
| 715 | _flow.declared_type_of(target_variable) | |
| 716 | else | |
| 717 | expression_value.type | |
| 718 | fi | |
| 719 | fi | |
| 720 | ||
| 721 | _logger.roll_back() | |
| 722 | _logger.release(mark) | |
| 723 | catch e: Exception | |
| 724 | _logger.release(mark) | |
| 725 | return null | |
| 726 | finally | |
| 727 | _symbol_use_locations.end_suppress() | |
| 728 | yrt | |
| 729 | ||
| 730 | return type | |
| 731 | si | |
| 732 | ||
| 733 | visit_expression_statement(expression: Trees.Statements.EXPRESSION) is | |
| 734 | let inner_value = expression.expression.value | |
| 735 | if inner_value? then | |
| 736 | Value.check_is_consumable_allow_void(_logger, expression.expression.location, inner_value) | |
| 737 | ||
| 738 | // A call whose own type parameter appears only in its | |
| 739 | // return type is left carrying it for whatever consumes | |
| 740 | // the value to settle, and a statement consumes nothing, | |
| 741 | // so nothing will. Reported only on an otherwise clean | |
| 742 | // walk, where it is not the consequence of another error. | |
| 743 | if | |
| 744 | _logger.is_clean /\ | |
| 745 | inner_value.type? /\ | |
| 746 | inner_value.type.has_function_generic_argument_foreign_to(_symbol_table.current_function) | |
| 747 | then | |
| 748 | _logger.error(expression.expression.location, "cannot infer type here") | |
| 749 | fi | |
| 750 | fi | |
| 751 | ||
| 752 | if !expression.want_value then | |
| 753 | if expression.expression.must_be_consumed then | |
| 754 | _logger.error(expression.expression.location, "{expression.expression.description} result is not used") | |
| 755 | fi | |
| 756 | ||
| 757 | // A re-walk that no longer wants a value (e.g. the | |
| 758 | // enclosing literal's return type settled to void | |
| 759 | // between walks) must not leave behind the value an | |
| 760 | // earlier value-wanting walk recorded on the | |
| 761 | // statement — generate-il's val-block tail emission | |
| 762 | // reads the last statement's value even for a void | |
| 763 | // block and would emit that stale IR alongside the | |
| 764 | // fresh one. | |
| 765 | expression.compile_expressions_state.value = null | |
| 766 | ||
| 767 | return | |
| 768 | fi | |
| 769 | ||
| 770 | expression.compile_expressions_state.value = expression.expression.value | |
| 771 | si | |
| 772 | ||
| 773 | pre_return(r: Trees.Statements.RETURN) -> bool is | |
| 774 | // Stamp the innermost-val-block target onto the RETURN | |
| 775 | // AST node so visit_return and generate-il dispatch | |
| 776 | // consistently from a single source of truth, even if the | |
| 777 | // val-block stack is unwound by the time those fire. | |
| 778 | r.val_block_target = _visitor.innermost_val_block | |
| 779 | ||
| 780 | if let target = r.val_block_target then | |
| 781 | target.has_targeted_return = true | |
| 782 | fi | |
| 783 | ||
| 784 | if !r.expression? then | |
| 785 | return false | |
| 786 | fi | |
| 787 | ||
| 788 | if let block = r.val_block_target then | |
| 789 | // Return targets a val-block — push the block's | |
| 790 | // pushed-down expected_type (if any) onto the return | |
| 791 | // expression so it participates in inference / | |
| 792 | // overload resolution against the consumer's context, | |
| 793 | // mirroring the function-return path. | |
| 794 | if let expected = block.expected_type then | |
| 795 | if let error_message = block.expected_type_error_message then | |
| 796 | r.expression.set_expected_type(expected, error_message) | |
| 797 | fi | |
| 798 | fi | |
| 799 | ||
| 800 | return false | |
| 801 | fi | |
| 802 | ||
| 803 | let function = _symbol_table.current_function | |
| 804 | ||
| 805 | assert function? else "return outside a function" | |
| 806 | ||
| 807 | if | |
| 808 | !function.return_type? \/ | |
| 809 | function.return_type.is_sentinel \/ | |
| 810 | function.return_type.matches(_innate_symbol_lookup.get_void_type()) | |
| 811 | then | |
| 812 | return false | |
| 813 | fi | |
| 814 | ||
| 815 | r.expression!.set_expected_type(function.return_type!, "cannot return value of type {{0}} where {{1}} expected") | |
| 816 | ||
| 817 | return false | |
| 818 | si | |
| 819 | ||
| 820 | visit_return(r: Trees.Statements.RETURN) is | |
| 821 | // Control does not fall through a return — the rest of | |
| 822 | // the enclosing block is unreachable. | |
| 823 | _flow.set_unreachable() | |
| 824 | ||
| 825 | if let block = r.val_block_target then | |
| 826 | // Return targets the innermost enclosing val-block: | |
| 827 | // collect the expression's type into the block's LUB | |
| 828 | // pool and stop here. Function-level return-type | |
| 829 | // inference, async-SM rewiring, and the various | |
| 830 | // assignability / wrap rules below are scoped to | |
| 831 | // function-return; a val-targeted return is a | |
| 832 | // local control transfer, not a function exit. | |
| 833 | if let expression = r.expression then | |
| 834 | if let value = expression.value then | |
| 835 | if let value_type = value.type then | |
| 836 | if !value.check_is_consumable(_logger, expression.location) then | |
| 837 | return | |
| 838 | fi | |
| 839 | ||
| 840 | if !value_type.is_error then | |
| 841 | if let expected = block.expected_type then | |
| 842 | // Honour the pushed-down expected_type | |
| 843 | // the same way function-return does: | |
| 844 | // error if the expression's type isn't | |
| 845 | // assignable. | |
| 846 | if !expected.is_void /\ !expected.is_assignable_from(value_type) then | |
| 847 | let error_message mut = block.expected_type_error_message | |
| 848 | if !error_message? then | |
| 849 | error_message = "cannot return value of type {{0}} where {{1}} expected" | |
| 850 | fi | |
| 851 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(value_type, expected) | |
| 852 | _logger.error( | |
| 853 | expression.location, | |
| 854 | string.format(error_message, value_type, expected) | |
| 855 | ) | |
| 856 | return | |
| 857 | fi | |
| 858 | fi | |
| 859 | ||
| 860 | block.return_types.add(value_type) | |
| 861 | fi | |
| 862 | fi | |
| 863 | fi | |
| 864 | else | |
| 865 | // Bare `return;` inside a val-block. Diagnose | |
| 866 | // only when an outer expected_type makes the | |
| 867 | // value requirement unambiguous (typed `let` | |
| 868 | // initializer, function argument, value- | |
| 869 | // returning `=> body`). want_value alone reads | |
| 870 | // stale for inferred-return lambdas — the | |
| 871 | // lambda's return type isn't pinned until after | |
| 872 | // the body walks, so a bare return that's | |
| 873 | // consistent with a void inference would error | |
| 874 | // here prematurely. Generate-il stores the | |
| 875 | // default of the result type on the no- | |
| 876 | // diagnostic path so the IL still verifies. | |
| 877 | if let expected = block.expected_type then | |
| 878 | if !expected.is_void then | |
| 879 | _logger.error(r.location, "return without value from val block requiring a value") | |
| 880 | fi | |
| 881 | fi | |
| 882 | fi | |
| 883 | ||
| 884 | return | |
| 885 | fi | |
| 886 | ||
| 887 | let function = _symbol_table.current_function | |
| 888 | ||
| 889 | if !function? \/ !function.return_type? then | |
| 890 | // FIXME: null function happens for properties, null return type happens for anonymous functions | |
| 891 | return | |
| 892 | fi | |
| 893 | ||
| 894 | // State-machine async: `return X` provides X of type T | |
| 895 | // (the Task element type), generate-il stashes X into | |
| 896 | // `_result` and leaves to success_label. | |
| 897 | let async_sm = Semantic.Symbols.async_state_machine_for(function) | |
| 898 | if async_sm? /\ async_sm.frame? then | |
| 899 | _visit_return_state_machine_async(r, function, async_sm) | |
| 900 | return | |
| 901 | fi | |
| 902 | ||
| 903 | // A value whose type still holds placeholders has to fit the | |
| 904 | // declared return type, which bounds them as an argument slot | |
| 905 | // bounds a placeholder passed to it. | |
| 906 | if | |
| 907 | let value = r.expression?.value /\ | |
| 908 | value.type? /\ | |
| 909 | value.type.contains_inferred /\ | |
| 910 | !function.return_type!.is_sentinel | |
| 911 | then | |
| 912 | _match_propagator.propagate_match(function.return_type!, value.type!) | |
| 913 | fi | |
| 914 | ||
| 915 | if r.expression? then | |
| 916 | if let | |
| 917 | r.expression.value? /\ | |
| 918 | value.type? /\ | |
| 919 | value.check_is_consumable(_logger, r.expression.location) | |
| 920 | then | |
| 921 | let null_join = Semantic.ARM_NULL_JOIN() | |
| 922 | ||
| 923 | // A null says a value can be absent without saying | |
| 924 | // what it holds when present, so it settles an | |
| 925 | // inferred return at nothing: it records that the | |
| 926 | // return is optional, and leaves what it is optional | |
| 927 | // of to the other returns, or to the slot the | |
| 928 | // literal goes into. | |
| 929 | if function.return_type!.is_inferred /\ null_join.is_genuine_null(value.type!) then | |
| 930 | function.returned_genuine_null = true | |
| 931 | elif function.return_type!.is_inferred then | |
| 932 | // Filling in a previously-undeclared return | |
| 933 | // type from this return statement. is_inferred | |
| 934 | // (today only INFERRED_RETURN_TYPE) rather than | |
| 935 | // is_sentinel: a function whose return type was | |
| 936 | // already set to ERROR by an earlier failing | |
| 937 | // return shouldn't get silently overwritten | |
| 938 | // with a concrete type by a later (validly- | |
| 939 | // typed) return — the original error should | |
| 940 | // remain reported and the return-type contract | |
| 941 | // should stay ERROR until the user fixes the | |
| 942 | // source. | |
| 943 | // | |
| 944 | // Async closures: wrap a bare-T body return | |
| 945 | // to Task[T] before pinning, so the closure's | |
| 946 | // signature matches its SM emission shape. | |
| 947 | // Values already typed Task[?] pass through. | |
| 948 | let value_type: Semantic.Types.Type? mut = value.type! | |
| 949 | ||
| 950 | if | |
| 951 | function.wrap_inferred_return_as_task /\ | |
| 952 | value_type.is_settled /\ | |
| 953 | !_task_conversion.is_task_type(value_type) | |
| 954 | then | |
| 955 | let task_type = _innate_symbol_lookup.get_task_type(value_type) | |
| 956 | ||
| 957 | // Wrap as `Tasks.TASK.from_result(orig)`. | |
| 958 | // Guard on `is_settled` so we don't fire | |
| 959 | // during iterative-inference walks with a | |
| 960 | // partially-resolved body type — those | |
| 961 | // would over-wrap to `Task[Task[?]]`. | |
| 962 | if task_type? /\ _task_conversion.try_wrap_value_as_task_return(r, task_type, _visitor) then | |
| 963 | // The wrap replaced r.expression and | |
| 964 | // re-walked it — read the fresh value, | |
| 965 | // not the pre-wrap snapshot. | |
| 966 | value_type = r.expression!.value!.type | |
| 967 | fi | |
| 968 | fi | |
| 969 | ||
| 970 | // A block body settles its return here, where an | |
| 971 | // expression body settles in visit(Bodies.EXPRESSION). | |
| 972 | // Both have to settle a value type at the slot's | |
| 973 | // reference type, or the literal is typed at the | |
| 974 | // struct and the conversion is left to the | |
| 975 | // delegate's assignment, which cannot carry it. | |
| 976 | if | |
| 977 | let settled = | |
| 978 | Semantic.VALUE_RETURN_PIN.of( | |
| 979 | function.pin_inferred_return_at, value_type) | |
| 980 | then | |
| 981 | value_type = settled | |
| 982 | fi | |
| 983 | ||
| 984 | let pinned = Semantic.MAYBE_RETURN_PIN.of(function, value_type!, _innate_symbol_lookup) | |
| 985 | ||
| 986 | function.set_return_type(Semantic.NULL_RETURN_JOIN.of(pinned, function.returned_genuine_null, _innate_symbol_lookup)) | |
| 987 | elif | |
| 988 | function.return_type!.matches(_innate_symbol_lookup.get_void_type()) /\ | |
| 989 | !function.return_type!.is_type_variable /\ | |
| 990 | !function.return_type!.is_error | |
| 991 | then | |
| 992 | _logger | |
| 993 | .error( | |
| 994 | r.expression!.location, | |
| 995 | "cannot return value from function of void type" | |
| 996 | ) | |
| 997 | elif !function.return_type!.is_assignable_from(value.type!) then | |
| 998 | // Implicit T → TASK[T] widening at return position | |
| 999 | // (the C# `async` return-rewrap rule applied to ghūl). | |
| 1000 | // Synthesises Tasks.TASK.from_result(orig) around the | |
| 1001 | // original expression and re-resolves the wrapper. | |
| 1002 | // Restricted to return position — variable init / | |
| 1003 | // argument passing slot boundaries don't widen this way. | |
| 1004 | if _task_conversion.try_wrap_value_as_task_return(r, function.return_type!, _visitor) then | |
| 1005 | // wrap succeeded — type now matches | |
| 1006 | elif function.return_type_was_inferred /\ null_join.is_genuine_null(value.type!) then | |
| 1007 | // The return settled on an earlier value and | |
| 1008 | // this one says it can also be absent. | |
| 1009 | function.returned_genuine_null = true | |
| 1010 | ||
| 1011 | function.set_return_type( | |
| 1012 | Semantic.NULL_RETURN_JOIN.of(function.return_type!, function.returned_genuine_null, _innate_symbol_lookup)) | |
| 1013 | elif function.return_type_was_inferred then | |
| 1014 | let lub = Semantic.LEAST_UPPER_BOUND_MAP() | |
| 1015 | lub.add(function.return_type!) | |
| 1016 | lub.add(value.type!) | |
| 1017 | let widened = lub.get_result() | |
| 1018 | ||
| 1019 | if widened? then | |
| 1020 | function.set_return_type(widened) | |
| 1021 | else | |
| 1022 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(value.type, function.return_type) | |
| 1023 | _logger | |
| 1024 | .error( | |
| 1025 | r.expression!.location, | |
| 1026 | "cannot return value of type {value.type} where {function.return_type} expected" | |
| 1027 | ) | |
| 1028 | fi | |
| 1029 | else | |
| 1030 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(value.type, function.return_type) | |
| 1031 | _logger | |
| 1032 | .error( | |
| 1033 | r.expression!.location, | |
| 1034 | "cannot return value of type {value.type} where {function.return_type} expected" | |
| 1035 | ) | |
| 1036 | fi | |
| 1037 | fi | |
| 1038 | ||
| 1039 | _visitor.check_non_optional(function.return_type, r.expression, r.expression!.location) | |
| 1040 | ||
| 1041 | _pure_slots.check_store(r.expression!.location, function.return_type, r.expression!.value) | |
| 1042 | ||
| 1043 | return | |
| 1044 | fi | |
| 1045 | else | |
| 1046 | // Void async: bare `return;` sugar — synthesise | |
| 1047 | // `return Tasks.TASK.completed_task;` and re-walk. | |
| 1048 | // `is_void_async` is stamped on a body that awaits, so | |
| 1049 | // it covers the state-machine lowering; a machine-less | |
| 1050 | // Tasks.TASK return completes the same way. | |
| 1051 | if | |
| 1052 | function.is_void_async \/ | |
| 1053 | _task_conversion.is_void_task_return(function.return_type) | |
| 1054 | then | |
| 1055 | r.expression = Semantic.TASK_CONVERSION.build_completed_task_expression(r.location) | |
| 1056 | r.expression!.walk(_visitor) | |
| 1057 | ||
| 1058 | return | |
| 1059 | fi | |
| 1060 | ||
| 1061 | // In a generator a bare `return` ends the stream, so the | |
| 1062 | // declared `Pipe[T]` is not a value any return carries. | |
| 1063 | if Semantic.Symbols.state_machine_for(function)? then | |
| 1064 | return | |
| 1065 | fi | |
| 1066 | ||
| 1067 | // A machine-less return of some other result-less | |
| 1068 | // task-like completes through its builder too - IL | |
| 1069 | // generation emits the driving sequence for the bare | |
| 1070 | // return - so no value is missing here either. | |
| 1071 | if _task_conversion.completes_without_machine(function.return_type) then | |
| 1072 | return | |
| 1073 | fi | |
| 1074 | ||
| 1075 | if !function.return_type!.matches(_innate_symbol_lookup.get_void_type()) then | |
| 1076 | _logger | |
| 1077 | .warn( | |
| 1078 | r.location, | |
| 1079 | "return-without-value", | |
| 1080 | "return without value from non void function returns default value of type {function.return_type}" | |
| 1081 | ) | |
| 1082 | fi | |
| 1083 | fi | |
| 1084 | si | |
| 1085 | ||
| 1086 | // Type-check `return X` against the element type T (not | |
| 1087 | // Task[T]); bare `return;` is left for generate-il to map to | |
| 1088 | // `leave success_label`. | |
| 1089 | _visit_return_state_machine_async( | |
| 1090 | r: Trees.Statements.RETURN, | |
| 1091 | function: Semantic.Symbols.Function, | |
| 1092 | async_sm: Semantic.Symbols.ASYNC_STATE_MACHINE | |
| 1093 | ) is | |
| 1094 | let frame = async_sm.frame | |
| 1095 | ||
| 1096 | assert frame? else "async state machine has no frame at return" | |
| 1097 | ||
| 1098 | if r.expression? then | |
| 1099 | let expression_value = r.expression.value | |
| 1100 | ||
| 1101 | if | |
| 1102 | !expression_value? \/ | |
| 1103 | !expression_value.type? \/ | |
| 1104 | !expression_value.check_is_consumable(_logger, r.expression.location) | |
| 1105 | then | |
| 1106 | return | |
| 1107 | fi | |
| 1108 | ||
| 1109 | if frame.is_void_async then | |
| 1110 | if !_try_await_returned_value(r, expression_value.type!, null) then | |
| 1111 | _logger.error( | |
| 1112 | r.expression!.location, | |
| 1113 | "cannot return value from function of void type" | |
| 1114 | ) | |
| 1115 | fi | |
| 1116 | return | |
| 1117 | fi | |
| 1118 | ||
| 1119 | // Async-closure inference: lambdas with | |
| 1120 | // INFERRED_RETURN_TYPE + wrap_inferred_return_as_task | |
| 1121 | // pin from the body's value-returning statement here. | |
| 1122 | // The SM frame's result_type follows via | |
| 1123 | // ensure_result_field. | |
| 1124 | if | |
| 1125 | function.return_type? /\ | |
| 1126 | function.return_type.is_inferred /\ | |
| 1127 | function.wrap_inferred_return_as_task /\ | |
| 1128 | expression_value.type? /\ | |
| 1129 | expression_value.type.is_settled /\ | |
| 1130 | !_task_conversion.is_task_type(expression_value.type!) | |
| 1131 | then | |
| 1132 | let task_type = | |
| 1133 | _task_conversion.async_return_type_for(function, expression_value.type!) | |
| 1134 | if task_type? then | |
| 1135 | function.set_return_type(task_type) | |
| 1136 | fi | |
| 1137 | fi | |
| 1138 | ||
| 1139 | let element_type = frame.result_type | |
| 1140 | ||
| 1141 | if !element_type? then | |
| 1142 | return | |
| 1143 | fi | |
| 1144 | ||
| 1145 | if | |
| 1146 | !element_type.is_assignable_from(expression_value.type!) /\ | |
| 1147 | !_try_await_returned_value(r, expression_value.type!, element_type) | |
| 1148 | then | |
| 1149 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(expression_value.type, element_type) | |
| 1150 | _logger.error( | |
| 1151 | r.expression!.location, | |
| 1152 | "cannot return value of type {expression_value.type} where {element_type} expected" | |
| 1153 | ) | |
| 1154 | return | |
| 1155 | fi | |
| 1156 | ||
| 1157 | _visitor.check_non_optional(element_type, r.expression, r.expression!.location) | |
| 1158 | ||
| 1159 | _pure_slots.check_store(r.expression!.location, element_type, r.expression!.value) | |
| 1160 | else | |
| 1161 | // A literal whose return was left to its body to settle: | |
| 1162 | // a bare `return` is only a return at all where the body | |
| 1163 | // carries no result, so it settles the literal as | |
| 1164 | // void-async here rather than waiting for a tail that | |
| 1165 | // may never deliver one. | |
| 1166 | if let rt = function.return_type then | |
| 1167 | if rt.is_inferred /\ function.wrap_inferred_return_as_task then | |
| 1168 | if let void_task = _innate_symbol_lookup.get_void_task_type() then | |
| 1169 | function.set_return_type(void_task) | |
| 1170 | function.is_void_async = true | |
| 1171 | fi | |
| 1172 | fi | |
| 1173 | fi | |
| 1174 | ||
| 1175 | let settled_frame = async_sm.frame ?? frame | |
| 1176 | ||
| 1177 | // Bare `return;`. For value-async this is an | |
| 1178 | // error — the result slot is non-void. For | |
| 1179 | // void-async it's the usual no-op. | |
| 1180 | if !settled_frame.is_void_async then | |
| 1181 | _logger.warn( | |
| 1182 | r.location, | |
| 1183 | "return-without-value", | |
| 1184 | "return without value from value-async function returns default value of type {settled_frame.result_type}" | |
| 1185 | ) | |
| 1186 | fi | |
| 1187 | fi | |
| 1188 | si | |
| 1189 | ||
| 1190 | // A returned value that does not fit the result slot but awaits | |
| 1191 | // to something that does is returned as its awaited result: the | |
| 1192 | // caller already holds this function's own task, so the | |
| 1193 | // returned task can only ever complete it. A void slot takes a | |
| 1194 | // value that awaits to nothing. Rewrites `r` to return the | |
| 1195 | // await and answers false, leaving `r` alone, when the value is | |
| 1196 | // not awaitable or its result does not fit either. | |
| 1197 | _try_await_returned_value( | |
| 1198 | r: Trees.Statements.RETURN, | |
| 1199 | value_type: Semantic.Types.Type, | |
| 1200 | element_type: Semantic.Types.Type? | |
| 1201 | ) -> bool is | |
| 1202 | let awaitable = _awaitable_resolver.try_resolve(value_type) | |
| 1203 | ||
| 1204 | if !awaitable? then | |
| 1205 | return false | |
| 1206 | fi | |
| 1207 | ||
| 1208 | let result_type = awaitable.result_type | |
| 1209 | ||
| 1210 | if element_type? then | |
| 1211 | if !element_type.is_assignable_from(result_type) then | |
| 1212 | return false | |
| 1213 | fi | |
| 1214 | elif !result_type.is_void then | |
| 1215 | return false | |
| 1216 | fi | |
| 1217 | ||
| 1218 | let awaited = Trees.Expressions.AWAIT(r.expression!.location, r.expression!) | |
| 1219 | ||
| 1220 | r.expression = awaited | |
| 1221 | ||
| 1222 | awaited.accept(_visitor) | |
| 1223 | ||
| 1224 | return true | |
| 1225 | si | |
| 1226 | ||
| 1227 | si | |
| 1228 | si |