Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions dag/test/claim/record_decl_spelling_identity_witness_test.dag
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module test.claim.record_decl_spelling_identity_witness_test

import test.fixture.record_decl_spelling.witness_support { record_decl_spelling_facts }
import v2.std.decl_facts_skeleton {
DeclFactResolvedAmbiguous,
DeclFactResolvedMissing,
DeclFactResolvedUnique,
resolve_decl_fact_by_qualified_name,
}
import v2.std.collection { Absent, Present }
import v2.std.diagnostic { Accepted, Rejected }
import v2.std.node { Node, Symbol }
import v2.std.node_query { find_named_child }

data record_decl_spelling_identity_note: String = "TWO SPELLINGS OF ONE DECLARATION MUST CARRY ONE SHAPE. `type X { a: A, b: B }` and `type X = \{ a: A, b: B \}` both declare a named record, and before the parse fix this witness defends they produced two different item shapes: the brace form hoisted the fields onto the declaration (Conj, children = fields), the `=` form left them inside an ANONYMOUS record type-expression and gave the declaration no children at all. Downstream that anonymous 2-child Conj is indistinguishable from a positional pair, which is what the Rust emitter rendered it as -- `pub type TranslateFoldAcc = (Rc<Node>, Rc<Node>)` -- while the SAME emitter kept writing `TranslateFoldAcc \{ source: .., rebuilt: .. \}` at construction and `.source`/`.rebuilt` at every read. Accepted with 0 diagnostics; refused by rustc as E0071 plus six E0609 on the 03_ingest board of 2026-08-23. WHY THIS GRAIN AND NOT AN EMIT CHECK: the defect emits no diagnostic, so `compile_dag_rust_emit_check` (which collapses a compile to a count of hard diagnostics) is green on both arms and cannot see it; and no builtin returns emitted target text. `decl_facts` returns the parsed declaration itself, which is exactly where the fork lived, so the assertion is made at the grain of the fix rather than two stages downstream of it. WHAT MAKES IT DISCRIMINATING: `concept_decl_node` marshals a NoConnective item to a UNIT node, so on the pre-fix parser the `=` arm resolves its fact and then finds no named child `first` -- red -- while the brace arm is green, and the alias control below is red-if-widened. dissolve-on: never -- permanent regression control for the spelling-identity law in type-declaration position."

data qn_brace_spelling: String = "test.fixture.record_decl_spelling.specimens.RecordDeclBraceSpelling"
data qn_equals_spelling: String = "test.fixture.record_decl_spelling.specimens.RecordDeclEqualsSpelling"
data qn_leaf_alias: String = "test.fixture.record_decl_spelling.specimens.RecordDeclLeafAlias"

fn record_decl_spelling_node_optional(qn: String) -> Node? {
match resolve_decl_fact_by_qualified_name(facts: record_decl_spelling_facts, qn: qn) {
DeclFactResolvedUnique { fact: fact } => Present { value: fact.node }
DeclFactResolvedMissing { qualified_name: _ } => Absent
DeclFactResolvedAmbiguous { qualified_name: _, match_count: _ } => Absent
}
}

fn record_decl_spelling_has_named_field(qn: String, field: Symbol) -> Bool {
match record_decl_spelling_node_optional(qn: qn) {
Present { value: node } =>
match find_named_child(root: node, name: field) {
Accepted { value: _, diagnostics: _ } => true
Rejected { diagnostics: _ } => false
}
Absent => false
}
}

test fn brace_spelling_carries_its_named_fields() -> Bool {
record_decl_spelling_has_named_field(qn: qn_brace_spelling, field: ^first)
&& record_decl_spelling_has_named_field(qn: qn_brace_spelling, field: ^second)
}

test fn equals_spelling_carries_the_same_named_fields() -> Bool {
record_decl_spelling_has_named_field(qn: qn_equals_spelling, field: ^first)
&& record_decl_spelling_has_named_field(qn: qn_equals_spelling, field: ^second)
}

