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

Defer integer literal type resolution until after type checking #711

Conversation

mohammadfawaz
Copy link
Contributor

@mohammadfawaz mohammadfawaz commented Jan 27, 2022

Fixes #653
This patch basically tries to remove the assumption that integer literals are 64 bit wide. This implies the following:

  1. Writing let x:u32 = 5; does not generate a warning.
  2. Writing let x:u8 = 256; generates an error because 256 does not fit in a u8.
    I was hoping to completely remove implicit casting between UnsignedIntegers in engine.rs, but there are more complex situations for which resolving the type of literals without the 64-bit assumption is not yet possible. E.g.:
struct Foo<T> {
  a: T,
}

fn main() {
  let foo = Foo { a: 256 };
  let x:u8 = foo.a;
}

Ideally, we would recognize 256 has to be a u8 and error out, but that requires more complex analysis than what I have in this change.

Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
@mohammadfawaz mohammadfawaz requested a review from sezna as a code owner January 27, 2022 18:32
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
@adlerjohn adlerjohn added compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request labels Jan 27, 2022
@adlerjohn
Copy link
Contributor

Ideally, we would recognize 256 has to be a u8

You mean u16?

@mohammadfawaz
Copy link
Contributor Author

mohammadfawaz commented Jan 27, 2022

Ideally, we would recognize 256 has to be a u8

You mean u16?

Oups - yes with 256 we should really get an error. 255 is okay. I updated the description, thanks.
E.g.: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=dd4566ec8364d4fdeb15615d75e5f581

sway-core/src/parse_tree/literal.rs Outdated Show resolved Hide resolved
sway-core/src/parse_tree/literal.rs Outdated Show resolved Hide resolved
sway-core/src/parse_tree/literal.rs Show resolved Hide resolved
sway-core/src/sway.pest Outdated Show resolved Hide resolved
sway-core/src/sway.pest Outdated Show resolved Hide resolved
sway-core/src/type_engine/engine.rs Outdated Show resolved Hide resolved
sway-core/src/type_engine/engine.rs Outdated Show resolved Hide resolved
sway-core/src/type_engine/engine.rs Outdated Show resolved Hide resolved
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Signed-off-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
sezna
sezna previously approved these changes Jan 28, 2022
Copy link
Contributor

@sezna sezna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved w/ nit -- up to you whether or not to address it.

…/Forc.toml

Co-authored-by: Alex Hansen <alex@alex-hansen.com>
@mohammadfawaz mohammadfawaz merged commit 8fe74e0 into FuelLabs:master Jan 28, 2022
@mohammadfawaz mohammadfawaz deleted the eliminate_implicit_casting_for_integers branch February 3, 2022 20:45
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 this pull request may close these issues.

Compiler does not always complain about type mismatch for returned values from functions
3 participants