diff --git a/integration_tests/test_set_constructor.py b/integration_tests/test_set_constructor.py new file mode 100644 index 0000000000..497819dbb7 --- /dev/null +++ b/integration_tests/test_set_constructor.py @@ -0,0 +1,17 @@ +def test_empty_set(): + a: set[i32] = set() + assert len(a) == 0 + a.add(2) + a.remove(2) + a.add(3) + assert a.pop() == 3 + + b: set[str] = set() + + assert len(b) == 0 + b.add('a') + b.remove('a') + b.add('b') + assert b.pop() == 3 + +test_empty_set() diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 4909e0461b..dd61ef6fdf 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -8622,6 +8622,20 @@ we will have to use something else. ASRUtils::type_to_str(type) + " type.", x.base.base.loc); } return; + } else if( call_name == "set" ) { + parse_args(x, args); + if (args.size() == 0) { + if( assign_asr_target != nullptr ) { + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, nullptr, 0, + ASRUtils::expr_type(assign_asr_target)); + } + else { + tmp = nullptr; + } + return ; + } + + throw SemanticError("set is only used for an empty set for now.", x.base.base.loc); } else if( call_name == "deepcopy" ) { parse_args(x, args); if( args.size() != 1 ) {