Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions gcc/rust/backend/rust-compile-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ class CompileExpr : public HIRCompileBase
return;

case HIR::Literal::FLOAT: {
printf ("FLOATY BOYO: [%s]\n", expr.as_string ().c_str ());

mpfr_t fval;
if (mpfr_init_set_str (fval, expr.as_string ().c_str (), 10,
MPFR_RNDN)
Expand All @@ -177,8 +175,6 @@ class CompileExpr : public HIRCompileBase
return;
}

printf ("tyty float is [%s]\n", tyty->as_string ().c_str ());

Btype *type = TyTyResolveCompile::compile (ctx, tyty);
translated
= ctx->get_backend ()->float_constant_expression (type, fval);
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/hir/tree/rust-hir.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ struct Literal
return Literal ("", CHAR, PrimitiveCoreType::CORETYPE_UNKNOWN);
}

void set_lit_type (LitType lt) { type = lt; }

// Returns whether literal is in an invalid state.
bool is_error () const { return value_as_string == ""; }
};
Expand Down
10 changes: 0 additions & 10 deletions gcc/rust/lex/rust-lex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1938,16 +1938,6 @@ Lexer::parse_decimal_int_or_float (Location loc)
PrimitiveCoreType type_hint = type_suffix_pair.first;
length += type_suffix_pair.second;

if (type_hint == CORETYPE_F32 || type_hint == CORETYPE_F64)
{
rust_error_at (
get_current_location (),
"invalid type suffix %qs for integer (decimal) literal",
get_type_hint_string (type_hint));
// 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.

Expand Down
25 changes: 23 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ class TypeCheckExpr : public TypeCheckBase
ok = context->lookup_builtin ("u128", &infered);
break;

case CORETYPE_F32:
expr.get_literal ()->set_lit_type (HIR::Literal::LitType::FLOAT);
ok = context->lookup_builtin ("f32", &infered);
break;
case CORETYPE_F64:
expr.get_literal ()->set_lit_type (HIR::Literal::LitType::FLOAT);
ok = context->lookup_builtin ("f64", &infered);
break;

default:
ok = context->lookup_builtin ("i32", &infered);
break;
Expand All @@ -197,8 +206,20 @@ class TypeCheckExpr : public TypeCheckBase
break;

case HIR::Literal::LitType::FLOAT: {
// FIXME need to respect the suffix if applicable
auto ok = context->lookup_builtin ("f32", &infered);
bool ok = false;

switch (expr.get_literal ()->get_type_hint ())
{
case CORETYPE_F32:
ok = context->lookup_builtin ("f32", &infered);
break;
case CORETYPE_F64:
ok = context->lookup_builtin ("f64", &infered);
break;
default:
ok = context->lookup_builtin ("f32", &infered);
break;
}
rust_assert (ok);
}
break;
Expand Down
11 changes: 11 additions & 0 deletions gcc/testsuite/rust.test/compilable/float_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
let a1: f32 = 1.0f32;
let a2: f64 = 2.0f64;
let a3: f32 = 3f32;
let a4: f64 = 4f64;

let b1 = 1.0f32;
let b2 = 2.0f64;
let b3 = 3f32;
let b4 = 4f64;
}