]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix ICE for reserved lifetime name
authorPhilip Herron <herron.philip@googlemail.com>
Thu, 3 Apr 2025 15:32:36 +0000 (16:32 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 8 Apr 2025 08:17:14 +0000 (10:17 +0200)
This is a reserved name so this changes the assertion to a diagnostic.

Fixes Rust-GCC#3647

gcc/rust/ChangeLog:

* typecheck/rust-typecheck-context.cc (TypeCheckContext::lookup_lifetime): emit error

gcc/testsuite/ChangeLog:

* rust/compile/issue-3647.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-typecheck-context.cc
gcc/testsuite/rust/compile/issue-3647.rs [new file with mode: 0644]

index f02e48467317227fcbe2ebb2b179c690356af3c0..9112b998f168d721124c8897c453ef1ff9d979bd 100644 (file)
@@ -514,7 +514,16 @@ TypeCheckContext::lookup_lifetime (const HIR::Lifetime &lifetime) const
 {
   if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED)
     {
-      rust_assert (lifetime.get_name () != "static");
+      if (lifetime.get_name () == "static")
+       {
+         rich_location r (line_table, lifetime.get_locus ());
+         r.add_fixit_insert_after (lifetime.get_locus (),
+                                   "static is a reserved lifetime name");
+         rust_error_at (r, ErrorCode::E0262,
+                        "invalid lifetime parameter name: %qs",
+                        lifetime.get_name ().c_str ());
+         return tl::nullopt;
+       }
       const auto name = lifetime.get_name ();
       auto it = lifetime_name_interner.find (name);
       if (it == lifetime_name_interner.end ())
diff --git a/gcc/testsuite/rust/compile/issue-3647.rs b/gcc/testsuite/rust/compile/issue-3647.rs
new file mode 100644 (file)
index 0000000..51d9478
--- /dev/null
@@ -0,0 +1,7 @@
+#![allow(dead_code)]
+type A = fn();
+
+type B = for<'static> fn();
+// { dg-error "invalid lifetime parameter name: .static. .E0262." "" { target *-*-* } .-1 }
+
+pub fn main() {}