@@ -1710,6 +1710,17 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
1710
1710
if (AST::is_a<AST::Name_t>(*s->m_slice ) || AST::is_a<AST::Subscript_t>(*s->m_slice )) {
1711
1711
ASR::ttype_t *type = ast_expr_to_asr_type (loc, *s->m_slice ,
1712
1712
is_allocatable, is_const, raise_error, abi, is_argument);
1713
+ if (!is_hashable (type)) {
1714
+ diag.add (diag::Diagnostic (
1715
+ " Unhashable type: '" + ASRUtils::type_to_str (type) + " '" ,
1716
+ diag::Level::Error, diag::Stage::Semantic, {
1717
+ diag::Label (" Mutable type '" + ASRUtils::type_to_str (type)
1718
+ + " ' cannot be stored in a set." ,
1719
+ {s->m_slice ->base .loc })
1720
+ })
1721
+ );
1722
+ throw SemanticAbort ();
1723
+ }
1713
1724
return ASRUtils::TYPE (ASR::make_Set_t (al, loc, type));
1714
1725
} else {
1715
1726
throw SemanticError (" Only Name in Subscript supported for now in `set`"
@@ -1745,6 +1756,17 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
1745
1756
}
1746
1757
ASR::ttype_t *key_type = ast_expr_to_asr_type (loc, *t->m_elts [0 ],
1747
1758
is_allocatable, is_const, raise_error, abi, is_argument);
1759
+ if (!is_hashable (key_type)) {
1760
+ diag.add (diag::Diagnostic (
1761
+ " Unhashable type: '" + ASRUtils::type_to_str (key_type) + " '" ,
1762
+ diag::Level::Error, diag::Stage::Semantic, {
1763
+ diag::Label (" Mutable type '" + ASRUtils::type_to_str (key_type)
1764
+ + " ' cannot become a key in dict. Hint: Use an immutable type for key." ,
1765
+ {t->m_elts [0 ]->base .loc })
1766
+ })
1767
+ );
1768
+ throw SemanticAbort ();
1769
+ }
1748
1770
ASR::ttype_t *value_type = ast_expr_to_asr_type (loc, *t->m_elts [1 ],
1749
1771
is_allocatable, is_const, raise_error, abi, is_argument);
1750
1772
raise_error_when_dict_key_is_float_or_complex (key_type, loc);
@@ -3788,7 +3810,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
3788
3810
ai.m_step , type, nullptr );
3789
3811
return false ;
3790
3812
} else if (ASR::is_a<ASR::Dict_t>(*type)) {
3791
- throw SemanticError (" unhashable type in dict: 'slice'" , loc);
3813
+ throw SemanticError (" Unhashable type in dict: 'slice'" , loc);
3792
3814
}
3793
3815
} else if (AST::is_a<AST::Tuple_t>(*m_slice) &&
3794
3816
ASRUtils::is_array (type)) {
@@ -6106,8 +6128,15 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
6106
6128
if (key_type == nullptr ) {
6107
6129
key_type = ASRUtils::expr_type (key);
6108
6130
if (!is_hashable (key_type)) {
6109
- throw SemanticError (" unhashable type: '" + ASRUtils::type_to_str (key_type) + " '" ,
6110
- key->base .loc );
6131
+ diag.add (diag::Diagnostic (
6132
+ " Unhashable type: '" + ASRUtils::type_to_str (key_type) + " '" ,
6133
+ diag::Level::Error, diag::Stage::Semantic, {
6134
+ diag::Label (" Mutable type '" + ASRUtils::type_to_str (key_type)
6135
+ + " ' cannot become a key in dict. Hint: Use an immutable type for key." ,
6136
+ {key->base .loc })
6137
+ })
6138
+ );
6139
+ throw SemanticAbort ();
6111
6140
}
6112
6141
} else {
6113
6142
if (!ASRUtils::check_equal_type (ASRUtils::expr_type (key), key_type)) {
@@ -6579,8 +6608,15 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
6579
6608
if (type == nullptr ) {
6580
6609
type = ASRUtils::expr_type (value);
6581
6610
if (!is_hashable (type)) {
6582
- throw SemanticError (" unhashable type: '" + ASRUtils::type_to_str (type) + " '" ,
6583
- type->base .loc );
6611
+ diag.add (diag::Diagnostic (
6612
+ " Unhashable type: '" + ASRUtils::type_to_str (type) + " '" ,
6613
+ diag::Level::Error, diag::Stage::Semantic, {
6614
+ diag::Label (" Mutable type '" + ASRUtils::type_to_str (type)
6615
+ + " ' cannot be stored in a set." ,
6616
+ {value->base .loc })
6617
+ })
6618
+ );
6619
+ throw SemanticAbort ();
6584
6620
}
6585
6621
} else {
6586
6622
if (!ASRUtils::check_equal_type (ASRUtils::expr_type (value), type)) {
0 commit comments