phenotyper 0.4.0

Core compiler library for the Phenotyper structural artifact definition language
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// SPDX-License-Identifier: Apache-2.0
//! Semantic validation pass over the IR.
//!
//! Implements REQ-COMP-009 validation phases:
//! - Type validation (plural integrity, union member validity)
//! - Render validation (directive constraints, field compatibility)
//! - Generation validation (cyclic type detection)

use std::collections::HashSet;

use crate::diagnostic::Diagnostic;
use crate::ir::{FieldDef, PhenotypeModule, PhenotypeType, RenderNode, SeparatorExpr, ValueType};
use crate::symbol::{Cardinality, FieldId, Requiredness, TypeId};

/// Validate the entire module.
pub fn validate_module(module: &PhenotypeModule, file: &str) -> Vec<Diagnostic> {
    let mut diags = Vec::new();

    for pt in &module.types {
        validate_type(pt, module, file, &mut diags);
    }

    validate_cyclic_types(module, file, &mut diags);

    diags
}

/// Validate a single phenotype type.
fn validate_type(
    pt: &PhenotypeType,
    module: &PhenotypeModule,
    file: &str,
    diags: &mut Vec<Diagnostic>,
) {
    // Validate render is non-empty
    if pt.render.is_empty() {
        diags.push(super::error(
            file,
            format!("type `{}` has no render expressions", pt.singular_name),
        ));
    }

    // Imported types inside unions are not supported yet (PHT-0027 keeps
    // union variant naming local); reject them honestly rather than let
    // codegen emit an unusable variant.
    for field in &pt.fields {
        if let ValueType::Union(members) = &field.ty {
            if members.iter().any(|m| matches!(m, ValueType::Imported(_))) {
                diags.push(super::error(
                    file,
                    format!(
                        "field `{}` in type `{}`: imported types are not yet supported \
                         inside unions",
                        field.name, pt.singular_name
                    ),
                ));
            }
        }
    }

    // Validate each render node
    for node in &pt.render {
        validate_render_node(node, pt, module, file, diags, false);
    }

    // PHT-0028: the annotation checks.
    validate_elasticity(pt, file, diags);
    validate_volatile_order(pt, module, file, diags);
}

/// PHT-0028: elasticity must be REALISABLE. A `drop` field must render
/// as unset does today — optional (the existing optional-outside-@ifset
/// check then guards every reference) or a collection (the join drops
/// tail items). A `truncate` field must be a scalar string — there is
/// nothing honest to cut in the middle of an int or a sub-phenotype.
fn validate_elasticity(pt: &PhenotypeType, file: &str, diags: &mut Vec<Diagnostic>) {
    for field in &pt.fields {
        match &field.annotations.elastic {
            Some(crate::ir::Elastic::Drop { .. }) => {
                let droppable = field.requiredness == Requiredness::Optional
                    || is_collection_type(&field.ty, &field.cardinality);
                if !droppable {
                    diags.push(super::error(
                        file,
                        format!(
                            "field `{}` in type `{}`: elastic(drop) needs a field that can \
                             render as unset — make it optional (and guard references \
                             with @ifset) or a collection",
                            field.name, pt.singular_name
                        ),
                    ));
                }
            }
            Some(crate::ir::Elastic::Truncate { .. })
                if !is_scalar_string(&field.ty)
                    || is_collection_type(&field.ty, &field.cardinality) =>
            {
                diags.push(super::error(
                    file,
                    format!(
                        "field `{}` in type `{}`: elastic(truncate) cuts text — it needs \
                         a scalar string field",
                        field.name, pt.singular_name
                    ),
                ));
            }
            Some(crate::ir::Elastic::Truncate { .. }) | None => {}
        }
    }
}

/// PHT-0028: stable-before-volatile, by construction. Within a type's
/// template, no STABLE field reference may follow the first volatile
/// one — a volatile byte invalidates every cached byte after it, so
/// stable content downstream of volatile content is a silent KV-reuse
/// loss (the GEN-081 field lesson, made a compile error). Literal text
/// is exempt: it belongs to whichever unit it annotates. Volatility is
/// transitive through same-module phenotype fields.
fn validate_volatile_order(
    pt: &PhenotypeType,
    module: &PhenotypeModule,
    file: &str,
    diags: &mut Vec<Diagnostic>,
) {
    let volatile_types = crate::ir::volatile_type_set(module);
    let is_volatile = |id: FieldId| -> bool {
        find_field(pt, id).is_some_and(|f| {
            f.annotations.volatile
                || referenced_type_id(&f.ty).is_some_and(|t| volatile_types.contains(&t))
        })
    };
    let mut first_volatile: Option<FieldId> = None;
    let mut refs: Vec<FieldId> = Vec::new();
    collect_field_refs(&pt.render, &mut refs);
    for id in refs {
        if is_volatile(id) {
            first_volatile.get_or_insert(id);
        } else if let Some(v) = first_volatile {
            diags.push(super::error(
                file,
                format!(
                    "type `{}`: stable field `{}` is referenced after volatile field `{}` —                      a volatile byte invalidates every cached byte after it; move the                      volatile reference last, or mark `{}` volatile too",
                    pt.singular_name,
                    field_id_name(pt, id),
                    field_id_name(pt, v),
                    field_id_name(pt, id),
                ),
            ));
            return; // one clear error per type beats a cascade
        }
    }
}

