@@ -4771,8 +4771,8 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
4771
4771
expr = expr.expr
4772
4772
}
4773
4773
if node.op == .amp {
4774
- if node.right is ast.Nil {
4775
- c.error ('invalid operation: cannot take address of nil' , node.right .pos ())
4774
+ if expr is ast.Nil {
4775
+ c.error ('invalid operation: cannot take address of nil' , expr .pos ())
4776
4776
}
4777
4777
if mut node.right is ast.PrefixExpr {
4778
4778
if node.right.op == .amp {
@@ -4782,76 +4782,78 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
4782
4782
if expr.op == .amp {
4783
4783
c.error ('cannot take the address of this expression' , expr.pos)
4784
4784
}
4785
- } else if mut node.right is ast.SelectorExpr {
4786
- if node.right .expr.is_literal () {
4787
- c.error ('cannot take the address of a literal value' , node.pos.extend (node.right .pos))
4788
- } else if node.right .expr is ast.StructInit {
4785
+ } else if mut expr is ast.SelectorExpr {
4786
+ if expr .expr.is_literal () {
4787
+ c.error ('cannot take the address of a literal value' , node.pos.extend (expr .pos))
4788
+ } else if expr .expr is ast.StructInit {
4789
4789
c.error ('should not create object instance on the heap to simply access a member' ,
4790
- node.pos.extend (node.right .pos))
4790
+ node.pos.extend (expr .pos))
4791
4791
}
4792
4792
right_sym := c.table.sym (right_type)
4793
- expr_sym := c.table.sym (node.right .expr_type)
4793
+ expr_sym := c.table.sym (expr .expr_type)
4794
4794
if expr_sym.kind == .struct && (expr_sym.info as ast.Struct ).is_minify
4795
- && (node.right .typ == ast.bool_type_idx || (right_sym.kind == .enum
4795
+ && (expr .typ == ast.bool_type_idx || (right_sym.kind == .enum
4796
4796
&& ! (right_sym.info as ast.Enum ).is_flag
4797
4797
&& ! (right_sym.info as ast.Enum ).uses_exprs)) {
4798
- c.error ('cannot take the address of field in struct `${c.table.type_to_str(node.right .expr_type)} `, which is tagged as `@[minify]`' ,
4799
- node.pos.extend (node.right .pos))
4798
+ c.error ('cannot take the address of field in struct `${c.table.type_to_str(expr .expr_type)} `, which is tagged as `@[minify]`' ,
4799
+ node.pos.extend (expr .pos))
4800
4800
}
4801
4801
4802
- if node.right .typ.has_flag (.option) {
4803
- c.error ('cannot take the address of an Option field' , node.pos.extend (node.right .pos))
4802
+ if expr .typ.has_flag (.option) {
4803
+ c.error ('cannot take the address of an Option field' , node.pos.extend (expr .pos))
4804
4804
}
4805
4805
}
4806
4806
}
4807
4807
// TODO: testing ref/deref strategy
4808
4808
right_is_ptr := right_type.is_ptr ()
4809
- if node.op == .amp && (! right_is_ptr || (right_is_ptr && node.right is ast.CallExpr )) {
4809
+ if node.op == .amp && (! right_is_ptr || (right_is_ptr && expr is ast.CallExpr )) {
4810
4810
if expr in [ast.BoolLiteral, ast.CallExpr, ast.CharLiteral, ast.FloatLiteral, ast.IntegerLiteral,
4811
4811
ast.InfixExpr, ast.StringLiteral, ast.StringInterLiteral] {
4812
4812
c.error ('cannot take the address of ${expr} ' , node.pos)
4813
4813
}
4814
- if mut node.right is ast.Ident {
4815
- if node.right .kind == .constant && ! c.inside_unsafe && c.pref.experimental {
4816
- c.warn ('cannot take the address of const outside `unsafe`' , node.right .pos)
4814
+ if mut expr is ast.Ident {
4815
+ if expr .kind == .constant && ! c.inside_unsafe && c.pref.experimental {
4816
+ c.warn ('cannot take the address of const outside `unsafe`' , expr .pos)
4817
4817
}
4818
4818
}
4819
- if node.right is ast.SelectorExpr {
4819
+ if expr is ast.SelectorExpr {
4820
4820
typ_sym := c.table.sym (right_type)
4821
4821
if typ_sym.kind == .map && ! c.inside_unsafe {
4822
4822
c.error ('cannot take the address of map values outside `unsafe`' , node.pos)
4823
4823
}
4824
4824
}
4825
- if mut node.right is ast.IndexExpr {
4826
- typ_sym := c.table.sym (node.right .left_type)
4825
+ if mut expr is ast.IndexExpr {
4826
+ typ_sym := c.table.sym (expr .left_type)
4827
4827
mut is_mut := false
4828
- if mut node.right .left is ast.Ident {
4829
- ident := node.right .left
4828
+ if mut expr .left is ast.Ident {
4829
+ ident := expr .left
4830
4830
// TODO: temporary, remove this
4831
4831
ident_obj := ident.obj
4832
4832
if ident_obj is ast.Var {
4833
4833
is_mut = ident_obj.is_mut
4834
4834
}
4835
4835
}
4836
- if typ_sym.kind == .map {
4837
- c.error ('cannot take the address of map values' , node.right.pos)
4838
- }
4839
4836
if ! c.inside_unsafe {
4840
4837
if typ_sym.kind == .array && is_mut {
4841
4838
c.error ('cannot take the address of mutable array elements outside unsafe blocks' ,
4842
- node.right.pos)
4839
+ expr.pos)
4840
+ }
4841
+
4842
+ if typ_sym.kind == .map {
4843
+ c.error ('cannot take the address of map values outside `unsafe`' ,
4844
+ expr.pos)
4843
4845
}
4844
4846
}
4845
4847
}
4846
4848
if ! c.inside_fn_arg && ! c.inside_unsafe {
4847
- c.mark_as_referenced (mut & node.right , false )
4849
+ c.mark_as_referenced (mut & expr , false )
4848
4850
}
4849
4851
return right_type.ref ()
4850
- } else if node.op == .amp && node.right ! is ast.CastExpr {
4852
+ } else if node.op == .amp && expr ! is ast.CastExpr {
4851
4853
if ! c.inside_fn_arg && ! c.inside_unsafe {
4852
- c.mark_as_referenced (mut & node.right , false )
4854
+ c.mark_as_referenced (mut & expr , false )
4853
4855
}
4854
- if node.right .is_auto_deref_var () {
4856
+ if expr .is_auto_deref_var () {
4855
4857
return right_type
4856
4858
} else {
4857
4859
return right_type.ref ()
@@ -4861,7 +4863,7 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
4861
4863
if node.op == .mul {
4862
4864
if right_type.has_flag (.option) {
4863
4865
c.error ('type `?${right_sym.name} ` is an Option, it must be unwrapped first; use `*var?` to do it' ,
4864
- node.right .pos ())
4866
+ expr .pos ())
4865
4867
}
4866
4868
if right_type.is_ptr () {
4867
4869
return right_type.deref ()
@@ -4874,11 +4876,11 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
4874
4876
if right_type.is_voidptr () {
4875
4877
c.error ('cannot dereference to void' , node.pos)
4876
4878
}
4877
- if mut node.right is ast.Ident {
4878
- if var := node.right. scope.find_var ('${node.right .name} ' ) {
4879
+ if mut expr is ast.Ident {
4880
+ if var := expr. scope.find_var ('${expr .name} ' ) {
4879
4881
if var.expr is ast.UnsafeExpr {
4880
4882
if var.expr.expr is ast.Nil {
4881
- c.error ('cannot deference a `nil` pointer' , node.right .pos)
4883
+ c.error ('cannot deference a `nil` pointer' , expr .pos)
4882
4884
}
4883
4885
}
4884
4886
}
0 commit comments