test fn leaf_alias_is_not_read_as_a_record() -> Bool {
!record_decl_spelling_has_named_field(qn: qn_leaf_alias, field: ^first)
}
24 changes: 24 additions & 0 deletions dag/test/fixture/record_decl_spelling/specimens.dag
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module test.fixture.record_decl_spelling.specimens

import std.types { String }

// The two authored spellings of ONE concept: a named record with two named fields.
// They are declared side by side so a consumer can ask whether the compiler carries them
// as the same declaration, which is the whole subject of the witness that reads this file.
// Nothing else in the repository declares both forms adjacently -- the corpus carries exactly
// one `type X = { .. }` site (v2.compiler.06_translate TranslateFoldAcc), and a fixture is what
// lets the claim be stated about the SPELLING rather than about that one production carrier.
type RecordDeclBraceSpelling {
first: String
second: String
}

type RecordDeclEqualsSpelling = {
first: String,
second: String
}

// Negative control: a `type X = <name>` alias is NOT a record declaration, and must keep the
// alias shape after the two spellings above are unified. Without this row the witness would
// pass just as well against a change that turned every `=` form into a record.
type RecordDeclLeafAlias = String
16 changes: 16 additions & 0 deletions dag/test/fixture/record_decl_spelling/witness_support.dag
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module test.fixture.record_decl_spelling.witness_support

import std.types { List }
import test.fixture.record_decl_spelling.specimens
import v2.std.decl_index { DeclFact, decl_facts }

// One pool scan for the whole cluster: a `data` initializer is evaluated once per interpreter
// context, a `fn` body re-runs the scan at every call site (the reason recorded on the sibling
// decl_facts fixture in dag/test/fixture/decl_facts_reflection/witness_support.dag).
// The root is exactly the specimen directory: no std, no shadow root, nothing whose parse cost
// this cluster does not read.
data record_decl_spelling_pool_roots: List<String> = [
"dag/test/fixture/record_decl_spelling",
]

data record_decl_spelling_facts: List<DeclFact> = decl_facts(pool_roots: record_decl_spelling_pool_roots)
33 changes: 31 additions & 2 deletions src/v1/02_parse.dag
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,35 @@ fn parse_type_body_from_prefix(prefix: ItemPrefixResult, start_span: SourceSpan)
}
}

// `type X = { a: A, b: B }` and `type X { a: A, b: B }` declare the same thing -- a named record
// -- and before this function they parsed into two different item shapes: the brace form hoists
// the fields onto the declaration (connective Conj, children = fields), the `=` form leaves them
// inside an ANONYMOUS record type-expression under `inferred`. Every downstream consumer that
// asks "is this a record named X" reads the first shape; the ones that read the second see an
// unnamed 2-child Conj, which is exactly the shape a positional pair has, so the Rust emitter
// rendered `type TranslateFoldAcc = (Rc<Node>, Rc<Node>)` while the SAME emitter kept writing
// `TranslateFoldAcc { source: .., rebuilt: .. }` at construction and `.source` at every read --
// accepted with 0 diagnostics, refused by rustc (7 blocks on the 03_ingest board, E0071/E0609).
// The fork is a second representation of one concept, so it is closed here at the parse boundary
// rather than compensated for in each target's renderer: after this, no stage can observe the
// difference between the two spellings, and no emitter needs to know the `=` form exists.
// A `where`-refined right-hand side is NOT a record (try_where_clause wraps its base in a Conj
// carrying a type_annotation), and neither is an empty brace pair, so both keep the alias shape.
fn alias_rhs_is_anonymous_record(te: Node) -> Bool {
te.connective == Conj
&& te.ident_span == none
&& te.type_annotation == none
&& (te.children |> count) > 0
}

