Skip to content

Commit 6457f44

Browse files
committed
Derive constructor signatures from types
1 parent 3995e50 commit 6457f44

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ It currently implements:
1515
- Lexer, lightweight parser, syntax AST, HIR signature table, and semantic checks for the review-critical v0.4.1 rules
1616
- Builtin signatures for the current fixture stdlib surface, including `Image`, `File`, `Map`, `ResourcePool`, `Json`, `Csv`, database/resource helpers, and cache/config helpers
1717
- HIR type and field tables for class/struct/resource declarations and handle fields
18+
- HIR constructor signatures derived from declared type fields
1819
- AST-driven mode and call checks for local-only features, named arguments, data effects, and retaining APIs
1920
- AST-driven body checks for local moves, early-exit-aware `fresh` returns, resource escape, resolved handle-field `take`, and managed closure captures
2021
- Focused check modules for mode, calls, body semantics, and forbidden operator behavior

src/hir.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,15 @@ impl Hir {
118118
}
119119

120120
fn insert_type(&mut self, type_info: TypeInfo) {
121+
let constructor = constructor_sig_from_type(&type_info);
121122
for field in type_info.fields.values() {
122123
self.fields_by_name
123124
.entry(field.name.clone())
124125
.or_default()
125126
.push(field.clone());
126127
}
127128
self.types.insert(type_info.name.clone(), type_info);
129+
self.insert_function(constructor);
128130
}
129131

130132
fn insert_builtins(&mut self) {
@@ -197,6 +199,28 @@ fn field_info_from_decl(field: &FieldDecl) -> FieldInfo {
197199
}
198200
}
199201

202+
fn constructor_sig_from_type(type_info: &TypeInfo) -> FunctionSig {
203+
let mut fields: Vec<&FieldInfo> = type_info.fields.values().collect();
204+
fields.sort_by(|left, right| left.name.cmp(&right.name));
205+
206+
FunctionSig {
207+
namespace: None,
208+
name: type_info.name.clone(),
209+
params: fields
210+
.into_iter()
211+
.map(|field| ParamSig {
212+
name: field.name.clone(),
213+
effect: None,
214+
type_name: field.type_name.clone(),
215+
})
216+
.collect(),
217+
return_type: Some(type_info.name.clone()),
218+
returns_fresh: true,
219+
retained_params: HashSet::new(),
220+
is_builtin: false,
221+
}
222+
}
223+
200224
fn qualified_key(namespace: &str, name: &str) -> String {
201225
format!("{namespace}.{name}")
202226
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// expect: RS0203 RS0204
2+
mode: managed
3+
4+
struct Response {
5+
status: Int
6+
body: String
7+
}
8+
9+
fn bad_response(body: read String) -> Result<fresh Response, HttpError> {
10+
return Response(status: 200, boody: read body)
11+
}

0 commit comments

Comments
 (0)