Skip to content

Commit

Permalink
gccrs: self paths are patterns but we dont have mappings for it
Browse files Browse the repository at this point in the history
With simple patterns we dont introduce any more inference varaibles as
they are already declared in a specific way. This would only lead to
more unconstrained inference varaibles than is required.

Fixes #3022

gcc/rust/ChangeLog:

	* hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_self): add location mappings
	* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_root_path): check for self

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3022.rs: New test.

Signed-off-by: Philip Herron <[email protected]>
  • Loading branch information
philberty committed Feb 13, 2025
1 parent 45fc433 commit 1eb4620
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions gcc/rust/hir/rust-ast-lower-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ ASTLoweringBase::lower_self (AST::Param &param)
Analysis::NodeMapping mapping (crate_num, self.get_node_id (),
mappings.get_next_hir_id (crate_num),
mappings.get_next_localdef_id (crate_num));
mappings.insert_location (mapping.get_hirid (), param.get_locus ());

if (self.has_type ())
{
Expand Down
5 changes: 4 additions & 1 deletion gcc/rust/typecheck/rust-hir-type-check-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset,
auto seg_is_module = mappings.lookup_module (ref).has_value ();
auto seg_is_crate = mappings.is_local_hirid_crate (ref);
auto seg_is_pattern = mappings.lookup_hir_pattern (ref).has_value ();
auto seg_is_self = is_root && !have_more_segments
&& seg.get_segment ().as_string () == "self";
if (seg_is_module || seg_is_crate)
{
// A::B::C::this_is_a_module::D::E::F
Expand Down Expand Up @@ -388,7 +390,8 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset,
if (lookup->get_kind () == TyTy::TypeKind::ERROR)
return new TyTy::ErrorType (expr.get_mappings ().get_hirid ());
}
else if (lookup->needs_generic_substitutions () && !seg_is_pattern)
else if (lookup->needs_generic_substitutions () && !seg_is_pattern
&& !seg_is_self)
{
lookup = SubstMapper::InferSubst (lookup, expr.get_locus ());
}
Expand Down
18 changes: 18 additions & 0 deletions gcc/testsuite/rust/compile/issue-3022.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[lang = "sized"]
trait Sized {}

trait Foo<T> {
fn foo(self) -> T;
}

struct Bar<T, U> {
// { dg-warning "struct is never constructed" "" { target *-*-* } .-1 }
value: U,
valte: T,
}

impl<T: Foo<U>, U> Foo<U> for Bar<T, U> {
fn foo(self) -> U {
self.value
}
}

0 comments on commit 1eb4620

Please sign in to comment.