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

Respect the f32 and f64 suffix on literals #126

Merged
merged 1 commit into from
Jan 8, 2021
Merged

Conversation

philberty
Copy link
Member

@philberty philberty commented Jan 6, 2021

Rust is permissive for integers being marked as floats so the check in the lexer can be removed here.

Rust is permissive for integers being marked as floats so the check in the
lexer can be removed here.
@philberty philberty linked an issue Jan 6, 2021 that may be closed by this pull request
@philberty philberty added this to the Core Datastructures milestone Jan 6, 2021
Copy link
Member

@SimplyTheOther SimplyTheOther left a comment

Choose a reason for hiding this comment

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

PR looks great. I had one query about whether integers given a float type hint are actually treated as floats, or whether they are treated as integers with the type hint being ignored (or complained about during type check or whatever).

// ignore invalid type suffix as everything else seems fine
type_hint = CORETYPE_UNKNOWN;
}

current_column += length;

str.shrink_to_fit ();
Copy link
Member

Choose a reason for hiding this comment

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

The code below here (the make_int) would be incorrect (assuming that an integer with a type hint of a float type is treated as a float). What should happen instead is that make_float is called if the type hint is 'f32' or 'f64', while 'make_int' should be called otherwise. This is assuming that I have understood the spec properly.

I suppose it depends on whether rust is actually permissive on integers being marked as floats (i.e. allows integers to be given a float type hint) or whether the float type hint means that the integer is treated as a float.

If my interpretation is wrong, feel free to ignore this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this was not clear from the spec to me either. From testing the rust compiler i found this is permissive:

fn main() {
    let a: i32 = 5i32;
    let b: f32 = 5f32;
}

Without this change we determine:

float1.rs:3:18: error: invalid type suffix ‘f32’ for integer (decimal) literal
    3 |     let b: f32 = 5f32;
      |   

Copy link
Member Author

Choose a reason for hiding this comment

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

What if i split this change into two parts one where we support f32 and f64 suffix but remove this integer support for float suffix as a separate PR

Copy link

Choose a reason for hiding this comment

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

Yeah this was not clear from the spec to me either.

It may be a good idea to document this kind of things in the reference (https://github.com/rust-lang/reference/), so other alternative implementations don't have to discover it again.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suffix on integer literals can enforce the type
3 participants