diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 5ad8a0074d..2c360fd51d 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -592,6 +592,7 @@ RUN(NAME test_set_len LABELS cpython llvm llvm_jit) RUN(NAME test_set_add LABELS cpython llvm llvm_jit) RUN(NAME test_set_remove LABELS cpython llvm llvm_jit) RUN(NAME test_set_discard LABELS cpython llvm llvm_jit) +RUN(NAME test_set_from_list LABELS cpython llvm llvm_jit) RUN(NAME test_set_clear LABELS cpython llvm) RUN(NAME test_set_pop LABELS cpython llvm) RUN(NAME test_global_set LABELS cpython llvm llvm_jit) diff --git a/integration_tests/test_set_from_list.py b/integration_tests/test_set_from_list.py new file mode 100644 index 0000000000..e06b4e40c1 --- /dev/null +++ b/integration_tests/test_set_from_list.py @@ -0,0 +1,14 @@ +from lpython import i32 + + +def test_set(): + s: set[i32] + s = set([1, 2, 2, 2, -1, 1, 1, 3]) + assert len(s) == 4 + + s2: set[str] + s2 = set(["a", "b", "b", "abc", "a"]) + assert len(s2) == 3 + + +test_set() diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 63c87dbe4a..fac917eaf4 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -8789,9 +8789,33 @@ we will have to use something else. tmp = nullptr; } return ; + } else if (args.size() > 1) { + throw SemanticError("set accepts only 1 argument for now, got " + + std::to_string(args.size()) + " arguments instead.", + x.base.base.loc); } - - throw SemanticError("set is only used for an empty set for now.", x.base.base.loc); + if ( assign_asr_target == nullptr ) { + throw SemanticError("set from list cannot be called without target type for now", x.base.base.loc); + } + ASR::expr_t *arg = args[0].m_value; + ASR::ttype_t *type = ASRUtils::expr_type(arg); + if(!ASR::is_a(*arg)) { + throw SemanticError("set accepts only list constant for now, got " + + ASRUtils::type_to_str(type) + " type.", x.base.base.loc); + } + ASR::ListConstant_t* list = ASR::down_cast(arg); + ASR::expr_t **m_args = list->m_args; + size_t n_args = list->n_args; + ASR::ttype_t* value_type = ASRUtils::get_contained_type(type); + ASR::ttype_t* target_type = ASRUtils::get_contained_type(ASRUtils::expr_type(assign_asr_target)); + if (!ASRUtils::check_equal_type(target_type, value_type)){ + std::string ltype = ASRUtils::type_to_str_python(target_type); + std::string rtype = ASRUtils::type_to_str_python(value_type); + throw SemanticError("type mismatch ('" + ltype + "' and '" + rtype + "')", x.base.base.loc); + } + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, m_args, n_args, + ASRUtils::expr_type(assign_asr_target)); + return ; } else if( call_name == "deepcopy" ) { parse_args(x, args); if( args.size() != 1 ) {