Skip to content

gccrs: Report constants whose evaluation fails - #5013

Open
u7k4rs6 wants to merge 1 commit into
Rust-GCC:masterfrom
u7k4rs6:fix-5012-const-eval-error
Open

u7k4rs6 wants to merge 1 commit into
Rust-GCC:masterfrom
u7k4rs6:fix-5012-const-eval-error

Conversation

@u7k4rs6

@u7k4rs6 u7k4rs6 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

If a constant couldn't be evaluated and nothing reported an error, gccrs used it anyway and the program read garbage. This reports E0080 in that case.

Reading a static was the usual way to get there. A constant reading one now gets E0013 from the const checker like rustc 1.49, and a static can read another static's initializer, which rustc allows (static B: i32 = A; used to leave B as 0). The note about the static not being initialized with a constant is gone for statics, it pointed at the wrong thing.

A plain fn nested in a const block is still checked as if it were const. That's an existing const checker thing, it gives the same false E0015 on master, filed as #5130.

Fixes #5012

@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.

minor style nit or does cp/constexpr call it report error?

Comment thread gcc/rust/backend/rust-compile-base.cc Outdated
TyTy::BaseType *expected_type, const Resolver::CanonicalPath &canonical_path,
HIR::Expr &const_value_expr, location_t locus, location_t expr_locus)
HIR::Expr &const_value_expr, location_t locus, location_t expr_locus,
bool report_failure)

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.

usually we call this emit_error

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

renamed to emit_error

@u7k4rs6
u7k4rs6 force-pushed the fix-5012-const-eval-error branch from f3eb614 to 8185cf9 Compare October 26, 2026 17:28
@u7k4rs6
u7k4rs6 requested a review from philberty October 26, 2026 19:23
Comment thread gcc/rust/backend/rust-constexpr.h Resolved
Comment on lines +7 to +8
static mut S: i32 = 4;
// { dg-message "not initialized with a constant expression" "" { target *-*-* } .-1 }

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.

Why would this error out? This is a perfectly valid static initializer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the static's fine, it's X reading it that errors (rustc gives E0080 too). the note on S is the old one ported from cp, wording's off ig. fix it here or in a follow up?

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.

rustc gives:

error[E0080]: constant accesses mutable global memory
 --> <source>:3:25
  |
3 | const X: i32 = unsafe { S }; 
  |                         ^ evaluation of `X` failed here

hmm i think this is confusing tbh.. because the error message is saying that S is not initilized with a constant but really the error is that S is a mutable static being read in X which is different so i dont think this is right or the locus and wording from cp constexpr is bad.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah the cp note was off, it's gone. a const reading a static now gets E0013 from the const checker, like rustc 1.49 (your E0080 is newer rustc). also found static B: i32 = A; silently gave 0, fixed that too

@u7k4rs6
u7k4rs6 force-pushed the fix-5012-const-eval-error branch 2 times, most recently from 825edbe to 7c805ec Compare October 29, 2026 16:36
@u7k4rs6
u7k4rs6 requested a review from CohenArthur October 30, 2026 09:00

@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.

this needs changed

If evaluating a constant failed and nothing reported an error, the
constant was used anyway and ended up uninitialized.  Report E0080
in that case.

Reading a static was the usual way to get there.  A constant reading
one now gets E0013 from the const checker like rustc 1.49, and a
static can read another static's initializer, which rustc allows.
The note saying the static wasn't initialized with a constant is gone
for statics, it pointed at the wrong thing.

Fixes Rust-GCC#5012

Assisted-by: Claude Opus 5.5 (Anthropic)

gcc/rust/ChangeLog:

	* backend/rust-constexpr.h (fold_expr): Add an optional
	non_constant_p out parameter.
	* backend/rust-constexpr.cc (fold_expr): Set it.
	(constant_value_1): Return the initializer of a static.
	(non_const_var_error): Don't add notes for statics.
	* backend/rust-compile-base.h
	(HIRCompileBase::compile_constant_item): Add an emit_error
	parameter.
	* backend/rust-compile-base.cc
	(HIRCompileBase::compile_constant_expr): Don't report failures.
	(HIRCompileBase::compile_constant_item): Return error_mark_node
	when evaluation fails, and report E0080 if nothing else did.
	* checks/errors/rust-const-checker.cc
	(ConstChecker::visit (PathInExpression &)): Report E0013 when a
	constant refers to a static.

gcc/testsuite/ChangeLog:

	* rust/compile/const-static-mut.rs: New test.
	* rust/compile/static-extern-read.rs: New test.
	* rust/execute/torture/static-read-static.rs: New test.

Signed-off-by: Utkarsh Bahuguna <utkarshbahuguna10@gmail.com>
@u7k4rs6
u7k4rs6 force-pushed the fix-5012-const-eval-error branch from 7c805ec to 28a2ae7 Compare November 27, 2026 06:51

This branch has not been deployed

No deployments
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.

Failed const evaluation silently leaves the constant uninitialized

3 participants