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

parser: require assign on type alias (#6477)

This commit is contained in:
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
import time
type MyTime time.Time
type MyTime = time.Time
fn (mut t MyTime) century() int {
return 1 + t.year % 100
@ -2223,7 +2223,7 @@ surrounding code).
struct C.sqlite3{}
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_close(&C.sqlite3) int
@ -2406,7 +2406,7 @@ $if option ? {
}
```
If you want an `if` to be evaluated at compile time it must be prefixed with a `$` sign.
If you want an `if` to be evaluated at compile time it must be prefixed with a `$` sign.
Right now it can be used to detect an OS, compiler, platform or compilation options.
`$if debug` is a special option like `$if windows` or `$if x32`.
If you're using a custom ifdef, then you do need `$if option ? {}` and compile with`v -d option`.

View File

@ -6,7 +6,7 @@ module builtin
__global g_m2_buf byteptr
__global g_m2_ptr byteptr
type FnExitCb fn()
type FnExitCb = fn()
fn C.atexit(f FnExitCb) int
pub fn exit(code int) {
@ -71,8 +71,8 @@ pub fn eprintln(s string) {
}
C.fflush(C.stdout)
C.fflush(C.stderr)
C.write(2, s.str, s.len)
C.write(2, c'\n', 1)
C.write(2, s.str, s.len)
C.write(2, c'\n', 1)
C.fflush(C.stderr)
}
@ -82,12 +82,12 @@ pub fn eprint(s string) {
}
C.fflush(C.stdout)
C.fflush(C.stderr)
C.write(2, s.str, s.len)
C.write(2, s.str, s.len)
C.fflush(C.stderr)
}
pub fn print(s string) {
C.write(1, s.str, s.len)
C.write(1, s.str, s.len)
}
const (

View File

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

View File

@ -91,7 +91,7 @@ fn test_cmp() {
assert 1 0
}
*/
type MyInt int
type MyInt = int
fn test_int_alias() {
i := MyInt(2)
@ -168,7 +168,7 @@ fn test_num_separator() {
// f32 or f64
assert 312_2.55 == 3122.55
assert 312_2.55 == 3122.55
}
fn test_int_decl() {

View File

@ -734,7 +734,7 @@ fn test_raw() {
assert raw3[7] == `\\`
assert raw3[8] == `x`
assert raw3[9] == `0`
assert raw3[10] == `0`
assert raw3[10] == `0`
}
fn test_raw_with_quotes() {
@ -862,7 +862,7 @@ fn test_string_literal_with_backslash(){
}
/*
type MyString string
type MyString = string
fn test_string_alias() {
s := MyString('hi')

View File

@ -1,6 +1,6 @@
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 {
mut:

View File

@ -13,7 +13,7 @@ pub const (
// Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types
// A handle to an object.
pub type HANDLE voidptr
pub type HANDLE = voidptr
// win: FILETIME
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
@ -335,7 +335,7 @@ pub:
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
// 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
// and not just Math.random(), use any of the existing implementations
// 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.
[inline]

View File

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

View File

@ -57,7 +57,7 @@ mut:
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 {
maxjobs int

View File

@ -9,9 +9,9 @@ import time
// was discussed. Needs consideration.
// Mutex HANDLE
type MHANDLE voidptr
type MHANDLE = voidptr
// 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.

View File

@ -6,7 +6,7 @@ module szip
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_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
pub type Duration i64
pub type Duration = i64
pub const(
nanosecond = Duration(1)

View File

@ -31,7 +31,7 @@ fn to_local_time(t Time) Time {
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
struct C.timespec {

View File

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

View File

@ -1,5 +1,5 @@
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 |
3 | fn main() {

View File

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

View File

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

View File

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

View File

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

View File

@ -1802,9 +1802,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
decl_pos)
}
mut sum_variants := []table.Type{}
if p.tok.kind == .assign {
p.next() // TODO require `=`
}
p.check(.assign)
if p.tok.kind == .key_fn {
// function type: `type mycallback fn(string, int)`
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
pub type Type int
pub type Type = int
pub type TypeInfo = Alias | Array | ArrayFixed | Chan | Enum | FnType | GenericStructInst |
Interface | Map | MultiReturn | Struct | SumType

View File

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

View File

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

View File

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