Skip to content

checker: allow int i32 to be used interchangeably#25317

Merged
spytheman merged 1 commit intovlang:masterfrom
kbkpbot:fix-checker-allow-bool-int-i32
Sep 16, 2025
Merged

checker: allow int i32 to be used interchangeably#25317
spytheman merged 1 commit intovlang:masterfrom
kbkpbot:fix-checker-allow-bool-int-i32

Conversation

@kbkpbot
Copy link
Contributor

@kbkpbot kbkpbot commented Sep 16, 2025

This is 3rd PR to support 64bit/32bit int as PR #25236 is the first one and #25298 is the second one.

Before apply v fmt -new_int -w . to the whole v codebase, it is need to ease some type check first.
This PR allow i32 <=> bool and also int <=> i32 type check.

@spytheman
Copy link
Member

Why is i32 <=> bool needed?

@vlang vlang deleted a comment from huly-for-github bot Sep 16, 2025
@JalonSolov
Copy link
Contributor

In a lot of C compilers bool is typedef int bool. On a 32-bit system, an int is i32, so bool will also be i32, and I've seen code where it was simply treated as an int rather than a separate type.

It used to be quite common to see

typedef int bool;
#define true 1
#define false 0

Then your code do

if (mybool == true) // or more often, `if (mybool)`
{
 ...
}

or whatever, which is all i32s.

@kbkpbot
Copy link
Contributor Author

kbkpbot commented Sep 16, 2025

Alternatively, we can mandate the addition of explicit type casting where necessary in the code, such as int(bool_value) , i32(bool_value) or bool(int_value), to ensure type matching. Such instances are not too many in the code and can be manually modified.

@JalonSolov
Copy link
Contributor

That would be the better thing all around, really, as you cannot guarantee that int == i32 in all C compilers. The only guarantee on sizes is the sizeof(char) == 1. Everything else is "implementation defined".

@kbkpbot
Copy link
Contributor Author

kbkpbot commented Sep 16, 2025

can we remove these from the checker?

		// allow bool & int to be used interchangeably for C functions
		if (got.idx() == ast.bool_type_idx
			&& expected.idx() in [ast.int_type_idx, ast.int_literal_type_idx, ast.i32_type_idx])
			|| (expected.idx() == ast.bool_type_idx
			&& got.idx() in [ast.int_type_idx, ast.int_literal_type_idx, ast.i32_type_idx]) {
			return
		}
		exp_sym := c.table.sym(expected)
		// unknown C types are set to int, allow int to be used for types like `&C.FILE`
		// eg. `C.fflush(C.stderr)` - error: cannot use `int` as `&C.FILE` in argument 1 to `C.fflush`
		if exp_is_ptr && exp_sym.language == .c && exp_sym.kind in [.placeholder, .struct]
			&& got == ast.int_type_idx {
			return
		}

I suggest remove these checks, and make explicit type casting in V source code.
C.fflush(C.stdout) => C.fflush(&C.FILE(C.stdout))

As int is not a pointer(&C.FILE) at all.

BTW:

pub enum FileBufferMode {
        fully_buffered = C._IOFBF
        line_buffered  = C._IOLBF
        not_buffered   = C._IONBF
}

need change to

pub enum FileBufferMode {
        fully_buffered = i32(C._IOFBF)
        line_buffered  = i32(C._IOLBF)
        not_buffered   = i32(C._IONBF)
}

@kbkpbot kbkpbot changed the title checker: allow bool int i32 to be used interchangeably for C functions checker: allow int i32 to be used interchangeably Sep 16, 2025
@kbkpbot
Copy link
Contributor Author

kbkpbot commented Sep 16, 2025

Or should we introduce something redecalre the C.xx's type in V? This will help integrate C code.

For example:

(C.stdio, C.stderr) = &C.FILE

@kbkpbot kbkpbot marked this pull request as draft September 16, 2025 09:08
@spytheman
Copy link
Member

Please do focus on the current PR, and avoid discussing things unrelated to it here.

There is https://github.com/vlang/v/discussions and Discord for chatting/discussing other topics.

@spytheman
Copy link
Member

  // unknown C types are set to int, allow int to be used for types like `&C.FILE`
  // eg. `C.fflush(C.stderr)` - error: cannot use `int` as `&C.FILE` in argument 1 to `C.fflush`

This should now just apply to i32 .
The C side is not going to change, just because V decided to be more like Go on a whim.

@spytheman
Copy link
Member

// allow bool & int to be used interchangeably for C functions

Similarly, this should apply to i32 now, and only for C functions, to allow for the same easy interoperability as before.

On the V side, now bool != int, and in the future it will be still != int and != i32.

@spytheman
Copy link
Member

Or should we introduce something redecalre the C.xx's type in V?
This will help integrate C code.

That can be a separate proposal/PR, for C globals/consts .

@kbkpbot kbkpbot force-pushed the fix-checker-allow-bool-int-i32 branch from 9772bb2 to 83697f7 Compare September 16, 2025 12:12
@kbkpbot kbkpbot marked this pull request as ready for review September 16, 2025 12:14
Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.
Thank you very much @kbkpbot 🙇🏻.

@spytheman spytheman merged commit 14ef765 into vlang:master Sep 16, 2025
166 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants