]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix segv in unsafe chcker
authorPhilip Herron <herron.philip@googlemail.com>
Fri, 4 Apr 2025 15:35:13 +0000 (16:35 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 28 Apr 2025 14:18:52 +0000 (16:18 +0200)
Trait constants were missing type resolution step, this adds that
as if it was a normal constant. The unsafe checker was missing a
null check.

Fixes Rust-GCC#3612

gcc/rust/ChangeLog:

* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): add null check
* hir/tree/rust-hir-item.h: add has_type helper
* typecheck/rust-hir-trait-resolve.cc (TraitItemReference::resolve_item):
add missing type checking

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/checks/errors/rust-unsafe-checker.cc
gcc/rust/hir/tree/rust-hir-item.h
gcc/rust/typecheck/rust-hir-trait-resolve.cc
gcc/testsuite/rust/compile/issue-3612.rs [new file with mode: 0644]

index 8aa59ee9e2843061d2ec0c6c1f74119f36b74de4..bb887a9911708177a29ec9fc807bba5a4727841d 100644 (file)
@@ -481,9 +481,14 @@ UnsafeChecker::visit (MethodCallExpr &expr)
   TyTy::BaseType *method_type;
   context.lookup_type (expr.get_method_name ().get_mappings ().get_hirid (),
                       &method_type);
+  if (!method_type || !method_type->is<TyTy::FnType> ())
+    return;
 
   auto &fn = static_cast<TyTy::FnType &> (*method_type);
 
+  // FIXME
+  // should probably use the defid lookup instead
+  // tl::optional<HIR::Item *> lookup_defid (DefId id);
   auto method = mappings.lookup_hir_implitem (fn.get_ref ());
   if (!unsafe_context.is_in_context () && method)
     check_unsafe_call (static_cast<Function *> (method->first),
index b9b105b2b2a98d1ec1fc9c627da016853bc359cb..37f599cdee4a4f1cc6c62f9f9018a7ce11a5cb98 100644 (file)
@@ -2070,6 +2070,8 @@ public:
 
   Identifier get_name () const { return name; }
 
+  bool has_type () const { return expr != nullptr; }
+
   bool has_expr () const { return expr != nullptr; }
 
   Type &get_type ()
index e78c19263dc18b0bb6aba313549d64cb35b59962..032bb5841272f019ba25a0acc2a3f6ef30fe5467 100644 (file)
@@ -384,7 +384,26 @@ TraitItemReference::resolve_item (HIR::TraitItemType &type)
 void
 TraitItemReference::resolve_item (HIR::TraitItemConst &constant)
 {
-  // TODO
+  TyTy::BaseType *ty = nullptr;
+  if (constant.has_type ())
+    ty = TypeCheckType::Resolve (constant.get_type ());
+
+  TyTy::BaseType *expr = nullptr;
+  if (constant.has_expr ())
+    expr = TypeCheckExpr::Resolve (constant.get_expr ());
+
+  bool have_specified_ty = ty != nullptr && !ty->is<TyTy::ErrorType> ();
+  bool have_expr_ty = expr != nullptr && !expr->is<TyTy::ErrorType> ();
+
+  if (have_specified_ty && have_expr_ty)
+    {
+      coercion_site (constant.get_mappings ().get_hirid (),
+                    TyTy::TyWithLocation (ty,
+                                          constant.get_type ().get_locus ()),
+                    TyTy::TyWithLocation (expr,
+                                          constant.get_expr ().get_locus ()),
+                    constant.get_locus ());
+    }
 }
 
 void
diff --git a/gcc/testsuite/rust/compile/issue-3612.rs b/gcc/testsuite/rust/compile/issue-3612.rs
new file mode 100644 (file)
index 0000000..5256d0a
--- /dev/null
@@ -0,0 +1,7 @@
+trait _St1 {
+    pub const UNDERFLOW: *const u16 = unsafe { [0u16; 1].as_ptr().offset(isize::MIN) };
+    // { dg-error "no method named .as_ptr. found in the current scope .E0599." "" { target *-*-* } .-1 }
+    // { dg-error "failed to resolve receiver in MethodCallExpr" "" { target *-*-* } .-2 }
+}
+
+fn main() {}