]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: typecheck: Special case builtin types when typechecking types.
authorArthur Cohen <arthur.cohen@embecosm.com>
Wed, 10 Jun 2026 15:49:03 +0000 (17:49 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 25 Jun 2026 17:21:44 +0000 (19:21 +0200)
Type definition conflicts are allowed in the case of builtin types. This is used in the
`core` crate to add some more functionality to the builtin types. The consequence of this
is that we need to special case the resolution of types in the case that their name could
refer to a module or to a builtin type.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): Add
special casing for resolving to the builtin type definition if the found type
has the same name and is a module.

gcc/testsuite/ChangeLog:

* rust/compile/type-with-builtin-type-name.rs: New test.

gcc/rust/typecheck/rust-hir-type-check-type.cc
gcc/testsuite/rust/compile/type-with-builtin-type-name.rs [new file with mode: 0644]

index 451261363fb0dda4b95b06bb262b1b85861f2883..a7455eb2277a792b3974c1aeb79bd0af513a8461 100644 (file)
@@ -31,6 +31,7 @@
 #include "rust-type-util.h"
 #include "rust-system.h"
 #include "rust-compile-base.h"
+#include "rust-resolve-builtins.h"
 
 namespace Rust {
 namespace Resolver {
@@ -349,6 +350,14 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset,
          // assign the ref_node_id if we've found something
          nr_ctx.lookup (ast_node_id, Resolver2_0::Namespace::Types)
            .map ([&ref_node_id] (NodeId resolved) { ref_node_id = resolved; });
+
+         // TODO: Should we add a special method to the name resolver to handle
+         // that case? Resolving something in the Types NS when we want to
+         // prioritize builtin types over modules or other conflicting things?
+         if (auto builtin_type_id
+             = Resolver2_0::Builtins::find_builtin_node_id (seg->to_string ()))
+           if (mappings.is_module (ref_node_id))
+             ref_node_id = builtin_type_id.value ();
        }
 
       // ref_node_id is the NodeId that the segments refers to.
@@ -411,7 +420,8 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset,
          // A::B::C::this_is_a_module
          //          ^^^^^^^^^^^^^^^^
          // This is an error, we are not expecting a module.
-         rust_error_at (seg->get_locus (), "expected value");
+         rust_error_at (seg->get_locus (), "expected value, got module");
+
          return new TyTy::ErrorType (path.get_mappings ().get_hirid ());
        }
 
diff --git a/gcc/testsuite/rust/compile/type-with-builtin-type-name.rs b/gcc/testsuite/rust/compile/type-with-builtin-type-name.rs
new file mode 100644 (file)
index 0000000..a3261a3
--- /dev/null
@@ -0,0 +1,8 @@
+#![feature(no_core)]
+#![no_core]
+
+struct i32;
+
+fn main() {
+    let _: i32 = i32;
+}