Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use Logging | |
| 3 | use Source | |
| 4 | ||
| 5 | use Semantic.Types.Type | |
| 6 | ||
| 7 | use IR.Values | |
| 8 | ||
| 9 | use Ghul.Pipes | |
| 10 | ||
| 11 | // Compiles the member-access family of expressions: member access, | |
| 12 | // identifier resolution, `?` (has-value), `!` (unwrap) and `ref`. | |
| 13 | // Split out of COMPILE_EXPRESSIONS, which delegates the matching | |
| 14 | // visit methods here. The exception-handling wrapper of | |
| 15 | // visit(identifier) stays on the visitor; visit_identifier is the | |
| 16 | // enclosed logic. | |
| 17 | class COMPILE_ACCESS is | |
| 18 | ||
| 19 | // Depth of enclosing assert conditions during the controlled | |
| 20 | // walk in the expressions pass. A presence test on a | |
| 21 | // never-optional value inside an assert is a deliberate | |
| 22 | // runtime trap for laundered nulls, not a redundant check, | |
| 23 | // so the static-type hint stays quiet there. | |
| 24 | assert_condition_depth: int public | |
| 25 | _logger: Logger | |
| 26 | _symbol_table: Semantic.SYMBOL_TABLE | |
| 27 | _symbol_loader: Semantic.SYMBOL_LOADER | |
| 28 | _symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS | |
| 29 | _innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup | |
| 30 | _overload_resolver: Semantic.OVERLOAD_RESOLVER | |
| 31 | _function_caller: Semantic.FUNCTION_CALLER | |
| 32 | _unit_variant_constructor: Semantic.UNIT_VARIANT_CONSTRUCTOR | |
| 33 | _flow: NARROWING_FLOW | |
| 34 | _condition_analyzer: CONDITION_ANALYZER | |
| 35 | _build_flags: Compiler.GLOBAL_BUILD_FLAGS | |
| 36 | _visitor: COMPILE_EXPRESSIONS | |
| 37 | _function_reference_resolver: Semantic.FUNCTION_REFERENCE_RESOLVER | |
| 38 | _shadowed_callable_finder: Semantic.SHADOWED_CALLABLE_FINDER | |
| 39 | ||
| 40 | init( | |
| 41 | logger: Logger, | |
| 42 | symbol_table: Semantic.SYMBOL_TABLE, | |
| 43 | symbol_loader: Semantic.SYMBOL_LOADER, | |
| 44 | symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS, | |
| 45 | innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup, | |
| 46 | overload_resolver: Semantic.OVERLOAD_RESOLVER, | |
| 47 | function_caller: Semantic.FUNCTION_CALLER, | |
| 48 | unit_variant_constructor: Semantic.UNIT_VARIANT_CONSTRUCTOR, | |
| 49 | flow: NARROWING_FLOW, | |
| 50 | condition_analyzer: CONDITION_ANALYZER, | |
| 51 | build_flags: Compiler.GLOBAL_BUILD_FLAGS, | |
| 52 | visitor: COMPILE_EXPRESSIONS | |
| 53 | ) is | |
| 54 | super.init() | |
| 55 | ||
| 56 | _logger = logger | |
| 57 | _symbol_table = symbol_table | |
| 58 | _symbol_loader = symbol_loader | |
| 59 | _symbol_use_locations = symbol_use_locations | |
| 60 | _innate_symbol_lookup = innate_symbol_lookup | |
| 61 | _overload_resolver = overload_resolver | |
| 62 | _function_caller = function_caller | |
| 63 | _unit_variant_constructor = unit_variant_constructor | |
| 64 | _flow = flow | |
| 65 | _condition_analyzer = condition_analyzer | |
| 66 | _build_flags = build_flags | |
| 67 | _visitor = visitor | |
| 68 | _function_reference_resolver = Semantic.FUNCTION_REFERENCE_RESOLVER(logger, symbol_table, symbol_loader, innate_symbol_lookup, overload_resolver) | |
| 69 | _shadowed_callable_finder = Semantic.SHADOWED_CALLABLE_FINDER(logger, symbol_table) | |
| 70 | si | |
| 71 | ||
| 72 | visit_member(member: Trees.Expressions.MEMBER) is | |
| 73 | let need_store = member.value? /\ isa Need.STORE(member.value) | |
| 74 | let need_deref mut = false | |
| 75 | ||
| 76 | if member.identifier.is_poisoned then | |
| 77 | member.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), member.location) | |
| 78 | return | |
| 79 | fi | |
| 80 | ||
| 81 | if member.left.value? then | |
| 82 | let left_value mut = member.left.value | |
| 83 | let type mut = left_value.type | |
| 84 | ||
| 85 | if type == null then | |
| 86 | _logger.poison(member.left.location, "member left has no type") | |
| 87 | ||
| 88 | member.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), member.location) | |
| 89 | return | |
| 90 | fi | |
| 91 | ||
| 92 | // `x?.y` never dereferences a null receiver, and | |
| 93 | // `x.has_value` is the optional protocol's presence | |
| 94 | // query — asking is not dereferencing. | |
| 95 | if !member.is_coalesce /\ !(member.identifier.name =~ "has_value") then | |
| 96 | _visitor.check_receiver_present(member.left) | |
| 97 | fi | |
| 98 | ||
| 99 | // TODO we could loop here to handle multiple levels of dereferencing | |
| 100 | if type.is_ref then | |
| 101 | need_deref = true | |
| 102 | // is_ref implies an element type; the runtime | |
| 103 | // invariant guarantees get_element_type() non-null. | |
| 104 | type = type.get_element_type()! | |
| 105 | ||
| 106 | left_value = DEREF(left_value, type) | |
| 107 | member.left.compile_expressions_state.value = left_value | |
| 108 | fi | |
| 109 | ||
| 110 | // Operation-constraint inference: when the receiver | |
| 111 | // is an INFERRED_VARIABLE_TYPE placeholder, record a | |
| 112 | // MEMBER_CONSTRAINT on the placeholder's origin so | |
| 113 | // the constraint-aware LUB can later filter candidate | |
| 114 | // types to those that actually expose this member. | |
| 115 | // mark_consumed_any drives the retry loop to re-walk | |
| 116 | // the body with the resolved type (if any). | |
| 117 | if isa Semantic.Types.INFERRED_VARIABLE_TYPE(type) then | |
| 118 | let placeholder = type | |
| 119 | ||
| 120 | _logger.mark_consumed_any_if(Semantic.INFERENCE_TRACE.add_constraint("access.member", placeholder.origin, Semantic.MEMBER_CONSTRAINT(member.identifier.name))) | |
| 121 | ||
| 122 | // The read waits with the receiver rather than | |
| 123 | // standing as an error: a consumer that pins this | |
| 124 | // value's type - a tuple a literal returns, say - | |
| 125 | // would otherwise fix the failure into a type no | |
| 126 | // later walk reaches. | |
| 127 | member.compile_expressions_state.value = | |
| 128 | if Semantic.OPERAND_WAIT.is_typed_by_the_call_site(placeholder) then | |
| 129 | DUMMY(Semantic.Types.INFERRED_JOIN_TYPE(placeholder), member.location) | |
| 130 | else | |
| 131 | DUMMY(Semantic.Types.ERROR(), member.location) | |
| 132 | fi | |
| 133 | ||
| 134 | return | |
| 135 | fi | |
| 136 | ||
| 137 | if !isa Semantic.Types.NAMED(type) then | |
| 138 | if !isa Semantic.Types.ERROR(type) /\ !type.is_inferred then | |
| 139 | // Suppress for ERROR (existing — error already | |
| 140 | // reported, the contract says don't pile on) | |
| 141 | // and for is_inferred placeholders (deferred- | |
| 142 | // inference: the lambda's arg type is awaiting | |
| 143 | // the call-site second-pass to fill it in, | |
| 144 | // and emitting a fatal "type has no members" | |
| 145 | // here breaks that flow). Both share the | |
| 146 | // intent of "don't surface a member-resolution | |
| 147 | // failure on a stand-in type". | |
| 148 | _logger.poison(member.left.location, "type has no members: {type}") | |
| 149 | fi | |
| 150 | ||
| 151 | member.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), member.location) | |
| 152 | return | |
| 153 | fi | |
| 154 | ||
| 155 | let named_type mut = type | |
| 156 | ||
| 157 | // For coalescing access against any optional, look | |
| 158 | // up the member on the unwrapped (inner) type so | |
| 159 | // `a?.b` consistently means "if a is present, access | |
| 160 | // b on the unwrapped a". A value-type optional like | |
| 161 | // NULLABLE[T] / MAYBE[T] otherwise resolves `value` | |
| 162 | // / `has_value` against the wrapper rather than the | |
| 163 | // payload, surprising the user and producing IL that | |
| 164 | // mixes wrapper-thissed calls with payload-typed | |
| 165 | // stack values. | |
| 166 | if member.is_coalesce /\ named_type.is_optional then | |
| 167 | let inner = named_type.optional_inner_type | |
| 168 | ||
| 169 | // The access tests one layer. Where the value inside | |
| 170 | // is itself optional - a generic T? instantiated at a | |
| 171 | // T? - the member would be read through that value | |
| 172 | // unchecked. | |
| 173 | if inner? /\ inner.is_optional then | |
| 174 | let target = _visitor.try_get_narrowing_target(member.left) | |
| 175 | let subject = if target? then target.name else "receiver" fi | |
| 176 | ||
| 177 | _logger.error(member.left.location, "{subject} may hold an absent value here") | |
| 178 | ||
| 179 | member.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), member.location) | |
| 180 | return | |
| 181 | fi | |
| 182 | ||
| 183 | if inner? /\ isa Semantic.Types.NAMED(inner) then | |
| 184 | named_type = inner | |
| 185 | fi | |
| 186 | fi | |
| 187 | ||
| 188 | let symbol mut = named_type.find_member(member.identifier.name) | |
| 189 | ||
| 190 | // A member that resolves to same-name types at several | |
| 191 | // generic arities comes back as a TYPE_GROUP. In value | |
| 192 | // position (`Foo.bar` with no `[...]`) collapse to the | |
| 193 | // arity-0 member; `Foo[T].bar` is handled earlier by | |
| 194 | // GENERIC_APPLICATION before reaching here. | |
| 195 | let member_group = cast Semantic.Symbols.TYPE_GROUP?(symbol) | |
| 196 | ||
| 197 | if member_group? then | |
| 198 | let arity_zero = member_group.find_by_generic_arguments_count(0) | |
| 199 | ||
| 200 | // A constructor call on the qualified name resolves to the | |
| 201 | // group's generic member when there is no constructible | |
| 202 | // arity-0 member (a static factory class shares the name | |
| 203 | // with a generic type, `Collections.KeyValuePair`); when | |
| 204 | // the arity-0 member is itself constructible, or this is a | |
| 205 | // plain value-position access, collapse to it. | |
| 206 | let for_call = | |
| 207 | if member.is_call_target /\ (!arity_zero? \/ arity_zero.is_abstract) then | |
| 208 | member_group.sole_generic_member() | |
| 209 | else | |
| 210 | null | |
| 211 | fi | |
| 212 | ||
| 213 | if for_call? then | |
| 214 | symbol = for_call | |
| 215 | elif arity_zero? then | |
| 216 | symbol = arity_zero | |
| 217 | else | |
| 218 | let counts = member_group.generic_arguments_counts |> map(a -> string => "{a}") |> join(" or ") | |
| 219 | _logger.error(member.identifier.location, "type {member.identifier.name} requires {counts} type arguments") | |
| 220 | member.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), member.location) | |
| 221 | return | |
| 222 | fi | |
| 223 | fi | |
| 224 | ||
| 225 | if !symbol? then | |
| 226 | let hint = try_describe_intersection_miss(named_type, member.identifier.name) | |
| 227 | ||
| 228 | if hint? then | |
| 229 | _logger.error(member.identifier.location, hint) | |
| 230 | else | |
| 231 | // A member that the narrowed type did have, on a | |
| 232 | // value whose narrowing a call ended, is missing | |
| 233 | // for that reason: the type is the widened one | |
| 234 | // again from the call onwards. The message is the | |
| 235 | // ordinary one, with the call it ended at. | |
| 236 | let dropped = | |
| 237 | DROPPED_NARROWING.for_member( | |
| 238 | _visitor.try_get_narrowing_target(member.left), | |
| 239 | _visitor.try_build_access_path(member.left), | |
| 240 | member.identifier.name, | |
| 241 | member.identifier.location | |
| 242 | ) | |
| 243 | ||
| 244 | if let kill = dropped then | |
| 245 | _logger.error( | |
| 246 | member.identifier.location, | |
| 247 | "member {member.identifier.name} not found in {named_type}", | |
| 248 | kill.crossing.location, | |
| 249 | DROPPED_NARROWING.message(kill) | |
| 250 | ) | |
| 251 | else | |
| 252 | _logger.error(member.identifier.location, "member {member.identifier.name} not found in {named_type}") | |
| 253 | fi | |
| 254 | fi | |
| 255 | ||
| 256 | member.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), member.location) | |
| 257 | return | |
| 258 | fi | |
| 259 | ||
| 260 | if _is_indexer_accessor(symbol) then | |
| 261 | _logger.error( | |
| 262 | member.identifier.location, | |
| 263 | "cannot call an indexer accessor directly", | |
| 264 | member.identifier.location, | |
| 265 | "help: index {named_type} with [ ] instead" | |
| 266 | ) | |
| 267 | ||
| 268 | member.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), member.location) | |
| 269 | return | |
| 270 | fi | |
| 271 | ||
| 272 | _symbol_use_locations.add_symbol_use(member.identifier.right_location, symbol) | |
| 273 | ||
| 274 | if _is_instance_only(symbol) /\ !left_value.is_consumable then | |
| 275 | _logger.error(member.identifier.location, "cannot access instance member {member.identifier.name} in {named_type}") | |
| 276 | ||
| 277 | member.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), member.location) | |
| 278 | return | |
| 279 | fi | |
| 280 | ||
| 281 | _symbol_loader.find_symbol = | |
| 282 | (name: string) is | |
| 283 | let result = named_type.find_member(name) | |
| 284 | ||
| 285 | return result | |
| 286 | si | |
| 287 | ||
| 288 | let value: Value? mut = _ | |
| 289 | ||
| 290 | if need_store then | |
| 291 | if member.is_coalesce then | |
| 292 | _logger.error(member.location, "cannot assign through coalescing member access") | |
| 293 | fi | |
| 294 | ||
| 295 | // A struct-typed receiver with no in-place address is a | |
| 296 | // temporary copy - a property get, method result or | |
| 297 | // indexer element - so a store through it would be | |
| 298 | // silently lost. Field chains, locals, self and refs | |
| 299 | // all carry an address and store in place. | |
| 300 | if | |
| 301 | symbol.is_instance /\ | |
| 302 | type.is_value_type /\ | |
| 303 | !left_value.has_address | |
| 304 | then | |
| 305 | _logger.error(member.location, "cannot assign through a copy of a struct value here") | |
| 306 | ||
| 307 | member.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), member.location) | |
| 308 | return | |
| 309 | fi | |
| 310 | ||
| 311 | let store_value = cast Need.STORE?(member.value)!.value | |
| 312 | ||
| 313 | if check_store_access(member.location, left_value, symbol, named_type) then | |
| 314 | value = symbol.store(member.location, left_value, store_value, _symbol_loader, false) | |
| 315 | fi | |
| 316 | ||
| 317 | // The written receiver may alias any receiver a | |
| 318 | // fact was proven on, so the stored member's own | |
| 319 | // facts and every path reading through it die — | |
| 320 | // keyed on the member symbol, not the receiver. | |
| 321 | _flow.on_member_store(symbol) | |
| 322 | else | |
| 323 | if symbol.is_unit_variant then | |
| 324 | if member.is_coalesce then | |
| 325 | _logger.error(member.location, "coalescing member access does not apply to a unit-variant constructor") | |
| 326 | fi | |
| 327 | ||
| 328 | let lowered = _unit_variant_constructor.try_load(member.location, symbol, member.expected_type, member) | |
| 329 | ||
| 330 | if lowered? then | |
| 331 | _symbol_use_locations.add_symbol_use(member.identifier.right_location, lowered.constructor) | |
| 332 | member.compile_expressions_state.value = lowered.value | |
| 333 | return | |
| 334 | fi | |
| 335 | fi | |
| 336 | ||
| 337 | if check_load_access(member.location, left_value, symbol, named_type) then | |
| 338 | if member.is_coalesce then | |
| 339 | value = _build_coalesce_load(member, symbol) | |
| 340 | ||
| 341 | if value? then | |
| 342 | member.compile_expressions_state.value = value | |
| 343 | return | |
| 344 | fi | |
| 345 | ||
| 346 | // Fallthrough on a build failure (e.g. | |
| 347 | // unsupported receiver shape): proceed with | |
| 348 | // a plain access so the user gets at most | |
| 349 | // one diagnostic instead of a cascade. | |
| 350 | value = symbol.load(member.location, left_value, _symbol_loader) | |
| 351 | else | |
| 352 | value = | |
| 353 | _function_reference_resolver.try_load(member.location, symbol, left_value, member.expected_type, member.is_call_target) ?? | |
| 354 | symbol.load(member.location, left_value, _symbol_loader) | |
| 355 | fi | |
| 356 | fi | |
| 357 | fi | |
| 358 | ||
| 359 | member.compile_expressions_state.value = value | |
| 360 | ||
| 361 | // Path narrowing: when the flow analysis has recorded | |
| 362 | // facts for this member-access path, surface the | |
| 363 | // access at the narrower view type so | |
| 364 | // `if isa Cat(x.y) then x.y.meow() fi` and | |
| 365 | // `if x.y? then x.y.z fi` both type-check. Mirrors the | |
| 366 | // local-variable path in visit_identifier. A reference | |
| 367 | // optional shares IL with its non-optional form (a type | |
| 368 | // view); NULLABLE[T] / MAYBE[T] get a `.value` | |
| 369 | // projection. | |
| 370 | if !need_store /\ member.value? then | |
| 371 | let path = _visitor.try_build_access_path(member) | |
| 372 | ||
| 373 | // `self.x` builds no path: it names the same | |
| 374 | // location the bare `x` does, so the flow analysis | |
| 375 | // keys their shared fact on the member symbol | |
| 376 | // instead. Read back under whichever key the guard | |
| 377 | // recorded, so both spellings see the same narrow. | |
| 378 | let self_target = | |
| 379 | if path? then | |
| 380 | null | |
| 381 | else | |
| 382 | _visitor.try_get_narrowing_target(member) | |
| 383 | fi | |
| 384 | ||
| 385 | if path? \/ self_target? then | |
| 386 | // The un-narrowed view of this load, for the | |
| 387 | // consumption judge: a use the wide type | |
| 388 | // already satisfies does not lean on the fact. | |
| 389 | let wide_type = | |
| 390 | if path? then | |
| 391 | member.value!.type | |
| 392 | else | |
| 393 | _flow.declared_type_of(self_target!) | |
| 394 | fi | |
| 395 | ||
| 396 | // Step 1: type narrow (isa T on x.y). Wraps the | |
| 397 | // loaded value at the recorded static subtype | |
| 398 | // when the composition is sound. Path-keyed | |
| 399 | // only — a symbol-keyed type narrow already | |
| 400 | // reaches the load through the narrowed | |
| 401 | // symbol's own type. | |
| 402 | if let recorded = _flow.narrowed_type_of_path(path) then | |
| 403 | let loaded = member.value! | |
| 404 | ||
| 405 | if loaded.type? then | |
| 406 | if let composed = _flow.compose_path_narrow(loaded.type, recorded) then | |
| 407 | member.compile_expressions_state.value = IR.Values.NARROW_VIEW(loaded, composed) | |
| 408 | _symbol_use_locations.replace_with_variable_use(member.identifier.right_location, symbol, member.value!) | |
| 409 | fi | |
| 410 | fi | |
| 411 | fi | |
| 412 | ||
| 413 | // Step 2: presence narrow (if x.y? then x.y). | |
| 414 | // Reads member.value again in case step 1 | |
| 415 | // already wrapped it. | |
| 416 | let loaded = member.value! | |
| 417 | ||
| 418 | let is_present = | |
| 419 | if path? then | |
| 420 | _flow.is_non_null_path(path) | |
| 421 | else | |
| 422 | _flow.is_non_null(self_target!) | |
| 423 | fi | |
| 424 | ||
| 425 | if loaded.type? /\ loaded.type.is_optional /\ is_present then | |
| 426 | let loaded_type = loaded.type | |
| 427 | let narrowed = loaded_type.as_non_optional() | |
| 428 | ||
| 429 | if !narrowed.is_optional then | |
| 430 | member.compile_expressions_state.value = IR.Values.NARROW_VIEW(loaded, narrowed) | |
| 431 | // Overwrite the earlier symbol-use entry so | |
| 432 | // HOVER on the member surfaces the narrowed | |
| 433 | // type — mirroring what visit_identifier does | |
| 434 | // for a narrowed local. Same location; put | |
| 435 | // wins for the last write. | |
| 436 | _symbol_use_locations.replace_with_variable_use(member.identifier.right_location, symbol, member.value!) | |
| 437 | elif loaded_type.is_value_type then | |
| 438 | let value_member = loaded_type.find_member("value") | |
| 439 | ||
| 440 | if value_member? then | |
| 441 | let projected = value_member.load(member.location, loaded, _symbol_loader) | |
| 442 | ||
| 443 | member.compile_expressions_state.value = IR.Values.NARROW_PROJECT(loaded, projected) | |
| 444 | _symbol_use_locations.replace_with_variable_use(member.identifier.right_location, symbol, member.value!) | |
| 445 | fi | |
| 446 | fi | |
| 447 | fi | |
| 448 | ||
| 449 | // Record the fact load for the kill ledger, | |
| 450 | // so the re-ask after the next solve covers it. | |
| 451 | if path? then | |
| 452 | _flow.record_load_of_path(member.location, path) | |
| 453 | else | |
| 454 | _flow.record_load_of_symbol(member.location, self_target!) | |
| 455 | fi | |
| 456 | fi | |
| 457 | fi | |
| 458 | fi | |
| 459 | si | |
| 460 | ||
| 461 | // Whether the fact behind an `!` operand has every crossing | |
| 462 | // discharged. When it does not, the unwrap is the remedy the | |
| 463 | // reliance error asks for, and must not be warned redundant. | |
| 464 | _unwrap_fact_validated(operand: Trees.Expressions.Expression?) -> bool is | |
| 465 | if !operand? then | |
| 466 | return true | |
| 467 | fi | |
| 468 | ||
| 469 | let target = _visitor.try_get_narrowing_target(operand) | |
| 470 | ||
| 471 | if target? then | |
| 472 | return _flow.symbol_fact_validated(target) | |
| 473 | fi | |
| 474 | ||
| 475 | if let member: Trees.Expressions.MEMBER = operand then | |
| 476 | let path = _visitor.try_build_access_path(member) | |
| 477 | ||
| 478 | if path? then | |
| 479 | return _flow.path_fact_validated(path) | |
| 480 | fi | |
| 481 | fi | |
| 482 | ||
| 483 | return true | |
| 484 | si | |
| 485 | ||
| 486 | // True iff any receiver in `expr`'s member-access chain has | |
| 487 | // been wrapped in a NARROW_VIEW by path narrowing. Used to | |
| 488 | // recognise loads whose result type reflects a narrowed | |
| 489 | // receiver — the declared receiver type is still optional at | |
| 490 | // this position, so an `!` against the narrowed type is | |
| 491 | // redundant rather than an error. | |
| 492 | _has_narrowed_receiver(expr: Trees.Expressions.Expression) -> bool is | |
| 493 | let cursor mut = expr | |
| 494 | ||
| 495 | while isa Trees.Expressions.MEMBER(cursor) do | |
| 496 | let member = cast Trees.Expressions.MEMBER(cursor) | |
| 497 | ||
| 498 | if member.left.value? /\ isa IR.Values.NARROW_VIEW(member.left.value) then | |
| 499 | return true | |
| 500 | fi | |
| 501 | ||
| 502 | cursor = member.left | |
| 503 | od | |
| 504 | ||
| 505 | return false | |
| 506 | si | |
| 507 | ||
| 508 | check_load_access(location: LOCATION, from: Value?, symbol: Semantic.Symbols.Symbol, type: Type) -> bool is | |
| 509 | // A synthesized member reads what the type holds whatever | |
| 510 | // its visibility: a variant's equality and hash read the | |
| 511 | // private members of its own union. | |
| 512 | if location.is_internal then | |
| 513 | return true | |
| 514 | fi | |
| 515 | ||
| 516 | if !symbol.is_accessible_to(_symbol_table.current_accessor) then | |
| 517 | _logger.error(location, "{symbol} is not accessible here") | |
| 518 | // The member is assembly-reachable, so let the access through to | |
| 519 | // avoid a cascade of downstream errors: the diagnostic stands but | |
| 520 | // the generated IL is still valid. | |
| 521 | return true | |
| 522 | fi | |
| 523 | ||
| 524 | if symbol.is_public_readable then | |
| 525 | return true | |
| 526 | fi | |
| 527 | ||
| 528 | if from? /\ from.is_self then | |
| 529 | return true | |
| 530 | fi | |
| 531 | ||
| 532 | let instance_context = _symbol_table.current_instance_context | |
| 533 | let instance_context_type = if instance_context? then instance_context.type else null fi | |
| 534 | ||
| 535 | if instance_context_type? /\ instance_context_type.is_assignable_from(type) then | |
| 536 | return true | |
| 537 | fi | |
| 538 | ||
| 539 | // A member reached through a specialization is the same | |
| 540 | // declaring type under a distinct symbol, so the | |
| 541 | // assignability test above misses a receiver that is the | |
| 542 | // current instance type specialized - reading a private | |
| 543 | // member off a second instance of the same generic, as an | |
| 544 | // equality or comparison method does. find_ancestor | |
| 545 | // compares root unspecialized symbols along the ancestor | |
| 546 | // chain, which holds for the specialized case and matches | |
| 547 | // the assignability result for the ordinary one. | |
| 548 | if instance_context_type? /\ type.symbol.find_ancestor(instance_context_type)? then | |
| 549 | return true | |
| 550 | fi | |
| 551 | ||
| 552 | _logger.error(location, "{symbol} is not publicly readable") | |
| 553 | ||
| 554 | return false | |
| 555 | si | |
| 556 | ||
| 557 | check_store_access(location: LOCATION, from: Value?, symbol: Semantic.Symbols.Symbol, type: Type) -> bool is | |
| 558 | if !symbol.is_accessible_to(_symbol_table.current_accessor) then | |
| 559 | _logger.error(location, "{symbol} is not accessible here") | |
| 560 | // Assembly-reachable: allow the store so the diagnostic does not | |
| 561 | // cascade into follow-on errors. | |
| 562 | return true | |
| 563 | fi | |
| 564 | ||
| 565 | if !symbol.is_private /\ !symbol.is_field then | |
| 566 | return true | |
| 567 | fi | |
| 568 | ||
| 569 | if from? /\ from.is_self then | |
| 570 | return true | |
| 571 | fi | |
| 572 | ||
| 573 | let instance_context = _symbol_table.current_instance_context | |
| 574 | let instance_context_type = if instance_context? then instance_context.type else null fi | |
| 575 | ||
| 576 | if instance_context_type? /\ instance_context_type.is_assignable_from(type) then | |
| 577 | return true | |
| 578 | fi | |
| 579 | ||
| 580 | // Same specialization-identity case as check_load_access | |
| 581 | // above: a second instance of the same generic is the | |
| 582 | // declaring type under a distinct symbol. | |
| 583 | if instance_context_type? /\ type.symbol.find_ancestor(instance_context_type)? then | |
| 584 | return true | |
| 585 | fi | |
| 586 | ||
| 587 | _logger.error(location, "{symbol} is not publicly assignable") | |
| 588 | ||
| 589 | return false | |
| 590 | si | |
| 591 | ||
| 592 | visit_identifier(identifier: Trees.Expressions.IDENTIFIER) is | |
| 593 | let need_store = identifier.value? /\ isa Need.STORE(identifier.value) | |
| 594 | ||
| 595 | let symbol mut = _visitor.find(identifier.identifier) | |
| 596 | ||
| 597 | if | |
| 598 | let shadowed = symbol /\ | |
| 599 | identifier.is_call_target /\ | |
| 600 | !identifier.identifier.qualifier?, | |
| 601 | callable = _shadowed_callable_finder.find(identifier.location, identifier.identifier.name, shadowed) | |
| 602 | then | |
| 603 | symbol = callable | |
| 604 | fi | |
| 605 | ||
| 606 | let group = cast Semantic.Symbols.TYPE_GROUP?(symbol) | |
| 607 | ||
| 608 | if group? then | |
| 609 | let bare = group.find_by_generic_arguments_count(0) | |
| 610 | ||
| 611 | // A constructor call on the bare name resolves to the group's | |
| 612 | // generic member when there is no constructible arity-0 | |
| 613 | // member — the reflected shape where a static factory class | |
| 614 | // (abstract, so not constructible) shares a name with a | |
| 615 | // generic type (`KeyValuePair` + `KeyValuePair[K,V]`). When | |
| 616 | // the arity-0 member *is* constructible (a plain non-generic | |
| 617 | // class overloaded with a generic one, `FOO` + `FOO[T]`), or | |
| 618 | // this is a plain value-position reference, collapse to it. | |
| 619 | let for_call = | |
| 620 | if identifier.is_call_target /\ (!bare? \/ bare.is_abstract) then | |
| 621 | group.sole_generic_member() | |
| 622 | else | |
| 623 | null | |
| 624 | fi | |
| 625 | ||
| 626 | if for_call? then | |
| 627 | symbol = for_call | |
| 628 | elif bare? then | |
| 629 | symbol = bare | |
| 630 | else | |
| 631 | let counts = group.generic_arguments_counts |> map(a -> string => "{a}") |> join(" or ") | |
| 632 | _logger.error(identifier.location, "type {identifier.identifier} requires {counts} type arguments") | |
| 633 | identifier.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), identifier.location) | |
| 634 | return | |
| 635 | fi | |
| 636 | fi | |
| 637 | ||
| 638 | if symbol? then | |
| 639 | if symbol.is_type then | |
| 640 | _symbol_use_locations.add_symbol_use(identifier.right_location, symbol) | |
| 641 | ||
| 642 | if symbol.is_unit_variant then | |
| 643 | let lowered = _unit_variant_constructor.try_load(identifier.location, symbol, identifier.expected_type, identifier) | |
| 644 | ||
| 645 | if lowered? then | |
| 646 | // Anchor HOVER and find-references to the | |
| 647 | // specialised init so the rendered owner | |
| 648 | // carries the resolved type arguments — | |
| 649 | // matching the parenthesised path's | |
| 650 | // bookkeeping in resolve_constructor. | |
| 651 | _symbol_use_locations.add_symbol_use(identifier.right_location, lowered.constructor) | |
| 652 | identifier.compile_expressions_state.value = lowered.value | |
| 653 | return | |
| 654 | fi | |
| 655 | fi | |
| 656 | ||
| 657 | let symbol_type = | |
| 658 | if let alias = cast Semantic.Symbols.TYPE_ALIAS?(symbol) /\ alias.argument_count > 0 then | |
| 659 | _bare_generic_alias_type(identifier.location, alias) | |
| 660 | else | |
| 661 | symbol.type | |
| 662 | fi | |
| 663 | ||
| 664 | if symbol_type? then | |
| 665 | identifier.compile_expressions_state.value = TYPE_EXPRESSION(symbol_type, identifier.location) | |
| 666 | else | |
| 667 | identifier.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), identifier.location) | |
| 668 | fi | |
| 669 | else | |
| 670 | // A loop label resolves like any symbol in scope but | |
| 671 | // is not a value; only `break` consumes the name. | |
| 672 | if isa Semantic.Symbols.LABEL(symbol) then | |
| 673 | _logger.error(identifier.location, "{symbol} names a loop label and has no value") | |
| 674 | identifier.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), identifier.location) | |
| 675 | return | |
| 676 | fi | |
| 677 | ||
| 678 | // Bare-identifier (implicit-self) access to a field, including | |
| 679 | // an inherited underscore field, is subject to the same policy | |
| 680 | // as explicit member access. Log but allow: the field is | |
| 681 | // assembly-reachable, so blocking would only cascade. | |
| 682 | if symbol.is_field /\ !symbol.is_accessible_to(_symbol_table.current_accessor) then | |
| 683 | _logger.error(identifier.location, "{symbol} is not accessible here") | |
| 684 | fi | |
| 685 | ||
| 686 | _symbol_loader.find_symbol = (name: string) => _visitor.find(name) | |
| 687 | ||
| 688 | // The function's own name, read from inside its own | |
| 689 | // body. Serve it before the ordinary load, which would | |
| 690 | // report the local as undefined - it is, and the | |
| 691 | // recurse field is what holds the function there. | |
| 692 | if !need_store then | |
| 693 | if let recurse = _visitor.try_load_self_reference(identifier.location, symbol) then | |
| 694 | identifier.compile_expressions_state.value = recurse | |
| 695 | ||
| 696 | _symbol_use_locations.add_symbol_use(identifier.right_location, symbol) | |
| 697 | ||
| 698 | return | |
| 699 | fi | |
| 700 | fi | |
| 701 | ||
| 702 | if _reject_struct_literal_self_read(identifier, symbol) then | |
| 703 | identifier.compile_expressions_state.value = | |
| 704 | DUMMY(Semantic.Types.ERROR(), identifier.location) | |
| 705 | ||
| 706 | return | |
| 707 | fi | |
| 708 | ||
| 709 | if need_store then | |
| 710 | let store_value = cast Need.STORE?(identifier.value)!.value | |
| 711 | identifier.compile_expressions_state.value = symbol.store(identifier.location, null, store_value, _symbol_loader, false) | |
| 712 | ||
| 713 | // Optional-narrowing, write side: a | |
| 714 | // non-optional right-hand side leaves a | |
| 715 | // declared-optional target known to hold a | |
| 716 | // value, so HOVER on the target describes | |
| 717 | // the state the assignment leaves behind — | |
| 718 | // agreeing with what a subsequent read | |
| 719 | // reports rather than with the declared | |
| 720 | // type. Gated on the same target shape the | |
| 721 | // flow analysis narrows, so hover never | |
| 722 | // claims a narrow that isn't applied. | |
| 723 | let narrowed: Semantic.Types.Type? mut = null | |
| 724 | ||
| 725 | let target = _visitor.try_get_narrowing_target(identifier) | |
| 726 | ||
| 727 | if target? /\ _visitor.is_non_optional_value(store_value) then | |
| 728 | let declared = _flow.declared_type_of(target) | |
| 729 | ||
| 730 | if declared? /\ declared.is_optional then | |
| 731 | let inner = declared.optional_inner_type | |
| 732 | ||
| 733 | if inner? /\ !inner.is_optional then | |
| 734 | narrowed = inner | |
| 735 | fi | |
| 736 | fi | |
| 737 | fi | |
| 738 | ||
| 739 | if narrowed? then | |
| 740 | _symbol_use_locations.add_variable_use(identifier.right_location, symbol, narrowed) | |
| 741 | else | |
| 742 | _symbol_use_locations.add_symbol_use(identifier.right_location, symbol) | |
| 743 | fi | |
| 744 | else | |
| 745 | identifier.compile_expressions_state.value = | |
| 746 | _function_reference_resolver.try_load(identifier.location, symbol, null, identifier.expected_type, identifier.is_call_target) ?? | |
| 747 | symbol.load(identifier.location, null, _symbol_loader) | |
| 748 | ||
| 749 | // Optional-narrowing: when flow analysis has | |
| 750 | // established this variable is non-null here, | |
| 751 | // surface the use as the non-optional type. | |
| 752 | // Reference `T?` is a NAMED-with-flag — IL | |
| 753 | // representation matches `T`, so the load gets | |
| 754 | // a type-view snapshot (mirrors how `isa`- | |
| 755 | // narrowing reaches the load via a mutated | |
| 756 | // `symbol.type`). The value-type lowerings | |
| 757 | // (`NULLABLE[T]` / `MAYBE[T]`) are wrapper | |
| 758 | // structs, so the load gets wrapped in | |
| 759 | // `NARROW_PROJECT` — emits `.value` at gen | |
| 760 | // time, but `visit_has_value` and | |
| 761 | // `visit_unwrap` can peel back to the | |
| 762 | // underlying wrapper for their own IR. | |
| 763 | let loaded = identifier.value | |
| 764 | ||
| 765 | // the load's snapshot type is null for an inference placeholder | |
| 766 | @suppress("presence-test-non-optional") | |
| 767 | if | |
| 768 | isa Semantic.Symbols.Variable(symbol) /\ | |
| 769 | loaded? /\ | |
| 770 | loaded.type? /\ | |
| 771 | loaded.type.is_optional | |
| 772 | then | |
| 773 | let variable = symbol | |
| 774 | ||
| 775 | if _flow.is_non_null(variable) then | |
| 776 | let loaded_type = loaded.type | |
| 777 | let narrowed = loaded_type.as_non_optional() | |
| 778 | ||
| 779 | if isa IR.Values.Load.SYMBOL(loaded) then | |
| 780 | if !narrowed.is_optional then | |
| 781 | (cast IR.Values.Load.SYMBOL(loaded)).narrow_snapshot_type(narrowed) | |
| 782 | elif loaded_type.is_value_type then | |
| 783 | let value_member = loaded_type.find_member("value") | |
| 784 | ||
| 785 | if value_member? then | |
| 786 | let projected = value_member.load(identifier.location, loaded, _symbol_loader) | |
| 787 | ||
| 788 | identifier.compile_expressions_state.value = IR.Values.NARROW_PROJECT(loaded, projected) | |
| 789 | fi | |
| 790 | fi | |
| 791 | else | |
| 792 | // A captured, reassigned local loads | |
| 793 | // through its box's `value` getter | |
| 794 | // rather than as a symbol load, so | |
| 795 | // the presence fact is applied as a | |
| 796 | // view over the plumbing call - the | |
| 797 | // same shape a narrowed property | |
| 798 | // read takes. | |
| 799 | if !narrowed.is_optional then | |
| 800 | identifier.compile_expressions_state.value = IR.Values.NARROW_VIEW(loaded, narrowed) | |
| 801 | elif loaded_type.is_value_type then | |
| 802 | let value_member = loaded_type.find_member("value") | |
| 803 | ||
| 804 | if value_member? then | |
| 805 | let projected = value_member.load(identifier.location, loaded, _symbol_loader) | |
| 806 | ||
| 807 | identifier.compile_expressions_state.value = IR.Values.NARROW_PROJECT(loaded, projected) | |
| 808 | fi | |
| 809 | fi | |
| 810 | fi | |
| 811 | fi | |
| 812 | elif let variable: Semantic.Symbols.Variable = symbol, load: IR.Values.Load.SYMBOL = loaded then | |
| 813 | _project_narrowed_carrier(identifier, variable, load) | |
| 814 | elif | |
| 815 | isa Semantic.Symbols.Property(symbol) /\ | |
| 816 | loaded? /\ | |
| 817 | _visitor.try_get_narrowing_target(identifier) == symbol | |
| 818 | then | |
| 819 | // Same presence fact, read through a | |
| 820 | // property rather than a field. The getter | |
| 821 | // call is not a symbol load, so it takes a | |
| 822 | // view wrapper instead of a snapshot — | |
| 823 | // matching how an explicit `receiver.prop` | |
| 824 | // is narrowed in visit_member. Gated on the | |
| 825 | // property being the flow analysis's own | |
| 826 | // narrowing target, so this reads the fact | |
| 827 | // exactly where the target rule recorded | |
| 828 | // one. The fact test sits in its own | |
| 829 | // condition so the narrowing-target query | |
| 830 | // above does not cross the fact the copy | |
| 831 | // below leans on. | |
| 832 | if | |
| 833 | loaded.type? /\ | |
| 834 | loaded.type.is_optional /\ | |
| 835 | _flow.is_non_null(symbol) | |
| 836 | then | |
| 837 | let loaded_type = loaded.type | |
| 838 | let narrowed = loaded_type.as_non_optional() | |
| 839 | ||
| 840 | if !narrowed.is_optional then | |
| 841 | identifier.compile_expressions_state.value = IR.Values.NARROW_VIEW(loaded, narrowed) | |
| 842 | elif loaded_type.is_value_type then | |
| 843 | let value_member = loaded_type.find_member("value") | |
| 844 | ||
| 845 | if value_member? then | |
| 846 | let projected = value_member.load(identifier.location, loaded, _symbol_loader) | |
| 847 | ||
| 848 | identifier.compile_expressions_state.value = IR.Values.NARROW_PROJECT(loaded, projected) | |
| 849 | fi | |
| 850 | fi | |
| 851 | fi | |
| 852 | fi | |
| 853 | ||
| 854 | // Record the fact load for the kill ledger, | |
| 855 | // so the re-ask after the next solve covers it. | |
| 856 | // Locals are filtered inside the recorder. | |
| 857 | _flow.record_load_of_symbol(identifier.location, symbol) | |
| 858 | ||
| 859 | // HOVER reads a narrowed local's type off | |
| 860 | // this use-site node, not the symbol — the | |
| 861 | // symbol's `type` is restored after the walk. | |
| 862 | _symbol_use_locations.add_variable_use(identifier.right_location, symbol, identifier.value!) | |
| 863 | ||
| 864 | // Definite assignment: reading a tracked | |
| 865 | // deferred-init local before it is assigned | |
| 866 | // on every path reaching here. An address-of | |
| 867 | // operand of `ref` is not a value read — its | |
| 868 | // read-or-write is decided by the resolved call. | |
| 869 | if | |
| 870 | !_build_flags.no_warn_definite_assignment /\ | |
| 871 | isa Semantic.Symbols.Variable(symbol) /\ | |
| 872 | !_visitor.is_reference_operand_target(symbol) | |
| 873 | then | |
| 874 | let variable = symbol | |
| 875 | ||
| 876 | if _flow.is_tracked(variable) /\ !_flow.is_assigned(variable) then | |
| 877 | _logger.warn(identifier.location, "definite-assignment", "{variable.name} may be used before it is assigned", variable.location, "variable declared here") | |
| 878 | fi | |
| 879 | fi | |
| 880 | fi | |
| 881 | fi | |
| 882 | ||
| 883 | else | |
| 884 | identifier.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), identifier.location) | |
| 885 | fi | |
| 886 | si | |
| 887 | ||
| 888 | visit_has_value(has_value: Trees.Expressions.HAS_VALUE) is | |
| 889 | ||
| 890 | if !has_value.left.value? then | |
| 891 | _logger.poison(has_value.left.location, "has value expression has no value") | |
| 892 | has_value.compile_expressions_state.value = DUMMY(_innate_symbol_lookup.get_bool_type(), has_value.location) | |
| 893 | return | |
| 894 | fi | |
| 895 | ||
| 896 | if | |
| 897 | !has_value.left.value.check_is_consumable(_logger, has_value.left.location) | |
| 898 | then | |
| 899 | _logger.error(has_value.left.location, "cannot use this here") | |
| 900 | has_value.compile_expressions_state.value = DUMMY(_innate_symbol_lookup.get_bool_type(), has_value.location) | |
| 901 | return | |
| 902 | fi | |
| 903 | ||
| 904 | // Peel a flow-narrowing projection back to its wrapper so | |
| 905 | // `has_value` lookup hits the wrapper's bool field. | |
| 906 | has_value.left.compile_expressions_state.value = IR.Values.NARROW_PROJECT.peel(has_value.left.value!) | |
| 907 | ||
| 908 | let left_value = has_value.left.value! | |
| 909 | let bool_type = _innate_symbol_lookup.get_bool_type() | |
| 910 | ||
| 911 | // Union `u?` → `isa Default(u)`. Both source-defined | |
| 912 | // and cross-asm unions carry the default variant on the | |
| 913 | // symbol; cross-asm unions get it via the | |
| 914 | // `DEFAULT_VARIANT_ATTRIBUTE` marker read in | |
| 915 | // `SYMBOL_FACTORY.materialize_variant`. A union without | |
| 916 | // a default variant short-circuits to a bare null check | |
| 917 | // — `value`/`has_value` are no longer the convention | |
| 918 | // for unions. | |
| 919 | if !left_value.type!.is_value_type then | |
| 920 | let classy = _condition_analyzer.get_classy_for_narrowing(left_value.type) | |
| 921 | ||
| 922 | if classy? /\ (classy.is_union \/ classy.is_variant) then | |
| 923 | let default_variant = _get_default_variant_for_operand(classy) | |
| 924 | ||
| 925 | if default_variant? then | |
| 926 | has_value.compile_expressions_state.value = | |
| 927 | _build_default_variant_isa( | |
| 928 | has_value.location, | |
| 929 | left_value, | |
| 930 | default_variant, | |
| 931 | bool_type | |
| 932 | ) | |
| 933 | else | |
| 934 | has_value.compile_expressions_state.value = HAS_VALUE(left_value, bool_type) | |
| 935 | fi | |
| 936 | ||
| 937 | return | |
| 938 | fi | |
| 939 | fi | |
| 940 | ||
| 941 | let has_value_member = | |
| 942 | left_value.type!.find_member("has_value") | |
| 943 | let has_value_valid mut = false | |
| 944 | ||
| 945 | if has_value_member? then | |
| 946 | if !isa Semantic.Symbols.Property(has_value_member) then | |
| 947 | _logger.error( | |
| 948 | has_value.left.location, | |
| 949 | "has_value member must be a bool property" | |
| 950 | ) | |
| 951 | elif !has_value_member.type!.matches(bool_type) then | |
| 952 | _logger.error( | |
| 953 | has_value.left.location, | |
| 954 | "has_value property must be bool" | |
| 955 | ) | |
| 956 | else | |
| 957 | has_value_valid = true | |
| 958 | fi | |
| 959 | fi | |
| 960 | ||
| 961 | ||
| 962 | let target = _visitor.try_get_narrowing_target(has_value.left) | |
| 963 | ||
| 964 | if left_value.type!.is_optional then | |
| 965 | // the test that itself proves presence is not | |
| 966 | // redundant; one on an operand already proven to | |
| 967 | // hold a value is | |
| 968 | if target? /\ _flow.is_non_null(target) /\ _flow.symbol_fact_validated(target) then | |
| 969 | _logger.warn( | |
| 970 | has_value.location, | |
| 971 | "redundant-presence-test", | |
| 972 | "'?' is redundant here" | |
| 973 | ) | |
| 974 | fi | |
| 975 | elif !has_value_member? then | |
| 976 | // no live has_value layer to consult: a non-optional | |
| 977 | // operand narrowed from optional gets the flow-specific | |
| 978 | // "redundant here"; a never-optional value type is an | |
| 979 | // error; a never-optional reference is redundant by its | |
| 980 | // static type and gets the plain "redundant" - the type | |
| 981 | // guarantees presence everywhere, not just here. | |
| 982 | let declared = if target? then _flow.declared_type_of(target) else null fi | |
| 983 | ||
| 984 | if declared? /\ declared.is_optional then | |
| 985 | if !target? \/ _flow.symbol_fact_validated(target) then | |
| 986 | _logger.warn( | |
| 987 | has_value.location, | |
| 988 | "redundant-presence-test", | |
| 989 | "'?' is redundant here" | |
| 990 | ) | |
| 991 | fi | |
| 992 | elif left_value.type!.is_value_type then | |
| 993 | _logger.error(has_value.left.location, "not optional") | |
| 994 | has_value.compile_expressions_state.value = DUMMY(bool_type, has_value.location) | |
| 995 | return | |
| 996 | elif assert_condition_depth == 0 /\ !left_value.type!.is_inferred then | |
| 997 | // A member path reads as non-optional here when a | |
| 998 | // presence fact is applied to the view; a re-test | |
| 999 | // of one whose getters are unproven or whose | |
| 1000 | // crossings are undischarged is the very | |
| 1001 | // re-establishment the reliance judge asks for, | |
| 1002 | // never redundant. | |
| 1003 | let operand_path = | |
| 1004 | if isa Trees.Expressions.MEMBER(has_value.left) then | |
| 1005 | _visitor.try_build_access_path(cast Trees.Expressions.MEMBER(has_value.left)) | |
| 1006 | else | |
| 1007 | null | |
| 1008 | fi | |
| 1009 | ||
| 1010 | if !operand_path? \/ _flow.path_fact_validated(operand_path) then | |
| 1011 | _logger.warn( | |
| 1012 | has_value.location, | |
| 1013 | "presence-test-non-optional", | |
| 1014 | "'?' is redundant" | |
| 1015 | ) | |
| 1016 | fi | |
| 1017 | fi | |
| 1018 | fi | |
| 1019 | ||
| 1020 | if !left_value.type!.is_value_type then | |
| 1021 | has_value.compile_expressions_state.value = | |
| 1022 | get_reference_has_value( | |
| 1023 | has_value.location, | |
| 1024 | left_value, | |
| 1025 | if has_value_valid then has_value_member else null fi, | |
| 1026 | bool_type | |
| 1027 | ) | |
| 1028 | elif has_value_member? then | |
| 1029 | if has_value_valid then | |
| 1030 | has_value.compile_expressions_state.value = | |
| 1031 | has_value_member.load( | |
| 1032 | has_value.location, | |
| 1033 | left_value, | |
| 1034 | _symbol_loader | |
| 1035 | ) | |
| 1036 | else | |
| 1037 | has_value.compile_expressions_state.value = DUMMY(bool_type, has_value.location) | |
| 1038 | fi | |
| 1039 | fi | |
| 1040 | si | |
| 1041 | ||
| 1042 | visit_unwrap(unwrap: Trees.Expressions.UNWRAP) is | |
| 1043 | ||
| 1044 | let need_store = unwrap.value? /\ isa Need.STORE(unwrap.value) | |
| 1045 | ||
| 1046 | // Peel a flow-narrowing projection back to its wrapper so | |
| 1047 | // `!` runs against the optional shape it was designed for | |
| 1048 | // (avoids "no value member" on the already-projected `T`). | |
| 1049 | unwrap.left.compile_expressions_state.value = IR.Values.NARROW_PROJECT.peel(unwrap.left.value) | |
| 1050 | ||
| 1051 | let unwrap_left_value = unwrap.left.value | |
| 1052 | ||
| 1053 | if !unwrap_left_value? \/ !unwrap_left_value.type? then | |
| 1054 | _logger.poison(unwrap.left.location, "unwrap expression has no value") | |
| 1055 | unwrap.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), unwrap.location) | |
| 1056 | return | |
| 1057 | fi | |
| 1058 | ||
| 1059 | if | |
| 1060 | !unwrap_left_value.check_is_consumable(_logger, unwrap.left.location) | |
| 1061 | then | |
| 1062 | _logger.error(unwrap.left.location, "cannot use this here") | |
| 1063 | unwrap.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), unwrap.location) | |
| 1064 | return | |
| 1065 | fi | |
| 1066 | ||
| 1067 | if unwrap_left_value.type!.is_ref then | |
| 1068 | // is_ref implies element type per the runtime invariant | |
| 1069 | let element_type = unwrap_left_value.type!.get_element_type()! | |
| 1070 | ||
| 1071 | if need_store then | |
| 1072 | let store_value = cast Need.STORE?(unwrap.value)!.value | |
| 1073 | unwrap.compile_expressions_state.value = IR.Values.Store.INDIRECT(unwrap_left_value, store_value, element_type) | |
| 1074 | else | |
| 1075 | unwrap.compile_expressions_state.value = DEREF(unwrap_left_value, element_type) | |
| 1076 | fi | |
| 1077 | ||
| 1078 | return | |
| 1079 | fi | |
| 1080 | ||
| 1081 | let value_member = unwrap_left_value.type!.find_member("value") | |
| 1082 | ||
| 1083 | if !unwrap_left_value.type!.is_value_type then | |
| 1084 | let classy = _condition_analyzer.get_classy_for_narrowing(unwrap_left_value.type) | |
| 1085 | let default_variant = _get_default_variant_for_operand(classy) | |
| 1086 | ||
| 1087 | if classy? /\ default_variant? then | |
| 1088 | // Union `u!` → cast to the default variant (no | |
| 1089 | // IL cast when the operand is already narrowed | |
| 1090 | // to that variant), with single-field projection | |
| 1091 | // so the canonical "unwrap to the sole field" | |
| 1092 | // rule is preserved. | |
| 1093 | unwrap.compile_expressions_state.value = | |
| 1094 | _build_default_variant_unwrap( | |
| 1095 | unwrap.location, | |
| 1096 | unwrap_left_value, | |
| 1097 | classy, | |
| 1098 | default_variant | |
| 1099 | ) | |
| 1100 | else | |
| 1101 | // a plain reference type: `!` asserts non-null. The | |
| 1102 | // result type drops the `?` so an inferred `let` | |
| 1103 | // binding or a non-optional slot downstream sees a | |
| 1104 | // present value rather than a may-be-null one. A | |
| 1105 | // reference `T?` is already just `T` at IL, so `!` | |
| 1106 | // has no dereference of its own to force a throw | |
| 1107 | // with — wrap it so an absent value throws here | |
| 1108 | // rather than silently reaching wherever the | |
| 1109 | // now-non-optional value is next read. | |
| 1110 | let left_value = unwrap_left_value | |
| 1111 | ||
| 1112 | if left_value.type!.is_optional then | |
| 1113 | let checked_value = REFERENCE_NULL_CHECK(left_value, left_value.type!) | |
| 1114 | unwrap.compile_expressions_state.value = TYPE_WRAPPER(left_value.type!.as_non_optional(), checked_value) | |
| 1115 | elif value_member? /\ left_value.type!.find_member("has_value")? then | |
| 1116 | // an option-shaped reference type: flow | |
| 1117 | // analysis does not track its has_value | |
| 1118 | // layer, so `!` re-asserts it and is never | |
| 1119 | // redundant. | |
| 1120 | unwrap.compile_expressions_state.value = left_value | |
| 1121 | else | |
| 1122 | let target = _visitor.try_get_narrowing_target(unwrap.left) | |
| 1123 | let declared = if target? then _flow.declared_type_of(target) else null fi | |
| 1124 | ||
| 1125 | // A NARROW_VIEW is a member-access path read | |
| 1126 | // at its non-optional type because a path | |
| 1127 | // fact holds here — the declared member is | |
| 1128 | // optional, so the unwrap is redundant, not | |
| 1129 | // an error. A load whose receiver chain | |
| 1130 | // contains a NARROW_VIEW is the same case one | |
| 1131 | // hop deeper: the load resolved a member on a | |
| 1132 | // narrowed receiver, so its result type | |
| 1133 | // reflects the narrow rather than the | |
| 1134 | // declared receiver type. | |
| 1135 | if declared? /\ declared.is_optional \/ isa IR.Values.NARROW_VIEW(left_value) \/ _has_narrowed_receiver(unwrap.left) then | |
| 1136 | // non-optional only because flow analysis | |
| 1137 | // has narrowed it at this point; an `!` | |
| 1138 | // covering an undischarged crossing is | |
| 1139 | // load-bearing, not redundant | |
| 1140 | if !_build_flags.no_warn_redundant_unwrap /\ _unwrap_fact_validated(unwrap.left) then | |
| 1141 | _logger.warn( | |
| 1142 | unwrap.location, | |
| 1143 | "redundant-unwrap", | |
| 1144 | "'!' is redundant here" | |
| 1145 | ) | |
| 1146 | elif NARROWING_STUDY.enabled then | |
| 1147 | // Study output: an `!` kept quiet | |
| 1148 | // because its fact has an undischarged | |
| 1149 | // crossing. The set of these is the | |
| 1150 | // ceremony the analysis still requires; | |
| 1151 | // grep the tag when measuring it. | |
| 1152 | IO.Std.error.write_line("LOAD-BEARING-UNWRAP {unwrap.location}") | |
| 1153 | fi | |
| 1154 | ||
| 1155 | unwrap.compile_expressions_state.value = left_value | |
| 1156 | else | |
| 1157 | _logger.error(unwrap.left.location, "cannot unwrap this") | |
| 1158 | unwrap.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), unwrap.location) | |
| 1159 | fi | |
| 1160 | fi | |
| 1161 | fi | |
| 1162 | elif value_member? then | |
| 1163 | // The `value` getter this loads (Nullable[T].Value, | |
| 1164 | // MAYBE[T].value) already throws when absent, so `!` | |
| 1165 | // here needs no explicit has-value check of its own. | |
| 1166 | if | |
| 1167 | !_build_flags.no_warn_redundant_unwrap /\ | |
| 1168 | unwrap_left_value.type!.is_optional | |
| 1169 | then | |
| 1170 | let unwrap_target = _visitor.try_get_narrowing_target(unwrap.left) | |
| 1171 | ||
| 1172 | if unwrap_target? /\ _flow.is_non_null(unwrap_target) /\ _flow.symbol_fact_validated(unwrap_target) then | |
| 1173 | _logger.warn( | |
| 1174 | unwrap.location, | |
| 1175 | "redundant-unwrap", | |
| 1176 | "'!' is redundant here" | |
| 1177 | ) | |
| 1178 | fi | |
| 1179 | fi | |
| 1180 | ||
| 1181 | unwrap.compile_expressions_state.value = value_member.load(unwrap.location, unwrap_left_value, _symbol_loader) | |
| 1182 | else | |
| 1183 | _logger.error(unwrap.left.location, "cannot unwrap this") | |
| 1184 | unwrap.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), unwrap.location) | |
| 1185 | fi | |
| 1186 | ||
| 1187 | // `x!` asserts `x` holds a value — record it, so a | |
| 1188 | // subsequent dereference of `x` is not flagged. Skipped | |
| 1189 | // when the target's current type is already non-optional | |
| 1190 | // (the redundant-unwrap warning fires separately at that | |
| 1191 | // site; recording the presence bit only grows every | |
| 1192 | // downstream env for nothing). | |
| 1193 | let target = _visitor.try_get_narrowing_target(unwrap.left) | |
| 1194 | let target_type = if target? then target.type else null fi | |
| 1195 | ||
| 1196 | if target? /\ target_type? /\ target_type.is_optional then | |
| 1197 | _flow.mark_non_null(target) | |
| 1198 | _flow.report_narrowing_site( | |
| 1199 | unwrap.location, | |
| 1200 | "narrowing-unwrap", | |
| 1201 | "►", | |
| 1202 | INLAY_TYPE.render(target_type.as_non_optional()) | |
| 1203 | ) | |
| 1204 | fi | |
| 1205 | ||
| 1206 | // TODO, we could handle ref and ptr here as well | |
| 1207 | si | |
| 1208 | ||
| 1209 | visit_reference(reference: Trees.Expressions.REFERENCE) is | |
| 1210 | let left_value = reference.left.value | |
| 1211 | ||
| 1212 | if !left_value? \/ !left_value.type? then | |
| 1213 | _logger.poison(reference.left.location, "reference expression has no value") | |
| 1214 | reference.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), reference.location) | |
| 1215 | return | |
| 1216 | elif let element_address = _array_element_address(reference, left_value) then | |
| 1217 | left_value.check_is_consumable(_logger, reference.left.location) | |
| 1218 | ||
| 1219 | reference.compile_expressions_state.value = element_address | |
| 1220 | return | |
| 1221 | elif !left_value.has_address then | |
| 1222 | _logger.warn(reference.left.location, "reference-to-expression", "reference to an expression") | |
| 1223 | fi | |
| 1224 | ||
| 1225 | left_value.check_is_consumable(_logger, reference.left.location) | |
| 1226 | ||
| 1227 | reference.compile_expressions_state.value = ADDRESS(left_value, _innate_symbol_lookup.get_reference_type(left_value.type!)) | |
| 1228 | si | |
| 1229 | ||
| 1230 | // An element of an array is addressable, though reading it goes | |
| 1231 | // through the indexer and yields a copy, so a `ref` to one takes | |
| 1232 | // the element's address directly. | |
| 1233 | _array_element_address(reference: Trees.Expressions.REFERENCE, element: Value) -> Value? is | |
| 1234 | if let index: Trees.Expressions.INDEX = reference.left, array = index.left.value, position = index.index.value, position_type = position.type then | |
| 1235 | if isa Semantic.Types.ARRAY(array.type) /\ _innate_symbol_lookup.get_int_type().compare(position_type) == Semantic.Types.MATCH.SAME then | |
| 1236 | return ARRAY_ELEMENT_ADDRESS(array, position, element.type!, _innate_symbol_lookup.get_reference_type(element.type!)) | |
| 1237 | fi | |
| 1238 | fi | |
| 1239 | ||
| 1240 | return null | |
| 1241 | si | |
| 1242 | ||
| 1243 | get_reference_has_value( | |
| 1244 | location: LOCATION, | |
| 1245 | value: Value, | |
| 1246 | member: Semantic.Symbols.Symbol?, | |
| 1247 | bool_type: Type | |
| 1248 | ) -> Value is | |
| 1249 | let null_check = HAS_VALUE(value, bool_type) | |
| 1250 | ||
| 1251 | if member? then | |
| 1252 | let member_check = member.load(location, value, _symbol_loader) | |
| 1253 | ||
| 1254 | let and_identifier = Trees.Identifiers.Identifier(location, "/\\") | |
| 1255 | let and_symbol = _visitor.find(and_identifier) | |
| 1256 | ||
| 1257 | if and_symbol? /\ isa Semantic.Symbols.FUNCTION_GROUP(and_symbol) then | |
| 1258 | _symbol_loader.find_symbol = (name: string) => _visitor.find(name) | |
| 1259 | ||
| 1260 | let argument_types = Collections.LIST[Type]() | |
| 1261 | argument_types.add(bool_type) | |
| 1262 | argument_types.add(bool_type) | |
| 1263 | ||
| 1264 | let overload_result = | |
| 1265 | _overload_resolver.resolve( | |
| 1266 | location, | |
| 1267 | and_symbol, | |
| 1268 | argument_types, | |
| 1269 | false, | |
| 1270 | false, | |
| 1271 | false | |
| 1272 | ) | |
| 1273 | ||
| 1274 | if overload_result? then | |
| 1275 | let function = overload_result.function | |
| 1276 | if function.is_unsafe_constraints then | |
| 1277 | _logger.warn(location, "unchecked-constraints", "call to {function} has unchecked constraints") | |
| 1278 | fi | |
| 1279 | ||
| 1280 | let arguments = Collections.LIST[Value]() | |
| 1281 | arguments.add(null_check) | |
| 1282 | arguments.add(member_check) | |
| 1283 | ||
| 1284 | return function.call( | |
| 1285 | location, | |
| 1286 | null, | |
| 1287 | arguments, | |
| 1288 | bool_type, | |
| 1289 | _function_caller | |
| 1290 | ) | |
| 1291 | fi | |
| 1292 | fi | |
| 1293 | fi | |
| 1294 | ||
| 1295 | return null_check | |
| 1296 | si | |
| 1297 | ||
| 1298 | // For a `?` / `!` operand, identify the default variant to | |
| 1299 | // dispatch against. Returns the union's `default_variant` | |
| 1300 | // when the operand is the union (null when the union has | |
| 1301 | // no default — a source union without one, or an older | |
| 1302 | // cross-asm union compiled before the | |
| 1303 | // `DEFAULT_VARIANT_ATTRIBUTE` marker existed); returns the | |
| 1304 | // variant itself when the operand is already narrowed to | |
| 1305 | // its parent union's default variant so the caller can | |
| 1306 | // skip an extra cast. Null for any other shape — the | |
| 1307 | // caller's branch then short-circuits `?` to a bare null | |
| 1308 | // check and `!` to a non-null assert on the operand. | |
| 1309 | _get_default_variant_for_operand( | |
| 1310 | classy: Semantic.Symbols.Classy? | |
| 1311 | ) -> Semantic.Symbols.VARIANT? is | |
| 1312 | if !classy? then | |
| 1313 | return null | |
| 1314 | fi | |
| 1315 | ||
| 1316 | if classy.is_union then | |
| 1317 | let union_classy = cast Semantic.Symbols.UNION?(classy)! | |
| 1318 | return union_classy.default_variant | |
| 1319 | fi | |
| 1320 | ||
| 1321 | if classy.is_variant then | |
| 1322 | let variant_classy = cast Semantic.Symbols.VARIANT?(classy)! | |
| 1323 | ||
| 1324 | if !variant_classy.owner? \/ !isa Semantic.Symbols.UNION(variant_classy.owner) then | |
| 1325 | return null | |
| 1326 | fi | |
| 1327 | ||
| 1328 | let owner_union = cast Semantic.Symbols.UNION(variant_classy.owner) | |
| 1329 | ||
| 1330 | if owner_union.default_variant == variant_classy then | |
| 1331 | return variant_classy | |
| 1332 | fi | |
| 1333 | fi | |
| 1334 | ||
| 1335 | return null | |
| 1336 | si | |
| 1337 | ||
| 1338 | // A variable held in a value-type optional carrier - `Nullable[T]` | |
| 1339 | // or `MAYBE[T]` - still holds the carrier where a type test has | |
| 1340 | // narrowed it: the narrowing reaches the load as its type, but | |
| 1341 | // what is stored is the struct. The payload is projected out and | |
| 1342 | // viewed at the narrowed type, rather than the struct being read | |
| 1343 | // as though it already were one. | |
| 1344 | _project_narrowed_carrier(identifier: Trees.Expressions.IDENTIFIER, variable: Semantic.Symbols.Variable, load: IR.Values.Load.SYMBOL) is | |
| 1345 | let declared = _flow.declared_type_of(variable) | |
| 1346 | let narrowed = load.type | |
| 1347 | ||
| 1348 | if !declared? \/ !narrowed? \/ !declared.is_value_type \/ !declared.is_optional \/ narrowed.is_optional then | |
| 1349 | return | |
| 1350 | fi | |
| 1351 | ||
| 1352 | let inner = declared.optional_inner_type | |
| 1353 | let value_member = declared.find_member("value") | |
| 1354 | ||
| 1355 | if !inner? \/ !value_member? then | |
| 1356 | return | |
| 1357 | fi | |
| 1358 | ||
| 1359 | load.narrow_snapshot_type(declared) | |
| 1360 | ||
| 1361 | let projected: IR.Values.Value mut = value_member.load(identifier.location, load, _symbol_loader) | |
| 1362 | ||
| 1363 | if !narrowed.is_equivalent_to(inner) then | |
| 1364 | projected = IR.Values.CAST(narrowed, projected, false) | |
| 1365 | ||
| 1366 | if narrowed.is_value_type then | |
| 1367 | projected = IR.Values.UNBOX(projected) | |
| 1368 | fi | |
| 1369 | fi | |
| 1370 | ||
| 1371 | identifier.compile_expressions_state.value = IR.Values.NARROW_PROJECT(load, projected) | |
| 1372 | si | |
| 1373 | ||
| 1374 | // `u?` lowers to `isa Default(u)`. The receiver's type | |
| 1375 | // (often a generic instantiation of the union) drives | |
| 1376 | // specialisation of the variant target type so the emitted | |
| 1377 | // `isinst` references a loadable closed-generic class. | |
| 1378 | _build_default_variant_isa( | |
| 1379 | location: LOCATION, | |
| 1380 | operand: Value, | |
| 1381 | default_variant: Semantic.Symbols.VARIANT, | |
| 1382 | bool_type: Type | |
| 1383 | ) -> Value is | |
| 1384 | let variant_type = | |
| 1385 | _condition_analyzer.try_get_variant_type_for_classy( | |
| 1386 | operand.type!, | |
| 1387 | default_variant | |
| 1388 | ) | |
| 1389 | ||
| 1390 | if !variant_type? then | |
| 1391 | return DUMMY(bool_type, location) | |
| 1392 | fi | |
| 1393 | ||
| 1394 | return IR.Values.ISA(bool_type, variant_type, operand) | |
| 1395 | si | |
| 1396 | ||
| 1397 | // `u!` lowers to `cast Default(u)`, with single-field | |
| 1398 | // projection so the existing rule "default variant with one | |
| 1399 | // field unwraps to that field" rides on the new lowering. | |
| 1400 | // When the operand is already narrowed to the default | |
| 1401 | // variant the cast is elided — the CLR accepts loading the | |
| 1402 | // field directly off the variant-shaped receiver. | |
| 1403 | _build_default_variant_unwrap( | |
| 1404 | location: LOCATION, | |
| 1405 | operand: Value, | |
| 1406 | classy: Semantic.Symbols.Classy, | |
| 1407 | default_variant: Semantic.Symbols.VARIANT | |
| 1408 | ) -> Value is | |
| 1409 | let variant_type = | |
| 1410 | _condition_analyzer.try_get_variant_type_for_classy( | |
| 1411 | operand.type!, | |
| 1412 | default_variant | |
| 1413 | ) | |
| 1414 | ||
| 1415 | if !variant_type? then | |
| 1416 | return DUMMY(Semantic.Types.ERROR(), location) | |
| 1417 | fi | |
| 1418 | ||
| 1419 | let variant_value: Value mut = operand | |
| 1420 | ||
| 1421 | // The cast is checked: `u!` on another variant throws here, rather | |
| 1422 | // than leaving a null in a slot whose type says it is the default | |
| 1423 | // variant. | |
| 1424 | if !classy.is_variant then | |
| 1425 | variant_value = IR.Values.CAST(variant_type, operand, true) | |
| 1426 | fi | |
| 1427 | ||
| 1428 | // Single-field unwrap rides on the cast: a default | |
| 1429 | // variant declaring exactly one field — own or inherited | |
| 1430 | // from the union's primary header — projects to that | |
| 1431 | // field. Multi-field defaults return the variant itself | |
| 1432 | // so callers can read every field via member access. | |
| 1433 | if default_variant.field_count == 1 then | |
| 1434 | let field_name = default_variant.get_destructure_member_name(0) | |
| 1435 | ||
| 1436 | if field_name? then | |
| 1437 | let field_member = variant_type.find_member(field_name) | |
| 1438 | ||
| 1439 | if field_member? then | |
| 1440 | return field_member.load(location, variant_value, _symbol_loader) | |
| 1441 | fi | |
| 1442 | fi | |
| 1443 | fi | |
| 1444 | ||
| 1445 | return variant_value | |
| 1446 | si | |
| 1447 | ||
| 1448 | // Build a hinted "narrow further" message when a member | |
| 1449 | // access on a ONE_OF fails to find the member on the root | |
| 1450 | // but every in-set subtype has a same-named member. Returns | |
| 1451 | // null when the hint doesn't apply (receiver isn't a ONE_OF, | |
| 1452 | // or the name is missing on at least one in-set subtype). | |
| 1453 | // Subtype-private fields can't be accessed through ONE_OF | |
| 1454 | // without IL-side subtype-dispatching support — for now we | |
| 1455 | // just direct the user to narrow further. Applies uniformly | |
| 1456 | // to a union's variants and a closed class's subclasses. | |
| 1457 | try_describe_intersection_miss( | |
| 1458 | receiver_type: Type, | |
| 1459 | member_name: string | |
| 1460 | ) -> string? is | |
| 1461 | if !isa Semantic.Types.ONE_OF(receiver_type) then | |
| 1462 | return null | |
| 1463 | fi | |
| 1464 | ||
| 1465 | let one_of = receiver_type | |
| 1466 | ||
| 1467 | let any_subtype mut = false | |
| 1468 | for subtype in one_of.subtypes do | |
| 1469 | if !subtype.find_member(member_name)? then | |
| 1470 | return null | |
| 1471 | fi | |
| 1472 | any_subtype = true | |
| 1473 | od | |
| 1474 | ||
| 1475 | if !any_subtype then | |
| 1476 | return null | |
| 1477 | fi | |
| 1478 | ||
| 1479 | let names = System.Text.StringBuilder() | |
| 1480 | let seen_any mut = false | |
| 1481 | for subtype in one_of.subtypes do | |
| 1482 | if seen_any then | |
| 1483 | names.append(" | ") | |
| 1484 | fi | |
| 1485 | names.append(subtype.name) | |
| 1486 | seen_any = true | |
| 1487 | od | |
| 1488 | ||
| 1489 | return "member {member_name} is ambiguous in {names}" | |
| 1490 | si | |
| 1491 | ||
| 1492 | // Whether the member can only be reached through a receiver. A | |
| 1493 | // method group reports nothing itself, so it is judged by its | |
| 1494 | // overloads: a group holding a static one is reachable through | |
| 1495 | // the type, and only a group that is entirely instance is not. | |
| 1496 | _is_instance_only(symbol: Semantic.Symbols.Symbol) -> bool is | |
| 1497 | if let group: Semantic.Symbols.FUNCTION_GROUP = symbol then | |
| 1498 | return | |
| 1499 | group.functions.count > 0 /\ | |
| 1500 | group.functions |> all(f => f.is_instance) | |
| 1501 | fi | |
| 1502 | ||
| 1503 | return symbol.is_instance | |
| 1504 | si | |
| 1505 | ||
| 1506 | // A function literal written in a struct's instance member cannot | |
| 1507 | // read that struct: a delegate's target is an object, and a | |
| 1508 | // struct's instance member holds self as a managed pointer, so | |
| 1509 | // there is nothing for the literal to bind to. Copying the struct | |
| 1510 | // into the literal would give it a snapshot rather than the | |
| 1511 | // receiver, which is a different program from the one written, so | |
| 1512 | // the read is refused and the author copies what the literal needs | |
| 1513 | // into a local first. | |
| 1514 | _reject_struct_literal_self_read( | |
| 1515 | identifier: Trees.Expressions.IDENTIFIER, | |
| 1516 | symbol: Semantic.Symbols.Symbol | |
| 1517 | ) -> bool is | |
| 1518 | if !_is_instance_only(symbol) then | |
| 1519 | return false | |
| 1520 | fi | |
| 1521 | ||
| 1522 | // The member's own declaring type, rather than the closure's | |
| 1523 | // IL owner: that is only the struct once the literal has been | |
| 1524 | // lowered to a delegate, which has not happened while the body | |
| 1525 | // is still being walked. | |
| 1526 | if isa Semantic.Symbols.Closure(_symbol_table.current_function) then | |
| 1527 | if isa Semantic.Symbols.STRUCT(symbol.owner) then | |
| 1528 | _logger.error( | |
| 1529 | identifier.location, | |
| 1530 | "cannot access instance member {symbol.name} of a struct in a function literal" | |
| 1531 | ) | |
| 1532 | ||
| 1533 | return true | |
| 1534 | fi | |
| 1535 | fi | |
| 1536 | ||
| 1537 | return false | |
| 1538 | si | |
| 1539 | ||
| 1540 | // An indexer's accessors carry the CLR-required names, which are | |
| 1541 | // not a member surface the language offers: an indexer is reached | |
| 1542 | // by indexing. A name that resolves to one is refused rather than | |
| 1543 | // left unfound, so the diagnostic can say what to write. | |
| 1544 | _is_indexer_accessor(symbol: Semantic.Symbols.Symbol) -> bool is | |
| 1545 | if let group: Semantic.Symbols.FUNCTION_GROUP = symbol then | |
| 1546 | return group.functions |> any(f => f.is_indexer_accessor) | |
| 1547 | fi | |
| 1548 | ||
| 1549 | if let function: Semantic.Symbols.Function = symbol then | |
| 1550 | return function.is_indexer_accessor | |
| 1551 | fi | |
| 1552 | ||
| 1553 | return false | |
| 1554 | si | |
| 1555 | ||
| 1556 | // Build the IR value for an `a?.b` access. Returns the | |
| 1557 | // COALESCE_LOAD wrap when the member is a readable field or | |
| 1558 | // property; returns null (so the caller falls back to a plain | |
| 1559 | // access) on shapes that aren't lowered here. Emits a | |
| 1560 | // diagnostic for each unsupported shape so the user learns | |
| 1561 | // why the `?.` had no effect. | |
| 1562 | _build_coalesce_load( | |
| 1563 | member: Trees.Expressions.MEMBER, | |
| 1564 | symbol: Semantic.Symbols.Symbol | |
| 1565 | ) -> Value? is | |
| 1566 | // A method selection is coalesced at the enclosing call - | |
| 1567 | // the whole call, argument evaluation included, must | |
| 1568 | // short-circuit on an absent receiver - so a function | |
| 1569 | // group falls through silently to the plain load the call | |
| 1570 | // path consumes. An uncalled method reference through | |
| 1571 | // `?.` then fails downstream exactly as it does through | |
| 1572 | // `.`. | |
| 1573 | if isa Semantic.Symbols.FUNCTION_GROUP(symbol) then | |
| 1574 | return null | |
| 1575 | fi | |
| 1576 | ||
| 1577 | if | |
| 1578 | !isa Semantic.Symbols.Field(symbol) /\ | |
| 1579 | !isa Semantic.Symbols.Property(symbol) | |
| 1580 | then | |
| 1581 | _logger.error( | |
| 1582 | member.location, | |
| 1583 | "coalescing member access supports fields, properties and method calls only" | |
| 1584 | ) | |
| 1585 | return null | |
| 1586 | fi | |
| 1587 | ||
| 1588 | return build_coalesce_wrap( | |
| 1589 | member, | |
| 1590 | symbol.is_instance, | |
| 1591 | from => symbol.load(member.location, from, _symbol_loader) | |
| 1592 | ) | |
| 1593 | si | |
| 1594 | ||
| 1595 | // Shared lowering for `a?.b` accesses and `a?.m(...)` calls: | |
| 1596 | // receiver presence diagnostics, unwrap of the optional | |
| 1597 | // receiver, optional widening of the result and the | |
| 1598 | // COALESCE_LOAD composition. `build_member_value` produces | |
| 1599 | // the member load or call against the supplied receiver | |
| 1600 | // stand-in: the original receiver value when it is statically | |
| 1601 | // present, or a STACK_TOP of the unwrapped receiver type | |
| 1602 | // inside the short-circuit arm. `receiver_consumed` is false | |
| 1603 | // when the member is static - the present arm then pops the | |
| 1604 | // tested receiver instead of feeding it to the member value. | |
| 1605 | // Returns null, with any diagnostic already emitted, when the | |
| 1606 | // shape can't be lowered. | |
| 1607 | build_coalesce_wrap( | |
| 1608 | member: Trees.Expressions.MEMBER, | |
| 1609 | receiver_consumed: bool, | |
| 1610 | build_member_value: (Value) -> Value? | |
| 1611 | ) -> Value? is | |
| 1612 | build_optional_subject_wrap( | |
| 1613 | member.left, | |
| 1614 | member.location, | |
| 1615 | receiver_consumed, | |
| 1616 | "'?.'", | |
| 1617 | build_member_value | |
| 1618 | ) | |
| 1619 | si | |
| 1620 | ||
| 1621 | // The same lowering for a `~>` thread-first call, whose | |
| 1622 | // subject is tested for presence the way a `?.` receiver is: | |
| 1623 | // absent skips the call - argument evaluation included - and | |
| 1624 | // yields the absent value; present runs it against the | |
| 1625 | // unwrapped subject. | |
| 1626 | build_propagating_subject_wrap( | |
| 1627 | subject: Trees.Expressions.Expression, | |
| 1628 | build_member_value: (Value) -> Value? | |
| 1629 | ) -> Value? is | |
| 1630 | build_optional_subject_wrap( | |
| 1631 | subject, | |
| 1632 | subject.location, | |
| 1633 | true, | |
| 1634 | "'~>'", | |
| 1635 | raw => ( | |
| 1636 | let raw_type = raw.type | |
| 1637 | ||
| 1638 | if !raw_type? then | |
| 1639 | null | |
| 1640 | ||
| 1641 | // Inside the short-circuit arm the stand-in for | |
| 1642 | // the unwrapped subject sits on the evaluation | |
| 1643 | // stack, where only a value built to consume it | |
| 1644 | // first can read it. A call through a stored | |
| 1645 | // function value pushes its callee before its | |
| 1646 | // arguments, so the subject cannot stay where the | |
| 1647 | // wrap left it: spill it to a local and hand the | |
| 1648 | // built value a load of that local instead. | |
| 1649 | elif isa IR.Values.STACK_TOP(raw) then | |
| 1650 | let local = IR.LOCAL(".propagate.{IR.TEMP.get_next_id()}", raw_type) | |
| 1651 | let built = build_member_value(IR.Values.Load.TEMP(local)) | |
| 1652 | ||
| 1653 | if built? then | |
| 1654 | IR.Values.SPILLED_SUBJECT(local, built) | |
| 1655 | else | |
| 1656 | null | |
| 1657 | fi | |
| 1658 | ||
| 1659 | // A statically present subject is an ordinary | |
| 1660 | // value; the built call generates it as its first | |
| 1661 | // argument in the caller's own order. | |
| 1662 | else | |
| 1663 | build_member_value(raw) | |
| 1664 | fi | |
| 1665 | ) | |
| 1666 | ) | |
| 1667 | si | |
| 1668 | ||
| 1669 | // Shared core of the coalescing wraps: an optional-typed | |
| 1670 | // expression - a `?.` receiver or a `~>` subject - tested for | |
| 1671 | // presence, with `build_member_value` producing the value | |
| 1672 | // against the supplied receiver stand-in: the original value | |
| 1673 | // when it is statically present, or a STACK_TOP of the | |
| 1674 | // unwrapped type inside the short-circuit arm. | |
| 1675 | // `receiver_consumed` is false when the present arm pops the | |
| 1676 | // tested value rather than feeding it to the member value. | |
| 1677 | // Returns null, with any diagnostic already emitted, when the | |
| 1678 | // shape can't be lowered. | |
| 1679 | build_optional_subject_wrap( | |
| 1680 | subject: Trees.Expressions.Expression, | |
| 1681 | diagnostic_location: Source.LOCATION, | |
| 1682 | receiver_consumed: bool, | |
| 1683 | spelling: string, | |
| 1684 | build_member_value: (Value) -> Value? | |
| 1685 | ) -> Value? is | |
| 1686 | let receiver = subject.value | |
| 1687 | ||
| 1688 | if !receiver? \/ !receiver.type? then | |
| 1689 | return null | |
| 1690 | fi | |
| 1691 | ||
| 1692 | let receiver_type = receiver.type | |
| 1693 | ||
| 1694 | // A non-optional receiver (declared so, or flow-narrowed | |
| 1695 | // from `T?` to `T`) is statically present, so the null | |
| 1696 | // test can never fail. One flow-narrowed from optional | |
| 1697 | // gets a redundancy warning; a never-optional value type | |
| 1698 | // is an error (a struct is never null, so the test has | |
| 1699 | // no defensive value); a never-optional reference stays | |
| 1700 | // legal with no diagnostic, as the null-defense idiom at | |
| 1701 | // unsound boundaries (reflected APIs, staged tree | |
| 1702 | // construction) where null can arrive despite the static | |
| 1703 | // type. The access is emitted directly — the conditional | |
| 1704 | // path is unsound for a value-type receiver (`dup; | |
| 1705 | // brfalse` rejects a struct on the stack) and unnecessary | |
| 1706 | // work for a reference receiver. The result is still | |
| 1707 | // widened to the optional shape the user asked for with | |
| 1708 | // `?.`. | |
| 1709 | if !receiver_type.is_optional then | |
| 1710 | ||
| 1711 | let target = _visitor.try_get_narrowing_target(subject) | |
| 1712 | let declared = if target? then _flow.declared_type_of(target) else null fi | |
| 1713 | ||
| 1714 | if declared? /\ declared.is_optional then | |
| 1715 | if !target? \/ _flow.symbol_fact_validated(target) then | |
| 1716 | _logger.warn( | |
| 1717 | diagnostic_location, | |
| 1718 | "redundant-coalesce", | |
| 1719 | "{spelling} is redundant here" | |
| 1720 | ) | |
| 1721 | fi | |
| 1722 | elif receiver_type.is_value_type then | |
| 1723 | _logger.error(subject.location, "receiver is not optional") | |
| 1724 | return null | |
| 1725 | fi | |
| 1726 | ||
| 1727 | let direct = build_member_value(receiver) | |
| 1728 | ||
| 1729 | if !direct? then | |
| 1730 | return null | |
| 1731 | fi | |
| 1732 | ||
| 1733 | return widen_coalesce_result(direct) | |
| 1734 | fi | |
| 1735 | ||
| 1736 | let receiver_is_value_optional = receiver_type.is_value_type | |
| 1737 | ||
| 1738 | // The member symbol lives on the receiver's underlying | |
| 1739 | // type regardless of the `?` flag, so the member value is | |
| 1740 | // built against a STACK_TOP of the non-optional type and | |
| 1741 | // emits the expected access or call ops with no preceding | |
| 1742 | // receiver gen. | |
| 1743 | let inner_type = | |
| 1744 | if receiver_is_value_optional then | |
| 1745 | receiver_type.optional_inner_type | |
| 1746 | else | |
| 1747 | receiver_type.as_non_optional() | |
| 1748 | fi | |
| 1749 | ||
| 1750 | if !inner_type? then | |
| 1751 | return null | |
| 1752 | fi | |
| 1753 | ||
| 1754 | // STACK_TOP (has_address=false): the inner-receiver | |
| 1755 | // value is on the stack, not its address. A struct | |
| 1756 | // member access reads the value off the top, and the | |
| 1757 | // existing call_struct_method path's `ADDRESS(from)` | |
| 1758 | // spill takes its address as needed. A STACK_TOP_ADDRESS | |
| 1759 | // here would tell that spill the address is already | |
| 1760 | // present and skip it — producing a `call instance` on a | |
| 1761 | // bare value, which segfaults at the JIT. | |
| 1762 | let stack_top: IR.Values.Value = IR.Values.STACK_TOP(inner_type) | |
| 1763 | ||
| 1764 | let raw = build_member_value(stack_top) | |
| 1765 | ||
| 1766 | if !raw? then | |
| 1767 | return null | |
| 1768 | fi | |
| 1769 | ||
| 1770 | let arm = _widen_coalesce_arm(raw) | |
| 1771 | ||
| 1772 | if !arm? then | |
| 1773 | return null | |
| 1774 | fi | |
| 1775 | ||
| 1776 | if receiver_is_value_optional then | |
| 1777 | // Build presence test (get_has_value) and value | |
| 1778 | // extract (get_value) against a STACK_TOP_ADDRESS so | |
| 1779 | // both consume the address dup'd by COALESCE_LOAD. | |
| 1780 | let address_stand_in = IR.Values.STACK_TOP_ADDRESS(receiver_type) | |
| 1781 | let has_value_member = receiver_type.find_member("has_value") | |
| 1782 | let value_member = receiver_type.find_member("value") | |
| 1783 | ||
| 1784 | if !has_value_member? \/ !value_member? then | |
| 1785 | _logger.poison( | |
| 1786 | diagnostic_location, | |
| 1787 | "coalescing member access receiver {receiver_type} missing has_value or value member" | |
| 1788 | ) | |
| 1789 | return null | |
| 1790 | fi | |
| 1791 | ||
| 1792 | let presence_test = has_value_member.load(diagnostic_location, address_stand_in, _symbol_loader) | |
| 1793 | ||
| 1794 | let value_extract = | |
| 1795 | if receiver_consumed then | |
| 1796 | value_member.load(diagnostic_location, address_stand_in, _symbol_loader) | |
| 1797 | else | |
| 1798 | null | |
| 1799 | fi | |
| 1800 | ||
| 1801 | return IR.Values.COALESCE_LOAD( | |
| 1802 | receiver, | |
| 1803 | presence_test, | |
| 1804 | value_extract, | |
| 1805 | arm.member_value, | |
| 1806 | arm.null_value, | |
| 1807 | arm.result_type, | |
| 1808 | receiver_consumed | |
| 1809 | ) | |
| 1810 | fi | |
| 1811 | ||
| 1812 | return IR.Values.COALESCE_LOAD(receiver, arm.member_value, arm.null_value, arm.result_type, receiver_consumed) | |
| 1813 | si | |
| 1814 | ||
| 1815 | // Re-seat a coalescing member access whose loaded member is | |
| 1816 | // itself then invoked - `a?.f(...)` where f is a | |
| 1817 | // function-typed field or property. The invocation must sit | |
| 1818 | // inside the short-circuit arm, so the original wrap's | |
| 1819 | // receiver plumbing is reused around the call value, which | |
| 1820 | // consumes the original member load from the same arm. | |
| 1821 | rewrap_coalesce_call( | |
| 1822 | original: IR.Values.COALESCE_LOAD, | |
| 1823 | call_value: Value | |
| 1824 | ) -> Value? is | |
| 1825 | let arm = _widen_coalesce_arm(call_value) | |
| 1826 | ||
| 1827 | if !arm? then | |
| 1828 | return null | |
| 1829 | fi | |
| 1830 | ||
| 1831 | if original.receiver_is_value_type then | |
| 1832 | return IR.Values.COALESCE_LOAD( | |
| 1833 | original.receiver, | |
| 1834 | original.presence_test!, | |
| 1835 | original.value_extract, | |
| 1836 | arm.member_value, | |
| 1837 | arm.null_value, | |
| 1838 | arm.result_type, | |
| 1839 | true | |
| 1840 | ) | |
| 1841 | fi | |
| 1842 | ||
| 1843 | return IR.Values.COALESCE_LOAD( | |
| 1844 | original.receiver, | |
| 1845 | arm.member_value, | |
| 1846 | arm.null_value, | |
| 1847 | arm.result_type, | |
| 1848 | true | |
| 1849 | ) | |
| 1850 | si | |
| 1851 | ||
| 1852 | // The direct result of a coalescing access or call whose | |
| 1853 | // receiver is statically present, widened to the optional | |
| 1854 | // shape the `?.` asked for. A void result stays void. | |
| 1855 | widen_coalesce_result(direct: Value) -> Value? is | |
| 1856 | let direct_type = direct.type | |
| 1857 | ||
| 1858 | if !direct_type? then | |
| 1859 | return null | |
| 1860 | fi | |
| 1861 | ||
| 1862 | if direct_type.is_void then | |
| 1863 | return direct | |
| 1864 | fi | |
| 1865 | ||
| 1866 | let direct_result = build_optional_type(direct_type, _innate_symbol_lookup) | |
| 1867 | ||
| 1868 | if !direct_type.is_optional /\ _is_value_optional(direct_result) then | |
| 1869 | return IR.Values.WRAP_OPTIONAL(direct_result, direct) | |
| 1870 | fi | |
| 1871 | ||
| 1872 | // Reference T and T? share IL — re-type the load. | |
| 1873 | if !direct_type.matches(direct_result) then | |
| 1874 | return IR.Values.TYPE_WRAPPER(direct_result, direct) | |
| 1875 | fi | |
| 1876 | ||
| 1877 | return direct | |
| 1878 | si | |
| 1879 | ||
| 1880 | // Widen a present-arm member value to the optional result | |
| 1881 | // shape of the enclosing `?.`: | |
| 1882 | // - void → no widening, no null sentinel. | |
| 1883 | // - reference T → T flagged optional, IL identity. | |
| 1884 | // - value T → NULLABLE[T] via WRAP_OPTIONAL (newobj | |
| 1885 | // Nullable<T>::.ctor(T)). | |
| 1886 | // - type parameter T → T? as its declared kind lowers it, | |
| 1887 | // through WRAP_OPTIONAL where that is | |
| 1888 | // NULLABLE[T] or MAYBE[T]. | |
| 1889 | // - already-optional U → identity. | |
| 1890 | _widen_coalesce_arm(raw: Value) -> COALESCE_ARM? is | |
| 1891 | let raw_type = raw.type | |
| 1892 | ||
| 1893 | if !raw_type? then | |
| 1894 | return null | |
| 1895 | fi | |
| 1896 | ||
| 1897 | if raw_type.is_void then | |
| 1898 | return COALESCE_ARM(raw, null, raw_type) | |
| 1899 | fi | |
| 1900 | ||
| 1901 | let result_type = build_optional_type(raw_type, _innate_symbol_lookup) | |
| 1902 | ||
| 1903 | let member_value: Value = | |
| 1904 | if !raw_type.is_optional /\ _is_value_optional(result_type) then | |
| 1905 | IR.Values.WRAP_OPTIONAL(result_type, raw) | |
| 1906 | else | |
| 1907 | raw | |
| 1908 | fi | |
| 1909 | ||
| 1910 | let null_value = _build_null_sentinel(result_type) | |
| 1911 | ||
| 1912 | if !null_value? then | |
| 1913 | return null | |
| 1914 | fi | |
| 1915 | ||
| 1916 | return COALESCE_ARM(member_value, null_value, result_type) | |
| 1917 | si | |
| 1918 | ||
| 1919 | // True when a widened result is a struct carrier, NULLABLE[T] or | |
| 1920 | // MAYBE[T], that a present value is constructed into. A type | |
| 1921 | // parameter reports is_value_type whatever its kind, so one | |
| 1922 | // left as its own optional form (a `class` parameter flagged | |
| 1923 | // optional, or an `optional` one) is excluded: its present | |
| 1924 | // value is already the result. | |
| 1925 | _is_value_optional(result_type: Type) -> bool => | |
| 1926 | result_type.is_value_type /\ !result_type.is_type_variable | |
| 1927 | ||
| 1928 | // The null-arm sentinel for a coalescing access of result | |
| 1929 | // type `result_type`: `ldnull` for a reference type, a fresh | |
| 1930 | // DEFAULT (which hoists a `.locals init` and pushes the zero | |
| 1931 | // value) for a value-type optional like NULLABLE[T] or | |
| 1932 | // MAYBE[T]. | |
| 1933 | _build_null_sentinel(result_type: Type?) -> Value? is | |
| 1934 | if !result_type? then | |
| 1935 | return null | |
| 1936 | fi | |
| 1937 | ||
| 1938 | if result_type.is_value_type then | |
| 1939 | return IR.Values.DEFAULT(result_type) | |
| 1940 | fi | |
| 1941 | ||
| 1942 | return IR.Values.NULL(result_type) | |
| 1943 | si | |
| 1944 | ||
| 1945 | // The `T?` form of `t`: NULLABLE[T] for a value type, the | |
| 1946 | // flagged reference for a reference type, and for a type | |
| 1947 | // parameter whatever its declared kind lowers `T?` to. | |
| 1948 | // `as_optional()` alone returns the receiver for a value type — | |
| 1949 | // leaving the result mistyped as `T` — so this dispatches the | |
| 1950 | // way `resolve_type_expressions` does for a written `T?`. | |
| 1951 | // Static so the rule can be pinned without standing up a full | |
| 1952 | // COMPILE_ACCESS instance. | |
| 1953 | build_optional_type(t: Type, lookup: Semantic.Lookups.InnateSymbolLookup) -> Type static is | |
| 1954 | if t.is_optional then | |
| 1955 | return t | |
| 1956 | fi | |
| 1957 | ||
| 1958 | if t.is_type_variable then | |
| 1959 | if let lowered = Semantic.TYPE_VARIABLE_OPTIONAL.of(t, lookup) then | |
| 1960 | return lowered | |
| 1961 | fi | |
| 1962 | fi | |
| 1963 | ||
| 1964 | if t.is_value_type then | |
| 1965 | return lookup.get_optional_type(t) | |
| 1966 | fi | |
| 1967 | ||
| 1968 | return t.as_optional() | |
| 1969 | si | |
| 1970 | ||
| 1971 | _bare_generic_alias_type(location: Source.LOCATION, alias: Semantic.Symbols.TYPE_ALIAS) -> Semantic.Types.Type is | |
| 1972 | if let class_type = BARE_GENERIC_ALIAS.class_type(alias) then | |
| 1973 | return class_type | |
| 1974 | fi | |
| 1975 | ||
| 1976 | _logger.error(location, "type alias {alias.name} needs type arguments here") | |
| 1977 | ||
| 1978 | return Semantic.Types.ERROR() | |
| 1979 | si | |
| 1980 | si | |
| 1981 | ||
| 1982 | // The present-arm pieces of a coalescing wrap: the member value | |
| 1983 | // widened to the optional result shape, the null-arm sentinel | |
| 1984 | // (absent when the member value is void) and the result type. | |
| 1985 | class COALESCE_ARM( | |
| 1986 | member_value: Value, | |
| 1987 | null_value: Value?, | |
| 1988 | result_type: Type | |
| 1989 | ) is | |
| 1990 | si | |
| 1991 | si |