1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

allow defining methods on aliases

This commit is contained in:
Alexander Medvednikov 2019-07-10 14:18:21 +02:00
parent 00ea112b66
commit fba8443f30
3 changed files with 11 additions and 12 deletions

View File

@ -144,7 +144,7 @@ fn (p mut Parser) fn_decl() {
// Don't allow modifying types from a different module // Don't allow modifying types from a different module
if !p.first_run() && !p.builtin_pkg && T.mod != p.mod { if !p.first_run() && !p.builtin_pkg && T.mod != p.mod {
println('T.mod=$T.mod') println('T.mod=$T.mod')
println('pkg=$p.mod') println('p.mod=$p.mod')
p.error('cannot define new methods on non-local type `$receiver_typ`') p.error('cannot define new methods on non-local type `$receiver_typ`')
} }
// (a *Foo) instead of (a mut Foo) is a common mistake // (a *Foo) instead of (a mut Foo) is a common mistake
@ -533,7 +533,7 @@ fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type strin
receiver := f.args.first() receiver := f.args.first()
if receiver.is_mut && !p.expr_var.is_mut { if receiver.is_mut && !p.expr_var.is_mut {
println('$method_call recv=$receiver.name recv_mut=$receiver.is_mut') println('$method_call recv=$receiver.name recv_mut=$receiver.is_mut')
p.error('`$p.expr_var.name` is imkey_mut') p.error('`$p.expr_var.name` is immutable')
} }
// if receiver is key_mut or a ref (&), generate & for the first arg // if receiver is key_mut or a ref (&), generate & for the first arg
if receiver.ref || (receiver.is_mut && !receiver_type.contains('*')) { if receiver.ref || (receiver.is_mut && !receiver_type.contains('*')) {

View File

@ -391,7 +391,7 @@ fn (p mut Parser) type_decl() {
// so specify "struct" // so specify "struct"
_struct := if !parent.contains('[') && !parent.starts_with('fn ') && !p.table.known_type(parent){'struct'} else { ''} _struct := if !parent.contains('[') && !parent.starts_with('fn ') && !p.table.known_type(parent){'struct'} else { ''}
p.gen_typedef('typedef $_struct $nt_pair; // type alias name="$name" parent="$parent"') p.gen_typedef('typedef $_struct $nt_pair; // type alias name="$name" parent="$parent"')
p.table.register_type_with_parent(name, parent) p.register_type_with_parent(name, parent)
} }
fn (p mut Parser) interface_method(field_name, receiver string) &Fn { fn (p mut Parser) interface_method(field_name, receiver string) &Fn {
@ -1066,7 +1066,7 @@ fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) {
p.log('assign_statement() name=$v.name tok=') p.log('assign_statement() name=$v.name tok=')
tok := p.tok tok := p.tok
if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{ if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{
p.error('`$v.name` is imkey_mut') p.error('`$v.name` is immutable')
} }
is_str := v.typ == 'string' is_str := v.typ == 'string'
switch tok { switch tok {
@ -1488,7 +1488,7 @@ fn (p mut Parser) var_expr(v Var) string {
// a++ and a-- // a++ and a--
if p.tok == .inc || p.tok == .dec { if p.tok == .inc || p.tok == .dec {
if !v.is_mut && !v.is_arg && !p.pref.translated { if !v.is_mut && !v.is_arg && !p.pref.translated {
p.error('`$v.name` is imkey_mut') p.error('`$v.name` is immutable')
} }
if typ != 'int' { if typ != 'int' {
if !p.pref.translated && !is_number_type(typ) { if !p.pref.translated && !is_number_type(typ) {
@ -1567,7 +1567,7 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string {
modifying := next.is_assign() || next == .inc || next == .dec modifying := next.is_assign() || next == .inc || next == .dec
is_vi := p.fileis('vi') is_vi := p.fileis('vi')
if !p.builtin_pkg && !p.pref.translated && modifying && !field.is_mut && !is_vi { if !p.builtin_pkg && !p.pref.translated && modifying && !field.is_mut && !is_vi {
p.error('cannot modify imkey_mut field `$field_name` (type `$typ.name`)') p.error('cannot modify immutable field `$field_name` (type `$typ.name`)')
} }
if !p.builtin_pkg && p.mod != typ.mod { if !p.builtin_pkg && p.mod != typ.mod {
} }
@ -1582,7 +1582,7 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string {
// Don't allow `str.len = 0` // Don't allow `str.len = 0`
if field.access_mod == .public && !p.builtin_pkg && p.mod != typ.mod { if field.access_mod == .public && !p.builtin_pkg && p.mod != typ.mod {
if !field.is_mut && !p.pref.translated && modifying { if !field.is_mut && !p.pref.translated && modifying {
p.error('cannot modify public imkey_mut field `$field_name` (type `$typ.name`)') p.error('cannot modify public immutable field `$field_name` (type `$typ.name`)')
} }
} }
p.gen(dot + field_name) p.gen(dot + field_name)
@ -1725,7 +1725,7 @@ fn (p mut Parser) index_expr(typ string, fn_ph int) string {
p.tok == .or_assign || p.tok == .and_assign || p.tok == .righ_shift_assign || p.tok == .or_assign || p.tok == .and_assign || p.tok == .righ_shift_assign ||
p.tok == .left_shift_assign { p.tok == .left_shift_assign {
if is_indexer && is_str && !p.builtin_pkg { if is_indexer && is_str && !p.builtin_pkg {
p.error('strings are imkey_mut') p.error('strings are immutable')
} }
assign_pos := p.cgen.cur_line.len assign_pos := p.cgen.cur_line.len
p.assigned_type = typ p.assigned_type = typ
@ -1822,7 +1822,7 @@ fn (p mut Parser) expression() string {
p.gen(', (') p.gen(', (')
// Imkey_mut? Can we push? // Imkey_mut? Can we push?
if !p.expr_var.is_mut && !p.pref.translated { if !p.expr_var.is_mut && !p.pref.translated {
p.error('`$p.expr_var.name` is imkey_mut (can\'t <<)') p.error('`$p.expr_var.name` is immutable (can\'t <<)')
} }
expr_type := p.expression() expr_type := p.expression()
// Two arrays of the same type? // Two arrays of the same type?

View File

@ -258,18 +258,17 @@ if parent == 'array' {
pkg = 'builtin' pkg = 'builtin'
} }
*/ */
datyp := Type { t.types << Type {
name: typ name: typ
parent: parent parent: parent
//mod: mod
} }
t.types << datyp
} }
fn (t mut Table) register_type2(typ Type) { fn (t mut Table) register_type2(typ Type) {
if typ.name.len == 0 { if typ.name.len == 0 {
return return
} }
// println('register type2 $typ.name')
for typ2 in t.types { for typ2 in t.types {
if typ2.name == typ.name { if typ2.name == typ.name {
return return