Skip to content

Commit 9887d78

Browse files
committed
fix: Dict::get
1 parent f6145d0 commit 9887d78

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

crates/erg_compiler/context/initialize/classes.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -2359,14 +2359,13 @@ impl Context {
23592359
)));
23602360
dict_.register_builtin_const(FUNC_AS_RECORD, Visibility::BUILTIN_PUBLIC, None, as_record);
23612361
let Def = type_q(TY_DEFAULT);
2362+
let K = type_q(TY_K);
2363+
let V = type_q(TY_V);
23622364
let get_t = no_var_fn_met(
2363-
dict_t.clone(),
2364-
vec![kw(KW_KEY, T.clone())],
2365+
dict! { K.clone() => V.clone() }.into(),
2366+
vec![kw(KW_KEY, K.clone())],
23652367
vec![kw_default(KW_DEFAULT, Def.clone(), NoneType)],
2366-
or(
2367-
proj_call(D.clone(), FUNDAMENTAL_GETITEM, vec![ty_tp(T.clone())]),
2368-
Def,
2369-
),
2368+
or(V.clone(), Def),
23702369
)
23712370
.quantify();
23722371
dict_.register_py_builtin(FUNC_GET, get_t, Some(FUNC_GET), 9);

crates/erg_compiler/context/initialize/funcs.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1077,8 +1077,9 @@ impl Context {
10771077
);
10781078
let E = mono_q(TY_E, subtypeof(mono(EQ)));
10791079
let E2 = mono_q(TY_E, subtypeof(mono(IRREGULAR_EQ)));
1080-
let op_t = bin_op(E.clone(), E, Bool).quantify()
1081-
& bin_op(E2.clone(), E2.clone(), E2.proj(OUTPUT)).quantify();
1080+
let op_t = (bin_op(E.clone(), E, Bool).quantify()
1081+
& bin_op(E2.clone(), E2.clone(), E2.proj(OUTPUT)).quantify())
1082+
.with_default_intersec_index(0);
10821083
self.register_builtin_py_impl(
10831084
OP_EQ,
10841085
op_t.clone(),

crates/erg_compiler/lower.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,17 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
12211221
Some(guard(namespace, target, to))
12221222
}
12231223
TokenKind::Symbol if &op.content[..] == "isinstance" => {
1224-
let to = self.module.context.expr_to_type(rhs.clone()).ok()?;
1224+
// isinstance(x, (T, U)) => x: T or U
1225+
let to = if let ast::Expr::Tuple(ast::Tuple::Normal(tys)) = rhs {
1226+
tys.elems.pos_args.iter().fold(Type::Never, |acc, ex| {
1227+
let Ok(ty) = self.module.context.expr_to_type(ex.expr.clone()) else {
1228+
return acc;
1229+
};
1230+
self.module.context.union(&acc, &ty)
1231+
})
1232+
} else {
1233+
self.module.context.expr_to_type(rhs.clone()).ok()?
1234+
};
12251235
Some(guard(namespace, target, to))
12261236
}
12271237
TokenKind::IsOp | TokenKind::DblEq => {

tests/should_err/mut_dict.er

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ dict = !d
44
dict.insert! "b", 2
55
_ = dict.get("a") == "a" # ERR
66
_ = dict.get("b") == "a" # ERR
7-
_ = dict.get("c") # ERR
7+
_ = dict.get("c") # OK
8+
_ = dict["b"] # OK
9+
_ = dict["c"] # ERR

tests/should_ok/dict.er

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ for! {"a": 1, "b": 2}.values(), i =>
66
dic = { "a": 1, "b": 2 }
77
assert dic.concat({ "c": 3 }) == { "a": 1, "b": 2, "c": 3 }
88
assert dic.diff({ "a": 1 }) == { "b": 2 }
9+
assert dic.get("a"+"b", 3) == 3
910
rec = dic.as_record()
1011
assert rec.a == 1 and rec.b == 2

0 commit comments

Comments
 (0)