Skip to content

Handle const blocks#3738

Merged
CohenArthur merged 4 commits into
Rust-GCC:masterfrom
CohenArthur:handle-const-blocks
May 27, 2025
Merged

Handle const blocks#3738
CohenArthur merged 4 commits into
Rust-GCC:masterfrom
CohenArthur:handle-const-blocks

Conversation

@CohenArthur

Copy link
Copy Markdown
Member

This adds some handling towards const { ... } expressions. I haven't added specific handling for const folding them, but they are properly handled by the const checker

@CohenArthur CohenArthur requested review from P-E-P and philberty April 15, 2025 15:34
{
auto inner_expr = ASTLoweringExpr::translate (expr.get_const_expr ());

// we know this will always be an `AnonConst`, or we have an issue. Let's

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should this not be in the visitor above?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

so I meant that the const block nodes should always just contain an AnonConst, which is why we do the assertion that it is actually one and then static cast and store an AnonConst instead of a unique_ptr<Expr> in the HIR::ConstBlock

AnonConst should be used within const blocks but also within things like array expressions (e.g. in let x = [15; size] size should technically be represented as an AnonConst) so the visitor needs to be a little generic

@philberty philberty left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM implementing this should be straight forward in the backend if you look at rust-compile-item.cc ConstantDecl we have wrapper for supporting constant blocks.

  ctx->push_const_context ();
  tree value
    = compile_constant_item (var.get_mappings ().get_hirid (), expr_type,
			     resolved_type, *canonical_path, const_value_expr,
			     var.get_locus (), const_value_expr.get_locus ());
  ctx->pop_const_context ();

Then you can do whatever you need with the value for your case you will be implementing this in rust-compile-expr.cc so it will just be:

  TyTy::BaseType *expr_type = nullptr;
  bool ok = ctx->get_tyctx ()->lookup_type (const.get_mappings ().get_hirid (),
					    &expr_type);
  rust_assert (ok);
  
    Resolver::CanonicalPath canonical_path
    = Resolver::CanonicalPath::create_empty ();
    
    HIR::Expr* const_value_expr = const.get_const_block ();

  ctx->push_const_context ();
  translated
    = compile_constant_item (const.get_mappings ().get_hirid (), expr_type,
			     expr_type, *canonical_path, const_value_expr,
			     const.get_locus (), const_value_expr.get_locus ());
  ctx->pop_const_context ();

something like that should just do it

@CohenArthur CohenArthur force-pushed the handle-const-blocks branch from 14dafa6 to 4717a95 Compare April 16, 2025 14:41
Comment thread gcc/rust/backend/rust-compile-expr.cc Outdated
Comment thread gcc/rust/hir/tree/rust-hir.cc
Comment thread gcc/rust/parse/rust-parse-impl.h
@CohenArthur CohenArthur force-pushed the handle-const-blocks branch from 4717a95 to e2e16f2 Compare May 2, 2025 14:20
@CohenArthur CohenArthur force-pushed the handle-const-blocks branch 2 times, most recently from baa8fbd to f815bcd Compare May 26, 2025 10:02
@CohenArthur

Copy link
Copy Markdown
Member Author

stupid uninitialized booleans getting me again. I have half a mind to add a safe_bool class to the project lol, or turn all of them into enums

@powerboat9

Copy link
Copy Markdown
Collaborator

stupid uninitialized booleans getting me again. I have half a mind to add a safe_bool class to the project lol, or turn all of them into enums

It's surprising that GCC isn't (?) warning about them

gcc/rust/ChangeLog:

	* ast/rust-expr.h: Declare AnonConst and ConstBlock and use them.
	* ast/rust-ast-full-decls.h: Likewise.
	* ast/rust-ast.cc: Add implementation for AnonConst and ConstBlock.
	* ast/rust-ast.h: Likewise.
	* ast/rust-ast-collector.cc (TokenCollector::visit): Likewise.
	* ast/rust-ast-collector.h: Likewise.
	* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise.
	* ast/rust-ast-visitor.h: Likewise.
	* expand/rust-derive.h: Likewise.
	* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise.
	* hir/rust-ast-lower-base.h: Likewise.
	* hir/rust-ast-lower-expr.cc (translate_operand_const): Likewise.
	* resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise.
	* resolve/rust-ast-resolve-base.h: Likewise.
	* resolve/rust-ast-resolve-expr.h: Likewise.
	* resolve/rust-ast-resolve-expr.cc: Likewise.
gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_const_block_expr): New function.
	* parse/rust-parse.h: Declare it.
gcc/rust/ChangeLog:

	* hir/tree/rust-hir-expr.h: New classes.
	* hir/tree/rust-hir-full-decls.h: Likewise.
	* hir/tree/rust-hir.cc: Handle AnonConst and ConstBlock.
	* backend/rust-compile-block.cc: Likewise.
	* backend/rust-compile-block.h: Likewise.
	* backend/rust-compile-expr.cc (CompileExpr::visit): Likewise.
	* backend/rust-compile-expr.h: Likewise.
	* checks/errors/borrowck/rust-bir-builder-expr-stmt.cc (ExprStmtBuilder::visit): Likewise.
	* checks/errors/borrowck/rust-bir-builder-expr-stmt.h: Likewise.
	* checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h: Likewise.
	* checks/errors/borrowck/rust-bir-builder-struct.h: Likewise.
	* checks/errors/borrowck/rust-function-collector.h: Likewise.
	* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit): Likewise.
	* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
	* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
	* checks/errors/rust-const-checker.h: Likewise.
	* checks/errors/rust-hir-pattern-analysis.cc (PatternChecker::visit): Likewise.
	* checks/errors/rust-hir-pattern-analysis.h: Likewise.
	* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
	* checks/errors/rust-unsafe-checker.h: Likewise.
	* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise.
	(translate_operand_out): Likewise.
	(translate_operand_inout): Likewise.
	(translate_operand_const): Likewise.
	* hir/rust-ast-lower-expr.h: Likewise.
	* hir/rust-hir-dump.cc (Dump::visit): Likewise.
	* hir/rust-hir-dump.h: Likewise.
	* hir/tree/rust-hir-expr-abstract.h: Likewise.
	* hir/tree/rust-hir-expr.cc (AnonConst::AnonConst): Likewise.
	(AnonConst::operator=): Likewise.
	(ConstBlock::ConstBlock): Likewise.
	(ConstBlock::operator=): Likewise.
	* hir/tree/rust-hir-visitor.h:
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise.
	(typecheck_inline_asm_operand): Likewise.
	* typecheck/rust-hir-type-check-expr.h: Likewise.
gcc/testsuite/ChangeLog:

	* rust/execute/torture/const_block1.rs: New test.
@CohenArthur CohenArthur force-pushed the handle-const-blocks branch from f815bcd to e598e1d Compare May 27, 2025 09:23
@CohenArthur

CohenArthur commented May 27, 2025

Copy link
Copy Markdown
Member Author

stupid uninitialized booleans getting me again. I have half a mind to add a safe_bool class to the project lol, or turn all of them into enums

It's surprising that GCC isn't (?) warning about them

yeah it bothers me a lot

@CohenArthur CohenArthur added this pull request to the merge queue May 27, 2025
Merged via the queue into Rust-GCC:master with commit 7e42c1e May 27, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants