Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use System.Exception | |
| 3 | ||
| 4 | use IO.Std | |
| 5 | ||
| 6 | use Logging | |
| 7 | use Source | |
| 8 | ||
| 9 | use IR.Values | |
| 10 | use IR.VALUE_CONVERTER | |
| 11 | use IR.VALUE_BOXER | |
| 12 | ||
| 13 | use Semantic.LEAST_UPPER_BOUND_MAP | |
| 14 | use Semantic.Types.Type | |
| 15 | ||
| 16 | use Syntax.Trees.Definitions.PRAGMA | |
| 17 | ||
| 18 | use Ghul.Pipes | |
| 19 | ||
| 20 | ||
| 21 | // Variable-left walks: simple, destructuring and literal leaves, variable visits and | |
| 22 | // symbol type assignment for destructuring. | |
| 23 | partial COMPILE_EXPRESSIONS is | |
| 24 | pre(left: Trees.Variables.SIMPLE_VARIABLE_LEFT) -> bool => false | |
| 25 | visit(left: Trees.Variables.SIMPLE_VARIABLE_LEFT) is | |
| 26 | let symbol = find(left.name) | |
| 27 | ||
| 28 | if symbol? /\ isa Semantic.Types.SettableTyped(symbol) then | |
| 29 | let typed_symbol = cast Semantic.Types.SettableTyped(symbol) | |
| 30 | let left_state = _variable_left_state.get_or_add(left) | |
| 31 | ||
| 32 | symbol.define() | |
| 33 | ||
| 34 | let right_location = | |
| 35 | if left_state.right_location? then | |
| 36 | left_state.right_location | |
| 37 | else | |
| 38 | left.location | |
| 39 | fi | |
| 40 | ||
| 41 | // Iterative-inference re-narrowing: an iter-N type | |
| 42 | // that's a sentinel or contains a placeholder may | |
| 43 | // be overwritten on iter N+1 with the now-narrower | |
| 44 | // value (e.g. Function[placeholder, int] → | |
| 45 | // Function[int, int] once the lambda arg resolves). | |
| 46 | // | |
| 47 | // is_settled alone isn't enough: an overload candidate | |
| 48 | // that loses to a sibling can still leave its own | |
| 49 | // still-unbound method type-parameter committed as | |
| 50 | // the right-hand value's type on an earlier iter - | |
| 51 | // e.g. C from a losing >>[A,B,C] candidate that | |
| 52 | // failed to bind C. That's a real, non-sentinel type, | |
| 53 | // so is_settled reports it as final - but the | |
| 54 | // parameter isn't one of this function's own generic | |
| 55 | // parameters, so it can never mean anything here and | |
| 56 | // must be re-derived rather than kept. | |
| 57 | let current_type = typed_symbol.type | |
| 58 | ||
| 59 | let needs_set mut = | |
| 60 | !current_type? \/ | |
| 61 | !current_type.is_settled \/ | |
| 62 | current_type.has_function_generic_argument_foreign_to(current_function) | |
| 63 | ||
| 64 | // An inferred local is typed by its initializer on every | |
| 65 | // walk: a settled type left on the symbol by an earlier | |
| 66 | // walk of this body may be narrower than what the | |
| 67 | // initializer now yields, and a placeholder the | |
| 68 | // initializer now carries has to be installed for its | |
| 69 | // uses to back-feed it. | |
| 70 | if !needs_set /\ !left_state.explicit_type? /\ left_state.right_value? then | |
| 71 | needs_set = true | |
| 72 | fi | |
| 73 | ||
| 74 | // A local with neither is typed by the join of what is | |
| 75 | // assigned to it. Once an assignment has waited on a value | |
| 76 | // still being inferred, a later walk can widen that join. | |
| 77 | if | |
| 78 | !needs_set /\ !left_state.explicit_type? /\ !left_state.right_value? /\ | |
| 79 | isa Semantic.Symbols.Variable(symbol) /\ symbol.joins_assignments | |
| 80 | then | |
| 81 | needs_set = true | |
| 82 | fi | |
| 83 | ||
| 84 | if | |
| 85 | !needs_set /\ | |
| 86 | REFUTABLE_LEAF_RETYPE.needs_retype( | |
| 87 | left.is_refutable, | |
| 88 | left_state.explicit_type?, | |
| 89 | left_state.right_value? | |
| 90 | ) | |
| 91 | then | |
| 92 | needs_set = true | |
| 93 | fi | |
| 94 | ||
| 95 | if needs_set then | |
| 96 | if left_state.explicit_type? then | |
| 97 | typed_symbol.set_type(left_state.explicit_type) | |
| 98 | elif left_state.right_value? then | |
| 99 | let right_value = left_state.right_value | |
| 100 | ||
| 101 | if left_state.awaits_later_use /\ isa Semantic.Symbols.Variable(symbol) then | |
| 102 | let variable = symbol | |
| 103 | let placeholder = Semantic.Types.INFERRED_VARIABLE_TYPE(variable) | |
| 104 | ||
| 105 | typed_symbol.set_type(placeholder) | |
| 106 | ||
| 107 | let rule = | |
| 108 | if left_state.awaits_empty_literal then | |
| 109 | Semantic.OBLIGATIONS.EMPTY_LITERAL | |
| 110 | else | |
| 111 | "later_use" | |
| 112 | fi | |
| 113 | ||
| 114 | Semantic.OBLIGATIONS.defer(rule, placeholder, left.location) | |
| 115 | elif !Value.check_is_consumable(_logger, right_location, right_value) then | |
| 116 | typed_symbol.set_type(Semantic.Types.ERROR()) | |
| 117 | elif | |
| 118 | _logger.is_clean /\ | |
| 119 | right_value.type!.has_function_generic_argument_foreign_to(current_function) /\ | |
| 120 | !left.is_refutable /\ | |
| 121 | isa Semantic.Symbols.Variable(symbol) /\ | |
| 122 | !(cast Semantic.Symbols.Variable(symbol)).is_mutable_marked | |
| 123 | then | |
| 124 | // The initializer resolved to a call whose own | |
| 125 | // type parameter nothing bound. An immutable | |
| 126 | // local waits on its later uses for the type, | |
| 127 | // and the call is typed from that on later walks. | |
| 128 | let variable = cast Semantic.Symbols.Variable(symbol) | |
| 129 | let placeholder = Semantic.Types.INFERRED_VARIABLE_TYPE(variable) | |
| 130 | ||
| 131 | left_state.typed_by_later_use = true | |
| 132 | ||
| 133 | typed_symbol.set_type(placeholder) | |
| 134 | ||
| 135 | Semantic.OBLIGATIONS.defer("later_use", placeholder, left.location) | |
| 136 | elif | |
| 137 | _logger.is_clean /\ | |
| 138 | right_value.type!.has_function_generic_argument_foreign_to(current_function) | |
| 139 | then | |
| 140 | // The initializer resolved to a call whose | |
| 141 | // own type parameter nothing bound: neither | |
| 142 | // its arguments nor any type the context | |
| 143 | // supplied. There is no type to settle to, | |
| 144 | // and keeping the parameter would write an | |
| 145 | // unbound !!N as this variable's type. | |
| 146 | // | |
| 147 | // Reported only on an otherwise clean walk: | |
| 148 | // an unbound parameter reaching here after | |
| 149 | // some other diagnostic is that diagnostic's | |
| 150 | // consequence, not a separate fault. | |
| 151 | _logger.error(right_location, "cannot infer type here") | |
| 152 | ||
| 153 | typed_symbol.set_type(Semantic.Types.ERROR()) | |
| 154 | elif | |
| 155 | right_value.type!.is_null /\ | |
| 156 | !right_value.type!.is_error /\ | |
| 157 | isa Semantic.Symbols.Variable(symbol) | |
| 158 | then | |
| 159 | // A `null` initializer says only that the | |
| 160 | // local can be absent; what is later | |
| 161 | // assigned to it gives the type. | |
| 162 | let variable = symbol | |
| 163 | ||
| 164 | if Semantic.INFERENCE_TRACE.add_lower_bound("variables.null_initializer", variable, right_value.type!) then | |
| 165 | _logger.mark_consumed_any() | |
| 166 | fi | |
| 167 | ||
| 168 | let inferred = variable.try_get_inferred_type() | |
| 169 | ||
| 170 | if inferred? /\ !inferred.is_sentinel then | |
| 171 | typed_symbol.set_type(inferred) | |
| 172 | else | |
| 173 | typed_symbol.set_type(Semantic.Types.INFERRED_VARIABLE_TYPE(variable)) | |
| 174 | fi | |
| 175 | elif let variable: Semantic.Symbols.Variable = symbol /\ variable.is_mutable_marked then | |
| 176 | // A `mut` local holds what is assigned to it | |
| 177 | // later as well as its initializer: its type is | |
| 178 | // their join, floored so an unrelated assignment | |
| 179 | // is still reported where it is written. | |
| 180 | variable.joins_assignments = true | |
| 181 | ||
| 182 | if Semantic.INFERENCE_TRACE.add_lower_bound("variables.mut_initializer", variable, right_value.type!) then | |
| 183 | _logger.mark_consumed_any() | |
| 184 | fi | |
| 185 | ||
| 186 | typed_symbol.set_type( | |
| 187 | Semantic.MUT_LOCAL_JOIN().type_for( | |
| 188 | right_value.type!, | |
| 189 | variable.try_get_inferred_type(), | |
| 190 | variable.lower_bounds | |
| 191 | ) | |
| 192 | ) | |
| 193 | elif Semantic.Types.NULL_ELEMENT.within(right_value.type!) then | |
| 194 | // A tuple with a `null` element and nothing to say | |
| 195 | // what that element holds has no type a variable | |
| 196 | // can take. | |
| 197 | _logger.error(right_location, "cannot infer type here") | |
| 198 | ||
| 199 | typed_symbol.set_type(Semantic.Types.ERROR()) | |
| 200 | else | |
| 201 | typed_symbol.set_type(right_value.type!) | |
| 202 | fi | |
| 203 | else | |
| 204 | // No explicit type and no initializer | |
| 205 | // (`let l;`). Try the LUB accumulated from | |
| 206 | // later assignments; otherwise stand-in with | |
| 207 | // an INFERRED_VARIABLE_TYPE placeholder so | |
| 208 | // subsequent assignments can attach | |
| 209 | // constraints and the body retry loop | |
| 210 | // resolves on iteration N+1. | |
| 211 | // | |
| 212 | // !is_sentinel deliberate (not is_settled): | |
| 213 | // if try_get_inferred_type returns a | |
| 214 | // composite-with-placeholder LUB, we commit | |
| 215 | // it on iter N. The needs_set path above | |
| 216 | // uses is_settled, so iter N+1 picks the | |
| 217 | // slot up again and may refresh with a | |
| 218 | // more-resolved value. !is_sentinel accepts | |
| 219 | // a one-iter delayed convergence in exchange | |
| 220 | // for not re-deriving the LUB every iter | |
| 221 | // while it's stable. | |
| 222 | if isa Semantic.Symbols.Variable(symbol) then | |
| 223 | let variable = symbol | |
| 224 | let inferred = variable.try_get_inferred_type() | |
| 225 | ||
| 226 | if inferred? /\ !inferred.is_sentinel then | |
| 227 | typed_symbol.set_type(inferred) | |
| 228 | else | |
| 229 | typed_symbol.set_type(Semantic.Types.INFERRED_VARIABLE_TYPE(variable)) | |
| 230 | fi | |
| 231 | fi | |
| 232 | fi | |
| 233 | elif | |
| 234 | left_state.explicit_type? /\ | |
| 235 | left_state.right_value? /\ | |
| 236 | Value.check_is_consumable(_logger, right_location, left_state.right_value) | |
| 237 | then | |
| 238 | let explicit_type = left_state.explicit_type! | |
| 239 | let right_value = left_state.right_value! | |
| 240 | ||
| 241 | // A value still holding placeholders - a local whose | |
| 242 | // function literal left a parameter to later use - | |
| 243 | // learns their types from the declared type, as an | |
| 244 | // argument learns them from its formal. | |
| 245 | if right_value.type!.contains_inferred then | |
| 246 | _overload_resolver.match_propagator.propagate_match(explicit_type, right_value.type) | |
| 247 | fi | |
| 248 | ||
| 249 | if | |
| 250 | !left.is_refutable /\ | |
| 251 | !explicit_type.is_assignable_from(right_value.type!) | |
| 252 | then | |
| 253 | // Refutable bindings (`if let p: T = e`) skip | |
| 254 | // this check: the type ascription is a | |
| 255 | // runtime narrowing test, the scrutinee is | |
| 256 | // typically wider than `T`, and the cast may | |
| 257 | // fail — that's the whole point of the | |
| 258 | // construct. | |
| 259 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(right_value.type, explicit_type) | |
| 260 | _logger.error( | |
| 261 | left_state.variable_location!, | |
| 262 | "{right_value.type} is not assignable to {explicit_type}") | |
| 263 | fi | |
| 264 | fi | |
| 265 | ||
| 266 | // A top-level variable is a field, and a field cannot hold a | |
| 267 | // ref. The type is left as it is, so its uses do not report | |
| 268 | // again. | |
| 269 | if | |
| 270 | isa Semantic.Symbols.GLOBAL_VARIABLE(symbol) /\ | |
| 271 | isa Semantic.Types.REFERENCE(typed_symbol.type) | |
| 272 | then | |
| 273 | _logger.error(left.name.location, "a top-level variable cannot hold a ref") | |
| 274 | fi | |
| 275 | ||
| 276 | if left_state.right_value? then | |
| 277 | left_state.value = symbol.store(left.location, null, left_state.right_value, _symbol_loader, true) | |
| 278 | ||
| 279 | // A non-optional initializer leaves the local | |
| 280 | // known to hold a value — even where its declared | |
| 281 | // type is `T?` — so a following dereference does | |
| 282 | // not warn. Skipped when the local's declared | |
| 283 | // type is already non-optional (the presence bit | |
| 284 | // is redundant and the hint would be noise). | |
| 285 | if | |
| 286 | isa Semantic.Symbols.Variable(symbol) /\ | |
| 287 | is_non_optional_value(left_state.right_value) | |
| 288 | then | |
| 289 | let variable = cast Semantic.Symbols.Variable(symbol) | |
| 290 | let variable_type = variable.type | |
| 291 | ||
| 292 | if variable_type? /\ variable_type.is_optional then | |
| 293 | _flow.mark_non_null(variable) | |
| 294 | _flow.report_narrowing_site( | |
| 295 | left.location, | |
| 296 | "narrowing-assign", | |
| 297 | "►", | |
| 298 | INLAY_TYPE.render(variable_type.as_non_optional()) | |
| 299 | ) | |
| 300 | fi | |
| 301 | fi | |
| 302 | fi | |
| 303 | else | |
| 304 | _logger.error(left.name.location, "couldn't find typed symbol for variable {left.name}") | |
| 305 | fi | |
| 306 | si | |
| 307 | ||
| 308 | pre(left: Trees.Variables.DESTRUCTURING_VARIABLE_LEFT) -> bool is | |
| 309 | // A destructured formal argument has no initializer to walk | |
| 310 | // a value from - its leaves' types are already assigned by | |
| 311 | // resolve-explicit-types from the parameter's aggregate | |
| 312 | // type, and generate-il sources the unpack directly from | |
| 313 | // the synthesised parameter symbol. Nothing here applies. | |
| 314 | if left.is_argument_left then | |
| 315 | return true | |
| 316 | fi | |
| 317 | ||
| 318 | // Per-element type ascription: each element of a destructure | |
| 319 | // pattern can carry its own `: T` (e.g. `(c: Cat, d: Dog)`, | |
| 320 | // or recursively `((x: int, y: int): Point, c: Color)`). | |
| 321 | // Walk the type_expression and pin the element's | |
| 322 | // `explicit_type` so the bound symbol gets the declared | |
| 323 | // type rather than the raw source-member type. The runtime | |
| 324 | // narrowing test for the refutable path is emitted later | |
| 325 | // by `gen_destructuring_initialize` in generate_il. | |
| 326 | for e in left.elements do | |
| 327 | if let e.type_expression? then | |
| 328 | type_expression.walk(self) | |
| 329 | ||
| 330 | if let type_expression.type? then | |
| 331 | _variable_left_state.get_or_add(e).explicit_type = type | |
| 332 | fi | |
| 333 | fi | |
| 334 | od | |
| 335 | ||
| 336 | let left_state = _variable_left_state.get_or_add(left) | |
| 337 | ||
| 338 | let from mut = left_state.right_value | |
| 339 | ||
| 340 | if !from? then | |
| 341 | _logger.error(left.location, "cannot destructure without initializer") | |
| 342 | return true | |
| 343 | fi | |
| 344 | ||
| 345 | let from_type mut = | |
| 346 | if left_state.explicit_type? then | |
| 347 | left_state.explicit_type | |
| 348 | else | |
| 349 | from.type | |
| 350 | fi | |
| 351 | ||
| 352 | if !from_type? then | |
| 353 | _logger.error(left.location, "oops: null type") | |
| 354 | ||
| 355 | from_type = Semantic.Types.ERROR() | |
| 356 | fi | |
| 357 | ||
| 358 | // Refutable destructure on an optional source: the `if let` | |
| 359 | // presence test has already excluded null at this point, so | |
| 360 | // resolve members against the unwrapped type and load them | |
| 361 | // from the unwrapped value. A value-type `T?` unwraps via | |
| 362 | // its synthesised `value` member; a reference `T?` keeps | |
| 363 | // the same backing value and just drops the optional flag. | |
| 364 | if left.is_refutable /\ from_type.is_optional then | |
| 365 | if from_type.is_value_type then | |
| 366 | let value_member = from_type.find_member("value") | |
| 367 | ||
| 368 | if value_member? then | |
| 369 | from = value_member.load(LOCATION.internal, from, _symbol_loader) | |
| 370 | from_type = from.type! | |
| 371 | fi | |
| 372 | else | |
| 373 | from_type = from_type.as_non_optional() | |
| 374 | from = IR.Values.TYPE_WRAPPER(from_type, from) | |
| 375 | fi | |
| 376 | fi | |
| 377 | ||
| 378 | // Destructure on a still-unresolved placeholder: emit a | |
| 379 | // DESTRUCTURE_CONSTRAINT(member_count) on the | |
| 380 | // placeholder's origin so the body-retry loop can filter | |
| 381 | // candidate types against it, then defer rather than | |
| 382 | // hard-erroring "cannot destructure". Without this the | |
| 383 | // body-retry never gets a useful signal — the destructure | |
| 384 | // type is fixed and the lambda's RHS placeholder is | |
| 385 | // forced to resolve via some other use. | |
| 386 | if isa Semantic.Types.INFERRED_VARIABLE_TYPE(from_type) then | |
| 387 | let placeholder = from_type | |
| 388 | let constraint = Semantic.DESTRUCTURE_CONSTRAINT(left.elements.count) | |
| 389 | ||
| 390 | _logger.mark_consumed_any_if(Semantic.INFERENCE_TRACE.add_constraint("variables.destructure", placeholder.origin, constraint)) | |
| 391 | ||
| 392 | _defer_destructure_leaves(left, from_type) | |
| 393 | ||
| 394 | return true | |
| 395 | fi | |
| 396 | ||
| 397 | // Any other placeholder - a join still waiting on an operand - | |
| 398 | // has no origin to constrain and is not an error either: an | |
| 399 | // obligation of this walk, for a later one to settle. | |
| 400 | if from_type.is_inferred then | |
| 401 | Semantic.OBLIGATIONS.defer("destructure", from_type, left.location) | |
| 402 | ||
| 403 | _defer_destructure_leaves(left, from_type) | |
| 404 | ||
| 405 | return true | |
| 406 | fi | |
| 407 | ||
| 408 | let elements = left.elements | |
| 409 | ||
| 410 | // A group is either all named or all positional: the | |
| 411 | // parser reports one that mixes the two. It yields the | |
| 412 | // group anyway, so that the rest of the statement is | |
| 413 | // still walked, which is the one case where an element | |
| 414 | // here carries no name while its neighbour does - and | |
| 415 | // the mixing is already reported, so there is nothing | |
| 416 | // left to resolve. | |
| 417 | let is_named_group = elements.count > 0 /\ elements[0].source_field_name? | |
| 418 | ||
| 419 | let field_names: Collections.List[string?]? mut = null | |
| 420 | if is_named_group then | |
| 421 | if elements |> any(element => !element.source_field_name?) then | |
| 422 | return true | |
| 423 | fi | |
| 424 | ||
| 425 | let names = Collections.LIST[string?]() | |
| 426 | for element in elements do | |
| 427 | names.add(element.source_field_name!.name) | |
| 428 | od | |
| 429 | field_names = names | |
| 430 | fi | |
| 431 | ||
| 432 | let strategy = resolve_destructure_strategy(left.location, from_type, elements.count, field_names) | |
| 433 | ||
| 434 | let block = IR.Values.BLOCK(_innate_symbol_lookup.get_void_type()) | |
| 435 | ||
| 436 | if strategy.is_deconstruct then | |
| 437 | let deconstruct = strategy.deconstruct_function! | |
| 438 | ||
| 439 | for i in 0..elements.count do | |
| 440 | let element = elements[i] | |
| 441 | let element_type = deconstruct.arguments[i].get_element_type()! | |
| 442 | ||
| 443 | let element_state = _variable_left_state.get_or_add(element) | |
| 444 | ||
| 445 | element_state.right_value = IR.Values.DUMMY(element_type, element.location) | |
| 446 | ||
| 447 | element_state.variable_location = left_state.variable_location | |
| 448 | element_state.right_location = left_state.right_location | |
| 449 | ||
| 450 | element.walk(self) | |
| 451 | ||
| 452 | if element_state.value? then | |
| 453 | block.add(element_state.value) | |
| 454 | fi | |
| 455 | od | |
| 456 | else | |
| 457 | let members = strategy.members | |
| 458 | let get_from = from.get_temp_copier(block, "destructure") | |
| 459 | ||
| 460 | for i in 0..elements.count do | |
| 461 | let element = elements[i] | |
| 462 | let member = members[i] | |
| 463 | ||
| 464 | if member? then | |
| 465 | let element_state = _variable_left_state.get_or_add(element) | |
| 466 | ||
| 467 | element_state.right_value = member.load(LOCATION.internal, get_from(), _symbol_loader) | |
| 468 | ||
| 469 | element_state.variable_location = left_state.variable_location | |
| 470 | element_state.right_location = left_state.right_location | |
| 471 | ||
| 472 | element.walk(self) | |
| 473 | ||
| 474 | if element_state.value? then | |
| 475 | block.add(element_state.value) | |
| 476 | fi | |
| 477 | fi | |
| 478 | od | |
| 479 | fi | |
| 480 | ||
| 481 | block.close() | |
| 482 | ||
| 483 | left_state.value = block | |
| 484 | ||
| 485 | return true | |
| 486 | si | |
| 487 | ||
| 488 | // While the source of a destructure is still being inferred, each | |
| 489 | // name it declares stands as a placeholder of its own, so its uses | |
| 490 | // bound it rather than finding it undefined. It waits on the source, | |
| 491 | // which is what a later walk types it from, and what is reported if | |
| 492 | // nothing does. | |
| 493 | _defer_destructure_leaves(left: Trees.Variables.VariableLeft, source: Type) is | |
| 494 | let elements = left.elements | |
| 495 | ||
| 496 | if !elements? then | |
| 497 | return | |
| 498 | fi | |
| 499 | ||
| 500 | for element in elements do | |
| 501 | if element.is_simple_name then | |
| 502 | if let variable: Semantic.Symbols.Variable = find(element.name!) then | |
| 503 | variable.define() | |
| 504 | ||
| 505 | if let explicit_type = _variable_left_state.get_or_add(element).explicit_type then | |
| 506 | variable.set_type(explicit_type) | |
| 507 | elif !variable.type? \/ !variable.type.is_settled then | |
| 508 | let placeholder = Semantic.Types.INFERRED_VARIABLE_TYPE(variable) | |
| 509 | ||
| 510 | variable.set_type(placeholder) | |
| 511 | ||
| 512 | Semantic.OBLIGATIONS.defer("destructure", source, element.location) | |
| 513 | fi | |
| 514 | fi | |
| 515 | else | |
| 516 | _defer_destructure_leaves(element, source) | |
| 517 | fi | |
| 518 | od | |
| 519 | si | |
| 520 | ||
| 521 | visit(destructure_left: Trees.Variables.DESTRUCTURING_VARIABLE_LEFT) is | |
| 522 | si | |
| 523 | ||
| 524 | // A literal leaf inside a destructure pattern — a runtime | |
| 525 | // equality test, not a binding. The expression carries its | |
| 526 | // own type (literal kinds map to fixed types; an enum-member | |
| 527 | // name expression types to its enum). The expression is | |
| 528 | // walked via default descent; the source position's type is | |
| 529 | // pushed down as a constraint so a `null` leaf picks up the | |
| 530 | // source position's nullable type (other literal kinds | |
| 531 | // inherit the constraint as a no-op, since their type is | |
| 532 | // fixed by their token kind). Literal-vs-source mismatches | |
| 533 | // are diagnosed eagerly by `visit(LITERAL_VARIABLE_LEFT)` | |
| 534 | // below, not via the constraint message. | |
| 535 | pre(left: Trees.Variables.LITERAL_VARIABLE_LEFT) -> bool is | |
| 536 | let right_value = _variable_left_state.get_or_add(left).right_value | |
| 537 | ||
| 538 | if right_value? /\ right_value.type? then | |
| 539 | left.expression.set_expected_type( | |
| 540 | right_value.type, | |
| 541 | "literal pattern type {{0}} is not comparable to source position type {{1}}" | |
| 542 | ) | |
| 543 | fi | |
| 544 | return false | |
| 545 | si | |
| 546 | ||
| 547 | visit(left: Trees.Variables.LITERAL_VARIABLE_LEFT) is | |
| 548 | // State survives between walks of the same node, so drop any | |
| 549 | // test a previous walk built before deciding to build one. | |
| 550 | let leaf_state = _variable_left_state.get_or_add(left) | |
| 551 | ||
| 552 | leaf_state.match_test = null | |
| 553 | leaf_state.match_operand = null | |
| 554 | ||
| 555 | // A matching leaf in a non-refutable context (plain `let`) | |
| 556 | // is a silent no-op at runtime — the pattern would never | |
| 557 | // actually test the source value, so subsequent bindings | |
| 558 | // run as if the leaf were a wildcard. Reject it loudly | |
| 559 | // here so the user is forced to write either `if let` | |
| 560 | // / `case`-when (where the leaf becomes an actual | |
| 561 | // equality test) or remove the leaf. | |
| 562 | if !left.is_refutable then | |
| 563 | _logger.error( | |
| 564 | left.location, | |
| 565 | "a matching leaf is only allowed inside a refutable binding (if let or case-when arm)" | |
| 566 | ) | |
| 567 | ||
| 568 | return | |
| 569 | fi | |
| 570 | ||
| 571 | // A leaf whose type is incompatible with the source | |
| 572 | // position's type can never match — the runtime equality | |
| 573 | // test is statically dead. Reject with a clear diagnostic | |
| 574 | // here rather than letting the IL emit and surface as a | |
| 575 | // less helpful comparison-operator error later. | |
| 576 | if let | |
| 577 | rv = leaf_state.right_value, source_type = rv.type, | |
| 578 | ev = left.expression.value, literal_type = ev.type | |
| 579 | then | |
| 580 | if | |
| 581 | !source_type.is_assignable_from(literal_type) /\ | |
| 582 | !literal_type.is_assignable_from(source_type) | |
| 583 | then | |
| 584 | _logger.error( | |
| 585 | left.location, | |
| 586 | "a leaf of type {literal_type} cannot match source position of type {source_type}" | |
| 587 | ) | |
| 588 | ||
| 589 | return | |
| 590 | fi | |
| 591 | ||
| 592 | _build_literal_leaf_test(left, source_type, ev) | |
| 593 | fi | |
| 594 | si | |
| 595 | ||
| 596 | // Build the runtime test a literal leaf performs against its | |
| 597 | // source position, the way `=~` (falling back to `<>`) would, so | |
| 598 | // a leaf whose type declares an equality operator matches by | |
| 599 | // value rather than by reference. Shares the `case` `when` | |
| 600 | // label builder, so the two refutable constructs agree on what | |
| 601 | // a literal matches. | |
| 602 | // | |
| 603 | // The source position's value does not exist until IL | |
| 604 | // generation walks the pattern, so the test is built against a | |
| 605 | // hole that generation fills in. Leaving the test unbuilt is | |
| 606 | // always safe: generation falls back to the raw compare, which | |
| 607 | // is what a bare `==` does too. | |
| 608 | _build_literal_leaf_test( | |
| 609 | left: Trees.Variables.LITERAL_VARIABLE_LEFT, | |
| 610 | source_type: Semantic.Types.Type, | |
| 611 | literal_value: IR.Values.Value | |
| 612 | ) is | |
| 613 | // A `null` leaf is a presence test rather than a value | |
| 614 | // comparison; a null operand would build a null-typed temp | |
| 615 | // the back end cannot encode. Generation matches absence. | |
| 616 | if literal_value.type?.is_null ?? false then | |
| 617 | return | |
| 618 | fi | |
| 619 | ||
| 620 | let operand = IR.Values.WRAPPER(IR.Values.DUMMY(source_type, left.location)) | |
| 621 | ||
| 622 | let test = | |
| 623 | build_equality_test(operand, literal_value, "=~", left.location) ?? | |
| 624 | build_equality_test(operand, literal_value, "<>", left.location) | |
| 625 | ||
| 626 | if !test? then | |
| 627 | return | |
| 628 | fi | |
| 629 | ||
| 630 | let state = _variable_left_state.get_or_add(left) | |
| 631 | ||
| 632 | state.match_operand = operand | |
| 633 | state.match_test = test | |
| 634 | si | |
| 635 | ||
| 636 | // The local an initializer `[]` would type, when the literal has | |
| 637 | // no context of its own: no written type on the literal or the | |
| 638 | // local, nothing pushed into the literal, and a plain immutable | |
| 639 | // local to bind. A mutable local joins its later assignments | |
| 640 | // instead. | |
| 641 | _empty_literal_awaiting_use(variable: Trees.Variables.VARIABLE, init: Trees.Expressions.Expression) -> Semantic.Symbols.Variable? is | |
| 642 | if variable.is_explicit_type \/ variable.is_refutable \/ variable.is_argument then | |
| 643 | return null | |
| 644 | fi | |
| 645 | ||
| 646 | let sequence = cast Trees.Expressions.SEQUENCE?(init) | |
| 647 | ||
| 648 | if | |
| 649 | !sequence? \/ | |
| 650 | sequence.elements.expressions.count != 0 \/ | |
| 651 | !isa Trees.TypeExpressions.INFER(sequence.type_expression) \/ | |
| 652 | sequence.expected_type? | |
| 653 | then | |
| 654 | return null | |
| 655 | fi | |
| 656 | ||
| 657 | if let simple: Trees.Variables.SIMPLE_VARIABLE_LEFT = variable.left then | |
| 658 | if let local: Semantic.Symbols.Variable = find(simple.name) /\ !local.is_mutable_marked then | |
| 659 | return local | |
| 660 | fi | |
| 661 | fi | |
| 662 | ||
| 663 | return null | |
| 664 | si | |
| 665 | ||
| 666 | // The plain immutable local an initializer with no type of its own | |
| 667 | // is typed from: a `_()` with nothing pushed into it, or a call a | |
| 668 | // previous walk found leaving a type parameter of its own unbound. | |
| 669 | _typed_by_later_use(variable: Trees.Variables.VARIABLE, init: Trees.Expressions.Expression) -> Semantic.Symbols.Variable? is | |
| 670 | if variable.is_explicit_type \/ variable.is_refutable \/ variable.is_argument then | |
| 671 | return null | |
| 672 | fi | |
| 673 | ||
| 674 | if let simple: Trees.Variables.SIMPLE_VARIABLE_LEFT = variable.left then | |
| 675 | if let local: Semantic.Symbols.Variable = find(simple.name) /\ !local.is_mutable_marked then | |
| 676 | let state = _variable_left_state.get_or_add(simple) | |
| 677 | ||
| 678 | if isa Trees.Expressions.CONSTRUCT(init) /\ !init.expected_type? then | |
| 679 | state.typed_by_later_use = true | |
| 680 | fi | |
| 681 | ||
| 682 | if state.typed_by_later_use then | |
| 683 | return local | |
| 684 | fi | |
| 685 | fi | |
| 686 | fi | |
| 687 | ||
| 688 | return null | |
| 689 | si | |
| 690 | ||
| 691 | pre(variable: Trees.Variables.VARIABLE) -> bool is | |
| 692 | // Attribute pragmas on a formal-argument parameter — walk | |
| 693 | // their argument expressions here since this override | |
| 694 | // suppresses VARIABLE's own default child walk (returns | |
| 695 | // true below). | |
| 696 | if variable.pragmas? then | |
| 697 | for pragma in variable.pragmas do | |
| 698 | pragma.walk(self) | |
| 699 | od | |
| 700 | fi | |
| 701 | ||
| 702 | variable.type_expression.walk(self) | |
| 703 | ||
| 704 | // push explicit type down into the variable left | |
| 705 | if variable.type_ran_on then | |
| 706 | _variable_left_state.get_or_add(variable.left).explicit_type = Semantic.Types.ERROR() | |
| 707 | elif let te_type = variable.type_expression.type /\ variable.is_explicit_type then | |
| 708 | _variable_left_state.get_or_add(variable.left).explicit_type = te_type | |
| 709 | fi | |
| 710 | ||
| 711 | if variable.is_refutable then | |
| 712 | variable.left.mark_refutable_recursive() | |
| 713 | fi | |
| 714 | ||
| 715 | // A bare `_` initializer of a simple local with no | |
| 716 | // explicit type is treated exactly like a no-initializer | |
| 717 | // `let`: the type is inferred from later assignments. | |
| 718 | // `_` only contributes the definite-assignment fact — the | |
| 719 | // LET deferred-init tracking keys off `initializer?`, | |
| 720 | // which is true here, so the variable is not warned. | |
| 721 | let bare_default = | |
| 722 | isa Trees.Expressions.DEFAULT(variable.initializer) /\ | |
| 723 | !(cast Trees.Expressions.DEFAULT(variable.initializer)).type_expression? /\ | |
| 724 | !variable.is_explicit_type /\ | |
| 725 | variable.left.is_simple_name | |
| 726 | ||
| 727 | if let init = variable.initializer /\ !bare_default /\ !variable.is_argument then | |
| 728 | if let te = variable.type_expression, te_type = te.type /\ !isa Trees.TypeExpressions.INFER(te) then | |
| 729 | // if we have both an explicit type and an initializer, we | |
| 730 | // can push a type constraint down into the initializer | |
| 731 | init.set_expected_type(te_type, "{{0}} is not assignable to {{1}}") | |
| 732 | fi | |
| 733 | ||
| 734 | // An untyped local initialized with `[]` takes its type | |
| 735 | // from its later uses: once a walk has settled the local, | |
| 736 | // the literal is typed from it, and until then the local | |
| 737 | // waits as a placeholder its uses can bound. The local | |
| 738 | // whose uses never settle it is defaulted by the fixing | |
| 739 | // step once the walks have stopped, and this walk is the | |
| 740 | // one after that: the literal then has no constraint | |
| 741 | // pushed into it and takes its own object element type. | |
| 742 | let waits_on_use = _empty_literal_awaiting_use(variable, init) | |
| 743 | ||
| 744 | let awaits mut = false | |
| 745 | let empty_literal mut = false | |
| 746 | ||
| 747 | if let waiting = waits_on_use /\ !Semantic.OBLIGATIONS.has_default_for(waiting) then | |
| 748 | let inferred = waiting.try_get_inferred_type() | |
| 749 | ||
| 750 | if Semantic.EMPTY_LITERAL_LOCAL.decide(inferred) == Semantic.EmptyLiteralLocal.TYPE_FROM_USE then | |
| 751 | init.set_expected_type(inferred!, "{{0}} is not assignable to {{1}}") | |
| 752 | else | |
| 753 | awaits = true | |
| 754 | empty_literal = true | |
| 755 | fi | |
| 756 | elif let later = _typed_by_later_use(variable, init) then | |
| 757 | // No default to fall back to: the local waits until its | |
| 758 | // uses settle it, and is reported if nothing does. | |
| 759 | let inferred = later.try_get_inferred_type() | |
| 760 | ||
| 761 | if inferred? /\ inferred.is_settled then | |
| 762 | init.set_expected_type(inferred, "{{0}} is not assignable to {{1}}") | |
| 763 | elif isa Trees.Expressions.CONSTRUCT(init) then | |
| 764 | init.set_expected_type(Semantic.Types.INFERRED_VARIABLE_TYPE(later), "{{0}} is not assignable to {{1}}") | |
| 765 | awaits = true | |
| 766 | fi | |
| 767 | fi | |
| 768 | ||
| 769 | let await_state = _variable_left_state.get_or_add(variable.left) | |
| 770 | ||
| 771 | await_state.awaits_later_use = awaits | |
| 772 | await_state.awaits_empty_literal = empty_literal | |
| 773 | ||
| 774 | init.walk(self) | |
| 775 | ||
| 776 | // push the initializer value down into the variable left | |
| 777 | let left_state = _variable_left_state.get_or_add(variable.left) | |
| 778 | ||
| 779 | if let init_value = init.value then | |
| 780 | left_state.right_value = init_value | |
| 781 | else | |
| 782 | left_state.right_value = IR.Values.DUMMY(Semantic.Types.ERROR(), init.location) | |
| 783 | fi | |
| 784 | ||
| 785 | left_state.variable_location = variable.location | |
| 786 | left_state.right_location = init.location | |
| 787 | fi | |
| 788 | ||
| 789 | // No-type, no-initializer is now allowed: the variable's | |
| 790 | // type is inferred from later assignments via the | |
| 791 | // INFERRED_VARIABLE_TYPE placeholder + LUB accumulator | |
| 792 | // (#1174 — see visit(SIMPLE_VARIABLE_LEFT) below). If | |
| 793 | // no assignment ever fires, the placeholder remains | |
| 794 | // and the variable's first use will produce a | |
| 795 | // "cannot infer" error. | |
| 796 | ||
| 797 | variable.left.walk(self) | |
| 798 | ||
| 799 | // Generator: register the new local on the state-machine | |
| 800 | // frame so a closure later in the body that captures it | |
| 801 | // references the frame field rather than a | |
| 802 | // CLR-local slot that doesn't exist inside MoveNext. | |
| 803 | // No-op outside a generator function; idempotent on the | |
| 804 | // field (declare_local_field returns the existing field | |
| 805 | // and only refreshes its type on subsequent calls). | |
| 806 | declare_state_machine_local_fields(variable.left) | |
| 807 | ||
| 808 | return true | |
| 809 | si | |
| 810 | ||
| 811 | visit(variable: Trees.Variables.VARIABLE) is | |
| 812 | // A typed `let x: T = e` — check the initializer against | |
| 813 | // the declared type. An untyped `let` infers its type | |
| 814 | // from the initializer, so there is no slot to violate. | |
| 815 | let type_expression = variable.type_expression | |
| 816 | let initializer = variable.initializer | |
| 817 | ||
| 818 | if initializer? then | |
| 819 | check_non_optional( | |
| 820 | type_expression.type, | |
| 821 | initializer, | |
| 822 | initializer.location | |
| 823 | ) | |
| 824 | ||
| 825 | _pure_slots.check_store(initializer.location, type_expression.type, initializer.value) | |
| 826 | fi | |
| 827 | ||
| 828 | if let pragmas = variable.pragmas then | |
| 829 | let target = symbol_for(variable) | |
| 830 | ||
| 831 | for pragma in pragmas do | |
| 832 | _attribute_resolver.resolve(pragma, target) | |
| 833 | od | |
| 834 | fi | |
| 835 | si | |
| 836 | ||
| 837 | // TODO used by for loop - needs removing | |
| 838 | set_symbol_type(left: Trees.Variables.VariableLeft, type: Type) is | |
| 839 | ||
| 840 | if left.is_simple_name then | |
| 841 | // is_simple_name => SIMPLE_VARIABLE_LEFT, whose name is non-null | |
| 842 | let symbol = find(left.name!) | |
| 843 | ||
| 844 | if symbol? /\ isa Semantic.Types.SettableTyped(symbol) then | |
| 845 | let typed_symbol = cast Semantic.Types.SettableTyped(symbol) | |
| 846 | ||
| 847 | symbol.define() | |
| 848 | ||
| 849 | typed_symbol.set_type(type) | |
| 850 | else | |
| 851 | _logger.error(left.location, "couldn't find typed symbol for variable {left.name}") | |
| 852 | fi | |
| 853 | else | |
| 854 | set_symbol_destructure_types(left, type) | |
| 855 | fi | |
| 856 | si | |
| 857 | ||
| 858 | // TODO used by for loop - needs removing | |
| 859 | set_symbol_destructure_types(left: Trees.Variables.VariableLeft, from_type: Type) is | |
| 860 | let elements = left.elements | |
| 861 | ||
| 862 | if !elements? then | |
| 863 | return | |
| 864 | fi | |
| 865 | ||
| 866 | let is_named_group = elements.count > 0 /\ elements[0].source_field_name? | |
| 867 | ||
| 868 | let field_names: Collections.List[string?]? mut = null | |
| 869 | if is_named_group then | |
| 870 | let names = Collections.LIST[string?]() | |
| 871 | for element in elements do | |
| 872 | // is_named_group: the parser enforces all-or-nothing | |
| 873 | // source field names per destructure group | |
| 874 | names.add(element.source_field_name!.name) | |
| 875 | od | |
| 876 | field_names = names | |
| 877 | fi | |
| 878 | ||
| 879 | let strategy = resolve_destructure_strategy(left.location, from_type, elements.count, field_names) | |
| 880 | ||
| 881 | if strategy.is_deconstruct then | |
| 882 | let deconstruct = strategy.deconstruct_function! | |
| 883 | ||
| 884 | for i in 0..elements.count do | |
| 885 | let element = elements[i] | |
| 886 | ||
| 887 | let element_type = deconstruct.arguments[i].get_element_type()! | |
| 888 | ||
| 889 | if element.is_simple_name then | |
| 890 | let symbol = find(element.name!) | |
| 891 | ||
| 892 | if symbol? /\ isa Semantic.Types.SettableTyped(symbol) then | |
| 893 | let typed_symbol = cast Semantic.Types.SettableTyped(symbol) | |
| 894 | ||
| 895 | symbol.define() | |
| 896 | ||
| 897 | typed_symbol.set_type(element_type) | |
| 898 | else | |
| 899 | _logger.error(element.location, "couldn't find typed symbol for destructuring element {element.name}") | |
| 900 | fi | |
| 901 | else | |
| 902 | set_symbol_destructure_types(element, element_type) | |
| 903 | fi | |
| 904 | od | |
| 905 | ||
| 906 | return | |
| 907 | fi | |
| 908 | ||
| 909 | let members = strategy.members | |
| 910 | ||
| 911 | for i in 0..elements.count do | |
| 912 | let element = elements[i] | |
| 913 | let member = members[i] | |
| 914 | ||
| 915 | if member? then | |
| 916 | let member_type = member.type | |
| 917 | ||
| 918 | if element.is_simple_name then | |
| 919 | let symbol = find(element.name!) | |
| 920 | ||
| 921 | if symbol? /\ isa Semantic.Types.SettableTyped(symbol) then | |
| 922 | let typed_symbol = cast Semantic.Types.SettableTyped(symbol) | |
| 923 | ||
| 924 | symbol.define() | |
| 925 | ||
| 926 | if member_type? then | |
| 927 | typed_symbol.set_type(member_type) | |
| 928 | fi | |
| 929 | else | |
| 930 | _logger.error(element.location, "couldn't find typed symbol for destructuring element {element.name}") | |
| 931 | fi | |
| 932 | elif member_type? then | |
| 933 | set_symbol_destructure_types(element, member_type) | |
| 934 | fi | |
| 935 | fi | |
| 936 | od | |
| 937 | si | |
| 938 | ||
| 939 | // TODO used by for loop - needs removing | |
| 940 | get_destructure_types(type: Type?) -> Collections.List[Type] is | |
| 941 | let result = Collections.LIST[Type]() | |
| 942 | ||
| 943 | if !type? \/ !type.is_value_tuple then | |
| 944 | return result | |
| 945 | fi | |
| 946 | ||
| 947 | get_destructure_types_into(type, result) | |
| 948 | ||
| 949 | return result | |
| 950 | si | |
| 951 | ||
| 952 | // TODO used by for loop - needs removing | |
| 953 | get_destructure_types_into(type: Type, into: Collections.MutableList[Type]) is | |
| 954 | let result = Collections.LIST[Type]() | |
| 955 | ||
| 956 | for t in type.arguments do | |
| 957 | if t.is_value_tuple then | |
| 958 | get_destructure_types_into(t, into) | |
| 959 | else | |
| 960 | into.add(t) | |
| 961 | fi | |
| 962 | od | |
| 963 | si | |
| 964 | ||
| 965 | si | |
| 966 | si |