From: Arthur Cohen <[email protected]>
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.
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/ea3adb2cf4d302991ce671588cbe770931d64e34
The commit has NOT been mentioned in any issue.
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4404
gcc/rust/typecheck/rust-hir-type-check-type.cc | 12 +++++++++++-
.../rust/compile/type-with-builtin-type-name.rs | 8 ++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/rust/compile/type-with-builtin-type-name.rs
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc
b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index 451261363..a7455eb22 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 000000000..a3261a33c
--- /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;
+}
--
2.54.0