https://gcc.gnu.org/g:b80c154cf0fda31a6fa9ce85c60bfcb82966cd67
commit r17-1900-gb80c154cf0fda31a6fa9ce85c60bfcb82966cd67 Author: Arthur Cohen <[email protected]> Date: Wed Jun 10 17:49:03 2026 +0200 gccrs: typecheck: Special case builtin types when typechecking types. 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. Diff: --- gcc/rust/typecheck/rust-hir-type-check-type.cc | 12 +++++++++++- gcc/testsuite/rust/compile/type-with-builtin-type-name.rs | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index 451261363fb0..a7455eb2277a 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -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 index 000000000000..a3261a33ce0f --- /dev/null +++ b/gcc/testsuite/rust/compile/type-with-builtin-type-name.rs @@ -0,0 +1,8 @@ +#![feature(no_core)] +#![no_core] + +struct i32; + +fn main() { + let _: i32 = i32; +}
