From db832831ed15ad25aec99a7d4a87b7019e5f1aa4 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 17 Nov 2021 17:38:41 +0000 Subject: Probe for candidates on based on the actual receiver type Impl blocks Self type is a TypeNoBouns which means it can be for types such as: impl &T {}. I think we might need to change the probe algorithm for method calls to be fully based on the autoderef rather than trying to filter based on the Self type. More investigation is needed for the probe phase here. Fixes #808 --- gcc/rust/typecheck/rust-hir-type-check-expr.h | 22 ++++++++++++++++++---- gcc/testsuite/rust/compile/torture/issue-808.rs | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/rust/compile/torture/issue-808.rs (limited to 'gcc') diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index d5d12b4..c206592 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -265,15 +265,29 @@ public: bool probe_impls = !receiver_is_generic; bool ignore_mandatory_trait_items = !receiver_is_generic; + auto probe_type = probe_impls ? receiver_tyty : root; auto candidates - = PathProbeType::Probe (root, expr.get_method_name ().get_segment (), + = PathProbeType::Probe (probe_type, + expr.get_method_name ().get_segment (), probe_impls, probe_bounds, ignore_mandatory_trait_items); if (candidates.empty ()) { - rust_error_at (expr.get_locus (), - "failed to resolve the PathExprSegment to any item"); - return; + if (probe_impls) + { + candidates + = PathProbeType::Probe (root, + expr.get_method_name ().get_segment (), + probe_impls, probe_bounds, + ignore_mandatory_trait_items); + } + + if (candidates.empty ()) + { + rust_error_at (expr.get_locus (), + "failed to resolve the PathExprSegment to any item"); + return; + } } std::vector adjustments; diff --git a/gcc/testsuite/rust/compile/torture/issue-808.rs b/gcc/testsuite/rust/compile/torture/issue-808.rs new file mode 100644 index 0000000..bfbf774 --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/issue-808.rs @@ -0,0 +1,23 @@ +pub trait Foo { + type Target; + // { dg-warning "unused name" "" { target *-*-* } .-1 } + + fn bar(&self) -> &Self::Target; + // { dg-warning "unused name .self." "" { target *-*-* } .-1 } + // { dg-warning "unused name .Foo::bar." "" { target *-*-* } .-2 } +} + +impl Foo for &T { + type Target = T; + + fn bar(&self) -> &T { + *self + } +} + +pub fn main() { + let a: i32 = 123; + let b: &i32 = &a; + + b.bar(); +} -- cgit v1.1