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

Typecheck void type expressions in return and assignment #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

StrongerXi
Copy link

Problem

The following 4 programs compile without error, but they obviously shouldn't.

Assigning expression with void type.

void foo() { }

void bar() {
  int x = foo();
}

Return nothing in a non-void type function.

int bar() {
  return;
}

Return void type expression in a non-void function.

void foo() {}

int bar() {
  return foo();
}

Return non-void type expression in a void type function.

void foo() {
  return 1;
}

Proposal

I added exactly 4 small chunks of code in parse.c to handle the cases above accordingly.

Error reporting is also a critical part of compiler, and since the compiler prevents variable declaration with void type already, we should probably handle these cases too. I would even propose to separate out type-checking into a phase, but this really depends on your focus in this project.

rurban pushed a commit to rurban/chibicc that referenced this pull request Feb 17, 2023
rurban added a commit to rurban/chibicc that referenced this pull request Feb 17, 2023
where the C compiler should throw compile-time errors
for GH rui314#41
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.

1 participant