Skip to content

Commit 41f03e9

Browse files
committed
Fixed type mismatch
1 parent 24a03ad commit 41f03e9

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ RUN(NAME test_set_len LABELS cpython llvm llvm_jit)
592592
RUN(NAME test_set_add LABELS cpython llvm llvm_jit)
593593
RUN(NAME test_set_remove LABELS cpython llvm llvm_jit)
594594
RUN(NAME test_set_discard LABELS cpython llvm llvm_jit)
595+
RUN(NAME test_set_from_list LABELS cpython llvm llvm_jit)
595596
RUN(NAME test_set_clear LABELS cpython llvm)
596597
RUN(NAME test_set_pop LABELS cpython llvm)
597598
RUN(NAME test_global_set LABELS cpython llvm llvm_jit)

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8790,23 +8790,32 @@ we will have to use something else.
87908790
}
87918791
return ;
87928792
} else if (args.size() > 1) {
8793-
throw SemanticError("set() accepts only 1 argument for now, got " +
8793+
throw SemanticError("set accepts only 1 argument for now, got " +
87948794
std::to_string(args.size()) + " arguments instead.",
87958795
x.base.base.loc);
87968796
}
8797+
if ( assign_asr_target == nullptr ) {
8798+
throw SemanticError("set from list cannot be called without target type for now", x.base.base.loc);
8799+
}
87978800
ASR::expr_t *arg = args[0].m_value;
87988801
ASR::ttype_t *type = ASRUtils::expr_type(arg);
8799-
if(ASR::is_a<ASR::ListConstant_t>(*arg)) {
8800-
ASR::ListConstant_t* list = ASR::down_cast<ASR::ListConstant_t>(arg);
8801-
ASR::expr_t **m_args = list->m_args;
8802-
size_t n_args = list->n_args;
8803-
8804-
tmp = ASR::make_SetConstant_t(al, x.base.base.loc, m_args, n_args,
8805-
ASRUtils::expr_type(assign_asr_target));
8806-
return ;
8807-
}
8808-
throw SemanticError("set() accepts only list for now, got " +
8802+
if(!ASR::is_a<ASR::ListConstant_t>(*arg)) {
8803+
throw SemanticError("set accepts only list for now, got " +
88098804
ASRUtils::type_to_str(type) + " type.", x.base.base.loc);
8805+
}
8806+
ASR::ListConstant_t* list = ASR::down_cast<ASR::ListConstant_t>(arg);
8807+
ASR::expr_t **m_args = list->m_args;
8808+
size_t n_args = list->n_args;
8809+
ASR::ttype_t* value_type = ASRUtils::get_contained_type(type);
8810+
ASR::ttype_t* target_type = ASRUtils::get_contained_type(ASRUtils::expr_type(assign_asr_target));
8811+
if (!ASRUtils::check_equal_type(target_type, value_type)){
8812+
std::string ltype = ASRUtils::type_to_str_python(target_type);
8813+
std::string rtype = ASRUtils::type_to_str_python(value_type);
8814+
throw SemanticError("type mismatch ('" + ltype + "' and '" + rtype + "')", x.base.base.loc);
8815+
}
8816+
tmp = ASR::make_SetConstant_t(al, x.base.base.loc, m_args, n_args,
8817+
ASRUtils::expr_type(assign_asr_target));
8818+
return ;
88108819
} else if( call_name == "deepcopy" ) {
88118820
parse_args(x, args);
88128821
if( args.size() != 1 ) {

0 commit comments

Comments
 (0)