fn type_item_from_alias_rhs(name: String, name_span: SourceSpan, start_span: SourceSpan, type_params: List<Node>, te: Node) -> Node {
if alias_rhs_is_anonymous_record(te: te) {
Node { name: name, span: start_span, ident_span: Present { value: name_span }, children: te.children, connective: Conj, params: type_params, inferred: none, return_cardinality: Required, uses: [], body: none, transport: none, properties: [], type_annotation: none, is_self_recursive: false, has_non_tail_self_call: false, match_pattern: none, expr_data: NoExprData }
} else {
Node { name: name, span: start_span, ident_span: Present { value: name_span }, children: [], connective: NoConnective, params: type_params, inferred: Present { value: Resolved { node: te } }, return_cardinality: Required, uses: [], body: none, transport: none, properties: [], type_annotation: none, is_self_recursive: false, has_non_tail_self_call: false, match_pattern: none, expr_data: NoExprData }
}
}

fn parse_type_body_after_eq(tokens: TokenStream, ctx: ParseContext, name: String, name_span: SourceSpan, start_span: SourceSpan, type_params: List<Node>) -> ItemResult {
let dummy = Node { name: name, span: start_span, ident_span: Present { value: name_span }, children: [], params: type_params, inferred: none, return_cardinality: Required, uses: [], body: none, connective: NoConnective, transport: none, properties: [], type_annotation: none, is_self_recursive: false, has_non_tail_self_call: false, match_pattern: none, expr_data: NoExprData }

Expand Down Expand Up @@ -1960,15 +1989,15 @@ fn parse_type_body_after_eq(tokens: TokenStream, ctx: ParseContext, name: String
if has_err(err: r.err) { return ItemResult { item: dummy, tokens: r.tokens, ctx: r.ctx, err: r.err } }
let wr = try_where_clause(tokens: r.tokens, ctx: r.ctx, base_te: r.type_expr, start_span: start_span)
if has_err(err: wr.err) { return ItemResult { item: dummy, tokens: wr.tokens, ctx: wr.ctx, err: wr.err } }
let item = Node { name: name, span: start_span, ident_span: Present { value: name_span }, children: [], connective: NoConnective, params: type_params, inferred: Present { value: Resolved { node: wr.type_expr } }, return_cardinality: Required, uses: [], body: none, transport: none, properties: [], type_annotation: none, is_self_recursive: false, has_non_tail_self_call: false, match_pattern: none, expr_data: NoExprData }
let item = type_item_from_alias_rhs(name: name, name_span: name_span, start_span: start_span, type_params: type_params, te: wr.type_expr)
ItemResult { item: item, tokens: skip_newlines(tokens: wr.tokens), ctx: wr.ctx, err: none }
}
} else {
let r = parse_type_expr(tokens: tokens, ctx: ctx)
if has_err(err: r.err) { return ItemResult { item: dummy, tokens: r.tokens, ctx: r.ctx, err: r.err } }
let wr = try_where_clause(tokens: r.tokens, ctx: r.ctx, base_te: r.type_expr, start_span: start_span)
if has_err(err: wr.err) { return ItemResult { item: dummy, tokens: wr.tokens, ctx: wr.ctx, err: wr.err } }
let item = Node { name: name, span: start_span, ident_span: Present { value: name_span }, children: [], connective: NoConnective, params: type_params, inferred: Present { value: Resolved { node: wr.type_expr } }, return_cardinality: Required, uses: [], body: none, transport: none, properties: [], type_annotation: none, is_self_recursive: false, has_non_tail_self_call: false, match_pattern: none, expr_data: NoExprData }
let item = type_item_from_alias_rhs(name: name, name_span: name_span, start_span: start_span, type_params: type_params, te: wr.type_expr)
ItemResult { item: item, tokens: skip_newlines(tokens: wr.tokens), ctx: wr.ctx, err: none }
}
}
Expand Down
116 changes: 72 additions & 44 deletions src/v1/stage0/src/v1_compiler_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4169,6 +4169,64 @@ pub fn parse_type_body_from_prefix(
}
}

pub fn alias_rhs_is_anonymous_record(te: Rc<Node>) -> bool {
((((te.connective.clone() == Connective::Conj) && (te.ident_span.clone() == None))
&& (te.type_annotation.clone() == None))
&& ((te.children.clone().len() as i64) > 0))
}

