diff --git a/dag/gunbc/instruments/native_emission_controls.dag b/dag/gunbc/instruments/native_emission_controls.dag index d04eee387e7..fe9ec9de5e6 100644 --- a/dag/gunbc/instruments/native_emission_controls.dag +++ b/dag/gunbc/instruments/native_emission_controls.dag @@ -2,6 +2,7 @@ module gunbc.instruments.native_emission_controls import std.types { Bool, Int, List, Set, String } import v2.std.algebra { filter } +import std.list { duplicated_by_key } import gunbc.instruments.native_emission_controls_homonym { ControlBoxed, narrow_slot_value, narrow_slots_equal } import std.resource_contract { Observation, Read } import std.integer { UInt8, UInt8Ready, UInt8OutOfRange, uint8_of_int } @@ -296,6 +297,26 @@ fn claim_driver_filesystem_cases() -> List { ] } +// RULE: an empty-map turbofish whose type argument arrives erased defers to the target's inference +// (v1.compiler.emit_rust rust_empty_map_init_expr, type_node_carries_erased_type_argument), as an +// unbound formal already does. std.list duplicated_by_key builds `DuplicatedScan { firsts: +// empty_map(), .. }` whose field is `Map`; infer leaves that T erased, it rendered as a +// compile_error!, and std.list did not compile. The cases call the real kernel: +// each repeated key's FIRST element once, in the order the keys were found to repeat, and nothing +// for a list without repeats. +fn generic_empty_map_turbofish_cases() -> List { + [ + NativeEmissionCase { + identity: "generic_empty_map_duplicated_by_key_reports_each_repeat_once", + held: duplicated_by_key(xs: ["a", "b", "a", "c", "b", "a"], key: x => x) == ["a", "b"] + }, + NativeEmissionCase { + identity: "generic_empty_map_red_no_repeat_reports_nothing", + held: duplicated_by_key(xs: ["a", "b", "c"], key: x => x) == [] + } + ] +} + // RULE: an effectful fold step is realized as a loop in the enclosing async frame // (v1.compiler.emit_rust emit_rust_effectful_fold_loop). Iterator::fold takes its step as a // synchronous closure, so a step that reaches an effectful callee put that callee's `.await?` inside @@ -437,11 +458,13 @@ data native_emission_expected_identities: List = [ "list_index_red_in_range_is_not_absent", "list_index_past_the_end_is_absent", "list_index_negative_is_absent", - "list_index_into_empty_is_absent" + "list_index_into_empty_is_absent", + "generic_empty_map_duplicated_by_key_reports_each_repeat_once", + "generic_empty_map_red_no_repeat_reports_nothing" ] fn native_emission_cases() -> List { - concat(concat(same_leaf_declaration_cases(), concat(concat(concat(concat(concat(concat(header_bound_cases(), fn_value_bound_cases()), filter_in_branch_condition_cases()), claim_driver_filesystem_cases()), effectful_fold_step_cases()), capability_alias_output_cases()), fold_accumulator_formals_cases())), list_index_cases()) + concat(concat(concat(same_leaf_declaration_cases(), concat(concat(concat(concat(concat(concat(header_bound_cases(), fn_value_bound_cases()), filter_in_branch_condition_cases()), claim_driver_filesystem_cases()), effectful_fold_step_cases()), capability_alias_output_cases()), fold_accumulator_formals_cases())), list_index_cases()), generic_empty_map_turbofish_cases()) } fn case_row(c: NativeEmissionCase) -> String { diff --git a/src/v1/05_emit_rust.dag b/src/v1/05_emit_rust.dag index 6003e8804ac..02d41d0af00 100644 --- a/src/v1/05_emit_rust.dag +++ b/src/v1/05_emit_rust.dag @@ -11618,11 +11618,29 @@ fn rust_empty_map_kv_type_str(map_type: Node, shared_types: Set, source_ // a module-scope `data ...: Map = empty_map()` in a module with no generic scope at // all. Two sites asking one question is the second representation DESIGN section 2 forbids, so the // question is asked once, here, and both sites read the answer. +// AN ERASED TYPE ARGUMENT DECLINES TO THE TARGET'S INFERENCE, EXACTLY AS AN UNBOUND FORMAL DOES. The +// arm above already defers `::<_, _>()` when the emitter cannot name the formals in this fn's scope. +// A key or value whose inferred node is a compiler error is the same state seen one stage earlier: +// v1.compiler.infer does not bind a generic record's parameter at an unannotated record literal +// (std.list duplicated_by_key's `DuplicatedScan { firsts: empty_map(), .. }` inside a fold), so the +// field type arrives erased and rendered as `compile_error!("UNRESOLVED_CompilerError")`. Rendering +// that refusal is what rust_witness_carrier_from_type_node's note forbids -- an erased first source +// must decline so the next authority is consulted -- and here the next authority is rustc, which +// infers T from the field's later `map_insert` or refuses loudly; nothing is widened. The upstream +// boundary stays open and is named, not absorbed: generic record instantiation at infer +// (declared_type_conformance_note's "generic record instantiation" lane). +fn type_node_carries_erased_type_argument(n: Node) -> Bool { + n.children |> any(c => + let t = child_type_node(ch: c) + (t.inferred != none && is_compiler_error(inferred: t.inferred.value)) || type_node_carries_erased_type_argument(n: t)) +} + fn rust_empty_map_init_expr(map_type: Node, shared_types: Set, source_indices: Map, emit_info: EmitGraphInfo) -> String { let kv_type_str = rust_empty_map_kv_type_str(map_type: map_type, shared_types: shared_types, source_indices: source_indices) if kv_type_str == "" { rust_shared_wrap_ctor(inner_expr: "HashMap::new() /* BRIDGE: empty_map value type unresolved */") - } else if rust_fold_rendered_type_has_any_spurious_generic(type_str: concat("<", kv_type_str, ">"), generic_param_names: emit_info.fn_generic_param_names) { + } else if rust_fold_rendered_type_has_any_spurious_generic(type_str: concat("<", kv_type_str, ">"), generic_param_names: emit_info.fn_generic_param_names) + || type_node_carries_erased_type_argument(n: map_type) { "v1_rt::rc_empty_map::<_, _>()" } else { concat("v1_rt::rc_empty_map::<", kv_type_str, ">()") diff --git a/src/v1/stage0/src/v1_compiler_emit_rust.rs b/src/v1/stage0/src/v1_compiler_emit_rust.rs index f2e9fe6fcbf..9e460127f5e 100644 --- a/src/v1/stage0/src/v1_compiler_emit_rust.rs +++ b/src/v1/stage0/src/v1_compiler_emit_rust.rs @@ -24825,6 +24825,24 @@ pub fn rust_empty_map_kv_type_str( } } +pub fn type_node_carries_erased_type_argument(n: Rc) -> bool { + stacker::maybe_grow(512 * 1024, 2 * 1024 * 1024, || { + let mut __found = false; + for c in n.children.clone().iter().cloned() { + if { + let t = crate::v1_compiler_infer_types::child_type_node(c.clone()); + (((t.inferred.clone() != std::option::Option::None) + && crate::v1_std_core::is_compiler_error(t.inferred.clone().clone().unwrap())) + || type_node_carries_erased_type_argument(t.clone())) + } { + __found = true; + break; + } + } + __found + }) +} + pub fn rust_empty_map_init_expr( map_type: Rc, shared_types: Rc>, @@ -24842,13 +24860,14 @@ pub fn rust_empty_map_init_expr( "HashMap::new() /* BRIDGE: empty_map value type unresolved */".to_string(), ) } else { - if rust_fold_rendered_type_has_any_spurious_generic( + if (rust_fold_rendered_type_has_any_spurious_generic( v1_rt::concat( v1_rt::concat("<".to_string(), kv_type_str.clone()), ">".to_string(), ), emit_info.fn_generic_param_names.clone(), - ) { + ) || type_node_carries_erased_type_argument(map_type.clone())) + { "v1_rt::rc_empty_map::<_, _>()".to_string() } else { v1_rt::concat(