parser: require assign on type alias (#6477)

pull/6478/head
Daniel Däschle 2020-09-25 12:02:32 +02:00 committed by GitHub
parent 2ea94d621f
commit abc98c273c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 49 additions and 43 deletions

View File

@ -652,7 +652,7 @@ However, you _can_ redeclare a type.
```v ```v
import time import time
type MyTime time.Time type MyTime = time.Time
fn (mut t MyTime) century() int { fn (mut t MyTime) century() int {
return 1 + t.year % 100 return 1 + t.year % 100
@ -2223,7 +2223,7 @@ surrounding code).
struct C.sqlite3{} struct C.sqlite3{}
struct C.sqlite3_stmt{} struct C.sqlite3_stmt{}
type FnSqlite3Callback fn(voidptr, int, &charptr, &charptr) int type FnSqlite3Callback = fn(voidptr, int, &charptr, &charptr) int
fn C.sqlite3_open(charptr, &&C.sqlite3) int fn C.sqlite3_open(charptr, &&C.sqlite3) int
fn C.sqlite3_close(&C.sqlite3) int fn C.sqlite3_close(&C.sqlite3) int

View File

@ -6,7 +6,7 @@ module builtin
__global g_m2_buf byteptr __global g_m2_buf byteptr
__global g_m2_ptr byteptr __global g_m2_ptr byteptr
type FnExitCb fn() type FnExitCb = fn()
fn C.atexit(f FnExitCb) int fn C.atexit(f FnExitCb) int
pub fn exit(code int) { pub fn exit(code int) {

View File

@ -199,7 +199,7 @@ pub:
context_record &ContextRecord context_record &ContextRecord
} }
type VectoredExceptionHandler fn(&ExceptionPointers)u32 type VectoredExceptionHandler = fn(&ExceptionPointers)u32
fn C.AddVectoredExceptionHandler(u32, C.PVECTORED_EXCEPTION_HANDLER) fn C.AddVectoredExceptionHandler(u32, C.PVECTORED_EXCEPTION_HANDLER)
fn add_vectored_exception_handler(handler VectoredExceptionHandler) { fn add_vectored_exception_handler(handler VectoredExceptionHandler) {

View File

@ -91,7 +91,7 @@ fn test_cmp() {
assert 1 0 assert 1 0
} }
*/ */
type MyInt int type MyInt = int
fn test_int_alias() { fn test_int_alias() {
i := MyInt(2) i := MyInt(2)

View File

@ -862,7 +862,7 @@ fn test_string_literal_with_backslash(){
} }
/* /*
type MyString string type MyString = string
fn test_string_alias() { fn test_string_alias() {
s := MyString('hi') s := MyString('hi')

View File

@ -1,6 +1,6 @@
module eventbus module eventbus
pub type EventHandlerFn fn(receiver voidptr, args voidptr, sender voidptr) pub type EventHandlerFn = fn (receiver voidptr, args voidptr, sender voidptr)
pub struct Publisher { pub struct Publisher {
mut: mut:

View File

@ -13,7 +13,7 @@ pub const (
// Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types // Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types
// A handle to an object. // A handle to an object.
pub type HANDLE voidptr pub type HANDLE = voidptr
// win: FILETIME // win: FILETIME
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime // https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
@ -335,7 +335,7 @@ pub:
context_record &ContextRecord context_record &ContextRecord
} }
pub type VectoredExceptionHandler fn(&ExceptionPointers)u32 pub type VectoredExceptionHandler = fn (&ExceptionPointers) u32
// This is defined in builtin because we use vectored exception handling // This is defined in builtin because we use vectored exception handling
// for our unhandled exception handler on windows // for our unhandled exception handler on windows

View File

@ -6,7 +6,7 @@ module sys
// Until there's a portable, JS has a seeded way to produce random numbers // Until there's a portable, JS has a seeded way to produce random numbers
// and not just Math.random(), use any of the existing implementations // and not just Math.random(), use any of the existing implementations
// as the System's RNG // as the System's RNG
type SysRNG WyRandRNG type SysRNG = WyRandRNG
// In the JS version, we simply return the same int as is normally generated. // In the JS version, we simply return the same int as is normally generated.
[inline] [inline]

View File

@ -230,7 +230,7 @@ fn simple_log(txt string) {
* Token Structs * Token Structs
* *
******************************************************************************/ ******************************************************************************/
pub type FnValidator fn (byte) bool pub type FnValidator = fn (byte) bool
struct Token{ struct Token{
mut: mut:
ist rune ist rune
@ -293,7 +293,7 @@ mut:
group_stack_index int = -1 // continuous save on capturing groups group_stack_index int = -1 // continuous save on capturing groups
} }
pub type FnLog fn (string) pub type FnLog = fn (string)
pub pub
struct RE { struct RE {

View File

@ -57,7 +57,7 @@ mut:
thread_contexts []voidptr thread_contexts []voidptr
} }
pub type ThreadCB fn(p &PoolProcessor, idx int, task_id int)voidptr pub type ThreadCB = fn (p &PoolProcessor, idx int, task_id int) voidptr
pub struct PoolProcessorConfig { pub struct PoolProcessorConfig {
maxjobs int maxjobs int

View File

@ -9,9 +9,9 @@ import time
// was discussed. Needs consideration. // was discussed. Needs consideration.
// Mutex HANDLE // Mutex HANDLE
type MHANDLE voidptr type MHANDLE = voidptr
// Semaphore HANDLE // Semaphore HANDLE
type SHANDLE voidptr type SHANDLE = voidptr
//[init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function. //[init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function.

View File

@ -6,7 +6,7 @@ module szip
struct C.zip_t {} struct C.zip_t {}
type Zip C.zip_t type Zip = C.zip_t
fn C.zip_open(byteptr, int, byte) &Zip fn C.zip_open(byteptr, int, byte) &Zip
fn C.zip_close(&Zip) fn C.zip_close(&Zip)

View File

@ -360,7 +360,7 @@ fn convert_ctime(t C.tm, microsecond int) Time {
} }
// A lot of these are taken from the Go library // A lot of these are taken from the Go library
pub type Duration i64 pub type Duration = i64
pub const( pub const(
nanosecond = Duration(1) nanosecond = Duration(1)

View File

@ -31,7 +31,7 @@ fn to_local_time(t Time) Time {
return convert_ctime(loc_tm, t.microsecond) return convert_ctime(loc_tm, t.microsecond)
} }
type time_t voidptr type time_t = voidptr
// in most systems, these are __quad_t, which is an i64 // in most systems, these are __quad_t, which is an i64
struct C.timespec { struct C.timespec {

View File

@ -23,7 +23,7 @@ struct MsvcResult {
// shell32 for RegOpenKeyExW etc // shell32 for RegOpenKeyExW etc
// Mimics a HKEY // Mimics a HKEY
type RegKey voidptr type RegKey = voidptr
// Taken from the windows SDK // Taken from the windows SDK
const ( const (

View File

@ -1,5 +1,5 @@
vlib/v/checker/tests/alias_type_exists.vv:1:1: error: type `Bird` doesn't exist vlib/v/checker/tests/alias_type_exists.vv:1:1: error: type `Bird` doesn't exist
1 | type Pigeon Bird 1 | type Pigeon = Bird
| ~~~~~~~~~~~ | ~~~~~~~~~~~
2 | 2 |
3 | fn main() { 3 | fn main() {

View File

@ -1,4 +1,4 @@
type Pigeon Bird type Pigeon = Bird
fn main() { fn main() {

View File

@ -496,7 +496,7 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
f.write('pub ') f.write('pub ')
} }
ptype := f.type_to_str(node.parent_type) ptype := f.type_to_str(node.parent_type)
f.write('type $node.name $ptype') f.write('type $node.name = $ptype')
} }
ast.FnTypeDecl { ast.FnTypeDecl {
if node.is_pub { if node.is_pub {

View File

@ -8,9 +8,9 @@ type Uint = byte | u16 | u32 | u64
type Float = f32 | f64 type Float = f32 | f64
// Alias type // Alias type
type MyInt int type MyInt = int
pub type Abc f32 pub type Abc = f32
// Fn type decl // Fn type decl
type EmptyFn = fn () type EmptyFn = fn ()

View File

@ -13,9 +13,9 @@ Float =
f64 f64
// Alias type // Alias type
type MyInt int type MyInt = int
pub type Abc f32 pub type Abc = f32
// Fn type decl // Fn type decl

View File

@ -1802,9 +1802,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
decl_pos) decl_pos)
} }
mut sum_variants := []table.Type{} mut sum_variants := []table.Type{}
if p.tok.kind == .assign { p.check(.assign)
p.next() // TODO require `=`
}
if p.tok.kind == .key_fn { if p.tok.kind == .key_fn {
// function type: `type mycallback fn(string, int)` // function type: `type mycallback fn(string, int)`
fn_name := p.prepend_mod(name) fn_name := p.prepend_mod(name)

View File

@ -0,0 +1,5 @@
vlib/v/parser/tests/expecting_assign_type_alias.vv:1:10: error: unexpected name `int`, expecting `=`
1 | type Ttt int
| ~~~
2 |
3 | fn main() {}

View File

@ -0,0 +1,3 @@
type Ttt int
fn main() {}

View File

@ -13,7 +13,7 @@ module table
import strings import strings
pub type Type int pub type Type = int
pub type TypeInfo = Alias | Array | ArrayFixed | Chan | Enum | FnType | GenericStructInst | pub type TypeInfo = Alias | Array | ArrayFixed | Chan | Enum | FnType | GenericStructInst |
Interface | Map | MultiReturn | Struct | SumType Interface | Map | MultiReturn | Struct | SumType

View File

@ -1,4 +1,4 @@
type MyInt int type MyInt = int
fn test_shift_operators() { fn test_shift_operators() {
// check that shift works with all integer types // check that shift works with all integer types

View File

@ -6,7 +6,7 @@ fn (h Human) str() string {
return 'Human: $h.name' return 'Human: $h.name'
} }
type Person Human type Person = Human
fn (h Person) str() string { fn (h Person) str() string {
return 'Person: $h.name' return 'Person: $h.name'

View File

@ -1,6 +1,6 @@
type Myint int type Myint = int
type Myf32 f32 type Myf32 = f32
type Myf64 f64 type Myf64 = f64
fn test_type_alias() { fn test_type_alias() {
i := Myint(10) i := Myint(10)
@ -28,7 +28,7 @@ struct Mystruct {
mut: mut:
i int i int
} }
type Mystruct_2 Mystruct type Mystruct_2 = Mystruct
fn test_type_alias_struct() { fn test_type_alias_struct() {
mut s := Mystruct_2{} mut s := Mystruct_2{}