pub fn type_item_from_alias_rhs(
name: String,
name_span: Rc<SourceSpan>,
start_span: Rc<SourceSpan>,
type_params: Rc<Vec<Rc<Node>>>,
te: Rc<Node>,
) -> Rc<Node> {
if alias_rhs_is_anonymous_record(te.clone()) {
Rc::new(Node {
name: name.clone(),
span: start_span.clone(),
ident_span: Some(name_span.clone()),
children: te.children.clone(),
connective: Connective::Conj,
params: type_params.clone(),
inferred: None,
return_cardinality: Cardinality::Required,
uses: Rc::new(vec![]),
body: None,
transport: None,
properties: Rc::new(vec![]),
type_annotation: None,
is_self_recursive: false,
has_non_tail_self_call: false,
match_pattern: None,
expr_data: Rc::new(ExprData::NoExprData),
ident: None,
})
} else {
Rc::new(Node {
name: name.clone(),
span: start_span.clone(),
ident_span: Some(name_span.clone()),
children: Rc::new(vec![]),
connective: Connective::NoConnective,
params: type_params.clone(),
inferred: Some(Rc::new(InferredNode::Resolved { node: te.clone() })),
return_cardinality: Cardinality::Required,
uses: Rc::new(vec![]),
body: None,
transport: None,
properties: Rc::new(vec![]),
type_annotation: None,
is_self_recursive: false,
has_non_tail_self_call: false,
match_pattern: None,
expr_data: Rc::new(ExprData::NoExprData),
ident: None,
})
}
}

pub fn parse_type_body_after_eq(
tokens: Rc<TokenStream>,
ctx: Rc<ParseContext>,
Expand Down Expand Up @@ -4390,28 +4448,13 @@ pub fn parse_type_body_after_eq(
err: wr.err.clone(),
});
}
let item = Rc::new(Node {
name: name.clone(),
span: start_span.clone(),
ident_span: Some(name_span.clone()),
children: Rc::new(vec![]),
connective: Connective::NoConnective,
params: type_params.clone(),
inferred: Some(Rc::new(InferredNode::Resolved {
node: wr.type_expr.clone(),
})),
return_cardinality: Cardinality::Required,
uses: Rc::new(vec![]),
body: None,
transport: None,
properties: Rc::new(vec![]),
type_annotation: None,
is_self_recursive: false,
has_non_tail_self_call: false,
match_pattern: None,
expr_data: Rc::new(ExprData::NoExprData),
ident: None,
});
let item = type_item_from_alias_rhs(
name.clone(),
name_span.clone(),
start_span.clone(),
type_params.clone(),
wr.type_expr.clone(),
);
Rc::new(ItemResult {
item: item.clone(),
tokens: skip_newlines(wr.tokens.clone()),
Expand Down Expand Up @@ -4446,28 +4489,13 @@ pub fn parse_type_body_after_eq(
err: wr.err.clone(),
});
}
let item = Rc::new(Node {
name: name.clone(),
span: start_span.clone(),
ident_span: Some(name_span.clone()),
children: Rc::new(vec![]),
connective: Connective::NoConnective,
params: type_params.clone(),
inferred: Some(Rc::new(InferredNode::Resolved {
node: wr.type_expr.clone(),
})),
return_cardinality: Cardinality::Required,
uses: Rc::new(vec![]),
body: None,
transport: None,
properties: Rc::new(vec![]),
type_annotation: None,
is_self_recursive: false,
has_non_tail_self_call: false,
match_pattern: None,
expr_data: Rc::new(ExprData::NoExprData),
ident: None,
});
let item = type_item_from_alias_rhs(
name.clone(),
name_span.clone(),
start_span.clone(),
type_params.clone(),
wr.type_expr.clone(),
);
Rc::new(ItemResult {
item: item.clone(),
tokens: skip_newlines(wr.tokens.clone()),
Expand Down
Loading