From 53f4a8601c1db2068390dce7012c16b383864740 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Sat, 17 Sep 2022 10:04:49 +0100 Subject: Static Items must be const evaluated Statics like constants need to have a singular value they are not functions to be lazy evaluated. So to evaluate a block expr we can just reuse our const code to resolve this to a singular value. --- gcc/rust/backend/rust-compile-item.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'gcc') diff --git a/gcc/rust/backend/rust-compile-item.cc b/gcc/rust/backend/rust-compile-item.cc index 96c4e7f..8ba17c9 100644 --- a/gcc/rust/backend/rust-compile-item.cc +++ b/gcc/rust/backend/rust-compile-item.cc @@ -43,13 +43,18 @@ CompileItem::visit (HIR::StaticItem &var) rust_assert (ok); tree type = TyTyResolveCompile::compile (ctx, resolved_type); - tree value = CompileExpr::Compile (var.get_expr (), ctx); const Resolver::CanonicalPath *canonical_path = nullptr; ok = ctx->get_mappings ()->lookup_canonical_path ( var.get_mappings ().get_nodeid (), &canonical_path); rust_assert (ok); + HIR::Expr *const_value_expr = var.get_expr (); + ctx->push_const_context (); + tree value = compile_constant_item (ctx, resolved_type, canonical_path, + const_value_expr, var.get_locus ()); + ctx->pop_const_context (); + std::string name = canonical_path->get (); std::string asm_name = ctx->mangle_item (resolved_type, *canonical_path); -- cgit v1.1 From 6b6a1c70f47237a5c2db76db62d7f8956244dee6 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Sat, 17 Sep 2022 10:05:59 +0100 Subject: Statics are a coercion site Statics can be assigned to a block expression meaning they need to behave similarly to constant items. --- gcc/rust/typecheck/rust-hir-type-check-toplevel.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/typecheck/rust-hir-type-check-toplevel.cc b/gcc/rust/typecheck/rust-hir-type-check-toplevel.cc index c4688db..a2113d0 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-toplevel.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-toplevel.cc @@ -261,11 +261,11 @@ TypeCheckTopLevel::visit (HIR::StaticItem &var) TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (var.get_expr ()); TyTy::BaseType *unified - = unify_site (var.get_mappings ().get_hirid (), - TyTy::TyWithLocation (type, var.get_type ()->get_locus ()), - TyTy::TyWithLocation (expr_type, - var.get_expr ()->get_locus ()), - var.get_locus ()); + = coercion_site (var.get_mappings ().get_hirid (), + TyTy::TyWithLocation (type, var.get_type ()->get_locus ()), + TyTy::TyWithLocation (expr_type, + var.get_expr ()->get_locus ()), + var.get_locus ()); context->insert_type (var.get_mappings (), unified); } -- cgit v1.1 From a6bb21fca5b9ad5cadcaa6a3dba481972ce0ed7e Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Sat, 17 Sep 2022 10:06:48 +0100 Subject: remove bad assertion --- gcc/rust/backend/rust-tree.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/backend/rust-tree.cc b/gcc/rust/backend/rust-tree.cc index f587835..0c393d9 100644 --- a/gcc/rust/backend/rust-tree.cc +++ b/gcc/rust/backend/rust-tree.cc @@ -974,9 +974,10 @@ rs_type_quals (const_tree type) return TYPE_UNQUALIFIED; quals = TYPE_QUALS (type); /* METHOD and REFERENCE_TYPEs should never have quals. */ - gcc_assert ( - (TREE_CODE (type) != METHOD_TYPE && !TYPE_REF_P (type)) - || ((quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)) == TYPE_UNQUALIFIED)); + // gcc_assert ( + // (TREE_CODE (type) != METHOD_TYPE && !TYPE_REF_P (type)) + // || ((quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)) == + // TYPE_UNQUALIFIED)); return quals; } -- cgit v1.1 From 5f01d6c53734802955568771e75df68ffc2fd0d8 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Sun, 18 Sep 2022 10:37:06 +0100 Subject: Add testcase for const-eval issue from rust-blog see: https://blog.rust-lang.org/2022/09/15/const-eval-safety-rule-revision.html --- gcc/testsuite/rust/compile/rust-const-blog-issue.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 gcc/testsuite/rust/compile/rust-const-blog-issue.rs (limited to 'gcc') diff --git a/gcc/testsuite/rust/compile/rust-const-blog-issue.rs b/gcc/testsuite/rust/compile/rust-const-blog-issue.rs new file mode 100644 index 0000000..a5ea2eb --- /dev/null +++ b/gcc/testsuite/rust/compile/rust-const-blog-issue.rs @@ -0,0 +1,12 @@ +// { dg-excess-errors "accessing value of" } +mod mem { + extern "rust-intrinsic" { + #[rustc_const_stable(feature = "const_transmute", since = "1.46.0")] + fn transmute(_: T) -> U; + } +} + +pub static FOO: () = unsafe { + let illegal_ptr2int: usize = mem::transmute(&()); + let _copy = illegal_ptr2int; +}; -- cgit v1.1