/// Every field reference in template order, nested bodies flattened in
/// place (an @ifset's body sits where the @ifset sits).
fn collect_field_refs(nodes: &[RenderNode], out: &mut Vec<FieldId>) {
    for node in nodes {
        match node {
            RenderNode::Text(_) | RenderNode::ParentFieldRef { .. } => {}
            RenderNode::Emit(id) => out.push(*id),
            RenderNode::Join { field, separator } => {
                out.push(*field);
                if let SeparatorExpr::Field(id) = separator {
                    out.push(*id);
                }
            }
            RenderNode::Eol { field } => {
                if let Some(id) = field {
                    out.push(*id);
                }
            }
            RenderNode::IfSet { field, body } | RenderNode::IfNotEmpty { field, body } => {
                out.push(*field);
                collect_field_refs(body, out);
            }
        }
    }
}

/// Validate a render node in context.
///
/// `inside_ifset_for` tracks whether we're inside an `@ifset` block for a
/// particular field, which relaxes the "optional field outside @ifset" check.
#[allow(clippy::only_used_in_recursion)]
fn validate_render_node(
    node: &RenderNode,
    pt: &PhenotypeType,
    module: &PhenotypeModule,
    file: &str,
    diags: &mut Vec<Diagnostic>,
    inside_guard: bool,
) {
    match node {
        // Parent-scoped field refs are valid by construction — scope was
        // verified during IR lowering. No additional validation needed.
        RenderNode::ParentFieldRef { .. } => {}

        RenderNode::Emit(field_id) => {
            if let Some(field) = find_field(pt, *field_id) {
                // T-062: Direct @emit of plural field → error
                if is_collection_type(&field.ty, &field.cardinality) {
                    diags.push(super::error_with_suggestion(
                        file,
                        format!(
                            "cannot directly emit collection field `{}` in type `{}`",
                            field.name, pt.singular_name
                        ),
                        "collection fields must be rendered with @join".to_string(),
                        format!("use @join({}, separator) instead", field.name),
                    ));
                }

                // T-063: Direct @emit of optional fields outside @ifset → error
                if field.requiredness == Requiredness::Optional && !inside_guard {
                    diags.push(super::error_with_suggestion(
                        file,
                        format!(
                            "cannot directly emit optional field `{}` in type `{}`",
                            field.name, pt.singular_name
                        ),
                        "optional fields must be guarded by @ifset".to_string(),
                        format!("use @ifset({}) {{ @({}) }} instead", field.name, field.name),
                    ));
                }
            }
        }

        RenderNode::Join { field, separator } => {
            // T-064: Validate separator type — must be singular scalar string
            if let Some(sep_field) = match separator {
                SeparatorExpr::Field(fid) => find_field(pt, *fid),
                SeparatorExpr::Literal(_) => None,
            } {
                validate_separator_field(sep_field, pt, file, diags);
            }

            // Join field should be a collection
            if let Some(field_def) = find_field(pt, *field) {
                if !is_collection_type(&field_def.ty, &field_def.cardinality) {
                    diags.push(super::error(
                        file,
                        format!(
                            "@join field `{}` in type `{}` is not a collection",
                            field_def.name, pt.singular_name
                        ),
                    ));
                }
            }
        }

        RenderNode::Eol { field } => {
            // Eol with a field: the field should be a string
            if let Some(fid) = field {
                if let Some(field_def) = find_field(pt, *fid) {
                    if !is_scalar_string(&field_def.ty) {
                        diags.push(super::error(
                            file,
                            format!(
                                "@eol field `{}` in type `{}` must be a string type",
                                field_def.name, pt.singular_name
                            ),
                        ));
                    }
                }
            }
        }

        RenderNode::IfSet { field, body } => {
            // T-063a: @ifset on a non-optional field → error
            if let Some(field_def) = find_field(pt, *field) {
                if field_def.requiredness != Requiredness::Optional {
                    diags.push(super::error_with_explanation(
                        file,
                        format!(
                            "@ifset on non-optional field `{}` in type `{}`",
                            field_def.name, pt.singular_name
                        ),
                        "@ifset is only meaningful for optional fields".to_string(),
                    ));
                }
            }

            // Validate body (inside guard context)
            if body.is_empty() {
                diags.push(super::error(
                    file,
                    format!(
                        "@ifset block for `{}` in type `{}` has empty body",
                        field_id_name(pt, *field),
                        pt.singular_name
                    ),
                ));
            }
            for child in body {
                validate_render_node(child, pt, module, file, diags, true);
            }
        }

        RenderNode::IfNotEmpty { field, body } => {
            // T-063b: @ifnotempty on a non-collection field → error
            if let Some(field_def) = find_field(pt, *field) {
                if !is_collection_type(&field_def.ty, &field_def.cardinality) {
                    diags.push(super::error_with_explanation(
                        file,
                        format!(
                            "@ifnotempty on non-collection field `{}` in type `{}`",
                            field_def.name, pt.singular_name
                        ),
                        "@ifnotempty is only meaningful for collection fields (plural types or cardinalized fields)"
                            .to_string(),
                    ));
                }
            }

            // Validate body (inside guard context)
            if body.is_empty() {
                diags.push(super::error(
                    file,
                    format!(
                        "@ifnotempty block for `{}` in type `{}` has empty body",
                        field_id_name(pt, *field),
                        pt.singular_name
                    ),
                ));
            }
            for child in body {
                validate_render_node(child, pt, module, file, diags, true);
            }
        }

        RenderNode::Text(_) => {
            // String literals are always valid
        }
    }
}

