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

Compiler does not always complain about type mismatch for returned values from functions #653

Closed
m-fawaz opened this issue Jan 16, 2022 · 1 comment · Fixed by #711
Closed
Assignees
Labels
compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request

Comments

@m-fawaz
Copy link
Contributor

m-fawaz commented Jan 16, 2022

Example:
We do not expect the following two functions to compile:

fn foo() -> u32 {
     let val:u64 = 0;     
     val
}
 fn foo() -> u64 {
      let val:u32 = 0;     
      val
 }

But the compiler does not give any compile errors.
However, the compiler does complain when bool is expected. E.g.

 fn foo() -> bool {
      let val:u32 = 0;     
      val
 }

Rust does not allow implicit conversions as shown in the first two functions. Sway should probably not allow that either.

@m-fawaz m-fawaz added compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request labels Jan 16, 2022
@adlerjohn adlerjohn moved this to Todo in Fuel Network Jan 16, 2022
@mohammadfawaz
Copy link
Contributor

Another minor thing to fix here is to avoid emitting a warning in cases such as the following:

  let x:u32 = 1;

The compiler assumes that the type of 1 is u64 and so, we get the following warning:

This cast, from integer type of width sixty four to integer type of width thirty two, will lose precision

Rust does not complain about the pattern above. It only complains if the integer does not actually fit in the variable type. E.g.

  let x:u8 = 256; // 255 is fine.

leads to a compile error.

@mohammadfawaz mohammadfawaz self-assigned this Jan 25, 2022
Repository owner moved this from Todo to Done in Fuel Network Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants