Skip to content

Commit ea3adb2

Browse files
committed
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.
1 parent c4ccf59 commit ea3adb2

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

gcc/rust/typecheck/rust-hir-type-check-type.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "rust-type-util.h"
3232
#include "rust-system.h"
3333
#include "rust-compile-base.h"
34+
#include "rust-resolve-builtins.h"
3435

3536
namespace Rust {
3637
namespace Resolver {
@@ -349,6 +350,14 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset,
349350
// assign the ref_node_id if we've found something
350351
nr_ctx.lookup (ast_node_id, Resolver2_0::Namespace::Types)
351352
.map ([&ref_node_id] (NodeId resolved) { ref_node_id = resolved; });
353+
354+
// TODO: Should we add a special method to the name resolver to handle
355+
// that case? Resolving something in the Types NS when we want to
356+
// prioritize builtin types over modules or other conflicting things?
357+
if (auto builtin_type_id
358+
= Resolver2_0::Builtins::find_builtin_node_id (seg->to_string ()))
359+
if (mappings.is_module (ref_node_id))
360+
ref_node_id = builtin_type_id.value ();
352361
}
353362

354363
// 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,
411420
// A::B::C::this_is_a_module
412421
// ^^^^^^^^^^^^^^^^
413422
// This is an error, we are not expecting a module.
414-
rust_error_at (seg->get_locus (), "expected value");
423+
rust_error_at (seg->get_locus (), "expected value, got module");
424+
415425
return new TyTy::ErrorType (path.get_mappings ().get_hirid ());
416426
}
417427

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
struct i32;
5+
6+
fn main() {
7+
let _: i32 = i32;
8+
}

0 commit comments

Comments
 (0)