From 91b16af14cb4f7cdf6414b1314c35202d5883fd9 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Thu, 2 Jun 2022 12:13:43 +0100 Subject: Fixup name canonicalization for impl blocks When we generate the path for impl items we need to base this of the Self type but this was ignoring cases like pointers, references or slices. This meant generic slices had the same path has generic pointers etc. The only reason we didn't end up with a linker symbol clash is due to the symbol hash. --- gcc/rust/resolve/rust-ast-resolve-item.cc | 2 ++ gcc/rust/resolve/rust-ast-resolve-type.cc | 49 ++++++++++++++++++++----------- gcc/rust/resolve/rust-ast-resolve-type.h | 7 +---- 3 files changed, 35 insertions(+), 23 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc index 38e7713..603037e 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.cc +++ b/gcc/rust/resolve/rust-ast-resolve-item.cc @@ -613,6 +613,7 @@ ResolveItem::visit (AST::InherentImpl &impl_block) resolver->get_name_scope ().pop (); return; } + rust_assert (!self_cpath.is_empty ()); // Setup paths bool canonicalize_type_args = !impl_block.has_generics (); @@ -637,6 +638,7 @@ ResolveItem::visit (AST::InherentImpl &impl_block) = CanonicalPath::new_seg (impl_block.get_node_id (), seg_buf); cpath = canonical_prefix.append (seg); } + // done setup paths auto Self diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc b/gcc/rust/resolve/rust-ast-resolve-type.cc index 14178801..2b5c684 100644 --- a/gcc/rust/resolve/rust-ast-resolve-type.cc +++ b/gcc/rust/resolve/rust-ast-resolve-type.cc @@ -209,43 +209,58 @@ ResolveTypeToCanonicalPath::visit (AST::SliceType &slice) void ResolveType::visit (AST::ReferenceType &type) { - type.get_type_referenced ()->accept_vis (*this); - - if (canonical_path != nullptr && canonical_path->size () > 0) + CanonicalPath path = CanonicalPath::create_empty (); + resolved_node + = ResolveType::go (type.get_type_referenced ().get (), type.get_node_id (), + canonicalize_type_with_generics, &path); + if (canonical_path != nullptr) { - std::string seg = canonical_path->get (); - *canonical_path = CanonicalPath::new_seg (type.get_node_id (), "&" + seg); + std::string ref_type_str = type.is_mut () ? "mut" : ""; + std::string ref_path = "&" + ref_type_str + " " + path.get (); + *canonical_path = canonical_path->append ( + CanonicalPath::new_seg (type.get_node_id (), ref_path)); } } void ResolveType::visit (AST::RawPointerType &type) { - type.get_type_pointed_to ()->accept_vis (*this); - - if (canonical_path != nullptr && canonical_path->size () > 0) + CanonicalPath path = CanonicalPath::create_empty (); + resolved_node + = ResolveType::go (type.get_type_pointed_to ().get (), type.get_node_id (), + canonicalize_type_with_generics, &path); + if (canonical_path != nullptr) { - std::string seg = canonical_path->get (); - *canonical_path = CanonicalPath::new_seg (type.get_node_id (), "*" + seg); + std::string ptr_type_str + = type.get_pointer_type () == AST::RawPointerType::CONST ? "const" + : "mut"; + std::string ptr_path = "*" + ptr_type_str + " " + path.get (); + *canonical_path = canonical_path->append ( + CanonicalPath::new_seg (type.get_node_id (), ptr_path)); } } void ResolveType::visit (AST::InferredType &type) -{ - ok = true; -} +{} void ResolveType::visit (AST::NeverType &type) -{ - ok = true; -} +{} void ResolveType::visit (AST::SliceType &type) { - type.get_elem_type ()->accept_vis (*this); + CanonicalPath path = CanonicalPath::create_empty (); + resolved_node + = ResolveType::go (type.get_elem_type ().get (), type.get_node_id (), + canonicalize_type_with_generics, &path); + if (canonical_path != nullptr) + { + std::string slice_path = "[" + path.get () + "]"; + *canonical_path = canonical_path->append ( + CanonicalPath::new_seg (type.get_node_id (), slice_path)); + } } ResolveRelativeTypePath::ResolveRelativeTypePath (CanonicalPath qualified_path) diff --git a/gcc/rust/resolve/rust-ast-resolve-type.h b/gcc/rust/resolve/rust-ast-resolve-type.h index 9334135..d10cec2 100644 --- a/gcc/rust/resolve/rust-ast-resolve-type.h +++ b/gcc/rust/resolve/rust-ast-resolve-type.h @@ -211,15 +211,12 @@ public: ResolveType resolver (parent, canonicalize_type_with_generics, canonical_path); type->accept_vis (resolver); - if (!resolver.ok) - rust_error_at (type->get_locus (), "unresolved type"); return resolver.resolved_node; }; void visit (AST::BareFunctionType &fntype) override { - ok = true; for (auto ¶m : fntype.get_function_params ()) ResolveType::go (param.get_type ().get (), fntype.get_node_id ()); @@ -253,8 +250,6 @@ public: return; } - ok = !rel_canonical_path.is_empty (); - // lets try and resolve in one go else leave it up to the type resolver to // figure outer @@ -331,7 +326,7 @@ public: void visit (AST::QualifiedPathInType &path) override { - ok = ResolveRelativeTypePath::go (path); + ResolveRelativeTypePath::go (path); } void visit (AST::ArrayType &type) override; -- cgit v1.1