Skip to content

When defining types in a function, use statement in impl functions causes unrelated type inference to fail #13895

Closed
@rben01

Description

@rben01

rust-analyzer version: 0.3.1334-standalone (74ae2dd 2022-12-25)

rustc version: rustc 1.67.0-nightly

relevant settings: None?

Description

In a particular context, type inference fails and RA does not show the type of a variable assigned to a function's return value. This is caused by the presence of a use statement in a function.

Steps to reproduce

  1. Inside an outer function definition...
  2. Define one struct, S1
  3. And give S1 a method get_pair(self) that returns a 2-tuple
  4. Define a second struct, S2, that contains an instance of S1
  5. And give S2 a method that both 1. has a use statement and 2. calls s1.get_pair()

Bug

The presence of the use statement causes type inference of the return type of s1.get_pair() to fail

Code snippet

As far as I know this snippet is minimal.

fn f() {
	struct S1;

	impl S1 {
		fn get_one(self) -> i32 {
			0
		}
		fn get_pair(self) -> (i32, i32) {
			(0, 0)
		}
	}

	struct S2 {
		s1: S1,
	}

	impl S2 {
		fn no_fail_1(self) -> i32 {
			use std::path;
			let x = self.s1.get_one(); // let x: i32
			x
		}

		fn no_fail_2(self) -> i32 {
			let pair = self.s1.get_pair(); // let pair: (i32, i32)
			pair.0
		}

		fn fail(self) -> i32 {
			use std::path;
			let pair = self.s1.get_pair(); // let pair: {unknown}
			pair.0
		}
	}
}

Screenshots

image

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-hirhir and hir-def relatedA-tytype system / type inference / traits / method resolutionC-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions