Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

void function returns false if throws #37

Closed
Chuvi-w opened this issue Oct 18, 2021 · 3 comments
Closed

void function returns false if throws #37

Chuvi-w opened this issue Oct 18, 2021 · 3 comments

Comments

@Chuvi-w
Copy link
Contributor

Chuvi-w commented Oct 18, 2021

public class CanThrow { public static void WillThrow(int a) { if (a<15){throw "a<15";} Console.WriteLine(a); } }

compiles into
void CanThrow_WillThrow(int a) { if (a < 15) { return false; } printf("%d\n", a); }

@pfusik pfusik closed this as completed in 68b009b Oct 18, 2021
@pfusik
Copy link
Collaborator

pfusik commented Oct 18, 2021

This was invalid Ć. The method should have been marked with throws, like in Java:

public class CanThrow { public static void WillThrow(int a) throws { if (a<15){throw "a<15";} Console.WriteLine(a); } }

I've added the missing error message.

C doesn't support exceptions, so they are emulated by magic return values, here false for an exception.

Thank you!

@Chuvi-w
Copy link
Contributor Author

Chuvi-w commented Oct 18, 2021

void funtion must not return anything.

warning: 'return' with a value, in function returning void [-Wreturn-type]

@pfusik
Copy link
Collaborator

pfusik commented Oct 18, 2021

There were bugs both in your code and cito. Please pull cito and rebuild.

C:\0\ci>cat 37.ci
public class CanThrow { public static void WillThrow(int a) { if (a<15){throw "a<15";} Console.WriteLine(a); } }
C:\0\ci>cito -o 37.c 37.ci
37.ci(1): ERROR: 'throw' in a method not marked 'throws'

C:\0\ci>cat 37-fixed.ci
public class CanThrow { public static void WillThrow(int a) throws { if (a<15){throw "a<15";} Console.WriteLine(a); } }
C:\0\ci>cito -o 37-fixed.c 37-fixed.ci

C:\0\ci>cat 37-fixed.c
// Generated automatically with "cito". Do not edit.
#include <stdio.h>
#include <stdlib.h>
#include "37-fixed.h"

struct CanThrow {
};

bool CanThrow_WillThrow(int a)
{
		if (a < 15) {
				return false;
		}
		printf("%d\n", a);
		return true;
}

C:\0\ci>gcc -c -Wall 37-fixed.c

C:\0\ci>

@pfusik pfusik mentioned this issue Oct 21, 2021
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

No branches or pull requests

2 participants