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

[Bug]The compiler detects the error if a function returns a value, and if it is not assigned to a variable. #547

Closed
aleo-debugger opened this issue Jan 12, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@aleo-debugger
Copy link

🐛 Bug Report

If a function returns a value, and if it is not assigned to a variable, the compiler detects this as an error “Mismatched types. Expected type(), found type u32
At the same time, if the function does not return a value, then it can be assigned to a variable as well as could be called without being assigned. Looks like an incorrect behaviour.

Steps to Reproduce

Code snippet to reproduce

function excar () {
    let ex = 33u32;
}
function exbaz () -> u32 {
    return 33u32 
}

function main() {
    let excar1 = excar();
    excar();   

    let exbaz1 = exbaz();
    exbaz();   
}

Stack trace & error message

 Compiling Starting...
 Compiling Compiling main program... ("c:\\Users\\test\\Aleo Studio\\function-return\\src/main.leo")
 Compiling     --> "c:\\Users\\test\\Aleo Studio\\function-return\\src/main.leo": 13:5
     |
  13 |      exbaz();
     |  ^^^^
     |
     = Mismatched types. Expected type `()`, found type `u32`.

Error: Crate("leo-compiler", "Program failed due to previous error")

Expected Behavior

(Write what you expected to happen here)

Your Environment

  • leo 1.0.7
  • rust 1.48.0
  • Edition: Windows 10 Pro Version: 20H2
    OS build: 19042.685
    Experience: Windows Feature Experience Pack 120.2212.551.0
@aleo-debugger aleo-debugger added the bug Something isn't working label Jan 12, 2021
@collinc97
Copy link
Collaborator

The above code is correct. By default, functions in Leo return the unit type (). https://en.wikipedia.org/wiki/Unit_type

If a function returns a specific type, then that result must be handled - assigned to a variable or used in a statement.

The following code is illegal in Leo

function main() {
    0u8; // Must be assigned to a variable or used in a statement.
}

Shoutout to @gluax for identifying this!

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
None yet
Development

No branches or pull requests

2 participants