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

Type check error on non-unit expression statements #2304

Closed
alexvitkov opened this issue Aug 14, 2023 · 4 comments · Fixed by #2314
Closed

Type check error on non-unit expression statements #2304

alexvitkov opened this issue Aug 14, 2023 · 4 comments · Fixed by #2314
Labels
bug Something isn't working

Comments

@alexvitkov
Copy link
Contributor

Aim

Non-unit expression statements inside a Block fail to type check, if they're not the last statement in the block:

fn main() {
    5;  // error here
    10;
}

Note that this type checks properly:

fn main() { 5; }

This is particularly weird in the case of function calls, since if we call a function and ignore its return value we get a cryptic error:

fn foo() -> Field {
    100
}

fn main() {
    foo(); // error: Expected type (), found type Field
    foo();
}

Expected Behavior

No type errors on any of these examples

Bug

The reason is that we set the statement's type to the expression type which is non-unit:

// type_check/stmt.rs:41
            HirStatement::Expression(expr_id) => {
                return self.check_expression(&expr_id);
            }

... and later on we attempt to unify it to the unit type:

/// type_check/expr.rs:241:
                        self.unify(&expr_type, &Type::Unit, || TypeCheckError::TypeMismatch {
                            expected_typ: Type::Unit.to_string(),
                            expr_typ: expr_type.to_string(),
                            expr_span: span,
                        });

To Reproduce

No response

Installation Method

Compiled from source

Nargo Version

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

Yes

Support Needs

No response

@alexvitkov alexvitkov added the bug Something isn't working label Aug 14, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Aug 14, 2023
@alexvitkov alexvitkov changed the title Type check error on ignored return value Type check error on non-unit expression statements Aug 14, 2023
@ghost
Copy link

ghost commented Aug 14, 2023

#1933 (comment)

@alexvitkov
Copy link
Contributor Author

huh...
closing since it's apparently intended

@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Aug 14, 2023
@jfecher
Copy link
Contributor

jfecher commented Aug 14, 2023

Right, this is intended as it is similar to the unused variable warning where the compiler warns if a variable is never used. Here, it is the expression itself that is never used. Like the unused variable warning though, this should likely be a warning rather than an error.

Note that this type checks properly:
fn main() { 5; }

Good catch, we should make sure this is consistent

@alexvitkov
Copy link
Contributor Author

I'll submit a PR that handles the case where there's only one expression with semi.

You're right we should probably convert it to a warning since it's a breaking change (there are tests failing if we handle that case and keep it an error). We could also print a nicer message while we're at it (e.g. Unused Expression (of type Field) instead of Expected (), got Field

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants