Skip to content
24 changes: 23 additions & 1 deletion dag/gunbc/instruments/native_emission_controls.dag
Original file line number Diff line number Diff line change
Expand Up @@ -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 extdeps.filesystem.filesystem_io { Filesystem }
import std.compiler_entry { CompilerEntryDriver, NativeClaimDriver, NativeClaimReport, NativeClaimTerminal, NativeClaimHeld, NativeClaimNotHeld }

Expand Down Expand Up @@ -147,6 +148,25 @@ fn claim_driver_filesystem_cases() -> List<NativeEmissionCase> {
]
}

// RULE: an empty-map turbofish renders in the enclosing fn's generic scope (v1.compiler.emit_rust
// rust_empty_map_kv_type_str through render_rust_arrow_position). std.list duplicated_by_key builds
// `DuplicatedScan { firsts: empty_map(), .. }` whose field is `Map<String, T>`; the T rendered by its
// erased inference as a compile_error!, so 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> {
[
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) == []
}
]
}

data native_emission_expected_identities: List<String> = [
"set_struct_eq_same_members_in_another_order",
"set_struct_eq_red_bare_field_differs",
Expand All @@ -163,11 +183,13 @@ data native_emission_expected_identities: List<String> = [
"filter_guard_receipt_blank_tail_accepts",
"filter_guard_receipt_red_nonblank_tail_refuses",
"filter_guard_receipt_red_empty_tag_short_circuits",
"generic_empty_map_duplicated_by_key_reports_each_repeat_once",
"generic_empty_map_red_no_repeat_reports_nothing",
"claim_driver_filesystem_absent_path_read_is_refused"
]

fn native_emission_cases() -> List<NativeEmissionCase> {
concat(concat(header_bound_cases(), filter_in_branch_condition_cases()), claim_driver_filesystem_cases())
concat(concat(concat(header_bound_cases(), filter_in_branch_condition_cases()), claim_driver_filesystem_cases()), generic_empty_map_turbofish_cases())
}

fn case_row(c: NativeEmissionCase) -> String {
Expand Down
20 changes: 19 additions & 1 deletion src/v1/05_emit_rust.dag
Original file line number Diff line number Diff line change
Expand Up @@ -11137,11 +11137,29 @@ fn rust_empty_map_kv_type_str(map_type: Node, shared_types: Set<String>, source_
// a module-scope `data ...: Map<String, String> = 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<String>, source_indices: Map<String, NewlineIndex>, 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, ">()")
Expand Down
23 changes: 21 additions & 2 deletions src/v1/stage0/src/v1_compiler_emit_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23774,6 +23774,24 @@ pub fn rust_empty_map_kv_type_str(
}
}

pub fn type_node_carries_erased_type_argument(n: Rc<Node>) -> 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<Node>,
shared_types: Rc<BTreeSet<String>>,
Expand All @@ -23791,13 +23809,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(
Expand Down