harn_parser/typechecker/
format.rs1use crate::ast::*;
9
10pub 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 TypeExpr::Union(members) if optional_sugar_inner(members).is_some() => {
30 format_type(m)
31 }
32 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
88pub 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
126fn 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
152pub(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}