Skip to content

Commit 4d9c25c

Browse files
committed
fix: trait impl bugs
1 parent 9045fa2 commit 4d9c25c

File tree

5 files changed

+252
-73
lines changed

5 files changed

+252
-73
lines changed

crates/erg_compiler/codegen.rs

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ use crate::hir::DefaultParamSignature;
3939
use crate::hir::GlobSignature;
4040
use crate::hir::ListWithLength;
4141
use crate::hir::{
42-
Accessor, Args, BinOp, Block, Call, ClassDef, Def, DefBody, Expr, GuardClause, Identifier,
43-
Lambda, List, Literal, NonDefaultParamSignature, Params, PatchDef, PosArg, ReDef, Record,
44-
Signature, SubrSignature, Tuple, UnaryOp, VarSignature, HIR,
42+
Accessor, Args, BinOp, Block, Call, ClassDef, Def, DefBody, Dict, Expr, GuardClause,
43+
Identifier, Lambda, List, Literal, NonDefaultParamSignature, Params, PatchDef, PosArg, ReDef,
44+
Record, Set, Signature, SubrSignature, Tuple, UnaryOp, VarSignature, HIR,
4545
};
4646
use crate::ty::codeobj::{CodeObj, CodeObjFlags, MakeFunctionFlags};
4747
use crate::ty::value::{GenTypeObj, ValueObj};
@@ -864,6 +864,51 @@ impl PyCodeGenerator {
864864
self.emit_args_311(args, AccessKind::Name);
865865
return;
866866
}
867+
"list_iterator" => {
868+
let list = Expr::Literal(Literal::new(ValueObj::List(vec![].into()), Token::DUMMY));
869+
let iter = Identifier::static_public("iter");
870+
let iter_call = iter.call(Args::single(PosArg::new(list)));
871+
let typ = Identifier::static_public("type");
872+
let typ_call = typ.call(Args::single(PosArg::new(iter_call.into())));
873+
self.emit_call(typ_call);
874+
return;
875+
}
876+
"set_iterator" => {
877+
let set = Expr::Set(Set::empty());
878+
let iter = Identifier::static_public("iter");
879+
let iter_call = iter.call(Args::single(PosArg::new(set)));
880+
let typ = Identifier::static_public("type");
881+
let typ_call = typ.call(Args::single(PosArg::new(iter_call.into())));
882+
self.emit_call(typ_call);
883+
return;
884+
}
885+
"dict_items" => {
886+
let dict = Expr::Dict(Dict::empty());
887+
let items = Identifier::static_public("iter");
888+
let items_call = items.call(Args::single(PosArg::new(dict)));
889+
let typ = Identifier::static_public("type");
890+
let typ_call = typ.call(Args::single(PosArg::new(items_call.into())));
891+
self.emit_call(typ_call);
892+
return;
893+
}
894+
"dict_keys" => {
895+
let dict = Expr::Dict(Dict::empty());
896+
let keys = Identifier::static_public("keys");
897+
let keys_call = dict.method_call(keys, Args::empty());
898+
let typ = Identifier::static_public("type");
899+
let typ_call = typ.call(Args::single(PosArg::new(keys_call.into())));
900+
self.emit_call(typ_call);
901+
return;
902+
}
903+
"dict_values" => {
904+
let dict = Expr::Dict(Dict::empty());
905+
let values = Identifier::static_public("values");
906+
let values_call = dict.method_call(values, Args::empty());
907+
let typ = Identifier::static_public("type");
908+
let typ_call = typ.call(Args::single(PosArg::new(values_call.into())));
909+
self.emit_call(typ_call);
910+
return;
911+
}
867912
_ => {}
868913
}
869914
let name = self
@@ -2754,6 +2799,46 @@ impl PyCodeGenerator {
27542799
self.emit_load_name_instr(Identifier::private("#sum"));
27552800
self.emit_args_311(args, Name);
27562801
}
2802+
"ListIterator" => {
2803+
let list = Expr::Literal(Literal::new(ValueObj::List(vec![].into()), Token::DUMMY));
2804+
let iter = Identifier::static_public("iter");
2805+
let iter_call = iter.call(Args::single(PosArg::new(list)));
2806+
let typ = Identifier::static_public("type");
2807+
let typ_call = typ.call(Args::single(PosArg::new(iter_call.into())));
2808+
self.emit_call(typ_call);
2809+
}
2810+
"SetIterator" => {
2811+
let set = Expr::Set(Set::empty());
2812+
let iter = Identifier::static_public("iter");
2813+
let iter_call = iter.call(Args::single(PosArg::new(set)));
2814+
let typ = Identifier::static_public("type");
2815+
let typ_call = typ.call(Args::single(PosArg::new(iter_call.into())));
2816+
self.emit_call(typ_call);
2817+
}
2818+
"DictItems" => {
2819+
let dict = Expr::Dict(Dict::empty());
2820+
let iter = Identifier::static_public("iter");
2821+
let items_call = iter.call(Args::single(PosArg::new(dict)));
2822+
let typ = Identifier::static_public("type");
2823+
let typ_call = typ.call(Args::single(PosArg::new(items_call.into())));
2824+
self.emit_call(typ_call);
2825+
}
2826+
"DictKeys" => {
2827+
let dict = Expr::Dict(Dict::empty());
2828+
let keys = Identifier::static_public("keys");
2829+
let keys_call = dict.method_call(keys, Args::empty());
2830+
let typ = Identifier::static_public("type");
2831+
let typ_call = typ.call(Args::single(PosArg::new(keys_call.into())));
2832+
self.emit_call(typ_call);
2833+
}
2834+
"DictValues" => {
2835+
let dict = Expr::Dict(Dict::empty());
2836+
let values = Identifier::static_public("values");
2837+
let values_call = dict.method_call(values, Args::empty());
2838+
let typ = Identifier::static_public("type");
2839+
let typ_call = typ.call(Args::single(PosArg::new(values_call.into())));
2840+
self.emit_call(typ_call);
2841+
}
27572842
other if local.ref_t().is_poly_meta_type() && other != "classof" => {
27582843
if self.py_version.minor <= Some(9) {
27592844
self.load_fake_generic();

crates/erg_compiler/context/initialize/traits.rs

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ impl Context {
3131
let mut named = Self::builtin_mono_trait(NAMED, 2);
3232
named.register_builtin_erg_decl(FUNC_NAME, Str, Visibility::BUILTIN_PUBLIC);
3333
let mut sized = Self::builtin_mono_trait(SIZED, 2);
34-
let t = fn0_met(mono(SIZED), Nat).quantify();
34+
let ret_t = if PYTHON_MODE { Int } else { Nat };
35+
let t = fn0_met(mono(SIZED), ret_t).quantify();
3536
sized.register_builtin_erg_decl(FUNDAMENTAL_LEN, t, Visibility::BUILTIN_PUBLIC);
3637
let mut copy = Self::builtin_mono_trait(COPY, 2);
3738
let Slf = mono_q(SELF, subtypeof(mono(COPY)));
@@ -227,15 +228,24 @@ impl Context {
227228
/* Iterable */
228229
let mut iterable = Self::builtin_poly_trait(ITERABLE, vec![PS::t_nd(TY_T)], 2);
229230
iterable.register_superclass(poly(OUTPUT, vec![ty_tp(T.clone())]), &output);
230-
let Slf = mono_q(SELF, subtypeof(poly(ITERABLE, vec![ty_tp(T.clone())])));
231-
let t = fn0_met(Slf.clone(), proj(Slf, ITER)).quantify();
232-
iterable.register_builtin_decl(
233-
FUNC_ITER,
234-
t,
235-
Visibility::BUILTIN_PUBLIC,
236-
Some(FUNDAMENTAL_ITER),
237-
);
238-
iterable.register_builtin_erg_decl(ITER, Type, Visibility::BUILTIN_PUBLIC);
231+
if PYTHON_MODE {
232+
let t = fn0_met(
233+
poly(ITERABLE, vec![ty_tp(T.clone())]),
234+
poly(ITERATOR, vec![ty_tp(T.clone())]),
235+
)
236+
.quantify();
237+
iterable.register_builtin_erg_decl(FUNDAMENTAL_ITER, t, Visibility::BUILTIN_PUBLIC);
238+
} else {
239+
let Slf = mono_q(SELF, subtypeof(poly(ITERABLE, vec![ty_tp(T.clone())])));
240+
let t = fn0_met(Slf.clone(), proj(Slf, ITER)).quantify();
241+
iterable.register_builtin_decl(
242+
FUNC_ITER,
243+
t,
244+
Visibility::BUILTIN_PUBLIC,
245+
Some(FUNDAMENTAL_ITER),
246+
);
247+
iterable.register_builtin_erg_decl(ITER, Type, Visibility::BUILTIN_PUBLIC);
248+
}
239249
let Slf = poly(ITERABLE, vec![ty_tp(T.clone())]);
240250
let U = type_q(TY_U);
241251
let t_map = fn1_met(
@@ -244,9 +254,10 @@ impl Context {
244254
poly(MAP, vec![ty_tp(U.clone())]),
245255
)
246256
.quantify();
247-
iterable.register_builtin_decl(
257+
iterable.register_builtin_py_impl(
248258
FUNC_MAP,
249259
t_map,
260+
Mutability::Immutable,
250261
Visibility::BUILTIN_PUBLIC,
251262
Some("Function::iterable_map"),
252263
);
@@ -268,9 +279,10 @@ impl Context {
268279
)
269280
.quantify();
270281
let t_filter = t_filter.with_default_intersec_index(1);
271-
iterable.register_builtin_decl(
282+
iterable.register_builtin_py_impl(
272283
FUNC_FILTER,
273284
t_filter,
285+
Mutability::Immutable,
274286
Visibility::BUILTIN_PUBLIC,
275287
Some("Function::iterable_filter"),
276288
);
@@ -279,9 +291,10 @@ impl Context {
279291
vec![TyParam::List(vec![ty_tp(Nat), ty_tp(T.clone())])],
280292
);
281293
let t_enumerate = fn0_met(Slf.clone(), poly(ITERATOR, vec![ty_tp(ret_t)])).quantify();
282-
iterable.register_builtin_decl(
294+
iterable.register_builtin_py_impl(
283295
FUNC_ENUMERATE,
284296
t_enumerate,
297+
Mutability::Immutable,
285298
Visibility::BUILTIN_PUBLIC,
286299
Some("Function::enumerate"),
287300
);
@@ -291,9 +304,10 @@ impl Context {
291304
poly(ZIP, vec![ty_tp(T.clone()), ty_tp(U.clone())]),
292305
)
293306
.quantify();
294-
iterable.register_builtin_decl(
307+
iterable.register_builtin_py_impl(
295308
FUNC_ZIP,
296309
t_zip,
310+
Mutability::Immutable,
297311
Visibility::BUILTIN_PUBLIC,
298312
Some("Function::zip"),
299313
);
@@ -304,59 +318,67 @@ impl Context {
304318
T.clone(),
305319
)
306320
.quantify();
307-
iterable.register_builtin_decl(
321+
iterable.register_builtin_py_impl(
308322
FUNC_REDUCE,
309323
t_reduce,
324+
Mutability::Immutable,
310325
Visibility::BUILTIN_PUBLIC,
311326
Some("Function::iterable_reduce"),
312327
);
313328
let t_nth = fn1_met(Slf.clone(), Nat, T.clone()).quantify();
314-
iterable.register_builtin_decl(
329+
iterable.register_builtin_py_impl(
315330
FUNC_NTH,
316331
t_nth,
332+
Mutability::Immutable,
317333
Visibility::BUILTIN_PUBLIC,
318334
Some("Function::iterable_nth"),
319335
);
320336
let t_skip = fn1_met(Slf.clone(), Nat, poly(ITERATOR, vec![ty_tp(T.clone())])).quantify();
321-
iterable.register_builtin_decl(
337+
iterable.register_builtin_py_impl(
322338
FUNC_SKIP,
323339
t_skip,
340+
Mutability::Immutable,
324341
Visibility::BUILTIN_PUBLIC,
325342
Some("Function::iterable_skip"),
326343
);
327344
let t_all = fn1_met(Slf.clone(), func1(T.clone(), Bool), Bool).quantify();
328-
iterable.register_builtin_decl(
345+
iterable.register_builtin_py_impl(
329346
FUNC_ALL,
330347
t_all,
348+
Mutability::Immutable,
331349
Visibility::BUILTIN_PUBLIC,
332350
Some("Function::iterable_all"),
333351
);
334352
let t_any = fn1_met(Slf.clone(), func1(T.clone(), Bool), Bool).quantify();
335-
iterable.register_builtin_decl(
353+
iterable.register_builtin_py_impl(
336354
FUNC_ANY,
337355
t_any,
356+
Mutability::Immutable,
338357
Visibility::BUILTIN_PUBLIC,
339358
Some("Function::iterable_any"),
340359
);
341360
let t_reversed = fn0_met(Slf.clone(), poly(ITERATOR, vec![ty_tp(T.clone())])).quantify();
342-
iterable.register_builtin_decl(
361+
iterable.register_builtin_py_impl(
343362
FUNC_REVERSED,
344363
t_reversed,
364+
Mutability::Immutable,
345365
Visibility::BUILTIN_PUBLIC,
346366
Some("Function::reversed"),
347367
);
348368
let t_position = fn1_met(Slf.clone(), func1(T.clone(), Bool), or(Nat, NoneType)).quantify();
349-
iterable.register_builtin_decl(
369+
iterable.register_builtin_py_impl(
350370
FUNC_POSITION,
351371
t_position,
372+
Mutability::Immutable,
352373
Visibility::BUILTIN_PUBLIC,
353374
Some("Function::iterable_position"),
354375
);
355376
let t_find =
356377
fn1_met(Slf.clone(), func1(T.clone(), Bool), or(T.clone(), NoneType)).quantify();
357-
iterable.register_builtin_decl(
378+
iterable.register_builtin_py_impl(
358379
FUNC_FIND,
359380
t_find,
381+
Mutability::Immutable,
360382
Visibility::BUILTIN_PUBLIC,
361383
Some("Function::iterable_find"),
362384
);
@@ -369,16 +391,18 @@ impl Context {
369391
poly(ITERATOR, vec![ty_tp(T.clone())]),
370392
)
371393
.quantify();
372-
iterable.register_builtin_decl(
394+
iterable.register_builtin_py_impl(
373395
FUNC_CHAIN,
374396
t_chain,
397+
Mutability::Immutable,
375398
Visibility::BUILTIN_PUBLIC,
376399
Some("Function::iterable_chain"),
377400
);
378401
let t_to_list = fn0_met(Slf.clone(), unknown_len_list_t(T.clone())).quantify();
379-
iterable.register_builtin_decl(
402+
iterable.register_builtin_py_impl(
380403
FUNC_TO_LIST,
381404
t_to_list,
405+
Mutability::Immutable,
382406
Visibility::BUILTIN_PUBLIC,
383407
Some("Function::list"),
384408
);
@@ -396,7 +420,7 @@ impl Context {
396420
);
397421
/* Container */
398422
let mut container = Self::builtin_poly_trait(CONTAINER, vec![PS::t_nd(TY_T)], 2);
399-
let op_t = fn1_met(mono(CONTAINER), T.clone(), Bool).quantify();
423+
let op_t = fn1_met(poly(CONTAINER, vec![ty_tp(T.clone())]), T.clone(), Bool).quantify();
400424
container.register_superclass(poly(OUTPUT, vec![ty_tp(T.clone())]), &output);
401425
container.register_builtin_erg_decl(FUNDAMENTAL_CONTAINS, op_t, Visibility::BUILTIN_PUBLIC);
402426
/* Collection */

crates/erg_compiler/hir.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,10 @@ impl Identifier {
554554
Call::new(Expr::Accessor(Accessor::Ident(self)), None, args)
555555
}
556556

557+
pub fn method_call(self, attr_name: Identifier, args: Args) -> Call {
558+
Call::new(Expr::Accessor(Accessor::Ident(self)), Some(attr_name), args)
559+
}
560+
557561
pub fn is_py_api(&self) -> bool {
558562
self.vi.py_name.is_some()
559563
}
@@ -1095,6 +1099,14 @@ impl_display_for_enum!(Dict; Normal, Comprehension);
10951099
impl_locational_for_enum!(Dict; Normal, Comprehension);
10961100
impl_t_for_enum!(Dict; Normal, Comprehension);
10971101

1102+
impl Dict {
1103+
pub fn empty() -> Self {
1104+
let l_brace = Token::from_str(TokenKind::LBrace, "{");
1105+
let r_brace = Token::from_str(TokenKind::RBrace, "}");
1106+
Self::Normal(NormalDict::new(l_brace, r_brace, HashMap::new(), vec![]))
1107+
}
1108+
}
1109+
10981110
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10991111
pub struct NormalSet {
11001112
pub l_brace: Token,
@@ -1194,6 +1206,19 @@ impl_display_for_enum!(Set; Normal, WithLength);
11941206
impl_locational_for_enum!(Set; Normal, WithLength);
11951207
impl_t_for_enum!(Set; Normal, WithLength);
11961208

1209+
impl Set {
1210+
pub fn empty() -> Self {
1211+
let l_brace = Token::from_str(TokenKind::LBrace, "{");
1212+
let r_brace = Token::from_str(TokenKind::RBrace, "}");
1213+
Self::Normal(NormalSet::new(
1214+
l_brace,
1215+
r_brace,
1216+
Type::Uninited,
1217+
Args::empty(),
1218+
))
1219+
}
1220+
}
1221+
11971222
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11981223
pub struct RecordAttrs(Vec<Def>);
11991224

@@ -3123,6 +3148,14 @@ impl Expr {
31233148
))
31243149
}
31253150

3151+
pub fn method_call(self, attr_name: Identifier, args: Args) -> Call {
3152+
Call::new(self, Some(attr_name), args)
3153+
}
3154+
3155+
pub fn method_call_expr(self, attr_name: Identifier, args: Args) -> Self {
3156+
Self::Call(self.method_call(attr_name, args))
3157+
}
3158+
31263159
pub fn attr(self, ident: Identifier) -> Accessor {
31273160
Accessor::attr(self, ident)
31283161
}

0 commit comments

Comments
 (0)