Skip to main content

harn_parser/typechecker/
format.rs

1//! Display helpers for type expressions and shape mismatches.
2//!
3//! `format_type` is the canonical pretty-printer for `TypeExpr` (also used
4//! by `harn-lsp` and `harn-fmt` via re-export). `shape_mismatch_detail`
5//! produces a one-line "missing field …" / "field 'x' has type …" diff that
6//! enriches type-error messages.
7
8use crate::ast::*;
9
10/// Pretty-print a type expression for display in error messages.
11pub fn format_type(ty: &TypeExpr) -> String {
12    match ty {
13        TypeExpr::Named(n) => n.clone(),
14        TypeExpr::Union(types) => {
15            if let Some(inner) = optional_sugar_inner(types) {
16                return format!("{}?", format_type(inner));
17            }
18            types
19                .iter()
20                .map(format_type)
21                .collect::<Vec<_>>()
22                .join(" | ")
23        }
24        TypeExpr::Intersection(types) => types
25            .iter()
26            .map(|m| match m {
27                // `T | nil` arms render as the sugared `T?`, which binds
28                // tighter than `&` and reads back unambiguously.
29                TypeExpr::Union(members) if optional_sugar_inner(members).is_some() => {
30                    format_type(m)
31                }
32                // Other nested unions still get parenthesised for readability.
33                TypeExpr::Union(_) => format!("({})", format_type(m)),
34                _ => format_type(m),
35            })
36            .collect::<Vec<_>>()
37            .join(" & "),
38        TypeExpr::Shape(fields) => {
39            let inner: Vec<String> = fields
40                .iter()
41                .map(|f| {
42                    let opt = if f.optional { "?" } else { "" };
43                    format!("{}{opt}: {}", f.name, format_type(&f.type_expr))
44                })
45                .collect();
46            format!("{{{}}}", inner.join(", "))
47        }
48        TypeExpr::OpenShape { fields, rests } => {
49            let mut parts: Vec<String> = fields
50                .iter()
51                .map(|f| {
52                    let opt = if f.optional { "?" } else { "" };
53                    format!("{}{opt}: {}", f.name, format_type(&f.type_expr))
54                })
55                .collect();
56            for rest in rests {
57                parts.push(format!("...{}", format_type(rest)));
58            }
59            format!("{{{}}}", parts.join(", "))
60        }
61        TypeExpr::List(inner) => format!("list<{}>", format_type(inner)),
62        TypeExpr::Iter(inner) => format!("iter<{}>", format_type(inner)),
63        TypeExpr::Generator(inner) => format!("Generator<{}>", format_type(inner)),
64        TypeExpr::Stream(inner) => format!("Stream<{}>", format_type(inner)),
65        TypeExpr::DictType(k, v) => format!("dict<{}, {}>", format_type(k), format_type(v)),
66        TypeExpr::Applied { name, args } => {
67            let args_str = args.iter().map(format_type).collect::<Vec<_>>().join(", ");
68            format!("{name}<{args_str}>")
69        }
70        TypeExpr::FnType {
71            params,
72            return_type,
73        } => {
74            let params_str = params
75                .iter()
76                .map(format_type)
77                .collect::<Vec<_>>()
78                .join(", ");
79            format!("fn({}) -> {}", params_str, format_type(return_type))
80        }
81        TypeExpr::Never => "never".to_string(),
82        TypeExpr::LitString(s) => format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"")),
83        TypeExpr::LitInt(v) => v.to_string(),
84        TypeExpr::Owned(inner) => format!("owned<{}>", format_type(inner)),
85    }
86}
87
88/// Produce a detail string describing why a Shape type is incompatible with
89/// another Shape type — e.g. "missing field 'age' (int)" or "field 'name'
90/// has type int, expected string". Returns `None` if both types are not shapes.
91pub fn shape_mismatch_detail(expected: &TypeExpr, actual: &TypeExpr) -> Option<String> {
92    if let (TypeExpr::Shape(ef), TypeExpr::Shape(af)) = (expected, actual) {
93        let mut details = Vec::new();
94        for field in ef {
95            if field.optional {
96                continue;
97            }
98            match af.iter().find(|f| f.name == field.name) {
99                None => details.push(format!(
100                    "missing field '{}' ({})",
101                    field.name,
102                    format_type(&field.type_expr)
103                )),
104                Some(actual_field) => {
105                    let e_str = format_type(&field.type_expr);
106                    let a_str = format_type(&actual_field.type_expr);
107                    if e_str != a_str {
108                        details.push(format!(
109                            "field '{}' has type {}, expected {}",
110                            field.name, a_str, e_str
111                        ));
112                    }
113                }
114            }
115        }
116        if details.is_empty() {
117            None
118        } else {
119            Some(details.join("; "))
120        }
121    } else {
122        None
123    }
124}
125
126/// If `types` is exactly two members and one is `nil`, return the
127/// non-`nil` member when it can be safely rendered as `T?`. Mirrors the
128/// formatter's rule in `harn-fmt::helpers::optional_sugar_inner`: only
129/// types that appear at primary precedence (or below) can be sugared,
130/// because postfix `?` parses tighter than `&` / `|` / `fn(...) -> ...`
131/// return positions.
132fn optional_sugar_inner(types: &[TypeExpr]) -> Option<&TypeExpr> {
133    if types.len() != 2 {
134        return None;
135    }
136    let nil_idx = types
137        .iter()
138        .position(|t| matches!(t, TypeExpr::Named(n) if n == "nil"))?;
139    let inner = &types[1 - nil_idx];
140    if matches!(
141        inner,
142        TypeExpr::Union(_) | TypeExpr::Intersection(_) | TypeExpr::FnType { .. }
143    ) {
144        return None;
145    }
146    if matches!(inner, TypeExpr::Named(n) if n == "nil") {
147        return None;
148    }
149    Some(inner)
150}
151
152/// Returns true when the type is obvious from the RHS expression
153/// (e.g. `let x = 42` is obviously int — no hint needed).
154pub(super) fn is_obvious_type(value: &SNode, _ty: &TypeExpr) -> bool {
155    matches!(
156        &value.node,
157        Node::IntLiteral(_)
158            | Node::FloatLiteral(_)
159            | Node::StringLiteral(_)
160            | Node::BoolLiteral(_)
161            | Node::NilLiteral
162            | Node::ListLiteral(_)
163            | Node::DictLiteral(_)
164            | Node::InterpolatedString(_)
165    )
166}