/// T-064: Validate separator field is a singular scalar string.
fn validate_separator_field(
    field: &FieldDef,
    pt: &PhenotypeType,
    file: &str,
    diags: &mut Vec<Diagnostic>,
) {
    if !is_scalar_string(&field.ty) {
        diags.push(super::error(
            file,
            format!(
                "@join separator field `{}` in type `{}` must be a string type",
                field.name, pt.singular_name
            ),
        ));
    }
    if field.cardinality != Cardinality::One {
        diags.push(super::error(
            file,
            format!(
                "@join separator field `{}` in type `{}` must be singular (not a collection)",
                field.name, pt.singular_name
            ),
        ));
    }
}

/// T-065: Detect cyclic type references.
///
/// A type graph has a cycle if type A has a required field of type A (directly
/// or transitively). Optional fields and collection fields break cycles.
fn validate_cyclic_types(module: &PhenotypeModule, file: &str, diags: &mut Vec<Diagnostic>) {
    for pt in &module.types {
        let mut visited = HashSet::new();
        if has_cycle(pt.id, module, &mut visited) {
            diags.push(super::error_with_explanation(
                file,
                format!("cyclic type definition involving `{}`", pt.singular_name),
                "recursive type definitions are not supported in v1".to_string(),
            ));
        }
    }
}

/// Check if following required, singular fields from `type_id` leads back to it.
fn has_cycle(type_id: TypeId, module: &PhenotypeModule, visited: &mut HashSet<TypeId>) -> bool {
    if !visited.insert(type_id) {
        return true;
    }

    if let Some(pt) = module.types.iter().find(|t| t.id == type_id) {
        for field in &pt.fields {
            // Only required, singular fields create true cycles
            if field.requiredness == Requiredness::Required && field.cardinality == Cardinality::One
            {
                if let Some(ref_id) = referenced_type_id(&field.ty) {
                    if has_cycle(ref_id, module, visited) {
                        return true;
                    }
                }
            }
        }
    }

    visited.remove(&type_id);
    false
}

// ─── Helpers ────────────────────────────────────────────────────────────────

/// Find a field by its `FieldId` within a phenotype type.
fn find_field(pt: &PhenotypeType, id: FieldId) -> Option<&FieldDef> {
    pt.fields.iter().find(|f| f.id == id)
}

/// Get the field name for a `FieldId`, or "<unknown>".
fn field_id_name(pt: &PhenotypeType, id: FieldId) -> String {
    find_field(pt, id)
        .map(|f| f.name.clone())
        .unwrap_or_else(|| "<unknown>".to_string())
}

/// Check if a value type is a scalar string primitive.
fn is_scalar_string(ty: &ValueType) -> bool {
    matches!(
        ty,
        ValueType::Primitive(crate::symbol::PrimitiveType::String)
    )
}

/// Check if a field represents a collection (plural type or cardinalized).
fn is_collection_type(ty: &ValueType, cardinality: &Cardinality) -> bool {
    // Cardinality-based collections
    if *cardinality != Cardinality::One {
        return true;
    }
    // Plural type references are collections — local or imported
    matches!(
        ty,
        ValueType::UserPlural { .. }
            | ValueType::Imported(crate::ir::ImportedRef {
                kind: crate::symbol::ImportedKind::Plural,
                ..
            })
    )
}

/// Extract the referenced TypeId from a value type, if it references a phenotype.
fn referenced_type_id(ty: &ValueType) -> Option<TypeId> {
    match ty {
        ValueType::UserSingular(id) => Some(*id),
        ValueType::UserPlural { collection_of } => Some(*collection_of),
        _ => None,
    }
}