aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-08-19 08:56:45 +0000
committerGitHub <noreply@github.com>2021-08-19 08:56:45 +0000
commit24838285ad98f9a91ea73ed50652de2b1c0e7e61 (patch)
tree4170aece28531c74eeaff13c2005c62e3c633059 /gcc
parent3a944e1b7a49dae8c0970f9a491121ab84ff2e0b (diff)
parentbb52e25b2d420325ca885c36a00e064e93a23ef4 (diff)
downloadgcc-24838285ad98f9a91ea73ed50652de2b1c0e7e61.zip
gcc-24838285ad98f9a91ea73ed50652de2b1c0e7e61.tar.gz
gcc-24838285ad98f9a91ea73ed50652de2b1c0e7e61.tar.bz2
Merge #637
637: Add support for const bool and const float r=philberty a=dkm From Mark Wielaard : https://gcc.gnu.org/pipermail/gcc-rust/2021-August/000143.html > Handle BOOL and FLOAT in ConstFoldExpr::visit (HIR::LiteralExpr) to > make it possible to create const bool, f32 and f64 constants. Add a > new testcase "primconsts.rs". Not yet handled are const char and &str > types. Co-authored-by: Mark Wielaard <mark@klomp.org>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/typecheck/rust-hir-const-fold.h30
-rw-r--r--gcc/testsuite/rust/compile/torture/primconsts.rs72
2 files changed, 102 insertions, 0 deletions
diff --git a/gcc/rust/typecheck/rust-hir-const-fold.h b/gcc/rust/typecheck/rust-hir-const-fold.h
index f6c6616..8efbb18 100644
--- a/gcc/rust/typecheck/rust-hir-const-fold.h
+++ b/gcc/rust/typecheck/rust-hir-const-fold.h
@@ -315,6 +315,36 @@ public:
}
return;
+ case HIR::Literal::BOOL: {
+ bool bval = literal_value->as_string ().compare ("true") == 0;
+ folded = ctx->get_backend ()->boolean_constant_expression (bval);
+ }
+ return;
+
+ case HIR::Literal::FLOAT: {
+ mpfr_t fval;
+ if (mpfr_init_set_str (fval, literal_value->as_string ().c_str (), 10,
+ MPFR_RNDN)
+ != 0)
+ {
+ rust_fatal_error (expr.get_locus (),
+ "bad floating-point number in literal");
+ return;
+ }
+
+ TyTy::BaseType *tyty = nullptr;
+ if (!tyctx->lookup_type (expr.get_mappings ().get_hirid (), &tyty))
+ {
+ rust_fatal_error (expr.get_locus (),
+ "did not resolve type for this literal expr");
+ return;
+ }
+
+ Btype *type = ConstFoldType::fold (tyty, ctx->get_backend ());
+ folded = ctx->get_backend ()->float_constant_expression (type, fval);
+ }
+ return;
+
/* handle other literals */
default:
diff --git a/gcc/testsuite/rust/compile/torture/primconsts.rs b/gcc/testsuite/rust/compile/torture/primconsts.rs
new file mode 100644
index 0000000..bcf9456
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/primconsts.rs
@@ -0,0 +1,72 @@
+const TRUE: bool = true;
+const FALSE: bool = !TRUE;
+
+const U8ZERO: u8 = 0;
+const U8ONE: u8 = U8ZERO + 1;
+const U16ZERO: u16 = 0;
+const U16ONE: u16 = U16ZERO + 1;
+const U32ZERO: u32 = 0;
+const U32ONE: u32 = U32ZERO + 1;
+const U64ZERO: u64 = 0;
+const U64ONE: u64 = U64ZERO + 1;
+const U128ZERO: u128 = 0;
+const U128ONE: u128 = U128ZERO + 1;
+
+const I8ZERO: i8 = 0;
+const I8ONE: i8 = I8ZERO + 1;
+const I16ZERO: i16 = 0;
+const I16ONE: i16 = I16ZERO + 1;
+const I32ZERO: i32 = 0;
+const I32ONE: i32 = I32ZERO + 1;
+const I64ZERO: i64 = 0;
+const I64ONE: i64 = I64ZERO + 1;
+const I128ZERO: i128 = 0;
+const I128ONE: i128 = I128ZERO + 1;
+
+const F32ZERO: f32 = 0.0;
+const F32ONE: f32 = F32ZERO + 1.0;
+const F64ZERO: f64 = 0.0;
+const F64ONE: f64 = F64ZERO + 1.0;
+
+const USIZEZERO: usize = 0;
+const USIZEONE: usize = USIZEZERO + 1;
+const ISIZEZERO: isize = 0;
+const ISIZEONE: isize = ISIZEZERO + 1;
+
+/* Not yet supported
+const CHARPI: char = '\u{03C0}';
+const STRHELLO: &str = "Hello World!";
+*/
+
+extern "C" { fn abort (); }
+
+pub fn main ()
+{
+ if TRUE == FALSE { unsafe { abort (); } }
+ if U8ZERO > U8ONE { unsafe { abort (); } }
+ if U16ZERO > U16ONE { unsafe { abort (); } }
+ if U32ZERO > U32ONE { unsafe { abort (); } }
+ if U64ZERO > U64ONE { unsafe { abort (); } }
+ if U128ZERO > U128ONE { unsafe { abort (); } }
+
+ if I8ONE <= I8ZERO { unsafe { abort (); } }
+ if I16ONE <= I16ZERO { unsafe { abort (); } }
+ if I32ONE <= I32ZERO { unsafe { abort (); } }
+ if I64ONE <= I64ZERO { unsafe { abort (); } }
+ if I128ONE <= I128ZERO { unsafe { abort (); } }
+
+ if F32ZERO + F32ONE != F32ONE { unsafe { abort (); } }
+ if F64ZERO + F64ONE != F64ONE { unsafe { abort (); } }
+
+ if USIZEZERO + USIZEONE - USIZEONE + USIZEZERO != USIZEZERO
+ {
+ unsafe { abort (); }
+ }
+ if ISIZEZERO + ISIZEONE - ISIZEONE + ISIZEZERO != ISIZEZERO
+ {
+ unsafe { abort (); }
+ }
+
+ // if CHARPI != '\u{03c0}' { unsafe { abort (); } }
+ // if STRHELLO != "Hello World!" { unsafe { abort (); } }
+}